sndplayer.c
14 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
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
/*====================================================================
* sndplayer.c
*
* Copyright 1993, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
* Inc.; the contents of this file may not be disclosed to third
* parties, copied or duplicated in any form, in whole or in part,
* without the prior written permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights
* in Technical Data and Computer Software clause at DFARS
* 252.227-7013, and/or in similar or successor clauses in the FAR,
* DOD or NASA FAR Supplement. Unpublished - rights reserved under the
* Copyright Laws of the United States.
*====================================================================*/
/*
* MT safeness: need two versions of the calls, one with MT safeness?
*/
#include <libaudio.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
typedef enum {
AL_SND_PLAYING,
AL_SND_STOPPED,
AL_SND_STOPPING
} ALSndState;
typedef enum {
AL_SNDP_PLAY_EVT,
AL_SNDP_STOP_EVT,
AL_SNDP_PAN_EVT,
AL_SNDP_VOL_EVT,
AL_SNDP_PITCH_EVT,
AL_SNDP_API_EVT,
AL_SNDP_DECAY_EVT,
AL_SNDP_END_EVT,
AL_SNDP_FX_EVT
} ALSndpMsgType;
typedef struct {
ALSound *sound; /* sound referenced here */
ALVoice *voices[1]; /* single voice for moment - maybe L/R at sometime */
float pitch; /* current playback pitch */
int state; /* play state for this sound */
short vol;
ALPan pan;
char fxMix;
char envState;
} ALSoundState;
typedef union {
ALEvent msg;
struct {
short type;
ALSoundState *state;
} common;
struct {
short type;
ALSoundState *state;
short vol;
ALMicroTime volDelta;
} env;
struct {
short type;
ALSoundState *state;
short vol;
} vol;
struct {
short type;
ALSoundState *state;
float pitch;
} pitch;
struct {
short type;
ALSoundState *state;
ALPan pan;
} pan;
struct {
short type;
ALSoundState *state;
char mix;
} fx;
} ALSndpEvent;
extern ALGlobals *slg;
static ALMicroTime _voiceHandler(void *node);
static ALWaveTable *_voiceCompletion(void *data, ALVoice *voice);
static void _handleEvent(ALSndPlayer *sndp, ALSndpEvent *event);
int alSndpHeapSize(ALSndpConfig *c)
{
return
c->maxSounds * sizeof(ALSoundState) +
c->maxEvents * sizeof(ALEventListItem);
}
void alSndpNew(ALSndPlayer *sndp, ALSndpConfig *c)
{
char *ptr = c->sndpHeap;
ALEvent evt;
/*
* Init member variables
*/
sndp->maxSounds = c->maxSounds;
sndp->target = -1;
sndp->frameTime = 2000; /* ######### Hack: should get this from ??? */
sndp->sndState = ptr;
ptr += c->maxSounds * sizeof(ALSoundState);
/*
* init the event queue
*/
alEvtqNew(&sndp->evtq, (ALEventListItem *)ptr, c->maxEvents);
ptr += c->maxEvents * sizeof(ALEventListItem);
/*
* add ourselves to the driver
*/
sndp->drvr = &slg->drvr;
sndp->node.next = NULL;
sndp->node.handler = _voiceHandler;
sndp->node.clientData = sndp;
alSynAddPlayer(sndp->drvr, &sndp->node);
/*
* Start responding to API events
*/
evt.type = AL_SNDP_API_EVT;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, sndp->frameTime);
sndp->nextDelta = alEvtqNextEvent(&sndp->evtq, &sndp->nextEvent);
_ASSERT(ptr <= (c->sndpHeap + c->sndpHeapLen));
}
void alSndpDelete(ALSndPlayer *sndp)
{
/*
* ### stop all voices and shut down properly
*/
alSynRemovePlayer(&slg->drvr, &sndp->node);
}
void alSndpAllocate(ALSndPlayer *sndp, ALSound *sound)
{
int i;
int tmp;
ALSoundState *sState = sndp->sndState;
for (i = 0; i < sndp->maxSounds; i++) {
if (!sState[i].sound) {
sState[i].sound = sound;
sState[i].state = AL_STOPPED;
sState[i].voices[0] = 0;
sState[i].pitch = 1.0;
sState[i].state = AL_STOPPED;
sState[i].pan = AL_PAN_CENTER;
sState[i].envState = 0;
sState[i].fxMix = AL_DEFAULT_FXMIX;
tmp = 32767*sound->sampleVolume/AL_VOL_FULL;
sState[i].vol = tmp;
break;
}
}
_ASSERT(i < sndp->maxSounds); /* nothing left to allocate */
}
void alSndpDeallocate(ALSndPlayer *sndp, ALSound *sound)
{
int i;
ALSoundState *sState = sndp->sndState;
/*
* Note: This is inefficient. We could add a player private field
* to the sound struct, but that would mean it could be allocated
* to only one player. We could also return an id to refer to the
* sound by (so we could use it as an index), but this would mean
* applications would have to store two items for one sound.
*/
for (i = 0; i < sndp->maxSounds; i++) {
if (sState[i].sound == sound) {
sState[i].sound = 0;
if (sndp->target == i) /* if we are deleting the target sound */
sndp->target = -1;
break;
}
}
}
void alSndpSetSound(ALSndPlayer *sndp, ALSound *snd)
{
int i;
ALSoundState *sState = sndp->sndState;
for (i = 0; i < sndp->maxSounds; i++) {
if (sState[i].sound == snd) {
sndp->target = i;
}
}
}
ALSound *alSndpGetSound(ALSndPlayer *sndp)
{
ALSoundState *sState = sndp->sndState;
if (sndp->target != -1)
return sState[sndp->target].sound;
else
return 0;
}
int alSndpPlay(ALSndPlayer *sndp)
{
ALSndpEvent evt;
ALSoundState *sState = sndp->sndState;
evt.common.type = AL_SNDP_PLAY_EVT;
evt.common.state = &sState[sndp->target];
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, 0);
}
int alSndpGetState(ALSndPlayer *sndp)
{
ALSoundState *sState = sndp->sndState;
return (sState + sndp->target)->state;
}
int alSndpStop(ALSndPlayer *sndp)
{
ALSndpEvent evt;
ALSoundState *sState = sndp->sndState;
/*
* Go immediately into the release phase
*/
evt.env.type = AL_SNDP_STOP_EVT;
evt.env.state = &sState[sndp->target];
evt.env.vol = 0;
evt.env.volDelta = sState->sound->envelope->releaseTime;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, 0);
}
int alSndpSetVol(ALSndPlayer *sndp, short vol)
{
ALSndpEvent evt;
ALSoundState *sState = sndp->sndState;
evt.vol.type = AL_SNDP_VOL_EVT;
evt.vol.state = &sState[sndp->target];
evt.vol.vol = vol;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, 0);
}
int alSndpSetPitch(ALSndPlayer *sndp, float pitch)
{
ALSndpEvent evt;
ALSoundState *sState = sndp->sndState;
/*
* If this is set during playback there will be
* a problem with the envelope lengths
*/
evt.pitch.type = AL_SNDP_PITCH_EVT;
evt.pitch.state = &sState[sndp->target];
evt.pitch.pitch = pitch;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, 0);
}
int alSndpSetFXmix(ALSndPlayer *sndp, char mix)
{
ALSndpEvent evt;
ALSoundState *sState = sndp->sndState;
evt.fx.type = AL_SNDP_FX_EVT;
evt.fx.state = &sState[sndp->target];
evt.fx.mix = mix;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, 0);
}
int alSndpSetPan(ALSndPlayer *sndp, ALPan pan)
{
ALSndpEvent evt;
ALSoundState *sState = sndp->sndState;
evt.pan.type = AL_SNDP_PAN_EVT;
evt.pan.pan = pan;
evt.pan.state = &sState[sndp->target];
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, 0);
}
/*************************************************************
* Sound Player private routines
*************************************************************/
ALMicroTime _voiceHandler(void *node)
{
ALSndPlayer *sndp = (ALSndPlayer *) node;
ALSndpEvent evt;
do {
switch (sndp->nextEvent.type) {
case (AL_SNDP_API_EVT):
evt.msg.type = AL_SNDP_API_EVT;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, sndp->frameTime);
break;
default:
_handleEvent(sndp, (ALSndpEvent *)&sndp->nextEvent);
break;
}
sndp->nextDelta = alEvtqNextEvent(&sndp->evtq, &sndp->nextEvent);
} while (sndp->nextDelta == 0);
sndp->curTime += sndp->nextDelta;
return sndp->nextDelta;
}
void _handleEvent(ALSndPlayer *sndp, ALSndpEvent *event)
{
ALVoiceConfig vc;
ALSound *snd;
ALVoice *voice;
ALPan pan;
float pitch;
ALMicroTime dur;
ALSndpEvent evt;
ALMicroTime delta;
short vol;
short tmp;
int vtmp;
ALSound *sound;
ALSoundState *state;
state = event->common.state;
snd = state->sound;
/*
* ########## What happens if I get a stop message from
* ########## app in between all this?
*/
switch (event->msg.type) {
case (AL_SNDP_PLAY_EVT):
vc.fxBus = 0; /* effect buss 0 */
vc.type = AL_DEFAULT_VOICE;
vc.priority = AL_DEFAULT_PRIORITY;
vtmp = snd->envelope->attackVolume*state->vol/AL_VOL_FULL;
vol = (short) vtmp;
tmp = state->pan - AL_PAN_CENTER + snd->samplePan;
tmp = MAX(tmp, AL_PAN_LEFT);
pan = (ALPan) MIN(tmp, AL_PAN_RIGHT);
pitch = state->pitch;
dur = snd->envelope->attackTime;
state->state = AL_PLAYING;
voice = alSynAllocVoice(sndp->drvr, &vc);
alSynStartVoice(sndp->drvr, voice, snd->wavetable[0]);
alSynSetVol(sndp->drvr, voice, vol, dur);
alSynSetPan(sndp->drvr, voice, pan);
alSynSetPitch(sndp->drvr, voice, pitch);
alSynSetFXMix(sndp->drvr, voice, state->fxMix);
state->voices[0] = voice;
/*
* set up callback for decay envelope
*/
evt.env.type = AL_SNDP_DECAY_EVT;
evt.env.state = state;
evt.env.vol = snd->envelope->decayVolume;
evt.env.volDelta = (ALMicroTime) snd->envelope->decayTime/state->pitch;
delta = (ALMicroTime) snd->envelope->attackTime/state->pitch;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, delta);
break;
case (AL_SNDP_STOP_EVT):
state->envState = 0;
vtmp = (event->env.vol * state->vol)/AL_VOL_FULL;
vol = (short) vtmp;
delta = event->env.volDelta;
alSynSetVol(sndp->drvr, state->voices[0], vol, delta);
evt.common.type = AL_SNDP_END_EVT;
evt.common.state = state;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, delta);
break;
case (AL_SNDP_PAN_EVT):
state->pan = event->pan.pan;
tmp = state->pan - AL_PAN_CENTER + snd->samplePan;
tmp = MAX(tmp, AL_PAN_LEFT);
pan = (ALPan) MIN(tmp, AL_PAN_RIGHT);
if (state->state == AL_PLAYING)
alSynSetPan(sndp->drvr, state->voices[0], pan);
break;
case (AL_SNDP_PITCH_EVT):
state->pitch = event->pitch.pitch;
if (state->state == AL_PLAYING)
alSynSetPitch(sndp->drvr, state->voices[0], state->pitch);
break;
case (AL_SNDP_FX_EVT):
state->fxMix = event->fx.mix;
if (state->state == AL_PLAYING)
alSynSetFXMix(sndp->drvr, state->voices[0], state->fxMix);
break;
case (AL_SNDP_VOL_EVT):
/*
* ### need to know what envelope state we are in -
* only set this if we are in decay state would be
* sensible?
*/
state->vol = event->vol.vol;
if (state->envState){
vtmp = snd->envelope->decayVolume * state->vol/AL_VOL_FULL;
alSynSetVol(sndp->drvr, state->voices[0], (short) vtmp, 0);
}
break;
case (AL_SNDP_DECAY_EVT):
/*
* The voice has theoretically reached its attack velocity
*/
state->envState = 1;
vtmp = (event->env.vol * state->vol)/AL_VOL_FULL;
vol = (short) vtmp;
delta = event->env.volDelta;
alSynSetVol(sndp->drvr, state->voices[0], vol, delta);
/*
* set up callback for release envelope
*/
evt.env.type = AL_SNDP_STOP_EVT;
evt.env.state = state;
evt.env.vol = snd->envelope->releaseVolume;
evt.env.volDelta = (ALMicroTime) snd->envelope->releaseTime/state->pitch;
alEvtqPostEvent(&sndp->evtq, (ALEvent *)&evt, delta);
break;
case (AL_SNDP_END_EVT):
alSynStopVoice(sndp->drvr, state->voices[0]);
alSynFreeVoice(sndp->drvr, state->voices[0]);
state->state = AL_STOPPED;
break;
default:
break;
}
}
#ifdef IMPLEMENTED
int alSndpPlayWave(ALSndPlayer *sndp, int waveID, int mode);
int sndpGetTime(SLid sndp, Time *t);
int sndpSetTime(SLid sndp, Time *t);
int sndpSetLoop(SLid sndp, u_long start, u_long end, int mode,
int count);
int sndpGetLoop(SLid sndp, u_long *start, u_long *end, int *mode,
int *count);
#endif