1 /*---------------------------------------------------------------------------*
2   Project:  THP Simple Player
3   File:     axseq.c
4 
5   Copyright (C)2002-2006 Nintendo  All Rights Reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: axseq.c,v $
14   Revision 1.7  2006/11/21 08:34:23  aka
15   Removed the zero buffer.
16 
17   Revision 1.6  2006/10/23 02:12:13  aka
18   Changed from AXInit() to AXInitSpecifyMem().
19   Changed from MIXInit() to MIXInitSpecifyMem().
20 
21   Revision 1.5  2006/10/11 02:51:09  aka
22   Revised AXInit() and MIXInit() and SYNInit().
23 
24   Revision 1.4  2006/03/06 09:59:09  kawaset
25   Eliminated warnings.
26 
27   Revision 1.3  2006/02/21 01:04:26  mitu
28   modified am.h path.
29 
30   Revision 1.2  2006/02/20 04:13:12  mitu
31   changed include path from dolphin/ to revolution/.
32 
33   Revision 1.1  2006/02/03 10:01:13  aka
34   Imported from Dolphin tree.
35 
36 
37     1     02/02/28 6:39p Akagi
38     Initial revision made by Suzuki-san (IRD).
39 
40   $NoKeywords: $
41 
42  *---------------------------------------------------------------------------*/
43 
44 #include <string.h>
45 #include <revolution.h>
46 #include <revolution/mix.h>
47 #include <revolution/seq.h>
48 #include <revolution/syn.h>
49 #include <revolution/axfx.h>
50 #include <revolution/mem.h>
51 
52 #include "axseq.h"
53 
54 #define AXBUFFER_ON_MEM2
55 #undef  AXBUFFER_ON_MEM2
56 
57 static void CallbackAudioFrame(void);
58 
59 static SEQSEQUENCE Sequence;
60 static u8          *Wavetable;
61 static u8          *Pcm;
62 static u8          *MidiFile;
63 static void        *axBuffer;
64 static void        *mixBuffer;
65 static void        *synBuffer;
66 
67 extern MEMHeapHandle __ExpHeap;
68 
CallbackAudioFrame(void)69 static void CallbackAudioFrame(void)
70 {
71     int old = OSEnableInterrupts();
72 
73     // run the sequencer
74     SEQRunAudioFrame();
75 
76     // run the synth
77     SYNRunAudioFrame();
78 
79     // tell the mixer to update settings
80     MIXUpdateSettings();
81 
82     OSRestoreInterrupts(old);
83 }
84 
85 /*---------------------------------------------------------------------------*
86  *---------------------------------------------------------------------------*/
LoadFileIntoRam(char * path)87 static void* LoadFileIntoRam(char *path)
88 {
89     DVDFileInfo handle;
90     u32         round_length;
91     s32         read_length;
92     void        *buffer;
93 
94     // Open File
95     if (!DVDOpen(path, &handle))
96     {
97         OSReport("WARNING! Failed to open %s\n", path);
98         return NULL;
99     }
100 
101     // Make sure file length is not 0
102     if (DVDGetLength(&handle) == 0)
103     {
104         OSReport("WARNING! File length is 0\n");
105         return NULL;
106     }
107 
108     round_length = OSRoundUp32B(DVDGetLength(&handle));
109     buffer       = MEMAllocFromExpHeapEx(__ExpHeap, round_length,  32);
110 
111     // Make sure we got a buffer
112     if (buffer == NULL)
113     {
114         OSReport("WARNING! Unable to allocate buffer\n");
115         return NULL;
116     }
117 
118     // Read Files
119     read_length = DVDRead(&handle, buffer, (s32)(round_length), 0);
120 
121     // Make sure we read the file correctly
122     if (read_length <= 0)
123     {
124         OSReport("WARNING! File lenght is wrong\n");
125         return NULL;
126     }
127 
128     return buffer;
129 }
130 
AXSeqSetup(void)131 void AXSeqSetup(void)
132 {
133 #ifdef AXBUFFER_ON_MEM2
134     axBuffer  = MEMAllocFromExpHeapEx(__ExpHeap, AXGetMemorySize(AX_MAX_VOICES), 32);
135 #else
136     axBuffer  = OSAlloc(AXGetMemorySize(AX_MAX_VOICES));
137 #endif
138     mixBuffer = MEMAllocFromExpHeap(__ExpHeap, MIXGetMemorySize(AX_MAX_VOICES));
139     synBuffer = MEMAllocFromExpHeap(__ExpHeap, SYNGetMemorySize(AX_MAX_VOICES));
140 
141     AXInitSpecifyMem(AX_MAX_VOICES, axBuffer); // initialize AX
142     MIXInitSpecifyMem(mixBuffer);              // initialize mixer
143     SYNInitSpecifyMem(synBuffer);              // initialize synthesizer
144     SEQInit();                       // initialize sequencer
145     AXRegisterCallback(&CallbackAudioFrame);
146 
147     Wavetable = LoadFileIntoRam(GM_WT);
148     Pcm       = LoadFileIntoRam(GM_PCM);
149     MidiFile  = LoadFileIntoRam(MIDI_FILE);
150 
151     SEQAddSequence( &Sequence,
152                     MidiFile,
153                     Wavetable,
154                     Pcm,
155                     NULL,
156                     16,
157                     15,
158                     1
159                     );
160 }
161 
SeqPlay(void)162 void SeqPlay(void)
163 {
164     SEQSetState(&Sequence, SEQ_STATE_RUNLOOPED);
165 
166     return;
167 }
168 
SeqStop(void)169 void SeqStop(void)
170 {
171     SEQSetState(&Sequence, SEQ_STATE_STOP);
172 
173     return;
174 }
175 
GetSeqState(void)176 BOOL GetSeqState(void)
177 {
178     if (SEQGetState(&Sequence) == SEQ_STATE_RUNLOOPED)
179     {
180         return TRUE;
181     }
182     else
183     {
184         return FALSE;
185     }
186 }
187