audiomgr.c
17.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
/******************************************************************************
* audiomgr.c
*
* This code implements the audio manager. This provides a low level
* interface to the audio library, and manages the routines needed to
* create the audio task.
*
* At the begining of each video retrace, the scheduler sends a message
* that wakes up the audio thread, which calls alAudioFrame to build an
* audio task. Once this task is built, it is sent to the scheduler, that
* will then send the task to the rsp for execution.
*
* 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.
*****************************************************************************/
/*---------------------------------------------------------------------*
Copyright (C) 1998 Nintendo. (Originated by SGI)
$RCSfile: audiomgr.c,v $
$Revision: 1.1.1.1 $
$Date: 2002/05/02 03:27:21 $
*---------------------------------------------------------------------*/
#include <ultralog.h>
#include <assert.h>
#include "audio.h"
#include "simple.h"
/**** type define's for structures unique to audiomgr ****/
typedef union {
struct {
short type;
} gen;
struct {
short type;
struct AudioInfo_s *info;
} done;
OSScMsg app;
} AudioMsg;
typedef struct AudioInfo_s {
short *data; /* Output data pointer */
short frameSamples; /* # of samples synthesized in this frame */
OSScTask task; /* scheduler structure */
AudioMsg msg; /* completion message */
} AudioInfo;
typedef struct {
Acmd *ACMDList[NUM_ACMD_LISTS];
AudioInfo *audioInfo[NUM_OUTPUT_BUFFERS];
OSThread thread;
OSMesgQueue audioFrameMsgQ;
OSMesg audioFrameMsgBuf[MAX_MESGS];
OSMesgQueue audioReplyMsgQ;
OSMesg audioReplyMsgBuf[MAX_MESGS];
ALGlobals g;
} AMAudioMgr;
typedef struct
{
ALLink node;
u32 startAddr;
u32 lastFrame;
char *ptr;
} AMDMABuffer;
typedef struct
{
u8 initialized;
AMDMABuffer *firstUsed;
AMDMABuffer *firstFree;
} AMDMAState;
/**** audio manager globals ****/
extern OSSched sc;
extern OSMesgQueue *sched_cmdQ;
AMAudioMgr __am;
static u64 audioStack[AUDIO_STACKSIZE/sizeof(u64)];
AMDMAState dmaState;
AMDMABuffer dmaBuffs[NUM_DMA_BUFFERS];
u32 audFrameCt = 0;
u32 nextDMA = 0;
u32 curAcmdList = 0;
u32 minFrameSize;
u32 frameSize;
u32 maxFrameSize;
u32 maxRSPCmds;
/** Queues and storage for use with audio DMA's ****/
OSIoMesg audDMAIOMesgBuf[NUM_DMA_MESSAGES];
OSMesgQueue audDMAMessageQ;
OSMesg audDMAMessageBuf[NUM_DMA_MESSAGES];
/**** private routines ****/
static void __amMain(void *arg);
static s32 __amDMA(s32 addr, s32 len, void *state);
static ALDMAproc __amDmaNew(AMDMAState **state);
static u32 __amHandleFrameMsg(AudioInfo *, AudioInfo *);
static void __amHandleDoneMsg(AudioInfo *);
static void __clearAudioDMA(void);
/******************************************************************************
* Audio Manager API
*****************************************************************************/
void amCreateAudioMgr(ALSynConfig *c, OSPri pri, amConfig *amc)
{
u32 i;
f32 fsize;
dmaState.initialized = FALSE; /* Reset this before the first call to __amDmaNew */
c->dmaproc = __amDmaNew;
c->outputRate = osAiSetFrequency(amc->outputRate);
/*
* Calculate the frame sample parameters from the
* video field rate and the output rate
*/
fsize = (f32) amc->framesPerField * c->outputRate / (f32) 60;
frameSize = (s32) fsize;
if (frameSize < fsize)
frameSize++;
if (frameSize & 0xf)
frameSize = (frameSize & ~0xf) + 0x10;
minFrameSize = frameSize - 16;
maxFrameSize = frameSize + EXTRA_SAMPLES + 16;
alInit(&__am.g, c);
dmaBuffs[0].node.prev = 0;
dmaBuffs[0].node.next = 0;
for (i=0; i<NUM_DMA_BUFFERS-1; i++)
{
alLink((ALLink*)&dmaBuffs[i+1],(ALLink*)&dmaBuffs[i]);
dmaBuffs[i].ptr = alHeapAlloc(c->heap, 1, DMA_BUFFER_LENGTH);
}
/* last buffer already linked, but still needs buffer */
dmaBuffs[i].ptr = alHeapAlloc(c->heap, 1, DMA_BUFFER_LENGTH);
for(i=0;i<NUM_ACMD_LISTS;i++)
__am.ACMDList[i] = (Acmd*)alHeapAlloc(c->heap, 1,
amc->maxACMDSize * sizeof(Acmd));
maxRSPCmds = amc->maxACMDSize;
/**** initialize the done messages ****/
for (i = 0; i < NUM_OUTPUT_BUFFERS; i++)
{
__am.audioInfo[i] = (AudioInfo *)alHeapAlloc(c->heap, 1,
sizeof(AudioInfo));
__am.audioInfo[i]->msg.done.type = OS_SC_DONE_MSG;
__am.audioInfo[i]->msg.done.info = __am.audioInfo[i];
__am.audioInfo[i]->data = alHeapAlloc(c->heap, 1, 4*maxFrameSize);
}
osCreateMesgQueue(&__am.audioReplyMsgQ, __am.audioReplyMsgBuf, MAX_MESGS);
osCreateMesgQueue(&__am.audioFrameMsgQ, __am.audioFrameMsgBuf, MAX_MESGS);
osCreateMesgQueue(&audDMAMessageQ, audDMAMessageBuf, NUM_DMA_MESSAGES);
osCreateThread(&__am.thread, 3, __amMain, 0,
(void *)(audioStack+AUDIO_STACKSIZE/sizeof(u64)), pri);
osStartThread(&__am.thread);
}
/******************************************************************************
*
* Audio Manager implementation. This thread wakes up at every retrace,
* and builds an audio task, which it returns to the scheduler, who then
* is responsible for its finally execution on the RSP. Once the task has
* finished execution, the scheduler sends back a message saying the task
* is complete. The audio is triple buffered because the switching to a new
* audio buffer does not occur exactly at the gfx swapbuffer time. With
* 3 buffers you ensure that the program does not destroy data before it is
* played.
*
*****************************************************************************/
static void __amMain(void *arg)
{
u32 validTask;
u32 done = 0;
AudioMsg *msg;
AudioInfo *lastInfo = 0;
OSScClient client;
osScAddClient(&sc, &client, &__am.audioFrameMsgQ);
while (!done)
{
(void) osRecvMesg(&__am.audioFrameMsgQ, (OSMesg *)&msg, OS_MESG_BLOCK);
switch (msg->gen.type)
{
case (OS_SC_RETRACE_MSG):
validTask = __amHandleFrameMsg(__am.audioInfo[audFrameCt % 3],
lastInfo);
if(validTask)
{
/* wait for done message */
osRecvMesg(&__am.audioReplyMsgQ, (OSMesg *)&msg,
OS_MESG_BLOCK);
__amHandleDoneMsg(msg->done.info);
lastInfo = msg->done.info;
}
break;
case (OS_SC_PRE_NMI_MSG):
/* what should we really do here? quit? ramp down volume? */
break;
case (QUIT_MSG):
done = 1;
break;
default:
break;
}
}
alClose(&__am.g);
}
/******************************************************************************
*
* __amHandleFrameMsg. First, clear the past audio dma's, then calculate
* the number of samples you will need for this frame. This value varies
* due to the fact that audio is synchronised off of the video interupt
* which can have a small amount of jitter in it. Varying the number of
* samples slightly will allow you to stay in synch with the video. This
* is an advantageous thing to do, since if you are in synch with the
* video, you will have fewer graphics yields. After you've calculated
* the number of frames needed, call alAudioFrame, which will call all
* of the synthesizer's players (sequence player and sound player) to
* generate the audio task list. If you get a valid task list back, put
* it in a task structure and send a message to the scheduler to let it
* know that the next frame of audio is ready for processing.
*
*****************************************************************************/
static u32 __amHandleFrameMsg(AudioInfo *info, AudioInfo *lastInfo)
{
s16 *audioPtr;
Acmd *cmdp;
s32 cmdLen;
int samplesLeft = 0;
OSScTask *t;
__clearAudioDMA(); /* call once a frame, before doing alAudioFrame */
audioPtr = (s16 *) osVirtualToPhysical(info->data);
if (lastInfo)
osAiSetNextBuffer(lastInfo->data, lastInfo->frameSamples<<2);
/* calculate how many samples needed for this frame to keep the DAC full */
/* this will vary slightly frame to frame, must recalculate every frame */
samplesLeft = osAiGetLength() >> 2; /* divide by four, to convert bytes */
/* to stereo 16 bit samples */
info->frameSamples = 16 + (frameSize - samplesLeft + EXTRA_SAMPLES)& ~0xf;
if(info->frameSamples < minFrameSize)
info->frameSamples = minFrameSize;
cmdp = alAudioFrame(__am.ACMDList[curAcmdList], &cmdLen, audioPtr,
info->frameSamples);
assert(cmdLen <= maxRSPCmds);
if(cmdLen == 0) /* no task produced, return zero to show no valid task */
return 0;
t = &info->task;
t->next = 0; /* paranoia */
t->msgQ = &__am.audioReplyMsgQ; /* reply to when finished */
t->msg = (OSMesg)&info->msg; /* reply with this message */
t->flags = OS_SC_NEEDS_RSP;
t->list.t.data_ptr = (u64 *) __am.ACMDList[curAcmdList];
t->list.t.data_size = (cmdp - __am.ACMDList[curAcmdList]) * sizeof(Acmd);
t->list.t.type = M_AUDTASK;
t->list.t.ucode_boot = (u64 *)rspbootTextStart;
t->list.t.ucode_boot_size =
((int) rspbootTextEnd - (int) rspbootTextStart);
t->list.t.flags = 0;
t->list.t.ucode = (u64 *) aspMainTextStart;
t->list.t.ucode_data = (u64 *) aspMainDataStart;
t->list.t.ucode_data_size = SP_UCODE_DATA_SIZE;
t->list.t.dram_stack = (u64 *) NULL;
t->list.t.dram_stack_size = 0;
t->list.t.output_buff = (u64 *) NULL;
t->list.t.output_buff_size = 0;
t->list.t.yield_data_ptr = NULL;
t->list.t.yield_data_size = 0;
#ifndef STOP_AUDIO
osSendMesg(sched_cmdQ, (OSMesg) t, OS_MESG_BLOCK);
#endif
curAcmdList ^= 1; /* swap which acmd list you use each frame */
return 1;
}
/******************************************************************************
*
* __amHandleDoneMsg. Really just debugging info in this frame. Checks
* to make sure we completed before we were out of samples.
*
*****************************************************************************/
static void __amHandleDoneMsg(AudioInfo *info)
{
s32 samplesLeft;
static int firstTime = 1;
samplesLeft = osAiGetLength()>>2;
if (samplesLeft == 0 && !firstTime)
{
PRINTF("audio: ai out of samples\n");
firstTime = 0;
}
}
/******************************************************************************
*
* __amDMA This routine handles the dma'ing of samples from rom to ram.
* First it checks the current buffers to see if the samples needed are
* already in place. Because buffers are linked sequentially by the
* addresses where the samples are on rom, it doesn't need to check all
* of them, only up to the address that it needs. If it finds one, it
* returns the address of that buffer. If it doesn't find the samples
* that it needs, it will initiate a DMA of the samples that it needs.
* In either case, it updates the lastFrame variable, to indicate that
* this buffer was last used in this frame. This is important for the
* __clearAudioDMA routine.
*
*****************************************************************************/
s32 __amDMA(s32 addr, s32 len, void *state)
{
void *foundBuffer;
s32 delta, addrEnd, buffEnd;
AMDMABuffer *dmaPtr, *lastDmaPtr;
lastDmaPtr = 0;
dmaPtr = dmaState.firstUsed;
addrEnd = addr+len;
/* first check to see if a currently existing buffer contains the
sample that you need. */
while(dmaPtr)
{
buffEnd = dmaPtr->startAddr + DMA_BUFFER_LENGTH;
if(dmaPtr->startAddr > addr) /* since buffers are ordered */
break; /* abort if past possible */
else if(addrEnd <= buffEnd) /* yes, found a buffer with samples */
{
dmaPtr->lastFrame = audFrameCt; /* mark it used */
foundBuffer = dmaPtr->ptr + addr - dmaPtr->startAddr;
return (int) osVirtualToPhysical(foundBuffer);
}
lastDmaPtr = dmaPtr;
dmaPtr = (AMDMABuffer*)dmaPtr->node.next;
}
/* get here, and you didn't find a buffer, so dma a new one */
/* get a buffer from the free list */
dmaPtr = dmaState.firstFree;
/*
* if you get here and dmaPtr is null, send back the a bogus
* pointer, it's better than nothing
*/
if(!dmaPtr)
return osVirtualToPhysical(dmaState.firstUsed);
dmaState.firstFree = (AMDMABuffer*)dmaPtr->node.next;
alUnlink((ALLink*)dmaPtr);
/* add it to the used list */
if(lastDmaPtr) /* if you have other dmabuffers used, add this one */
{ /* to the list, after the last one checked above */
alLink((ALLink*)dmaPtr,(ALLink*)lastDmaPtr);
}
else if(dmaState.firstUsed) /* if this buffer is before any others */
{ /* jam at begining of list */
lastDmaPtr = dmaState.firstUsed;
dmaState.firstUsed = dmaPtr;
dmaPtr->node.next = (ALLink*)lastDmaPtr;
dmaPtr->node.prev = 0;
lastDmaPtr->node.prev = (ALLink*)dmaPtr;
}
else /* no buffers in list, this is the first one */
{
dmaState.firstUsed = dmaPtr;
dmaPtr->node.next = 0;
dmaPtr->node.prev = 0;
}
foundBuffer = dmaPtr->ptr;
delta = addr & 0x1;
addr -= delta;
dmaPtr->startAddr = addr;
dmaPtr->lastFrame = audFrameCt; /* mark it */
audDMAIOMesgBuf[nextDMA].hdr.pri = OS_MESG_PRI_NORMAL;
audDMAIOMesgBuf[nextDMA].hdr.retQueue = &audDMAMessageQ;
audDMAIOMesgBuf[nextDMA].dramAddr = foundBuffer;
audDMAIOMesgBuf[nextDMA].devAddr = (u32)addr;
audDMAIOMesgBuf[nextDMA].size = DMA_BUFFER_LENGTH;
osEPiStartDma(handler, &audDMAIOMesgBuf[nextDMA++], OS_READ);
return (int) osVirtualToPhysical(foundBuffer) + delta;
}
/******************************************************************************
*
* __amDmaNew. Initialize the dma buffers and return the address of the
* procedure that will be used to dma the samples from rom to ram. This
* routine will be called once for each physical voice that is created.
* In this case, because we know where all the buffers are, and since
* they are not attached to a specific voice, we will only really do any
* initialization the first time. After that we just return the address
* to the dma routine.
*
*****************************************************************************/
ALDMAproc __amDmaNew(AMDMAState **state)
{
int i;
if(!dmaState.initialized) /* only do this once */
{
dmaState.firstUsed = 0;
dmaState.firstFree = &dmaBuffs[0];
dmaState.initialized = 1;
}
*state = &dmaState; /* state is never used in this case */
return __amDMA;
}
/******************************************************************************
*
* __clearAudioDMA. Routine to move dma buffers back to the unused list.
* First clear out your dma messageQ. Then check each buffer to see when
* it was last used. If that was more than FRAME_LAG frames ago, move it
* back to the unused list.
*
*****************************************************************************/
static void __clearAudioDMA(void)
{
u32 i;
OSIoMesg *iomsg;
AMDMABuffer *dmaPtr,*nextPtr;
/*
* Don't block here. If dma's aren't complete, you've had an audio
* overrun. (Bad news, but go for it anyway, and try and recover.
*/
for (i=0; i<nextDMA; i++)
{
if (osRecvMesg(&audDMAMessageQ,(OSMesg *)&iomsg,OS_MESG_NOBLOCK) == -1)
PRINTF("Dma not done\n");
#ifndef _FINALROM
if (logging)
osLogEvent(log, 17, 2, iomsg->devAddr, iomsg->size);
#endif
}
dmaPtr = dmaState.firstUsed;
while(dmaPtr)
{
nextPtr = (AMDMABuffer*)dmaPtr->node.next;
/* remove old dma's from list */
/* Can change FRAME_LAG value. Should be at least one. */
/* Larger values mean more buffers needed, but fewer DMA's */
if(dmaPtr->lastFrame + FRAME_LAG < audFrameCt)
{
if(dmaState.firstUsed == dmaPtr)
dmaState.firstUsed = (AMDMABuffer*)dmaPtr->node.next;
alUnlink((ALLink*)dmaPtr);
if(dmaState.firstFree)
alLink((ALLink*)dmaPtr,(ALLink*)dmaState.firstFree);
else
{
dmaState.firstFree = dmaPtr;
dmaPtr->node.next = 0;
dmaPtr->node.prev = 0;
}
}
dmaPtr = nextPtr;
}
nextDMA = 0; /* reset */
audFrameCt++;
}