-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnas_a.cpp
More file actions
427 lines (372 loc) · 9.06 KB
/
nas_a.cpp
File metadata and controls
427 lines (372 loc) · 9.06 KB
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
/*
Copyright (C) 1999 Michael Haardt <michael@moria.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
nas_a.c
Functions to play sound on the Network Audio System
*/
#ifdef AU_NAS
#ifdef ORIG_TIMPP
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#endif
#include <audio/audiolib.h>
#include <audio/soundlib.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef NO_STRING_H /* for memmove */
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef ORIG_TIMPP
#include "timidity.h"
#else
#include "config.h"
#endif
#include "common.h"
#include "output.h"
#include "controls.h"
#ifdef ORIG_TIMPP
#include "timer.h"
#endif
#include "instrum.h"
#include "playmidi.h"
#ifdef ORIG_TIMPP
#include "miditrace.h"
#endif
#define BUFFERED_SECS 3
#define LOW_WATER_PERCENT 40
#ifdef LITTLE_ENDIAN
#define LINEAR16_FORMAT AuFormatLinearSigned16LSB
#else
#define LINEAR16_FORMAT AuFormatLinearSigned16MSB
#endif
#define dpm nas_play_mode
static int opendevaudio(void);
static void closedevaudio(void);
#ifdef ORIG_TIMPP
static int writedevaudio(char *buf, int32 len);
static int ioctldevaudio(int request, void *arg);
static int acntl(int request, void *arg);
#else
static void output_data(int32 *buf, uint32 count);
static int driver_output_data(unsigned char *buf, uint32 count);
static void flush_output(void);
static void purge_output(void);
static int output_count(uint32 ct);
#endif
#ifdef ORIG_TIMPP
PlayMode dpm=
{
DEFAULT_RATE,
PE_16BIT|PE_SIGNED,
PF_PCM_STREAM|PF_CAN_TRACE,
-1,
{0},
"Network Audio Server", 'n',
(char*)0,
opendevaudio,
closedevaudio,
writedevaudio,
ioctldevaudio
};
#else
PlayMode dpm=
{
DEFAULT_RATE,
PE_16BIT|PE_SIGNED,
-1,
{0},
"Network Audio Server", 'n',
(char*)0,
opendevaudio,
closedevaudio,
output_data,
driver_output_data,
flush_output,
purge_output,
output_count
};
#endif
struct DevAudio
{
AuServer *aud;
AuFlowID flow;
char *data;
AuUint32 used;
AuUint32 capacity;
AuBool synced;
AuBool finished;
};
static struct DevAudio fd;
static int32 output_counter, play_counter, reset_samples;
static double play_start_time;
static void reset_sample_counters()
{
output_counter = play_counter = reset_samples = 0;
}
#ifndef ORIG_TIMPP
static int output_count(uint32 ct)
{
return play_counter;
}
#endif
static int32 current_samples(void)
{
#ifdef ORIG_TIMPP
double realtime, es;
realtime = get_current_calender_time();
if(play_counter == 0)
{
play_start_time = realtime;
return reset_samples;
}
es = dpm.rate * (realtime - play_start_time);
if(es >= play_counter)
{
/* out of play counter */
reset_samples += play_counter;
play_counter = 0;
play_start_time = realtime;
return reset_samples;
}
if(es < 0)
return 0; /* for safety */
return (int32)es + reset_samples;
#else
return 0;
#endif
}
static void play(struct DevAudio *fd, AuUint32 len)
{
int count;
count = len;
if(!(dpm.encoding & PE_MONO))
count /= 2;
if(dpm.encoding & PE_16BIT)
count /= 2;
if (len<fd->used)
{
AuWriteElement(fd->aud,fd->flow,0,len,fd->data,AuFalse,(AuStatus*)0);
memmove(fd->data,fd->data+len,fd->used-=len);
}
else
{
AuWriteElement(fd->aud,fd->flow,0,fd->used,fd->data,len!=fd->used,(AuStatus*)0);
fd->used=0;
}
fd->synced=AuTrue;
current_samples(); /* Update sample counter */
play_counter += count;
}
static AuBool nas_eventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler)
{
struct DevAudio *fd=(struct DevAudio*)handler->data;
switch (ev->type)
{
case AuEventTypeMonitorNotify:
{
fd->finished=AuTrue;
break;
}
case AuEventTypeElementNotify:
{
AuElementNotifyEvent *event=(AuElementNotifyEvent*)ev;
switch (event->kind)
{
case AuElementNotifyKindLowWater:
{
play(fd,event->num_bytes);
break;
}
case AuElementNotifyKindState: switch (event->cur_state)
{
case AuStatePause:
{
if (event->reason!=AuReasonUser) play(fd,event->num_bytes);
break;
}
case AuStateStop:
{
fd->finished=AuTrue;
break;
}
}
}
}
}
return AuTrue;
}
static void syncdevaudio(void)
{
AuEvent ev;
while ((!fd.synced) && (!fd.finished))
{
AuNextEvent(fd.aud,AuTrue,&ev);
AuDispatchEvent(fd.aud,&ev);
}
fd.synced=AuFalse;
}
static int opendevaudio(void)
{
AuDeviceID device=AuNone;
AuElement elements[2];
AuUint32 buf_samples;
int i;
/* Only supported PE_16BIT|PE_SIGNED yet */
#ifdef ORIG_TIMPP
dpm.encoding = validate_encoding(dpm.encoding,
PE_16BIT|PE_SIGNED,
PE_BYTESWAP|PE_ULAW|PE_ALAW);
#else
dpm.encoding = PE_16BIT|PE_SIGNED;
#endif
if (!(fd.aud = AuOpenServer((char*)0, 0, NULL, 0, NULL, NULL))) return -1;
fd.capacity = 0;
for (i=0; i<AuServerNumDevices(fd.aud); ++i)
{
if
(
AuDeviceKind(AuServerDevice(fd.aud,i))==AuComponentKindPhysicalOutput
&& AuDeviceNumTracks(AuServerDevice(fd.aud,i))==((dpm.encoding&PE_MONO)?1:2)
)
{
device=AuDeviceIdentifier(AuServerDevice(fd.aud,i));
break;
}
}
if (device==AuNone) return -1;
if (!(fd.flow=AuCreateFlow(fd.aud,NULL))) return -1;
buf_samples = dpm.rate*BUFFERED_SECS;
AuMakeElementImportClient(&elements[0],dpm.rate,LINEAR16_FORMAT,(dpm.encoding&PE_MONO)?1:2,AuTrue,buf_samples,(AuUint32) (buf_samples/100*LOW_WATER_PERCENT),0,NULL);
AuMakeElementExportDevice(&elements[1],0,device,dpm.rate,AuUnlimitedSamples,0,NULL);
AuSetElements(fd.aud,fd.flow,AuTrue,2,elements,NULL);
AuRegisterEventHandler(fd.aud,AuEventHandlerIDMask,0,fd.flow,nas_eventHandler,(AuPointer)&fd);
fd.capacity = buf_samples*((dpm.encoding&PE_MONO)?1:2)*AuSizeofFormat(LINEAR16_FORMAT);
if ((fd.data=(char*)malloc(fd.capacity))==(char*)0) return -1;
fd.used = 0;
fd.synced = AuFalse;
fd.finished = AuFalse;
AuStartFlow(fd.aud,fd.flow,NULL);
dpm.fd = 0;
reset_sample_counters();
return 0;
}
static void closedevaudio(void)
{
if(dpm.fd != -1)
{
AuStopFlow(fd.aud, fd.flow, NULL);
while (!fd.finished) syncdevaudio();
AuCloseServer(fd.aud);
free(fd.data);
dpm.fd = 0;
}
}
#ifdef ORIG_TIMPP
static int writedevaudio(char *buf, int32 len)
#else
static int driver_output_data(unsigned char *buf, uint32 count)
#endif
{
int done = 0;
#ifndef ORIG_TIMPP
int len = (int)count;
#endif
/* fprintf(stderr,"DOD: write count=%d\n", count); */
while ((fd.used+(len-done))>fd.capacity)
{
int stillFits=fd.capacity-fd.used;
memcpy(fd.data+fd.used,buf+done,stillFits);
done+=stillFits;
fd.used+=stillFits;
/* fprintf(stderr,"DOD: cap=%d stillFits=%d done=%d\n", fd.capacity, stillFits, done); */
syncdevaudio();
}
memcpy(fd.data+fd.used,buf+done,len-done);
fd.used+=(len-done);
output_counter += len;
/* fprintf(stderr,"DOD: return len=%d\n", len); */
return len;
}
#ifndef ORIG_TIMPP
static void output_data(int32 *buf, uint32 count)
{
int ocount;
if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
ocount = (int)count;
if (ocount) {
if (dpm.encoding & PE_16BIT)
{
/* Convert data to signed 16-bit PCM */
s32tos16(buf, count);
ocount *= 2;
}
else
{
/* Convert to 8-bit unsigned and write out. */
s32tou8(buf, count);
}
}
b_out(dpm.id_character, dpm.fd, (int *)buf, ocount);
}
static void flush_output(void)
{
output_data(0, 0);
reset_sample_counters();
}
static void purge_output(void)
{
b_out(dpm.id_character, dpm.fd, 0, -1);
AuStopFlow(fd.aud,fd.flow,NULL);
}
#endif
#ifdef ORIG_TIMPP
static int ioctldevaudio(int request, void *arg)
{
switch (request)
{
case PM_REQ_DISCARD:
if(!output_counter)
return 0;
AuStopFlow(fd.aud,fd.flow,NULL);
/* Restart playing is not work correctly.
* I don't know what I should do, so I re-open audio server.
*/
#if 0
AuStartFlow(fd.aud,fd.flow,NULL);
reset_sample_counters();
return 0;
#else
AuCloseServer(fd.aud);
free(fd.data);
return opendevaudio();
#endif
case PM_REQ_FLUSH:
reset_sample_counters();
return 0;
case PM_REQ_GETSAMPLES:
if(play_counter)
*(int *)arg = current_samples();
else
*(int *)arg = reset_samples;
return 0;
case PM_REQ_PLAY_START:
reset_sample_counters();
return 0;
}
return -1;
}
#endif
#endif /* AU_NAS */