-
Notifications
You must be signed in to change notification settings - Fork 8
/
liveio.c
499 lines (437 loc) · 14.6 KB
/
liveio.c
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* @file liveio.c
* Implementation of live audio play/record system. This is basically a wrapper
* the PortAudio library (http://portaudio.com)
*
* @author Chuck Wooters <[email protected]>
*/
#include "liveio.h"
typedef struct
{
int16_t* audiodata; /**< buffer pointer */
unsigned long len; /**< length of buffer pointed to by audiodata */
unsigned long pos_r; /**< current read position (for playing) */
unsigned long pos_w; /**< current write position (for recording) */
} LA_simpleData;
static int LA_InitDone = 0; /**< Has local audio been initialized? */
static PaError LA_LastError = paNoError; /**< Error code for the last error encountered */
static PaStream* LA_PlayStream = NULL; /**< Currently active play stream */
static PaStream* LA_RecordStream = NULL; /**< Currently active record stream */
static LA_simpleData LA_data; /**< Communication structure */
/**
* Return the current time (in seconds) according the record stream.
*/
double LA_get_cur_record_time(void) {
if (Pa_IsStreamActive(LA_RecordStream) == 1) {
return Pa_GetStreamTime(LA_RecordStream);
}
return -1.0;
}
double LA_get_cur_play_time(void) {
if (Pa_IsStreamActive(LA_PlayStream) == 1) {
return Pa_GetStreamTime(LA_PlayStream);
}
return -1.0;
}
void LA_stop_recording(void) {
if (LA_is_recording() == 1) {
Pa_CloseStream(LA_RecordStream);
}
}
void LA_stop_playing(void) {
if (LA_is_playing() == 1) {
Pa_CloseStream(LA_PlayStream);
}
}
/**
* A callback function for continuously recording audio into a buffer.
*/
static int _ContinuousRecordCallback(const void* inputBuffer,
void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData )
{
LA_simpleData *data = (LA_simpleData*)userData;
const int16_t* rptr = (const int16_t*)inputBuffer; /* read pointer */
unsigned long i;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
if( inputBuffer == NULL ) {
for( i=0; i<framesPerBuffer; i++ ) {
data->audiodata[data->pos_w++] = 0;
if (data->pos_w >= data->len) data->pos_w=0;
}
} else {
for( i=0; i<framesPerBuffer; i++ ) {
data->audiodata[data->pos_w++] = *rptr++;
if (data->pos_w >= data->len) data->pos_w=0;
}
}
return paContinue;
}
/**
* A callback function for recording audio into a buffer.
*/
static int _RecordCallback(const void* inputBuffer,
void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData )
{
LA_simpleData *data = (LA_simpleData*)userData;
const int16_t* rptr = (const int16_t*)inputBuffer;
int16_t* wptr = &data->audiodata[data->pos_w];
unsigned long framesToRec;
unsigned long i;
int finished;
unsigned long framesLeft = data->len - data->pos_w - 1;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
if( framesLeft < framesPerBuffer ) {
framesToRec = framesLeft;
finished = paComplete;
} else {
framesToRec = framesPerBuffer;
finished = paContinue;
}
if( inputBuffer == NULL ) {
for( i=0; i<framesToRec; i++ ) {
*wptr++ = 0;
}
} else {
for( i=0; i<framesToRec; i++ ) {
*wptr++ = *rptr++;
}
}
data->pos_w += framesToRec;
return finished;
}
/**
* A callback function for playing audio from a buffer.
*/
static int _PlayCallback( const void* inputBuffer,
void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData )
{
LA_simpleData* data = (LA_simpleData*)userData;
int16_t* out = (int16_t*) outputBuffer;
(void) timeInfo;
(void) statusFlags;
(void) inputBuffer;
int returnval=paContinue;
unsigned long nout = framesPerBuffer;
if ( (data->pos_r + framesPerBuffer) >= data->len) {
returnval = paComplete;
nout = data->len - data->pos_r -1;
}
for(unsigned long i=0; i<nout; i++) {
*out++ = data->audiodata[data->pos_r];
*out++ = data->audiodata[data->pos_r];
data->pos_r += 1;
}
return returnval;
}
/**
* A callback routine that is called when a record stream is
* finished.
*/
static void _RecordStreamFinished( void* userData )
{
int err = Pa_CloseStream(LA_RecordStream);
if (err != paNoError)
printf("PortAudio Error: %s\n",Pa_GetErrorText(err));
}
/**
* A callback routine that is called when a playback stream is
* finished.
*/
static void _PlayStreamFinished( void* userData )
{
int err = Pa_CloseStream(LA_PlayStream);
if (err != paNoError)
printf("PortAudio Error: %s\n",Pa_GetErrorText(err));
}
/**
* Determine if the recording stream is currently recording.
*
* @returns 1 if currently recording, 0 if not, and -1 if there was an error.
*/
int LA_is_recording(void) {
int res = Pa_IsStreamActive(LA_RecordStream);
if (res == 1 || res == 0) return res;
return -1; /* indicates and error */
}
/**
* Determine if the playback stream is currently playing something.
*
* @returns 1 if currently playing, 0 if not, and -1 if there was an error.
*/
int LA_is_playing(void) {
int res = Pa_IsStreamActive(LA_PlayStream);
if (res == 1 || res == 0) return res;
return -1; /* indicates and error */
}
/**
* Record from the mic using the user-specified callback and data structure.
*
* @param samprate Sampling rate of the audio
* @param cb A callback function to pass to Pa_OpenStream()
* @param cb_data A pointer to a data structure to be passed to the callback function
*
* @returns A value of type ::LaError_t where LA_OK indicates there were no problems, and a value of LA_ERROR
* indicates that some error occurred. Use ::LA_print_last_error() to print the error.
*
* @note This function is non-blocking and will return immediately (i.e. while it is still
* recording. So use ::LA_is_recording() to determine when the sound is finished playing.
*
*/
LaError_t LA_record_callback(const unsigned samprate, PaStreamCallback* cb, void* cb_data) {
if (LA_InitDone == 0) { /* initialize portaudio system, if needed */
int failed = LA_init();
if (failed) return LA_ERROR;
}
if (LA_is_recording() == 1) return LA_ERROR;
PaStreamParameters inputParameters;
inputParameters.device = Pa_GetDefaultInputDevice();
if (inputParameters.device == paNoDevice) {
LA_LastError = paNoDevice;
return LA_ERROR;
}
inputParameters.channelCount = 1;
inputParameters.sampleFormat = paInt16; /* 16 bit ints */
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
PaError err;
err = Pa_OpenStream(&LA_RecordStream,
&inputParameters,
NULL,
samprate,
paFramesPerBufferUnspecified,
paClipOff,
cb,
cb_data );
if( err != paNoError ) return LA_ERROR;
err = Pa_StartStream(LA_RecordStream);
if (err != paNoError) return LA_ERROR;
return LA_OK;
}
/**
* Record from the mic and save into a buffer.
*
* @param buffer A buffer of 16-bit ints
* @param bufsize Size of the \a buffer
* @param samprate Sampling rate of the audio
*
* @returns A value of type ::LaError_t where LA_OK indicates there were no problems, and a value of LA_ERROR
* indicates that some error occurred. Use ::LA_print_last_error() to print the error.
*
* @note This function is non-blocking and will return immediately (i.e. while it is still
* recording. So use ::LA_is_recording() to determine when the sound is finished playing.
*
*/
LaError_t LA_record(void* const buffer, unsigned long bufsize, unsigned samprate, int rectype) {
if (LA_InitDone == 0) { /* initialize portaudio system, if needed */
int failed = LA_init();
if (failed) return LA_ERROR;
}
if (LA_is_recording() == 1) return LA_ERROR;
LA_data.audiodata = (int16_t*) buffer;
LA_data.len = bufsize;
LA_data.pos_w = 0;
PaStreamParameters inputParameters;
inputParameters.device = Pa_GetDefaultInputDevice();
if (inputParameters.device == paNoDevice) {
LA_LastError = paNoDevice;
return LA_ERROR;
}
//printf( "Input device # %d.\n", inputParameters.device );
//printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency );
//printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency );
inputParameters.channelCount = 1;
inputParameters.sampleFormat = paInt16; /* 16 bit ints */
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
PaError err;
if (rectype == LA_REC_ONCE) {
err = Pa_OpenStream(&LA_RecordStream,
&inputParameters,
NULL,
samprate,
paFramesPerBufferUnspecified,
paClipOff,
_RecordCallback,
&LA_data );
} else if (rectype == LA_REC_CONT) {
err = Pa_OpenStream(&LA_RecordStream,
&inputParameters,
NULL,
samprate,
paFramesPerBufferUnspecified,
paClipOff,
_ContinuousRecordCallback,
&LA_data );
} else {
return LA_ERROR;
}
if( err != paNoError ) return LA_ERROR;
if (rectype == LA_REC_ONCE) {
err = Pa_SetStreamFinishedCallback(LA_RecordStream, _RecordStreamFinished);
if (err != paNoError) return LA_ERROR;
}
err = Pa_StartStream(LA_RecordStream);
if (err != paNoError) return LA_ERROR;
return LA_OK;
}
/**
* Play sound to the speakers using the user-specified callback and data structure.
*
* @param samprate Sampling rate of the audio
* @param cb A callback function to pass to Pa_OpenStream()
* @param cb_data A pointer to a data structure to be passed to the callback function
*
* @returns A value of type ::LaError_t where LA_OK indicates there were no problems, and a value of LA_ERROR
* indicates that some error occurred. Use ::LA_print_last_error() to print the error.
*
* @note This function is non-blocking and will return immediately (i.e. before the sound has
* finished playing. So use ::LA_is_playing() to determine when the sound is finished playing.
*
*/
LaError_t LA_play_callback(const unsigned samprate, PaStreamCallback* cb, void* cb_data) {
if (LA_InitDone == 0) { /* initialize portaudio system, if needed */
int failed = LA_init();
if (failed) return LA_ERROR;
}
if (LA_is_playing() == 1) return LA_ERROR;
PaStreamParameters outputParameters;
outputParameters.device = Pa_GetDefaultOutputDevice();
//outputParameters.device = 4; // Soundflower (2ch)
if (outputParameters.device == paNoDevice) {
LA_LastError = paNoDevice;
return LA_ERROR;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paInt16; /* 16 bit int output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaError err;
err = Pa_OpenStream(&LA_PlayStream,
NULL,
&outputParameters,
samprate,
paFramesPerBufferUnspecified,
paNoFlag,
cb,
cb_data );
if( err != paNoError ) return LA_ERROR;
err = Pa_StartStream(LA_PlayStream);
if (err != paNoError) return LA_ERROR;
return LA_OK;
}
/**
* Play sound from a buffer of audio data.
*
* @param buffer A buffer of 16-bit ints
* @param bufsize Size of the \a buffer
* @param samprate Sampling rate of the audio
*
* @returns A value of type ::LaError_t where LA_OK indicates there were no problems, and a value of LA_ERROR
* indicates that some error occurred. Use ::LA_print_last_error() to print the error.
*
* @note This function is non-blocking and will return immediately (i.e. before the sound has
* finished playing. So use ::LA_is_playing() to determine when the sound is finished playing.
*
*/
LaError_t LA_play(const void* const buffer, unsigned long bufsize, unsigned samprate) {
if (LA_InitDone == 0) { /* initialize portaudio system, if needed */
int failed = LA_init();
if (failed) return LA_ERROR;
}
if (LA_is_playing() == 1) return LA_ERROR;
LA_data.audiodata = (int16_t*) buffer;
LA_data.len = bufsize;
LA_data.pos_r = 0;
PaStreamParameters outputParameters;
outputParameters.device = Pa_GetDefaultOutputDevice();
if (outputParameters.device == paNoDevice) {
LA_LastError = paNoDevice;
return LA_ERROR;
}
//printf( "Output device # %d.\n", outputParameters.device );
//printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency );
//printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency );
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paInt16; /* 16 bit int output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaError err;
err = Pa_OpenStream(&LA_PlayStream,
NULL,
&outputParameters,
samprate,
paFramesPerBufferUnspecified,
paNoFlag,
_PlayCallback,
&LA_data );
if( err != paNoError ) return LA_ERROR;
err = Pa_SetStreamFinishedCallback(LA_PlayStream, _PlayStreamFinished);
if (err != paNoError) return LA_ERROR;
err = Pa_StartStream(LA_PlayStream);
if (err != paNoError) return LA_ERROR;
return LA_OK;
}
/**
* Print last error to specified stream.
*/
void LA_print_last_error(FILE *iostream) {
fprintf(iostream, "PortAudio Error: %s\n",Pa_GetErrorText(LA_LastError));
}
/**
* Initialize local audio system.
*
* @warning This must be called before using the local audio system.
*
* @returns 0 on success, or 1 if there was a problem
*/
LaError_t LA_init() {
if (LA_InitDone) {
int ok = LA_terminate();
if (!ok) return LA_ERROR;
}
LA_LastError = Pa_Initialize();
if (LA_LastError != paNoError) {
LA_InitDone = 0;
return LA_ERROR;
}
bzero(&LA_data, sizeof(LA_data));
LA_InitDone = 1;
return LA_OK;
}
/**
* Terminate the local audio system.
*
* @warning It is important to call this routine when you are done with the local
* audio system.
*
* @returns 0 on success, or 1 if there was a problem
*/
LaError_t LA_terminate() {
if (LA_InitDone == 0) { /* Can't terminate if it is not initialized */
LA_LastError = paNotInitialized;
return LA_ERROR;
}
LA_LastError = Pa_Terminate();
if (LA_LastError != paNoError) {
LA_InitDone = 0;
return LA_ERROR;
}
LA_InitDone = 1;
return LA_OK;
}