2020-06-01 15:53:18 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_AUDIO_RING_BUFFER_H_
|
|
|
|
#define MOZILLA_AUDIO_RING_BUFFER_H_
|
|
|
|
|
|
|
|
#include "AudioSampleFormat.h"
|
|
|
|
#include "mozilla/Span.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AudioRingBuffer works with audio sample format float or short. The
|
|
|
|
* implementation wrap around the RingBuffer thus it is not thread-safe. Reads
|
|
|
|
* and writes must happen in the same thread which may be different than the
|
2023-10-09 13:02:56 +00:00
|
|
|
* construction thread. The memory is pre-allocated in the constructor, but may
|
|
|
|
* also be re-allocated on the fly should a larger length be needed. The sample
|
|
|
|
* format has to be specified in order to be used.
|
2020-06-01 15:53:18 +00:00
|
|
|
*/
|
|
|
|
class AudioRingBuffer final {
|
|
|
|
public:
|
2020-09-17 06:13:40 +00:00
|
|
|
explicit AudioRingBuffer(uint32_t aSizeInBytes);
|
2020-06-01 15:53:18 +00:00
|
|
|
~AudioRingBuffer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the sample format to either short or float. The sample format must be
|
|
|
|
* set before the using any other method.
|
|
|
|
*/
|
|
|
|
void SetSampleFormat(AudioSampleFormat aFormat);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write `aBuffer.Length()` number of samples when the format is float.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Write(const Span<const float>& aBuffer);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write `aBuffer.Length()` number of samples when the format is short.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Write(const Span<const int16_t>& aBuffer);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write `aSamples` number of samples from `aBuffer`. Note the `aBuffer` does
|
|
|
|
* not change.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Write(const AudioRingBuffer& aBuffer, uint32_t aSamples);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
2023-10-09 13:02:55 +00:00
|
|
|
/**
|
|
|
|
* Write `aSamples` number of zeros before the beginning of the existing data.
|
|
|
|
*/
|
|
|
|
uint32_t PrependSilence(uint32_t aSamples);
|
|
|
|
|
2020-06-01 15:53:18 +00:00
|
|
|
/**
|
|
|
|
* Write `aSamples` number of zeros.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t WriteSilence(uint32_t aSamples);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read `aBuffer.Length()` number of samples when the format is float.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Read(const Span<float>& aBuffer);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read `aBuffer.Length()` number of samples when the format is short.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Read(const Span<int16_t>& aBuffer);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the internal buffer without extra copies when sample format is float.
|
|
|
|
* Check also the RingBuffer::ReadNoCopy() for more details.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t ReadNoCopy(
|
|
|
|
std::function<uint32_t(const Span<const float>&)>&& aCallable);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the internal buffer without extra copies when sample format is short.
|
|
|
|
* Check also the RingBuffer::ReadNoCopy() for more details.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t ReadNoCopy(
|
|
|
|
std::function<uint32_t(const Span<const int16_t>&)>&& aCallable);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove `aSamples` number of samples.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Discard(uint32_t aSamples);
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all available samples.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t Clear();
|
2020-06-01 15:53:18 +00:00
|
|
|
|
2023-10-09 13:02:56 +00:00
|
|
|
/**
|
2024-04-18 02:24:04 +00:00
|
|
|
* Increase the ring buffer size if necessary to at least the specified length
|
|
|
|
* in bytes. Must be divisible by the sample size.
|
|
|
|
* Will not deallocate memory if the underlying buffer is large enough.
|
|
|
|
* Returns false if memory allocation is required and fails.
|
2023-10-09 13:02:56 +00:00
|
|
|
*/
|
2024-04-18 02:24:04 +00:00
|
|
|
bool EnsureLengthBytes(uint32_t aLengthBytes);
|
2023-10-09 13:02:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of samples this buffer can hold.
|
|
|
|
*/
|
|
|
|
uint32_t Capacity() const;
|
|
|
|
|
2020-06-01 15:53:18 +00:00
|
|
|
/**
|
|
|
|
* Return true if the buffer is full.
|
|
|
|
*/
|
|
|
|
bool IsFull() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the buffer is empty.
|
|
|
|
*/
|
|
|
|
bool IsEmpty() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of samples available for writing.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t AvailableWrite() const;
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of samples available for reading.
|
|
|
|
*/
|
2020-09-17 06:13:40 +00:00
|
|
|
uint32_t AvailableRead() const;
|
2020-06-01 15:53:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
class AudioRingBufferPrivate;
|
|
|
|
UniquePtr<AudioRingBufferPrivate> mPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // MOZILLA_AUDIO_RING_BUFFER_H_
|