Ocean
Loading...
Searching...
No Matches
AMicrophone.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_MEDIA_ANDROID_A_MICROPHONE_H
9#define META_OCEAN_MEDIA_ANDROID_A_MICROPHONE_H
10
13
15
16#include <SLES/OpenSLES_Android.h>
17
18#include <queue>
19
20namespace Ocean
21{
22
23namespace Media
24{
25
26namespace Android
27{
28
29/**
30 * This class implements a microphone class for Android.
31 * @ingroup mediaandroid
32 */
33class OCEAN_MEDIA_A_EXPORT AMicrophone final :
34 virtual public AMedium,
35 virtual public Microphone
36{
37 friend class ALibrary;
38
39 protected:
40
41 /// The number of buffers in the queue.
42 static constexpr SLuint32 numberBuffers_ = 4u;
43
44 /**
45 * Definition of a vector holding sample elements.
46 */
47 using Buffer = std::vector<int16_t>;
48
49 public:
50
51 /**
52 * Starts the medium.
53 * @see Medium::start().
54 */
55 bool start() override;
56
57 /**
58 * Pauses the medium.
59 * @see Medium::pause():
60 */
61 bool pause() override;
62
63 /**
64 * Stops the medium.
65 * @see Medium::stop().
66 */
67 bool stop() override;
68
69 /**
70 * Returns whether the medium is started currently.
71 * @see Medium::isStarted().
72 */
73 bool isStarted() const override;
74
75 /**
76 * Returns the start timestamp.
77 * @see FiniteMedium::startTimestamp().
78 */
79 Timestamp startTimestamp() const override;
80
81 /**
82 * Returns the pause timestamp.
83 * @see FiniteMedium::pauseTimestamp().
84 */
85 Timestamp pauseTimestamp() const override;
86
87 /**
88 * Returns the stop timestamp.
89 * @see FiniteMedium::stopTimestamp().
90 */
91 Timestamp stopTimestamp() const override;
92
93 /**
94 * Returns the size of one mono chunk in elements.
95 * @return The size of one mono chunk, in elements
96 */
97 static constexpr size_t monoChunkElements();
98
99 protected:
100
101 /**
102 * Creates a new medium by a given url.
103 * @param slEngineInterface The interface of the SL engine, must be valid
104 * @param url Url of the medium
105 */
106 AMicrophone(const SLEngineItf& slEngineInterface, const std::string& url);
107
108 /**
109 * Destructs the live video object.
110 */
111 ~AMicrophone() override;
112
113 /**
114 * Initializes the audio and all corresponding resources.
115 * @param slEngineInterface The interface of the SL engine, must be valid
116 * @param microphoneTypes The types of the microphone to create
117 * @param microphoneConfigurations The configurations of the microphone to create
118 * @return True, if succeeded
119 */
120 bool initialize(const SLEngineItf& slEngineInterface, const MicrophoneTypes microphoneTypes, const MicrophoneConfigurations microphoneConfigurations);
121
122 /**
123 * Releases the audio and all corresponding resources.
124 * @return True, if succeeded
125 */
126 bool release();
127
128 /**
129 * Event callback function to fill the OpenSL buffer queue.
130 * @param bufferQueue The buffer queue to fill with the next samples, must be valid
131 */
132 void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue);
133
134 /**
135 * Static event callback function to fill the OpenSL buffer queue.
136 * @param bufferQueue The buffer queue to fill with the next samples, must be valid
137 * @param context The context of the event (the ALiveAudio object), must be valid
138 */
139 static void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue, void* context);
140
141 /**
142 * Translates the value of a recording preset to a readable string.
143 * @param presetValue The preset value to translate
144 * @return The readable string
145 */
146 static std::string translateRecordingPreset(const SLuint32 presetValue);
147
148 protected:
149
150 /// Start timestamp.
151 Timestamp startTimestamp_ = Timestamp(false);
152
153 /// Pause timestamp.
154 Timestamp pauseTimestamp_ = Timestamp(false);
155
156 /// Stop timestamp.
157 Timestamp stopTimestamp_ = Timestamp(false);
158
159 /// The SL recorder object.
160 SLObjectItf slRecorder_ = nullptr;
161
162 /// The SL recorder interface.
163 SLRecordItf slRecordInterface_ = nullptr;
164
165 /// The SL buffer queue interface.
166 SLAndroidSimpleBufferQueueItf slBufferQueueInterface_ = nullptr;
167
168 /// The buffer for the sample queue.
169 std::queue<Buffer> bufferQueue_;
170
171 /// The SL effect interface for the acoustic echo cancel effect.
172 SLAndroidEffectItf slAcousticEchoCancelEffect_ = nullptr;
173
174 /// The SL implementation id of the acoustic echo cancel effect.
175 SLInterfaceID slAcousticEchoCancelerEffectImplementationId_ = nullptr;
176};
177
179{
180 constexpr size_t samplesPerSecondMono = 48000; // 48kHz
181
182 return samplesPerSecondMono / 50; // 20ms
183}
184
185}
186
187}
188
189}
190
191#endif // META_OCEAN_MEDIA_ANDROID_A_MICROPHONE_H
This class implements the android library.
Definition ALibrary.h:37
This class implements the base class for all Medium objects in the Android library.
Definition AMedium.h:35
This class implements a microphone class for Android.
Definition AMicrophone.h:36
bool start() override
Starts the medium.
bool pause() override
Pauses the medium.
bool stop() override
Stops the medium.
Timestamp pauseTimestamp() const override
Returns the pause timestamp.
void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue)
Event callback function to fill the OpenSL buffer queue.
static void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue, void *context)
Static event callback function to fill the OpenSL buffer queue.
bool initialize(const SLEngineItf &slEngineInterface, const MicrophoneTypes microphoneTypes, const MicrophoneConfigurations microphoneConfigurations)
Initializes the audio and all corresponding resources.
bool release()
Releases the audio and all corresponding resources.
~AMicrophone() override
Destructs the live video object.
Timestamp stopTimestamp() const override
Returns the stop timestamp.
std::queue< Buffer > bufferQueue_
The buffer for the sample queue.
Definition AMicrophone.h:169
std::vector< int16_t > Buffer
Definition of a vector holding sample elements.
Definition AMicrophone.h:47
bool isStarted() const override
Returns whether the medium is started currently.
AMicrophone(const SLEngineItf &slEngineInterface, const std::string &url)
Creates a new medium by a given url.
static std::string translateRecordingPreset(const SLuint32 presetValue)
Translates the value of a recording preset to a readable string.
static constexpr size_t monoChunkElements()
Returns the size of one mono chunk in elements.
Definition AMicrophone.h:178
Timestamp startTimestamp() const override
Returns the start timestamp.
This class is the base class for all microphones.
Definition media/Microphone.h:37
MicrophoneTypes
Definition of individual microphone types.
Definition media/Microphone.h:44
MicrophoneConfigurations
Definition of individual microphone configurations.
Definition media/Microphone.h:59
This class implements a timestamp.
Definition Timestamp.h:63
The namespace covering the entire Ocean framework.
Definition Accessor.h:15