47
49
* @file AlsaLayer.h
48
* @brief Main sound class. Manages the data transfers between the application and the hardware.
50
* @brief Main sound class. Manages the data transfers between the application and the hardware.
51
class AlsaLayer : public AudioLayer {
55
* @param manager An instance of managerimpl
57
AlsaLayer( ManagerImpl* manager );
64
bool closeLayer( void );
67
* Check if no devices are opened, otherwise close them.
68
* Then open the specified devices by calling the private functions open_device
69
* @param indexIn The number of the card choosen for capture
70
* @param indexOut The number of the card choosen for playback
71
* @param sampleRate The sample rate
72
* @param frameSize The frame size
73
* @param stream To indicate which kind of stream you want to open
77
* @param plugin The alsa plugin ( dmix , default , front , surround , ...)
79
bool openDevice(int indexIn, int indexOut, int sampleRate, int frameSize, int stream, std::string plugin);
82
* Start the capture stream and prepare the playback stream.
83
* The playback starts accordingly to its threshold
86
void startStream(void);
89
* Stop the playback and capture streams.
90
* Drops the pending frames and put the capture and playback handles to PREPARED state
93
void stopStream(void);
96
* Query the capture device for number of bytes available in the hardware ring buffer
97
* @return int The number of bytes available
102
* Get data from the capture device
103
* @param buffer The buffer for data
104
* @param toCopy The number of bytes to get
105
* @return int The number of bytes acquired ( 0 if an error occured)
107
int getMic(void * buffer, int toCopy);
110
* Concatenate two strings. Used to build a valid pcm device name.
111
* @param plugin the alsa PCM name
112
* @param card the sound card number
113
* @param subdevice the subdevice number
114
* @return std::string the concatenated string
116
std::string buildDeviceTopo( std::string plugin, int card, int subdevice );
119
* Scan the sound card available on the system
120
* @param stream To indicate whether we are looking for capture devices or playback devices
124
* @return std::vector<std::string> The vector containing the string description of the card
126
std::vector<std::string> getSoundCardsInfo( int stream );
129
* Check if the given index corresponds to an existing sound card and supports the specified streaming mode
130
* @param card An index
131
* @param stream The stream mode
135
* @return bool True if it exists and supports the mode
138
bool soundCardIndexExist( int card , int stream );
141
* An index is associated with its string description
142
* @param description The string description
143
* @return int Its index
145
int soundCardGetIndex( std::string description );
148
* Get the current audio plugin.
149
* @return std::string The name of the audio plugin
151
std::string getAudioPlugin( void ) { return _audioPlugin; }
153
void audioCallback (void);
155
bool isCaptureActive (void);
160
AlsaLayer(const AlsaLayer& rh);
162
// Assignment Operator
163
AlsaLayer& operator=( const AlsaLayer& rh);
165
bool is_playback_prepared (void) { return _is_prepared_playback; }
166
bool is_capture_prepared (void) { return _is_prepared_capture; }
167
void prepare_playback (void) { _is_prepared_playback = true; }
168
void prepare_capture (void) { _is_prepared_capture = true; }
169
bool is_capture_running (void) { return _is_running_capture; }
170
bool is_playback_running (void) { return _is_running_playback; }
171
void start_playback (void) { _is_running_playback = true; }
172
void stop_playback (void) { _is_running_playback = false; _is_prepared_playback = false; }
173
void start_capture (void) { _is_running_capture = true; }
174
void stop_capture (void) { _is_running_capture = false; _is_prepared_capture = false; }
175
void close_playback (void) { _is_open_playback = false; }
176
void close_capture (void) { _is_open_capture = false; }
177
void open_playback (void) { _is_open_playback = true; }
178
void open_capture (void) { _is_open_capture = true; }
179
bool is_capture_open (void) { return _is_open_capture; }
180
bool is_playback_open (void) { return _is_open_playback; }
183
* Drop the pending frames and close the capture device
186
void closeCaptureStream( void );
187
void stopCaptureStream( void );
188
void startCaptureStream( void );
189
void prepareCaptureStream( void );
191
void closePlaybackStream( void );
192
void stopPlaybackStream( void );
193
void startPlaybackStream( void );
194
void preparePlaybackStream( void );
197
* Open the specified device.
199
* @param pcm_p The string name for the playback device
200
* @param pcm_c The string name for the capture device
201
* @param flag To indicate which kind of stream you want to open
205
* @return true if successful
208
bool open_device( std::string pcm_p, std::string pcm_c, int flag);
210
bool alsa_set_params( snd_pcm_t *pcm_handle, int type, int rate );
213
* Copy a data buffer in the internal ring buffer
215
* @param buffer The data to be copied
216
* @param length The size of the buffer
217
* @return int The number of frames actually copied
219
int write( void* buffer, int length);
222
* Read data from the internal ring buffer
224
* @param buffer The buffer to stock the read data
225
* @param toCopy The number of bytes to get
226
* @return int The number of frames actually read
228
int read( void* buffer, int toCopy);
233
* Recover from XRUN state for capture
236
void handle_xrun_capture( void );
239
* Recover from XRUN state for playback
242
void handle_xrun_playback( void );
244
void* adjustVolume( void* buffer , int len, int stream );
247
* Handles to manipulate playback stream
250
snd_pcm_t* _PlaybackHandle;
253
* Handles to manipulate capture stream
256
snd_pcm_t* _CaptureHandle;
259
* Alsa parameter - Size of a period in the hardware ring buffer
261
snd_pcm_uframes_t _periodSize;
264
* name of the alsa audio plugin used
266
std::string _audioPlugin;
268
/** Vector to manage all soundcard index - description association of the system */
269
std::vector<HwIDPair> IDSoundCards;
271
bool _is_prepared_playback;
272
bool _is_prepared_capture;
273
bool _is_running_playback;
274
bool _is_running_capture;
275
bool _is_open_playback;
276
bool _is_open_capture;
277
bool _trigger_request;
279
AudioThread* _audioThread;
281
/** Sample rate converter object */
282
SamplerateConverter* _converter;
284
// Allpass filter to remove DC offset
285
DcBlocker* dcblocker;
53
class AlsaLayer : public AudioLayer
58
* @param manager An instance of managerimpl
60
AlsaLayer (ManagerImpl* manager);
67
bool closeLayer (void);
70
* Check if no devices are opened, otherwise close them.
71
* Then open the specified devices by calling the private functions open_device
72
* @param indexIn The number of the card chosen for capture
73
* @param indexOut The number of the card chosen for playback
74
* @param sampleRate The sample rate
75
* @param frameSize The frame size
76
* @param stream To indicate which kind of stream you want to open
80
* @param plugin The alsa plugin ( dmix , default , front , surround , ...)
82
bool openDevice (int indexIn, int indexOut, int indexRing, int sampleRate, int frameSize, int stream, std::string plugin);
85
* Start the capture stream and prepare the playback stream.
86
* The playback starts accordingly to its threshold
89
void startStream (void);
92
* Stop the playback and capture streams.
93
* Drops the pending frames and put the capture and playback handles to PREPARED state
96
void stopStream (void);
99
* Query the capture device for number of bytes available in the hardware ring buffer
100
* @return int The number of bytes available
105
* Get data from the capture device
106
* @param buffer The buffer for data
107
* @param toCopy The number of bytes to get
108
* @return int The number of bytes acquired ( 0 if an error occured)
110
int getMic (void * buffer, int toCopy);
113
* Concatenate two strings. Used to build a valid pcm device name.
114
* @param plugin the alsa PCM name
115
* @param card the sound card number
116
* @param subdevice the subdevice number
117
* @return std::string the concatenated string
119
std::string buildDeviceTopo (std::string plugin, int card, int subdevice);
122
* Scan the sound card available on the system
123
* @param stream To indicate whether we are looking for capture devices or playback devices
127
* @return std::vector<std::string> The vector containing the string description of the card
129
std::vector<std::string> getSoundCardsInfo (int stream);
132
* Check if the given index corresponds to an existing sound card and supports the specified streaming mode
133
* @param card An index
134
* @param stream The stream mode
138
* @return bool True if it exists and supports the mode
141
bool soundCardIndexExist (int card , int stream);
144
* An index is associated with its string description
145
* @param description The string description
146
* @return int Its index
148
int soundCardGetIndex (std::string description);
151
* Get the current audio plugin.
152
* @return std::string The name of the audio plugin
154
std::string getAudioPlugin (void) {
158
void audioCallback (void);
160
bool isCaptureActive (void);
163
* Get the noise suppressor state
164
* @return true if noise suppressor activated
166
virtual bool getNoiseSuppressState (void) {
167
return AudioLayer::_noisesuppressstate;
171
* Set the noise suppressor state
172
* @param state true if noise suppressor active, false elsewhere
174
virtual void setNoiseSuppressState (bool state);
179
AlsaLayer (const AlsaLayer& rh);
181
// Assignment Operator
182
AlsaLayer& operator= (const AlsaLayer& rh);
184
bool is_playback_prepared (void) {
185
return _is_prepared_playback;
187
bool is_capture_prepared (void) {
188
return _is_prepared_capture;
190
void prepare_playback (void) {
191
_is_prepared_playback = true;
193
void prepare_capture (void) {
194
_is_prepared_capture = true;
196
bool is_capture_running (void) {
197
return _is_running_capture;
199
bool is_playback_running (void) {
200
return _is_running_playback;
202
void start_playback (void) {
203
_is_running_playback = true;
205
void stop_playback (void) {
206
_is_running_playback = false;
207
_is_prepared_playback = false;
209
void start_capture (void) {
210
_is_running_capture = true;
212
void stop_capture (void) {
213
_is_running_capture = false;
214
_is_prepared_capture = false;
216
void close_playback (void) {
217
_is_open_playback = false;
219
void close_capture (void) {
220
_is_open_capture = false;
222
void open_playback (void) {
223
_is_open_playback = true;
225
void open_capture (void) {
226
_is_open_capture = true;
228
bool is_capture_open (void) {
229
return _is_open_capture;
231
bool is_playback_open (void) {
232
return _is_open_playback;
236
* Drop the pending frames and close the capture device
239
void closeCaptureStream (void);
240
void stopCaptureStream (void);
241
void startCaptureStream (void);
242
void prepareCaptureStream (void);
244
void closePlaybackStream (void);
245
void stopPlaybackStream (void);
246
void startPlaybackStream (void);
247
void preparePlaybackStream (void);
250
* Open the specified device.
252
* @param pcm_p The string name for the playback device
253
* @param pcm_c The string name for the capture device
254
* @param flag To indicate which kind of stream you want to open
258
* @return true if successful
261
bool open_device (std::string pcm_p, std::string pcm_c, std::string pcm_r, int flag);
263
bool alsa_set_params (snd_pcm_t *pcm_handle, int type, int rate);
266
* Copy a data buffer in the internal ring buffer
268
* @param buffer The data to be copied
269
* @param length The size of the buffer
270
* @return int The number of frames actually copied
272
int write (void* buffer, int length, snd_pcm_t *handle);
275
* Read data from the internal ring buffer
277
* @param buffer The buffer to stock the read data
278
* @param toCopy The number of bytes to get
279
* @return int The number of frames actually read
281
int read (void* buffer, int toCopy);
284
* Recover from XRUN state for capture
287
void handle_xrun_capture (void);
290
* Recover from XRUN state for playback
293
void handle_xrun_playback (snd_pcm_t *handle);
295
void* adjustVolume (void* buffer , int len, int stream);
298
* Handles to manipulate playback stream
301
snd_pcm_t* _PlaybackHandle;
304
* Handles to manipulate ringtone stream
307
snd_pcm_t *_RingtoneHandle;
310
* Handles to manipulate capture stream
313
snd_pcm_t* _CaptureHandle;
316
* Alsa parameter - Size of a period in the hardware ring buffer
318
snd_pcm_uframes_t _periodSize;
321
* name of the alsa audio plugin used
323
std::string _audioPlugin;
325
/** Vector to manage all soundcard index - description association of the system */
326
std::vector<HwIDPair> IDSoundCards;
328
bool _is_prepared_playback;
329
bool _is_prepared_capture;
330
bool _is_running_playback;
331
bool _is_running_capture;
332
bool _is_open_playback;
333
bool _is_open_capture;
334
bool _trigger_request;
336
AudioThread* _audioThread;
338
/** Sample rate converter object */
339
SamplerateConverter* _converter;
341
// ofstream *captureFile;