forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioRingBuffer.h
114 lines (91 loc) · 2.95 KB
/
AudioRingBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- 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
* construction thread. The memory is pre-allocated in the constructor. The
* sample format has to be specified in order to be used.
*/
class AudioRingBuffer final {
public:
explicit AudioRingBuffer(int aSizeInBytes);
~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.
*/
int Write(const Span<const float>& aBuffer);
/**
* Write `aBuffer.Length()` number of samples when the format is short.
*/
int Write(const Span<const int16_t>& aBuffer);
/**
* Write `aSamples` number of samples from `aBuffer`. Note the `aBuffer` does
* not change.
*/
int Write(const AudioRingBuffer& aBuffer, int aSamples);
/**
* Write `aSamples` number of zeros.
*/
int WriteSilence(int aSamples);
/**
* Read `aBuffer.Length()` number of samples when the format is float.
*/
int Read(const Span<float>& aBuffer);
/**
* Read `aBuffer.Length()` number of samples when the format is short.
*/
int Read(const Span<int16_t>& aBuffer);
/**
* Read the internal buffer without extra copies when sample format is float.
* Check also the RingBuffer::ReadNoCopy() for more details.
*/
int ReadNoCopy(std::function<int(const Span<const float>&)>&& aCallable);
/**
* Read the internal buffer without extra copies when sample format is short.
* Check also the RingBuffer::ReadNoCopy() for more details.
*/
int ReadNoCopy(std::function<int(const Span<const int16_t>&)>&& aCallable);
/**
* Remove `aSamples` number of samples.
*/
int Discard(int aSamples);
/**
* Remove all available samples.
*/
int Clear();
/**
* 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.
*/
int AvailableWrite() const;
/**
* Return the number of samples available for reading.
*/
int AvailableRead() const;
private:
class AudioRingBufferPrivate;
UniquePtr<AudioRingBufferPrivate> mPtr;
};
} // namespace mozilla
#endif // MOZILLA_AUDIO_RING_BUFFER_H_