audio.c
17.5 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
/*====================================================================
* audio.c
*
* Synopsis:
*
* This code implements the application audio thread.
*
* 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: audio.c,v $
$Revision: 1.2 $
$Date: 2003/04/05 15:38:39 $
*---------------------------------------------------------------------*/
#include <ultralog.h>
#include <assert.h>
#include <sched.h>
#include "audio.h"
#include "music.h"
#include "sequence.h"
#include "performance.h"
#include "utils.h" /* romCopy */
#include "sndplayUI.h" /* SndP_PollAudioFrame */
#include "UniversalSP.h"
#include <../../libultra/include/sndp.h> /* HACK!!!!*/
#define CLICKS 1
#define CHECKCLICKSSIZE 0
extern OSSched sc;
extern u64 dram_stack[];
/**** private typedefs and defines ****/
#ifdef AUD_MEM_PROF
#define AMP_PRINT_INTERVAL 5000
u32 ampMaxCmds;
u32 ampMaxDMABufs;
u32 ampDMAcount;
u32 ampFrameCount;
u32 ampMaxUpdates;
#endif
/**** audio globals ****/
ALSeqFile *seqFile;
u8 *seqPtr;
s32 seqNo;
ALHeap hp;
static u8 audioHeap[AUDIO_HEAP_SIZE];
ALUSPlayer *seqp;
ALUSeq *seq;
ALSndPlayer *sndp;
ALInstrument *sfxInst;
AMAudioMgr __am;
static u64 audioStack[AUDIO_STACKSIZE/8];
AMDMAState dmaState;
AMDMABuffer dmaBuffs[NUM_DMA_BUFFERS];
u32 audioFrameCt;
u32 nextDMA = 0;
u32 curAcmdList = 0;
u32 minFrameSize;
u32 frameSize;
u32 maxFrameSize;
u32 rspCount;
/**** Queues and storage for use with PI manager ****/
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 void __amHandleFrameMsg(AudioInfo *, AudioInfo *);
static void __amHandleDoneMsg(AudioInfo *);
static void __clearAudioDMA(int nB);
static void __clearSfx(void);
/***********************************************************************
* Audio Manager API
**********************************************************************/
void
amCreateAudioMgr(ALSynConfig *c, OSPri pri)
{
u32 i;
f32 fsize;
#ifdef AUD_MEM_PROF
u32 *sp = (u32*)audioStack;
for(i=0;i<(AUDIO_STACKSIZE/4);i++)
sp[i] = 0xFEDCBA98;
#endif
c->dmaproc = __amDmaNew;
c->outputRate = osAiSetFrequency(OUTPUT_RATE);
/*
* Calculate the frame sample parameters from the
* video field rate and the output rate
*/
fsize = (f32) NUM_FIELDS * 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);
for(i=0;i<NUM_ACMD_LISTS;i++)
__am.ACMDList[i] = (Acmd*)alHeapAlloc(c->heap, 1, MAX_RSP_CMDS*sizeof(Acmd));
/*
* 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_AUDIO_MSGS);
osCreateMesgQueue(&__am.audioFrameMsgQ, __am.audioFrameMsgBuf, MAX_AUDIO_MSGS);
osCreateMesgQueue(&audDMAMessageQ, audDMAMessageBuf, NUM_DMA_MESSAGES);
osCreateThread(&__am.thread, 3, __amMain, 0,
(void *)(audioStack+AUDIO_STACKSIZE/8), pri);
osStartThread(&__am.thread);
}
/***********************************************************************
* Audio Manager implementation
**********************************************************************/
static void
__amMain(void *arg)
{
u32 usedBuffers = 0;
u32 done = 0;
AudioMsg *msg;
OSScClient client;
AudioInfo *lastInfo = 0;
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):
if (usedBuffers < NUM_OUTPUT_BUFFERS)
{
SndP_PollAudioFrame ();
__clearAudioDMA(nextDMA);
nextDMA = 0;
__amHandleFrameMsg(__am.audioInfo[audioFrameCt % 3], lastInfo);
__clearSfx();
usedBuffers++;
curAcmdList ^= 1;
audioFrameCt++;
}
osRecvMesg(&__am.audioReplyMsgQ, (OSMesg *)&msg,OS_MESG_BLOCK);
__amHandleDoneMsg(msg->done.info);
lastInfo = msg->done.info;
usedBuffers--;
break;
case (OS_SC_DONE_MSG):
rspCount = osGetCount() - rspCount;
Perf_RSPSample(rspCount);
break;
case (QUIT_MSG):
done = 1;
break;
default:
break;
}
}
alClose(&__am.g);
}
static void
__amHandleFrameMsg(AudioInfo *info, AudioInfo *lastInfo)
{
s16 *audioPtr;
Acmd *cmdp;
s32 cmdLen;
u32 samplesLeft = 0;
OSScTask *t;
u32 cpuCount;
audioPtr = (s16 *) osVirtualToPhysical(info->data);
if (lastInfo)
osAiSetNextBuffer(lastInfo->data, lastInfo->frameSamples<<2);
/**** Calculate how many samples I need for this frame to keep the DAC full ****/
samplesLeft = osAiGetLength()>>2;
info->frameSamples = 16 + (frameSize - samplesLeft + EXTRA_SAMPLES)& ~0xf;
if(info->frameSamples < minFrameSize)
info->frameSamples = minFrameSize;
/* Setup to time the audio frame processing. */
osInvalICache((void *)0x80000000, ICACHE_SIZE);
cpuCount = osGetCount();
cmdp = alAudioFrame(__am.ACMDList[curAcmdList], &cmdLen, audioPtr, info->frameSamples);
/* Calculate the frame count and store it for later displaying. */
cpuCount = osGetCount() - cpuCount;
Perf_CPUSample (cpuCount);
#ifdef AUD_MEM_PROF
if(seqp->drvr->paramCnt > ampMaxUpdates)
ampMaxUpdates = seqp->drvr->paramCnt;
if(cmdLen > ampMaxCmds)
ampMaxCmds = cmdLen;
ampFrameCount++;
if(ampFrameCount > AMP_PRINT_INTERVAL)
{
u32 *sp = (u32*)audioStack;
u32 i = 0;
u32 unusedStack = 0;
while(i<(AUDIO_STACKSIZE/4) && sp[i] == 0xFEDCBA98)
{
unusedStack += 4;
i++;
}
PRINTF("%d bytes of audio stack used\n",AUDIO_STACKSIZE-unusedStack);
PRINTF("Max Command list size = %d\n", ampMaxCmds);
PRINTF("Max DMABuffers used = %d\n", ampMaxDMABufs);
PRINTF("Max number synth updates used = %d\n", ampMaxUpdates);
ampFrameCount = 0;
}
#endif
if(cmdLen > MAX_RSP_CMDS)
PRINTF("cmdLen exceeded MAX_RSP_CMDS by %d\n",cmdLen - MAX_RSP_CMDS);
assert(cmdLen <= MAX_RSP_CMDS);
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.type = M_AUDTASK;
t->list.t.flags = OS_TASK_DP_WAIT;
t->list.t.ucode_boot = (u64 *)rspbootTextStart;
t->list.t.ucode_boot_size =
((int) rspbootTextEnd - (int) rspbootTextStart);
t->list.t.ucode = (u64 *) aspMainTextStart;
t->list.t.ucode_size = 0;
t->list.t.ucode_data = (u64 *) aspMainDataStart;
t->list.t.ucode_data_size = SP_UCODE_DATA_SIZE;
t->list.t.dram_stack = (u64*) dram_stack;
t->list.t.dram_stack_size = SP_DRAM_STACK_SIZE8;
t->list.t.output_buff = (u64*) 0;
t->list.t.output_buff_size = 0;
t->list.t.data_ptr = (u64 *) __am.ACMDList[curAcmdList];
t->list.t.data_size = (cmdp - __am.ACMDList[curAcmdList]) * sizeof(Acmd);
t->list.t.yield_data_ptr = audYieldBuf;
t->list.t.yield_data_size = OS_YIELD_AUDIO_SIZE;
if (t->list.t.data_size > MAX_RSP_CMDS*sizeof(Acmd))
PRINTF("audio cmd list too big!\n");
rspCount = osGetCount();
osSendMesg(osScGetCmdQ(&sc), (OSMesg) t, OS_MESG_BLOCK);
}
static void __clearSfx(void)
{
ALSndId i;
ALSoundState *sState = (ALSoundState*)sndp->sndState;
for( i = 0; i < sndp->maxSounds; i++)
{
if(sState[i].sound && sState[i].state == AL_STOPPED)
{
alSndpDeallocate(sndp, i);
}
}
}
static void __amHandleDoneMsg(AudioInfo *info)
{
u32 samplesLeft;
static u32 firstTime = 1;
samplesLeft = osAiGetLength()>>2;
if (samplesLeft == 0 && !firstTime)
{
PRINTF("audio: ai out of samples\n");
firstTime = 0;
}
}
s32 __amDMA(s32 addr, s32 len, void *state)
{
void *freeBuffer;
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 = audioFrameCt; /* mark it used */
freeBuffer = dmaPtr->ptr + addr - dmaPtr->startAddr;
return (int) osVirtualToPhysical(freeBuffer);
}
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;
assert(dmaPtr); /* be sure you have a buffer */
dmaState.firstFree = (AMDMABuffer*)dmaPtr->node.next;
alUnlink((ALLink*)dmaPtr);
/* add it to the used list */
if(lastDmaPtr) /* normal procedure */
{
alLink((ALLink*)dmaPtr,(ALLink*)lastDmaPtr);
}
else if(dmaState.firstUsed) /* 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;
}
freeBuffer = dmaPtr->ptr;
delta = addr & 0x1;
addr -= delta;
dmaPtr->startAddr = addr;
dmaPtr->lastFrame = audioFrameCt; /* mark it */
audDMAIOMesgBuf[nextDMA].hdr.pri = OS_MESG_PRI_NORMAL;
audDMAIOMesgBuf[nextDMA].hdr.retQueue = &audDMAMessageQ;
audDMAIOMesgBuf[nextDMA].dramAddr = freeBuffer;
audDMAIOMesgBuf[nextDMA].devAddr = (u32)addr;
audDMAIOMesgBuf[nextDMA].size = DMA_BUFFER_LENGTH;
osEPiStartDma(handler, &audDMAIOMesgBuf[nextDMA++], OS_READ);
return (int) osVirtualToPhysical(freeBuffer) + delta;
}
ALDMAproc __amDmaNew(AMDMAState **state)
{
int i;
if(!dmaState.initialized) /* only do this once */
{
dmaState.firstFree = &dmaBuffs[0];
for (i=0; i<NUM_DMA_BUFFERS-1; i++)
{
alLink((ALLink*)&dmaBuffs[i+1],(ALLink*)&dmaBuffs[i]);
dmaBuffs[i].ptr = alHeapAlloc(&hp, 1, DMA_BUFFER_LENGTH);
}
dmaState.initialized = 1;
}
*state = &dmaState; /* state is never used in this case */
return __amDMA;
}
static void __clearAudioDMA(int nBuffers)
{
int i;
OSIoMesg *iomsg;
AMDMABuffer *dmaPtr,*nextPtr;
/*
* Should not block here since if the correct number
* of messages haven't arrived the application has a bug
*/
for (i=0; i<nBuffers; i++)
{
if (osRecvMesg(&audDMAMessageQ,(OSMesg *)&iomsg,OS_MESG_NOBLOCK) == -1)
PRINTF("Dma not done\n");
if (logging)
osLogEvent(log, 17, 2, iomsg->devAddr, iomsg->size);
}
#ifdef AUD_MEM_PROF
ampDMAcount = 0;
dmaPtr = dmaState.firstUsed;
while(dmaPtr)
{
ampDMAcount++;
dmaPtr = (AMDMABuffer*)dmaPtr->node.next;
}
if(ampDMAcount > ampMaxDMABufs)
ampMaxDMABufs = ampDMAcount;
#endif
dmaPtr = dmaState.firstUsed;
while(dmaPtr)
{
nextPtr = (AMDMABuffer*)dmaPtr->node.next;
/* Can change this value. Should be at least one. */
/* Larger values mean more buffers needed, but fewer DMA's */
if(dmaPtr->lastFrame + FRAME_LAG < audioFrameCt) /* remove from used list */
{
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;
}
}
void
AudioInit (void)
{
ALSynConfig c;
ALSeqpConfig seqc;
ALSndpConfig SPConfig;
s32 len;
u32 bankLen;
ALBankFile *bankPtr,*sfxBankPtr;
ALBank *sfxBank;
u32 seqLen;
u32 i;
#ifdef AUD_MEM_PROF
u32 *sp = (u32*)audioStack;
u32 unusedStack = 0;
#endif
alHeapInit(&hp, audioHeap, sizeof(audioHeap));
/**** Load the bank file from ROM ****/
bankLen = _midibankSegmentRomEnd - _midibankSegmentRomStart;
bankPtr = alHeapAlloc(&hp, 1, bankLen);
romCopy(_midibankSegmentRomStart, (char *)bankPtr, bankLen);
alBnkfNew(bankPtr, (u8 *) _miditableSegmentRomStart);
/**** Load the sequence file header from ROM ****/
seqFile = alHeapAlloc(&hp, 1, 4);
romCopy(_seqSegmentRomStart, (u8 *)seqFile, 4);
len = 4 + seqFile->seqCount*sizeof(ALSeqData);
seqFile = alHeapAlloc(&hp, 1, len);
romCopy(_seqSegmentRomStart, (u8 *)seqFile, len);
alSeqFileNew(seqFile, (u8 *) _seqSegmentRomStart);
if (seqNo > seqFile->seqCount)
seqNo = 0;
seqPtr = alHeapAlloc(&hp, 1, MAX_SEQ_LENGTH);
seqLen = Seq_Load (seqFile, seqPtr, seqNo);
/**** Create the Audio Manager ****/
c.maxVVoices = MAX_SYNTH_VVOICES;
c.maxPVoices = MAX_SYNTH_PVOICES;
c.maxUpdates = MAX_SYNTH_UPDATES;
c.dmaproc = 0; /* audio mgr will fill this in */
c.fxType = AL_FX_NONE; /*AL_FX_SMALLROOM; */
c.heap = &hp;
amCreateAudioMgr(&c, AUDIO_PRIORITY);
/**** Create the sequence and the sequence player ****/
seqc.maxVoices = MAX_SEQP_VOICES;
seqc.maxEvents = MAX_SEQP_EVENTS;
seqc.maxChannels = 16;
seqc.heap = &hp;
seqc.initOsc = 0;
seqc.updateOsc = 0;
seqc.stopOsc = 0;
#ifdef _DEBUG
seqc.debugFlags = NO_VOICE_ERR_MASK |NOTE_OFF_ERR_MASK | NO_SOUND_ERR_MASK;
#endif
seqp = alHeapAlloc(&hp, 1, sizeof(ALUSPlayer));
seq = alHeapAlloc(&hp, 1, sizeof(ALUSeq));
alUSPNew(seqp, &seqc);
alUSNew(seq, seqPtr, seqLen);
alUSPSetSeq(seqp, seq);
alUSPSetBank(seqp, bankPtr->bankArray[0]);
/**** Copy the sfxbank(.ctl) file into rom */
bankLen = _sfxbankSegmentRomEnd - _sfxbankSegmentRomStart;
sfxBankPtr = alHeapAlloc(&hp, 1, bankLen);
romCopy(_sfxbankSegmentRomStart, (char *)sfxBankPtr, bankLen);
/*** Initialize the soundplayer ****/
sndp = alHeapAlloc(&hp, 1, sizeof(ALSndPlayer));
SPConfig.maxSounds = MAX_SNDP_VOICES;
SPConfig.maxEvents = MAX_SNDP_EVENTS;
SPConfig.heap = &hp;
alSndpNew(sndp, &SPConfig);
alBnkfNew(sfxBankPtr, _sfxtableSegmentRomStart);
sfxBank = sfxBankPtr->bankArray[0];
sfxInst = sfxBank->instArray[0];
#ifdef AUD_MEM_PROF
i = 0;
while(i<(AUDIO_STACKSIZE/4) && (sp[i] == 0xFEDCBA98))
{
unusedStack += 4;
i++;
}
PRINTF("%d bytes of heap used, %d bytes of stack used\n",
hp.cur - hp.base,AUDIO_STACKSIZE-unusedStack);
#endif
}