synthesizer.c
18.9 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*====================================================================
* synthesizer.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.
*====================================================================*/
/*
* Notes:
* - at what point does the driver start to get frame interrupts.
* probably need a start client interrupt routine
* - MT-safe public interfaces. use message passing from non-interrupt
* level public calls.
* - where do we load the rsp code??? On a per voice basis?
*/
#include <libaudio.h>
#include <abi.h>
#include <stdio.h>
#include "synthInternals.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
extern ALGlobals *slg;
typedef struct PVoice_s {
ALLink node;
struct ALVoice_s *vvoice;
void *rspCode;
ALFilter *sourceKnob;
ALFilter *channelKnob;
ALADPCMFilter decoder;
ALResampler resampler;
ALEnvMixer envmixer;
} PVoice;
/*
* prototypes for private driver functions
*/
static PVoice *_allocatePVoice(ALSynth *drvr);
static void _freePVoice(ALSynth *drvr, PVoice *pvoice);
static void _collectPVoices(ALSynth *drvr);
static int _timeToSamples(ALSynth *ALSynth, int micros);
static ALMicroTime _samplesToTime(ALSynth *synth, int samples);
/***********************************************************************
* Synthesis driver public interfaces
***********************************************************************/
int alSynHeapSize(ALSynConfig *c)
{
int size =
alHeapSize(c->maxVVoices, sizeof(ALVoice)) +
alHeapSize(c->maxPVoices, sizeof(PVoice));
}
void alSynNew(ALSynth *drvr, ALSynConfig *c)
{
int i;
ALVoice *vv;
PVoice *pv;
ALVoice *vvoices;
PVoice *pvoices;
int rv;
ALParam *pu;
ALHeap *hp = c->heap;
char *ptr;
union {
int i;
float f;
} ratio;
ALSave *save;
ALFilter *sources;
ALAuxBus *auxbus;
int bufSize;
drvr->head = NULL;
drvr->numPVoices = c->maxPVoices;
drvr->numVVoices = c->maxVVoices;
drvr->curTime = 0;
drvr->ratio = c->ratio;
drvr->outputRate = c->outputRate;
drvr->delta = 0; /* Keeps track of samples generated */
drvr->maxSamples = AL_MAX_RSP_SAMPLES;
drvr->maxOutSamples = ((int) ((float) drvr->maxSamples/c->ratio)) & ~0xf;
drvr->dma = (ALDMAproc) c->dmaproc;
/*osAiSetFrequency(drvr->outputRate)*/
save = alHeapAlloc(hp, 1, sizeof(ALSave));
alSaveNew(save);
ratio.f = drvr->ratio;
alSaveParam(save, AL_FILTER_SET_PITCH, (void *) ratio.i);
drvr->outputFilter = (ALFilter *)save;
/*
* allocate and initialize the auxilliary effects bus. at present
* we only support 1 effects bus.
*/
drvr->auxBus = alHeapAlloc(hp, 1, sizeof(ALAuxBus));
drvr->maxAuxBusses = 1;
sources = alHeapAlloc(hp, c->maxPVoices, sizeof(ALFilter *));
alAuxBusNew(drvr->auxBus, sources, c->maxPVoices);
/*
* allocate and initialize the main bus.
*/
drvr->mainBus = alHeapAlloc(hp, 1, sizeof(ALMainBus));
sources = alHeapAlloc(hp, c->maxPVoices, sizeof(ALFilter *));
alMainBusNew(drvr->mainBus, sources, c->maxPVoices);
bufSize = AL_FX_BUFFER_SIZE * sizeof(short);
ptr = alHeapAlloc(hp, 1, bufSize);
alSynAllocFX(drvr, AL_FX_SMALLROOM, 0, (short *)ptr, bufSize);
/*
* Build the virtual voice lists, and initialize the voices
*/
drvr->vFreeList.next = 0;
drvr->vFreeList.prev = 0;
vvoices = alHeapAlloc(hp, c->maxVVoices, sizeof(ALVoice));
for (i = 0; i < c->maxVVoices; i++) {
vv = &vvoices[i];
alLink((ALLink *)vv, &drvr->vFreeList);
vv->pvoice = 0;
vv->priority = 0;
vv->state = 0;
vv->table = 0;
}
drvr->vAllocList.next = 0;
drvr->vAllocList.prev = 0;
/*
* Build the physical voice lists
*/
drvr->pFreeList.next = 0;
drvr->pFreeList.prev = 0;
drvr->pLameList.next = 0;
drvr->pLameList.prev = 0;
pvoices = alHeapAlloc(hp, c->maxPVoices, sizeof(PVoice));
for (i = 0; i < c->maxPVoices; i++) {
pv = &pvoices[i];
alLink((ALLink *)pv, &drvr->pFreeList);
pv->vvoice = 0;
pv->rspCode = 0;
alAdpcmNew(&pv->decoder);
alAdpcmParam(&pv->decoder, AL_FILTER_SET_SOURCE, 0);
alAdpcmParam(&pv->decoder, AL_FILTER_SET_DMA_PROC, drvr->dma);
alResampleNew(&pv->resampler);
alResampleParam(&pv->resampler, AL_FILTER_SET_SOURCE, &pv->decoder);
alEnvmixerNew(&pv->envmixer);
alEnvmixerParam(&pv->envmixer, AL_FILTER_SET_SOURCE, &pv->resampler);
alAuxBusParam(drvr->auxBus, AL_FILTER_ADD_SOURCE, &pv->envmixer);
pv->channelKnob = (ALFilter *)&pv->envmixer;
}
alSaveParam(save, AL_FILTER_SET_SOURCE, drvr->mainBus);
drvr->pAllocList.next = 0;
drvr->pAllocList.prev = 0;
/*
* build the parameter update list
*/
drvr->paramList = alHeapAlloc(hp, c->maxUpdates, sizeof(ALParam));
drvr->paramCnt = 0;
drvr->paramMax = c->maxUpdates;
drvr->heap = hp;
}
void alSynDelete(ALSynth *drvr)
{
drvr->head = 0;
}
void alSynAddPlayer(ALSynth *drvr, ALPlayer *client)
{
client->callTime = client->timeLeft = 0;
client->next = drvr->head;
drvr->head = client;
}
void alSynRemovePlayer(ALSynth *drvr, ALPlayer *client)
{
ALPlayer *thing;
if (drvr->head != 0) {
for (thing = drvr->head; thing->next != 0; thing = thing->next) {
if (thing->next == client) {
thing->next = thing->next->next;
client->next = 0;
return;
}
}
}
return;
}
/*
* virtual voice functions
*/
ALVoice *alSynAllocVoice(ALSynth *drvr, ALVoiceConfig *vc)
{
ALVoice *voice;
voice = (ALVoice *)drvr->vFreeList.next;
/*
* ### what happens to competion routines if the voices are stolen?
*/
if (voice) {
alUnlink((ALLink *)voice);
alLink((ALLink *)voice, &drvr->vAllocList);
voice->priority = vc->priority;
voice->table = 0;
voice->fxBus = vc->fxBus;
voice->state = AL_STOPPED;
if (voice->pvoice = _allocatePVoice(drvr)) { /* intentional assign */
voice->pvoice->vvoice = voice;
/*
* ### assign the completion routine (either here or in
* ### start voice)
*/
/*
* ### deal with bus assignment
*/
}
}
return voice;
}
void alSynFreeVoice(ALSynth *drvr, ALVoice *voice)
{
_freePVoice(drvr, voice->pvoice);
alUnlink((ALLink *)voice);
alLink((ALLink *)voice, &drvr->vFreeList);
}
void alSynStartVoice(ALSynth *synth, ALVoice *voice, ALWaveTable *table)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
/*
* send the start message to the motion control filter
*/
update = &synth->paramList[synth->paramCnt++];
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_STATE;
update->data.i = AL_PLAYING;
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
/*
* assign the wavetable to the channel
*/
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_SET_WAVETABLE, table);
}
}
void alSynStopVoice(ALSynth *synth, ALVoice *voice)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
update = &synth->paramList[synth->paramCnt++];
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_STATE;
update->data.i = AL_STOPPED;
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
}
}
void alSynSetVol(ALSynth *synth, ALVoice *voice, short volume, ALMicroTime t)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
/*
* get new update struct from the free list
*/
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
update = &synth->paramList[synth->paramCnt++];
/*
* set offset and volume data
*/
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_VOLUME;
update->data.i = volume;
update->moredata.i = _timeToSamples(synth, t);
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
}
}
void alSynSetPitch(ALSynth *synth, ALVoice *voice, float pitch)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
/*
* get new update struct from the free list
*/
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
update = &synth->paramList[synth->paramCnt++];
/*
* set offset and pitch data
*/
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_PITCH;
update->data.f = pitch;
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
}
}
void alSynSetUnityPitch(ALSynth *synth, ALVoice *voice, float pitch)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
/*
* get new update struct from the free list
*/
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
update = &synth->paramList[synth->paramCnt++];
/*
* set offset and pitch data
*/
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_UNITY_PITCH;
update->data.f = pitch;
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
}
}
void alSynSetPan(ALSynth *synth, ALVoice *voice, char pan)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
/*
* get new update struct from the free list
*/
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
update = &synth->paramList[synth->paramCnt++];
/*
* set offset and pan data
*/
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_PAN;
update->data.i = pan;
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
}
}
void alSynSetFXMix(ALSynth *synth, ALVoice *voice, char fxmix)
{
ALParam *update;
ALFilter *f;
if (voice->pvoice) {
/*
* get new update struct from the free list
*/
ALFailIf(synth->paramCnt >= synth->paramMax, AL_NO_UPDATE_ERR);
update = &synth->paramList[synth->paramCnt++];
/*
* set offset and fxmix data
*/
update->delta = _timeToSamples(synth, synth->curTime);
update->type = AL_FILTER_SET_FXAMT;
update->data.i = fxmix;
update->next = 0;
f = voice->pvoice->channelKnob;
(*f->setParam)(f, AL_FILTER_ADD_UPDATE, update);
}
}
void alSynSetPriority(ALSynth *s, ALVoice *voice, short priority)
{}
short alSynGetPriority(ALSynth *s, ALVoice *voice)
{}
ALFxRef *alSynAllocFX(ALSynth *s, ALFxId fxid, short bus, short *mem, int size)
{
switch ( fxid ) {
case AL_FX_SMALLROOM:
case AL_FX_BIGROOM:
case AL_FX_ECHO:
alReverbNew(&s->auxBus[bus].fx[0], fxid, mem, size, s->ratio
* s->outputRate);
alReverbParam(&s->auxBus[bus].fx[0], AL_FILTER_SET_SOURCE,
&s->auxBus[bus]);
alMainBusParam(s->mainBus, AL_FILTER_ADD_SOURCE,&s->auxBus[bus].fx[0]);
break;
case AL_FX_NONE:
default:
alMainBusParam(s->mainBus, AL_FILTER_ADD_SOURCE, &s->auxBus[bus]);
break;
}
return (ALFxRef)(&s->auxBus[bus].fx[0]);
}
void alSynFreeFX(ALSynth *s, ALFxRef *fx)
{
}
void alSynSetFXParam(ALSynth *synth, ALFxRef *fx, short paramID, void *param)
{
ALFilter *f = (ALFilter *)fx;
(*f->setParam)(f, (int)paramID, param);
}
/***********************************************************************
* Synthesis driver private interfaces
***********************************************************************/
/*
* slAudioFrame() is called every video frame, and is based on the video
* frame interrupt. It is assumed to be an accurate time source for the
* clients.
*/
Acmd *alAudioFrame(Acmd *cmdList, int *cmdLen, short *outBuf, int outLen)
{
ALPlayer *client;
ALVoice *vvoice;
ALLink *dl;
ALMicroTime endTime; /* microseconds till the end of the frame */
ALFilter *output;
ALFilter *f;
ALSynth *drvr = &slg->drvr;
short tmp = 0; /* Starting buffer in DMEM */
Acmd *cmdlEnd = cmdList;
Acmd *cmdPtr;
int outCount;
int nOut;
float fCount;
short *lOutBuf = outBuf;
alHeapCheck(drvr->heap);
/*
* recieve messages to see if there is anything to be done before
* the next frame list is called
*
* ### unimplemented. Need MessageQueues with longer message lengths
* ### Chris will implement when he can get to it.
*/
/*
* initialize the update parameter free list
*/
drvr->paramCnt = 0;
/*
* Now that the internal rate of the driver is different
* from that of the output the number of internal samples generated
* per-frame needs to be calculated from the number requested
* and the internal rate.
*/
while (outLen > 0){
/*
* The number of requested output samples
* should be a multiple of 16
*/
nOut = MIN(drvr->maxOutSamples, outLen);
fCount = drvr->ratio*nOut + drvr->delta;
/*
* outCount is the number of internal samples (ie before
* output rate conversion
*/
outCount = (int) fCount;
/*
* Always request a multiple of 16
*/
if (outCount & 0xf)
outCount += 16 - (outCount & 0xf);
drvr->delta = fCount - (float) outCount;
/*
* run down list of clients and execute callback if needed this
* subframe. Here we do all the work for the frame at the start. Time
* offsets that occur before the next frame are executed "early".
*/
for (client = drvr->head; client != 0; client = client->next) {
/* ### not accurate time, truncation error */
endTime = _samplesToTime(drvr, outCount);
drvr->curTime = 0;
while (client->timeLeft < endTime) {
endTime -= client->timeLeft;
drvr->curTime += client->timeLeft;
client->timeLeft = (*client->handler)(client);
}
client->timeLeft -= endTime;
}
/*
* construct the command list for each physical voice by calling
* the head of the filter chain.
*/
cmdPtr = cmdlEnd;
aSegment(cmdPtr++, 0, 0);
output = drvr->outputFilter;
(*output->setParam)(output, AL_FILTER_SET_DRAM, lOutBuf);
cmdlEnd = (*output->handler)(output, &tmp, nOut, 0, cmdPtr);
outLen -= nOut;
lOutBuf += nOut<<1; /* For Stereo */
_collectPVoices(drvr); /* collect free physical voices */
}
*cmdLen = (int) (cmdlEnd - cmdList);
return cmdlEnd;
}
PVoice *_allocatePVoice(ALSynth *drvr)
{
ALLink *dl;
PVoice *pv;
PVoice *pvoice = 0;
int priority = AL_MAX_PRIORITY;
if ((dl = drvr->pLameList.next) != 0) { /* check the lame list first */
pvoice = (PVoice *) dl;
alUnlink(dl);
alLink(dl, &drvr->pAllocList);
} else if ((dl = drvr->pFreeList.next) != 0) { /* from the free list */
pvoice = (PVoice *) dl;
/* ### add it to the mixer */
alUnlink(dl);
alLink(dl, &drvr->pAllocList);
} else { /* steal one */
for (dl = drvr->pAllocList.next; dl != 0; dl = dl->next) {
pv = (PVoice *)dl;
if (pv->vvoice->priority < priority) {
pvoice = pv;
priority = pv->vvoice->priority;
}
}
/*
* ###
* probably need to do some clean up here to get rid of crud
* that is left over from the last voice.
*
* Might want to create a list of virtual voices that have had
* physical voices stolen from them so that they can be restarted
* when the voice becomes available again.
* ###
*/
}
return pvoice;
}
void _collectPVoices(ALSynth *drvr)
{
ALLink *dl;
PVoice *pv;
while ((dl = drvr->pLameList.next) != 0) {
pv = (PVoice *)dl;
/* ### remove from mixer */
alUnlink(dl);
alLink(dl, &drvr->pFreeList);
}
}
void _freePVoice(ALSynth *drvr, PVoice *pvoice)
{
/*
* move the voice from the allocated list to the lame list
*/
alUnlink((ALLink *)pvoice);
alLink((ALLink *)pvoice, &drvr->pLameList);
}
int _timeToSamples(ALSynth *synth, int micros)
{
int tmp1;
float tmp2 = (float) micros * synth->ratio * synth->outputRate / 1000000.0;
tmp1 = (int)tmp2;
tmp1 &= ~0xf; /* make it fall on a 16 sample boundary */
return tmp1;
}
ALMicroTime _samplesToTime(ALSynth *synth, int samples)
{
return (samples * 1000000)/(synth->ratio * synth->outputRate);
}
#ifdef IMPLEMENTED
int synSetLoop(Synth syn, u_long start, u_long end, int mode, int count);
#endif /* IMPLEMENTED */