-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.cpp
418 lines (366 loc) · 13.8 KB
/
audio.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "audio.hpp"
#include <iostream>
#include <cmath>
/* NOTE: I raised to a huge value (32768) to reduce clicking in output,
* but this caused CPU utilization to soar, so I reduced size again.
**/
#define FRAMES_PER_BUFFER paFramesPerBufferUnspecified //(1<<16) // PortAudio buf size. The examples use 256.
int SAMPLE_RATE;
using namespace std;
AudioBuf::AudioBuf( ){
this->num_samples=0;
this->data = NULL;
}
AudioBuf::AudioBuf( string filename ){
cerr << "audio file read unimplemented"<<endl;
}
AudioBuf::AudioBuf( duration_t length ){
this->num_samples = (unsigned int)ceil( length * SAMPLE_RATE );
this->data = new sample_t[this->num_samples];
}
AudioBuf::~AudioBuf(){
if( this->data ) delete this->data; // free sample array
}
sample_t& AudioBuf::operator[]( unsigned int index ) const{
return this->data[index];
}
AudioBuf AudioBuf::operator*( float gain ){
AudioBuf ret = AudioBuf( this->get_length() );
unsigned int i;
for( i=0; i<this->num_samples; i++ ){
ret[i] = (*this)[i] * gain;
}
return ret;
}
void AudioBuf::prepend_silence( duration_t silence_duration ){
cerr << "prepend_silence unimplemented"<<endl;
}
duration_t AudioBuf::get_length() const{
return ( this->num_samples / SAMPLE_RATE );
}
unsigned int AudioBuf::get_num_samples() const{
return this->num_samples;
}
sample_t* AudioBuf::get_array() const{
return this->data;
}
AudioBuf* AudioBuf::window( duration_t length, duration_t start ) const{
unsigned int start_index;
start_index = (unsigned int)floor( start * SAMPLE_RATE );
AudioBuf* ret = new AudioBuf( length );
unsigned int i;
for( i=0; i < ret->get_num_samples(); i++ ){
(*ret)[i] = this->data[start_index+i];
}
return ret;
}
void AudioBuf::mix( const AudioBuf &b ){
unsigned int i;
for( i=0; i < this->num_samples && i < b.get_num_samples(); i++ ){
this->data[i] += b[i];
}
}
AudioBuf AudioBuf::repeat( int repetitions ) const{
AudioBuf ret = AudioBuf( this->get_length() * repetitions );
unsigned int i;
for( i=0; i < ret.get_num_samples(); i++ ){
ret[i] = this->data[ i % this->get_num_samples() ];
}
return ret;
}
bool AudioBuf::write_to_file( string filename ) const{
cerr << "audio file writing unimplemented"<<endl;
return true;
}
#define HIGH_VOLUME_LEVEL (1.0)
#define LOW_VOLUME_LEVEL (0.000001) // zero value would break code below
#define FADE_TIME (0.1) // time in seconds taken to fade ping in/out @44.1kHz
AudioRequest::AudioRequest( const AudioBuf & buf ){
this->progress_index = 0; // set to zero so playback starts at beginning
this->audio = buf;
// store pointers to volume variables in the device structure.
this->targetVolumeLevel = 1.0;
this->currentVolumeLevel = 1.0;
// set volumeStepFactor such that multiplying (or dividing) by it before each
// audio sample will get from high to low (or low to high) volume levels
// after FADE_TIME
this->volumeStepFactor = pow( HIGH_VOLUME_LEVEL/LOW_VOLUME_LEVEL,
1.0/(FADE_TIME*44100) );
}
/** this constructor is used for recording, so we have to allocate an new
audio buffer */
AudioRequest::AudioRequest( duration_t len ){
this->progress_index = 0; // set to zero so playback starts at beginning
this->audio = *(new AudioBuf( len ));
}
bool AudioRequest::done(){
return this->progress_index > this->audio.get_num_samples();
}
void AudioRequest::fade( float final_level ){
this->targetVolumeLevel = final_level;
//wait for fade to finish, note that we give it an extra 500ms
Pa_Sleep( 1000 * FADE_TIME * 44100/SAMPLE_RATE );
}
void AudioDev::init(){
check_error( Pa_Initialize() ); // Initialize PortAudio
}
void AudioDev::terminate(){
check_error( Pa_Terminate() ); // Close PortAudio, this is important!
}
// reset volume levels
AudioDev::AudioDev(){}
AudioDev::AudioDev( unsigned int in_dev_num, unsigned int out_dev_num ){
this->choose_device( in_dev_num, out_dev_num );
}
AudioDev::~AudioDev(){}
bool AudioDev::choose_device( unsigned int in_dev_num,
unsigned int out_dev_num ){
// now create the device definition
this->in_params.channelCount = 1;
this->in_params.device = in_dev_num;
this->in_params.sampleFormat = paFloat32;
this->in_params.suggestedLatency = Pa_GetDeviceInfo(in_dev_num)->defaultHighInputLatency ;
this->in_params.hostApiSpecificStreamInfo = NULL; //See you specific host's API docs for info on using this field
this->out_params.channelCount = 2;
this->out_params.device = out_dev_num;
this->out_params.sampleFormat = paFloat32;
this->out_params.suggestedLatency = Pa_GetDeviceInfo(out_dev_num)->defaultHighOutputLatency;
this->out_params.hostApiSpecificStreamInfo = NULL; //See you specific host's API docs for info on using this field
// set global SAMPLE_RATE
int i;
const int rates[6] = {96000, 48000, 44100, 22050, 16000, 8000};
for( i=0; i<6; i++ ){
if( Pa_IsFormatSupported( &this->in_params,NULL,rates[i] ) == paNoError &&
Pa_IsFormatSupported( NULL,&this->out_params,rates[i] ) == paNoError ){
SAMPLE_RATE=rates[i];
cerr << "Sample rate = "<<SAMPLE_RATE<<"Hz"<<endl;
return true;
}
}
cerr<<"ERROR: no supported sample rate found!"<<endl;
return false;
}
vector<string> AudioDev::list_devices(){
vector<string> ret;
// get the total number of devices
int numDevices = Pa_GetDeviceCount();
if( numDevices < 0 ){
cerr<< "ERROR: Pa_CountDevices returned "<<numDevices<<endl;
}
// get data on each of the devices
const PaDeviceInfo *deviceInfo;
int i;
for( i=0; i<numDevices; i++ ){
deviceInfo = Pa_GetDeviceInfo( i );
ret.push_back( deviceInfo->name );
}
return ret;
}
pair<unsigned int,unsigned int> AudioDev::prompt_device(){
vector<string> devices = this->list_devices();
// get the total number of devices
int numDevices = devices.size();
// get data on each of the devices
cout << "These are the available audio devices:" << endl;
int i;
for( i=0; i<numDevices; i++ ){
cout << i << ": " << devices[i] <<endl;
}
unsigned int in_dev_num, out_dev_num;
// prompt user on which of the above devices to use
cout<<endl<<"Please enter an input device number [0-"<<(numDevices-1)<<"]: ";
cin >> in_dev_num;
cout<<endl<<"Please enter an output device number [0-"<<(numDevices-1)<<"]: ";
cin >> out_dev_num;
return make_pair( in_dev_num, out_dev_num );
}
/** here, we are copying data from the AudioBuf (stored in userData) into
outputBuffer and then updating the progress_index so that we know where
to start the next time this callback function is called */
int AudioDev::player_callback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData ){
/* Cast data passed through stream to our structure. */
AudioRequest *req = (AudioRequest*)userData;
sample_t *out = (sample_t*)outputBuffer;
(void) inputBuffer; /* Prevent unused variable warning. */
unsigned int i, total_samples = req->audio.get_num_samples();
for( i=req->progress_index; i < req->progress_index + framesPerBuffer; i++ ){
if( i < total_samples ){
*out++ = req->audio[i]; /* left */
}else{
*out++ = 0; // play silence if we've reached end of buffer
}
*out++ = 0; // right channel is silent
}
req->progress_index = i; // update progress index
// we would return 1 when playback is complete (ie when we want the stream
// to die), otherwise return 0
return req->done();
}
/** Similiar to player_callback, but "wrap around" buffer indices */
int AudioDev::oscillator_callback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData ){
/* Cast data passed through stream to our structure. */
AudioRequest *req = (AudioRequest*)userData;
sample_t *out = (sample_t*)outputBuffer;
(void) inputBuffer; /* Prevent unused variable warning. */
float target = req->targetVolumeLevel;
if( target < LOW_VOLUME_LEVEL ){ // we can't ever reach zero
target = LOW_VOLUME_LEVEL;
}
// TODO: this code is not exactly thread-safe, but consequences are
// probably not serious
unsigned int i, total_samples = req->audio.get_num_samples();
for( i=0; i<framesPerBuffer; i++ ){
if( req->currentVolumeLevel != target ){
if( req->currentVolumeLevel > target ){
req->currentVolumeLevel /= req->volumeStepFactor;
// verify that we didn't go too far
if( req->currentVolumeLevel < target )
req->currentVolumeLevel = target;
}else{
req->currentVolumeLevel *= req->volumeStepFactor;
// verify that we didn't go too far
if( req->currentVolumeLevel > target )
req->currentVolumeLevel = target;
}
}
*out++ = req->currentVolumeLevel *
req->audio[ ( req->progress_index + i ) % total_samples ]; // left
*out++ = 0; // right
}
// update progress index
req->progress_index = ( req->progress_index + i ) % total_samples;
return 0; // returning 0 makes the stream stay alive.
}
/** here, we are copying data from the input buffer into the AudioBuf.
and then updating the progress_index so that we know where
to start the next time this callback function is called */
int AudioDev::recorder_callback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData ){
/* Cast data passed through stream to our structure. */
AudioRequest *req = (AudioRequest*)userData;
sample_t *in = (sample_t*)inputBuffer;
(void) outputBuffer; /* Prevent unused variable warning. */
unsigned int i;
for( i=req->progress_index; i < req->progress_index + framesPerBuffer; i++ ){
if( i < req->audio.get_num_samples() ){
req->audio[i] = *in++; /* there is only one channel */
}
}
req->progress_index = i; // update progress index
// we would return 1 when playback is complete (ie when we want the stream
// to die), otherwise return 0
return req->done();
}
AudioRequest* AudioDev::nonblocking_play( const AudioBuf & buf ){
PaStream *stream;
AudioRequest *play_request = new AudioRequest( buf );
/* Open an audio I/O stream. */
check_error( Pa_OpenStream(
&stream,
NULL, /* no input channels */
&(this->out_params),
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, // we won't output out of range samples so don't bother
// otherwise set to paNoFlag,
AudioDev::player_callback, /* this is your callback function */
play_request ) ); /*This is a pointer that will be passed to
your callback*/
play_request->stream = stream;
// start playback
check_error( Pa_StartStream( stream ) );
// caller is responsible for freeing stream
return play_request;
}
AudioRequest* AudioDev::nonblocking_play_loop( const AudioBuf & buf ){
PaStream *stream;
AudioRequest *play_request = new AudioRequest( buf );
/* Open an audio I/O stream. */
check_error( Pa_OpenStream(
&stream,
NULL, /* no input channels */
&(this->out_params),
SAMPLE_RATE,
FRAMES_PER_BUFFER, /* frames per buffer */
paClipOff, // we won't output out of range samples so don't bother
// otherwise set to paNoFlag,
AudioDev::oscillator_callback, /* this is your callback function */
play_request ) ); /*This is a pointer that will be passed to
your callback*/
play_request->stream = stream;
// start playback
check_error( Pa_StartStream( stream ) );
// caller is responsible for freeing stream
return play_request;
}
void AudioDev::blocking_play( const AudioBuf & buf ){
PaStream *stream;
AudioRequest *play_request = new AudioRequest( buf );
/* Open an audio I/O stream. */
check_error( Pa_OpenStream(
&stream,
NULL, /* no input channels */
&(this->out_params),
SAMPLE_RATE,
FRAMES_PER_BUFFER, /* frames per buffer */
paClipOff, // we won't output out of range samples so don't bother
// otherwise set to paNoFlag,
AudioDev::player_callback, /* this is your callback function */
play_request ) ); /*This is a pointer that will be passed to
your callback*/
// start playback
check_error( Pa_StartStream( stream ) );
while( Pa_IsStreamActive( stream ) ) Pa_Sleep( 100 );
//check_error( Pa_StopStream( stream ) );
check_error( Pa_CloseStream( stream ) );
}
AudioBuf AudioDev::blocking_record( duration_t duration ){
PaStream *stream;
AudioRequest *rec_request = new AudioRequest( duration );
/* Open an audio I/O stream. */
check_error( Pa_OpenStream (
&stream,
&(this->in_params),
NULL, /* no output channels */
SAMPLE_RATE,
FRAMES_PER_BUFFER, /* frames per buffer */
paNoFlag,
AudioDev::recorder_callback, /* this is your callback function */
rec_request ) ); /*This is a pointer that will be passed to
your callback*/
// start recording
check_error( Pa_StartStream( stream ) );
// wait until done, sleeping 100ms at a time
while( Pa_IsStreamActive( stream ) ) Pa_Sleep( 100 );
//check_error( Pa_StopStream( stream ) );
check_error( Pa_CloseStream( stream ) );
return rec_request->audio;
}
/** This function could probably be more accurately (re: synchronization)
implemented by creating a new duplex stream. For sonar purposes,
it shouldn't be necessary*/
AudioBuf AudioDev::recordback( const AudioBuf & buf ){
// play audio
PaStream* s = nonblocking_play( buf );
// record echo
AudioBuf ret = blocking_record( buf.get_length() );
check_error( Pa_CloseStream( s ) ); // close stream to free up dev
return ret;
}
PaError AudioDev::check_error( PaError err ){
if( err < 0 ){
cerr << "PortAudio error: " << Pa_GetErrorText( err ) << endl;
}
return err;
}