-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCNFA_sun.c
277 lines (227 loc) · 6.31 KB
/
CNFA_sun.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
//Copyright 2015-2020 <>< Charles Lohr under the MIT/x11, NewBSD or ColorChord License. You choose.
#include "CNFA.h"
#include "os_generic.h"
#include <sys/audioio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
struct CNFADriverSun
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
char * devRec;
char * devPlay;
short * samplesRec;
short * samplesPlay;
og_thread_t threadPlay;
og_thread_t threadRec;
int bufsize;
int playback_handle;
int record_handle;
char playing;
char recording;
};
int CNFAStateSun( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
return ((r->playing)?2:0) | ((r->recording)?1:0);
}
void CloseCNFASun( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
if( r )
{
if( r->threadPlay ) OGJoinThread( r->threadPlay );
if( r->threadRec ) OGJoinThread( r->threadRec );
OGUSleep(2000);
if( r->playback_handle != -1 ) close (r->playback_handle);
if( r->record_handle != -1 ) close (r->record_handle);
OGUSleep(2000);
free( r->devRec );
free( r->devPlay );
free( r->samplesRec );
free( r->samplesPlay );
free( r );
}
}
void * RecThread( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
size_t nbytes = r->bufsize * (2 * r->channelsRec);
do
{
int nread = read( r->record_handle, r->samplesRec, nbytes );
if( nread < 0 )
{
fprintf( stderr, "Warning: Sun Recording Failed\n" );
break;
}
r->recording = 1;
r->callback( (struct CNFADriver *)r, NULL, r->samplesRec, 0, (nread / 2) / r->channelsRec);
} while( 1 );
r->recording = 0;
fprintf( stderr, "Sun Recording Stopped\n" );
return 0;
}
void * PlayThread( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
size_t nbytes = r->bufsize * (2 * r->channelsPlay);
int err;
r->callback( (struct CNFADriver *)r, r->samplesPlay, NULL, r->bufsize, 0 );
err = write( r->playback_handle, r->samplesPlay, nbytes );
while( err >= 0 )
{
r->callback( (struct CNFADriver *)r, r->samplesPlay, NULL, r->bufsize, 0 );
err = write( r->playback_handle, r->samplesPlay, nbytes );
r->playing = 1;
}
r->playing = 0;
fprintf( stderr, "Sun Playback Stopped\n" );
return 0;
}
static struct CNFADriverSun * InitSun( struct CNFADriverSun * r )
{
const char * devPlay = r->devPlay;
const char * devRec = r->devRec;
struct audio_info rinfo, pinfo;
if( devRec == NULL || strcmp ( devRec, "default" ) == 0 )
{
devRec = "/dev/audio";
}
if( devPlay == NULL || strcmp ( devPlay , "default" ) == 0 )
{
devPlay = "/dev/audio";
}
printf( "CNFA Sun Init -> devPlay: %s, channelsPlay: %d, spsPlay: %d, devRec: %s, channelsRec: %d, spsRec: %d\n", devPlay, r->channelsPlay, r->spsPlay, devRec, r->channelsRec, r->spsRec);
if( r->channelsPlay && r->channelsRec && strcmp (devPlay, devRec) == 0 )
{
if ( (r->playback_handle = r->record_handle = open (devPlay, O_RDWR)) < 0 )
{
fprintf (stderr, "cannot open audio device (%s)\n",
strerror (errno));
goto fail;
}
}
else
{
if( r->channelsPlay )
{
if ( (r->playback_handle = open (devPlay, O_WRONLY)) < 0 )
{
fprintf (stderr, "cannot open output audio device %s (%s)\n",
r->devPlay, strerror (errno));
goto fail;
}
}
if( r->channelsRec )
{
if ( (r->record_handle = open (devRec, O_RDONLY)) < 0 )
{
fprintf (stderr, "cannot open input audio device %s (%s)\n",
r->devRec, strerror (errno));
goto fail;
}
}
}
if( r->playback_handle )
{
AUDIO_INITINFO(&pinfo);
pinfo.play.precision = 16;
pinfo.play.encoding = AUDIO_ENCODING_LINEAR;
pinfo.play.sample_rate = r->spsPlay;
pinfo.play.channels = r->channelsPlay;
if ( ioctl(r->playback_handle, AUDIO_SETINFO, &pinfo) < 0 )
{
fprintf (stderr, "cannot set audio playback format (%s)\n",
strerror (errno));
goto fail;
}
if ( ioctl(r->playback_handle, AUDIO_GETINFO, &pinfo) < 0 )
{
fprintf (stderr, "cannot get audio record format (%s)\n",
strerror (errno));
goto fail;
}
r->spsPlay = pinfo.play.sample_rate;
r->channelsPlay = pinfo.play.channels;
if ( (r->samplesPlay = calloc(2 * r->channelsPlay, r->bufsize)) == NULL )
{
goto fail;
}
}
if( r->record_handle )
{
AUDIO_INITINFO(&rinfo);
rinfo.record.precision = 16;
rinfo.record.encoding = AUDIO_ENCODING_LINEAR;
rinfo.record.sample_rate = r->spsRec;
rinfo.record.channels = r->channelsRec;
if ( ioctl(r->record_handle, AUDIO_SETINFO, &rinfo) < 0 )
{
fprintf (stderr, "cannot set audio record format (%s)\n",
strerror (errno));
goto fail;
}
if ( ioctl(r->record_handle, AUDIO_GETINFO, &rinfo) < 0 )
{
fprintf (stderr, "cannot get audio record format (%s)\n",
strerror (errno));
goto fail;
}
r->spsRec = rinfo.record.sample_rate;
r->channelsRec = rinfo.record.channels;
if ( (r->samplesRec = calloc(2 * r->channelsRec, r->bufsize)) == NULL )
{
goto fail;
}
}
if( r->playback_handle )
{
r->threadPlay = OGCreateThread( PlayThread, r );
}
if( r->record_handle )
{
r->threadRec = OGCreateThread( RecThread, r );
}
printf( "CNFA Sun Init Out -> channelsPlay: %d, spsPlay: %d, channelsRec: %d, spsRec: %d\n", r->channelsPlay, r->spsPlay, r->channelsRec, r->spsRec);
return r;
fail:
if( r )
{
if( r->playback_handle != -1 ) close (r->playback_handle);
if( r->record_handle != -1 ) close (r->record_handle);
free( r->samplesPlay );
free( r->samplesRec );
free( r );
}
return 0;
}
void * InitSunDriver( 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 CNFADriverSun * r = (struct CNFADriverSun *)malloc( sizeof( struct CNFADriverSun ) );
r->CloseFn = CloseCNFASun;
r->StateFn = CNFAStateSun;
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->samplesPlay = NULL;
r->samplesRec = NULL;
r->playback_handle = -1;
r->record_handle = -1;
r->bufsize = sugBufferSize;
return InitSun(r);
}
REGISTER_CNFA( SUN, 10, "Sun", InitSunDriver );