n_synthesizer.c
8.15 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
/*====================================================================
*
* 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.
*====================================================================*/
#include "n_synthInternals.h"
#ifdef AUD_PROFILE
#include <os.h>
extern u32 cnt_index, drvr_num, drvr_cnt, drvr_max, drvr_min, lastCnt[];
extern u32 client_num, client_cnt, client_max, client_min;
#endif
#ifndef MIN
# define MIN(a,b) (((a)<(b))?(a):(b))
#endif
static s32 __n_nextSampleTime(ALPlayer **client);
static s32 _n_timeToSamplesNoRound( s32 micros);
/***********************************************************************
* Synthesis driver public interfaces
***********************************************************************/
void n_alSynNew(ALSynConfig *c)
{
s32 i;
N_PVoice *pv;
N_PVoice *pvoices;
ALHeap *hp = c->heap;
ALParam *params;
ALParam *paramPtr;
n_syn->head = NULL;
n_syn->n_seqp1 = NULL;
n_syn->n_seqp2 = NULL;
n_syn->n_sndp = NULL;
n_syn->numPVoices = c->maxPVoices;
n_syn->curSamples = 0;
n_syn->paramSamples = 0;
n_syn->outputRate = c->outputRate;
#ifndef N_MICRO
n_syn->maxOutSamples = AL_MAX_RSP_SAMPLES;
#else
n_syn->maxOutSamples = FIXED_SAMPLE;
#endif
n_syn->dma = (ALDMANew) c->dmaproc;
/******* save new *******************************/
n_syn->sv_dramout = 0;
n_syn->sv_first = 1;
/******* aux new *******************************/
n_syn->auxBus = (N_ALAuxBus *)alHeapAlloc(hp, 1, sizeof(N_ALAuxBus));
n_syn->auxBus->sourceCount = 0;
n_syn->auxBus->maxSources = c->maxPVoices;
n_syn->auxBus->sources = (N_PVoice **)
alHeapAlloc(hp, c->maxPVoices, sizeof(N_PVoice *));
/******* main new *******************************/
n_syn->mainBus = (N_ALMainBus *)alHeapAlloc(hp, 1, sizeof(N_ALMainBus));
/******* fx new *******************************/
if (c->fxType != AL_FX_NONE){
n_syn->auxBus->fx = n_alSynAllocFX( 0, c, hp);
n_syn->mainBus->filter.handler = (N_ALCmdHandler)n_alFxPull;
} else {
n_syn->mainBus->filter.handler = (N_ALCmdHandler)n_alAuxBusPull;
}
n_syn->pFreeList.next = 0;
n_syn->pFreeList.prev = 0;
n_syn->pLameList.next = 0;
n_syn->pLameList.prev = 0;
n_syn->pAllocList.next = 0;
n_syn->pAllocList.prev = 0;
pvoices = alHeapAlloc(hp, c->maxPVoices, sizeof(N_PVoice));
for (i = 0; i < c->maxPVoices; i++) {
pv = &pvoices[i];
alLink((ALLink *)pv, &n_syn->pFreeList);
pv->vvoice = 0;
alN_PVoiceNew(pv, n_syn->dma, hp);
n_syn->auxBus->sources[n_syn->auxBus->sourceCount++] = pv;
}
params = alHeapAlloc(hp, c->maxUpdates, sizeof(ALParam));
n_syn->paramList = 0;
for (i = 0; i < c->maxUpdates; i++) {
paramPtr= ¶ms[i];
paramPtr->next = n_syn->paramList;
n_syn->paramList = paramPtr;
}
n_syn->heap = hp;
}
Acmd *n_alAudioFrame(Acmd *cmdList, s32 *cmdLen, s16 *outBuf, s32 outLen)
{
ALPlayer *client;
Acmd *cmdlEnd = cmdList;
Acmd *cmdPtr;
s32 nOut;
s16 *lOutBuf = outBuf;
#ifdef AUD_PROFILE
lastCnt[++cnt_index] = osGetCount();
#endif
if (n_syn->head == 0) {
*cmdLen = 0;
return cmdList; /* nothing to do */
}
/*
* 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".
*/
#ifdef AUD_PROFILE
lastCnt[++cnt_index] = osGetCount();
#endif
/*
* paramSamples = time of next parameter change.
* curSamples = current sample time.
* so paramSamples - curSamples is the time until the next parameter change.
* if the next parameter change occurs within this frame time (outLen),
* then call back the client that contains the parameter change.
* Note, paramSamples must be rounded down to 16 sample boundary for use
* during the client handler.
*/
for (n_syn->paramSamples = __n_nextSampleTime( &client);
n_syn->paramSamples - n_syn->curSamples < outLen;
n_syn->paramSamples = __n_nextSampleTime( &client))
{
n_syn->paramSamples &= ~0xf;
client->samplesLeft += _n_timeToSamplesNoRound((*client->handler)(client));
}
/* for safety's sake, always store paramSamples aligned to 16 sample
boundary.
* this way, if an voice handler routine gets called outside
the N_ALVoiceHandler
* routine (alSynAllocVoice) it will get timestamped with an
aligned value and
* will be processed immediately next audio frame.
*/
n_syn->paramSamples &= ~0xf;
#ifdef AUD_PROFILE
PROFILE_AUD(client_num, client_cnt, client_max, client_min);
#endif
/*
* Now build the command list in small chunks
*/
while (outLen > 0){
nOut = MIN(n_syn->maxOutSamples, outLen);
/*
* construct the command list for each physical voice by calling
* the head of the filter chain.
*/
cmdPtr = cmdlEnd;
#ifndef N_MICRO
aSegment(cmdPtr++, 0, 0);
#endif
n_syn->sv_dramout = (s32)lOutBuf;
cmdlEnd = n_alSavePull(n_syn->curSamples, cmdPtr);
outLen -= nOut;
lOutBuf += nOut<<1; /* For Stereo */
n_syn->curSamples += nOut;
}
*cmdLen = (s32) (cmdlEnd - cmdList);
_n_collectPVoices(); /* collect free physical voices */
#ifdef AUD_PROFILE
PROFILE_AUD(drvr_num, drvr_cnt, drvr_max, drvr_min);
#endif
return cmdlEnd;
}
/***********************************************************************
* Synthesis driver private interfaces
***********************************************************************/
ALParam *__n_allocParam()
{
ALParam *update = 0;
if (n_syn->paramList) {
update = n_syn->paramList;
n_syn->paramList = n_syn->paramList->next;
update->next = 0;
}
return update;
}
void _n_freeParam(ALParam *param)
{
param->next = n_syn->paramList;
n_syn->paramList = param;
}
void _n_collectPVoices(void)
{
ALLink *dl;
N_PVoice *pv;
while ((dl = n_syn->pLameList.next) != 0) {
pv = (N_PVoice *)dl;
/* ### remove from mixer */
alUnlink(dl);
alLink(dl, &n_syn->pFreeList);
}
}
void _n_freePVoice(N_PVoice *pvoice)
{
/*
* move the voice from the allocated list to the lame list
*/
alUnlink((ALLink *)pvoice);
alLink((ALLink *)pvoice, &n_syn->pLameList);
}
/*
Add 0.5 to adjust the average affect of
the truncation error produced by casting
a float to an int.
*/
s32 _n_timeToSamplesNoRound( s32 micros)
{
f32 tmp = ((f32)micros) * n_syn->outputRate / 1000000.0 + 0.5;
return (s32)tmp;
}
s32 _n_timeToSamples( s32 micros)
{
return _n_timeToSamplesNoRound( micros) & ~0xf;
}
static s32 __n_nextSampleTime(ALPlayer **client)
{
ALMicroTime temp,delta = 0x7fffffff; /* max delta for s32 */
#if 0
ALPlayer *cl;
#endif
*client = 0;
#if 0
for (cl = n_syn->head; cl != 0; cl = cl->next) {
if ((cl->samplesLeft - n_syn->curSamples) < delta) {
*client = cl;
delta = cl->samplesLeft - n_syn->curSamples;
}
}
#endif
if( n_syn->n_sndp )
if( (temp = n_syn->n_sndp->samplesLeft - n_syn->curSamples) < delta ) {
*client = n_syn->n_sndp;
delta = temp;
}
if( n_syn->n_seqp1 )
if( (temp = n_syn->n_seqp1->samplesLeft - n_syn->curSamples) < delta ) {
*client = n_syn->n_seqp1;
delta = temp;
}
if( n_syn->n_seqp2 )
if( (n_syn->n_seqp2->samplesLeft - n_syn->curSamples) < delta ) {
*client = n_syn->n_seqp2;
}
return (*client)->samplesLeft;
}