-
Notifications
You must be signed in to change notification settings - Fork 16
/
CNFA_alsa.c
376 lines (316 loc) · 9.55 KB
/
CNFA_alsa.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
//Copyright 2015-2020 <>< Charles Lohr under the MIT/x11, NewBSD or ColorChord License. You choose.
#include "CNFA.h"
#include "os_generic.h"
#include <alsa/asoundlib.h>
#include <string.h>
struct CNFADriverAlsa
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
char * devRec;
char * devPlay;
snd_pcm_uframes_t bufsize;
og_thread_t threadPlay;
og_thread_t threadRec;
snd_pcm_t *playback_handle;
snd_pcm_t *record_handle;
char playing;
char recording;
};
int CNFAStateAlsa( void * v )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)v;
return ((r->playing)?2:0) | ((r->recording)?1:0);
}
void CloseCNFAAlsa( void * v )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)v;
if( r )
{
if( r->threadPlay ) OGJoinThread( r->threadPlay );
if( r->threadRec ) OGJoinThread( r->threadRec );
OGUSleep(2000);
if( r->playback_handle ) snd_pcm_close (r->playback_handle);
if( r->record_handle ) snd_pcm_close (r->record_handle);
OGUSleep(2000);
if( r->devRec ) free( r->devRec );
if( r->devPlay ) free( r->devPlay );
free( r );
}
}
static int SetHWParams( snd_pcm_t * handle, int * samplerate, short * channels, snd_pcm_uframes_t * bufsize, struct CNFADriverAlsa * a )
{
int err;
int bufs;
int dir;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
return -1;
}
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE )) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, (unsigned int*)samplerate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *channels)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
goto fail;
}
dir = 0;
if( (err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, bufsize, &dir)) < 0 )
{
fprintf( stderr, "cannot set period size. (%s)\n",
snd_strerror(err) );
goto fail;
}
//NOTE: This step is critical for low-latency sound.
bufs = *bufsize*3;
if( (err = snd_pcm_hw_params_set_buffer_size(handle, hw_params, bufs)) < 0 )
{
fprintf( stderr, "cannot set snd_pcm_hw_params_set_buffer_size size. (%s)\n",
snd_strerror(err) );
goto fail;
}
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
goto fail;
}
snd_pcm_hw_params_free (hw_params);
return 0;
fail:
snd_pcm_hw_params_free (hw_params);
return -2;
}
static int SetSWParams( struct CNFADriverAlsa * d, snd_pcm_t * handle, int isrec )
{
snd_pcm_sw_params_t *sw_params;
int err;
//Time for software parameters:
if( !isrec )
{
if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
snd_strerror (err));
goto failhard;
}
if ((err = snd_pcm_sw_params_current (handle, sw_params)) < 0) {
fprintf (stderr, "cannot initialize software parameters structure (%s) (%p)\n",
snd_strerror (err), handle);
goto fail;
}
int buffer_size = d->bufsize*3;
int period_size = d->bufsize;
printf( "PERIOD: %d BUFFER: %d\n", period_size, buffer_size );
if ((err = snd_pcm_sw_params_set_avail_min (handle, sw_params, period_size )) < 0) {
fprintf (stderr, "cannot set minimum available count (%s)\n",
snd_strerror (err));
goto fail;
}
//if ((err = snd_pcm_sw_params_set_stop_threshold(handle, sw_params, 512 )) < 0) {
// fprintf (stderr, "cannot set minimum available count (%s)\n",
// snd_strerror (err));
// goto fail;
//}
if ((err = snd_pcm_sw_params_set_start_threshold(handle, sw_params, buffer_size - period_size )) < 0) {
fprintf (stderr, "cannot set minimum available count (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
fprintf (stderr, "cannot set software parameters (%s)\n",
snd_strerror (err));
goto fail;
}
}
if ((err = snd_pcm_prepare (handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
goto fail;
}
return 0;
fail:
if( !isrec )
{
snd_pcm_sw_params_free (sw_params);
}
failhard:
return -1;
}
void * RecThread( void * v )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)v;
short samples[r->bufsize * r->channelsRec];
snd_pcm_start(r->record_handle);
do
{
int err = snd_pcm_readi( r->record_handle, samples, r->bufsize );
if( err < 0 )
{
fprintf( stderr, "Warning: ALSA Recording Failed\n" );
break;
}
if( err != r->bufsize )
{
fprintf( stderr, "Warning: ALSA Recording Underflow\n" );
}
r->recording = 1;
r->callback( (struct CNFADriver *)r, 0, samples, 0, err );
} while( 1 );
r->recording = 0;
fprintf( stderr, "ALSA Recording Stopped\n" );
return 0;
}
void * PlayThread( void * v )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)v;
short samples[r->bufsize * r->channelsPlay];
int err;
//int total_avail = snd_pcm_avail(r->playback_handle);
snd_pcm_start(r->playback_handle);
r->callback( (struct CNFADriver *)r, samples, 0, r->bufsize, 0 );
err = snd_pcm_writei(r->playback_handle, samples, r->bufsize);
while( err >= 0 )
{
// int avail = snd_pcm_avail(r->playback_handle);
// printf( "avail: %d\n", avail );
r->callback( (struct CNFADriver *)r, samples, 0, r->bufsize, 0 );
err = snd_pcm_writei(r->playback_handle, samples, r->bufsize);
if( err != r->bufsize )
{
fprintf( stderr, "Warning: ALSA Playback Overflow\n" );
}
r->playing = 1;
}
r->playing = 0;
fprintf( stderr, "ALSA Playback Stopped\n" );
return 0;
}
static struct CNFADriverAlsa * InitALSA( struct CNFADriverAlsa * r )
{
printf( "CNFA Alsa Init %p %p (%d %d) %d %d\n", r->playback_handle, r->record_handle, r->spsPlay, r->spsRec, r->channelsPlay, r->channelsRec );
int err;
if( r->channelsPlay )
{
if ((err = snd_pcm_open (&r->playback_handle, r->devPlay?r->devPlay:"default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open output audio device (%s)\n",
snd_strerror (err));
goto fail;
}
}
if( r->channelsRec )
{
if ((err = snd_pcm_open (&r->record_handle, r->devRec?r->devRec:"default", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open input audio device (%s)\n",
snd_strerror (err));
goto fail;
}
}
printf( "%p %p\n", r->playback_handle, r->record_handle );
if( r->playback_handle )
{
if( SetHWParams( r->playback_handle, &r->spsPlay, &r->channelsPlay, &r->bufsize, r ) < 0 )
goto fail;
if( SetSWParams( r, r->playback_handle, 0 ) < 0 )
goto fail;
}
if( r->record_handle )
{
if( SetHWParams( r->record_handle, &r->spsRec, &r->channelsRec, &r->bufsize, r ) < 0 )
goto fail;
if( SetSWParams( r, r->record_handle, 1 ) < 0 )
goto fail;
}
#if 0
if( r->playback_handle )
{
snd_async_handler_t *pcm_callback;
//Handle automatically cleaned up when stream closed.
err = snd_async_add_pcm_handler(&pcm_callback, r->playback_handle, playback_callback, r);
if(err < 0)
{
printf("Playback callback handler error: %s\n", snd_strerror(err));
}
}
if( r->record_handle )
{
snd_async_handler_t *pcm_callback;
//Handle automatically cleaned up when stream closed.
err = snd_async_add_pcm_handler(&pcm_callback, r->record_handle, record_callback, r);
if(err < 0)
{
printf("Record callback handler error: %s\n", snd_strerror(err));
}
}
#endif
if( r->playback_handle && r->record_handle )
{
err = snd_pcm_link ( r->playback_handle, r->record_handle );
if(err < 0)
{
printf("snd_pcm_link error: %s\n", snd_strerror(err));
}
}
if( r->playback_handle )
{
r->threadPlay = OGCreateThread( PlayThread, r );
}
if( r->record_handle )
{
r->threadRec = OGCreateThread( RecThread, r );
}
printf( "CNFA Alsa Init Out -> %p %p (%d %d) %d %d\n", r->playback_handle, r->record_handle, r->spsPlay, r->spsRec, r->channelsPlay, r->channelsRec );
return r;
fail:
if( r )
{
if( r->playback_handle ) snd_pcm_close (r->playback_handle);
if( r->record_handle ) snd_pcm_close (r->record_handle);
free( r );
}
fprintf( stderr, "Error: ALSA failed to start.\n" );
return 0;
}
void * InitALSADriver( CNFACBType cb, const char * your_name, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)malloc( sizeof( struct CNFADriverAlsa ) );
r->CloseFn = CloseCNFAAlsa;
r->StateFn = CNFAStateAlsa;
r->callback = cb;
r->opaque = opaque;
r->spsPlay = reqSPSPlay;
r->spsRec = reqSPSRec;
r->channelsPlay = reqChannelsPlay;
r->channelsRec = reqChannelsRec;
r->devRec = (inputSelect)?strdup(inputSelect):0;
r->devPlay = (outputSelect)?strdup(outputSelect):0;
r->playback_handle = 0;
r->record_handle = 0;
r->bufsize = sugBufferSize;
return InitALSA(r);
}
REGISTER_CNFA( ALSA, 10, "ALSA", InitALSADriver );