1 /*---------------------------------------------------------------------------*
2   Project:  THP Player
3   File:     main.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: main.c,v $
14   Revision 1.4  2006/10/23 02:12:22  aka
15   Changed from AXInit() to AXInitSpecifyMem().
16   Changed from MIXInit() to MIXInitSpecifyMem().
17 
18   Revision 1.3  2006/10/11 02:50:45  aka
19   Revised AXInit() and MIXInit().
20 
21   Revision 1.2  2006/06/08 06:11:30  aka
22   Removed setting of tempDisableFX for new AXFX.
23 
24   Revision 1.1  2006/02/03 10:01:27  aka
25   Imported from Dolphin tree.
26 
27 
28     2     03/11/25 11:24 Dante
29     Japanese to English translation of comments and text strings
30 
31     1     02/05/14 11:28a Suzuki
32     Initial checkin
33 
34   $NoKeywords: $
35 
36  *---------------------------------------------------------------------------*/
37 
38 #include <demo.h>
39 #include <revolution/axfx.h>
40 #include <revolution/mix.h>
41 #include <revolution/mem.h>
42 
43 #include "THPPlayerStrmAX.h"
44 
45 static void MovieDoneRender(void);
46 static void CallbackAudioFrame(void);
47 static void AXSetup(void);
48 
49 static AXFX_REVERBHI RevH;
50 
51 static MEMHeapHandle ExpHeap;
52 
main(void)53 void main(void)
54 {
55     u32             size, x, y;
56     u8              *buffer;
57     GXRenderModeObj *rmode;
58     GXColor         black;
59     THPVideoInfo    videoInfo;
60     THPAudioInfo    audioInfo;
61     s32             frame, audioTrack;
62     u16             buttonDown, dir;
63     char            *fileName;
64     BOOL            open, play, onMemory;
65     AXVPB           *left, *right;
66     void            *arenaMem2Lo;
67     void            *arenaMem2Hi;
68 
69     black.r = 0;
70     black.g = 0;
71     black.b = 0;
72     black.a = 255;
73 
74     DEMOInit(&GXNtsc480Int);
75 
76     arenaMem2Lo = OSGetMEM2ArenaLo();
77     arenaMem2Hi = OSGetMEM2ArenaHi();
78     ExpHeap   = MEMCreateExpHeap(arenaMem2Lo, (u32)arenaMem2Hi - (u32) arenaMem2Lo);
79 
80     AIInit(NULL);
81 
82     AXSetup();
83 
84     GXSetDispCopyGamma(GX_GM_1_0);
85     GXSetCopyFilter(FALSE, NULL, FALSE, NULL);
86     GXSetCopyClear(black, 0xffffff);
87 
88     rmode = DEMOGetRenderModeObj();
89 
90     THPPlayerInit();
91 
92     open     = FALSE;
93     play     = FALSE;
94     onMemory = FALSE;
95 
96     OSReport("\n");
97     OSReport("###########################################################\n");
98     OSReport("Push A Button     : Play the movie with audio\n");
99     OSReport("Push B Button     : Play the movie with audio on the memory\n");
100     OSReport("Push X Button     : Play the interlaced movie\n");
101     OSReport("Push Y Button     : Play the movie with multiple audio track,\n");
102     OSReport("                  : select audio track at random\n");
103     OSReport("Push L Trigger    : Pause / Restart the movie\n");
104     OSReport("Push R Trigger    : Skip one frame if the movie is paused\n");
105     OSReport("Push Start Button : Application end\n");
106     OSReport("###########################################################\n");
107 
108     while(1)
109     {
110         DEMOPadRead();
111 
112         buttonDown = DEMOPadGetButtonDown(PAD_CHAN0);
113         dir        = DEMOPadGetDirs(PAD_CHAN0);
114 
115         DEMOBeforeRender();
116 
117         if (play)
118         {
119             frame = THPPlayerDrawCurrentFrame(rmode, x, y, videoInfo.xSize, videoInfo.ySize);
120         }
121 
122         // Movie Playback DoneRender
123         MovieDoneRender();
124 
125         // Check player state
126         if (THPPlayerGetState() == THP_PLAYER_ERROR)
127         {
128             THPPlayerStop();
129             THPPlayerClose();
130             MEMFreeToExpHeap(ExpHeap, buffer);
131             OSHalt("Error happen");
132         }
133 
134         if (buttonDown & PAD_BUTTON_A)
135         {
136             fileName = "thpdemo/rebirth.thp";
137             open     = TRUE;
138             onMemory = FALSE;
139         }
140 
141         if (buttonDown & PAD_BUTTON_B)
142         {
143             fileName = "thpdemo/fish.thp";
144             open     = TRUE;
145             onMemory = TRUE;
146         }
147 
148         if (buttonDown & PAD_BUTTON_X)
149         {
150             fileName = "thpdemo/fish_int.thp";
151             open     = TRUE;
152             onMemory = FALSE;
153         }
154 
155         if (buttonDown & PAD_BUTTON_Y)
156         {
157             fileName = "thpdemo/fish_mlt.thp";
158             open     = TRUE;
159             onMemory = FALSE;
160         }
161 
162         if (buttonDown & PAD_TRIGGER_L)
163         {
164             if (THPPlayerGetState() == THP_PLAYER_PAUSE)
165             {
166                 THPPlayerPlay();
167             }
168             else
169             {
170                 THPPlayerPause();
171             }
172         }
173 
174         if (buttonDown & PAD_TRIGGER_R)
175         {
176             THPPlayerSkip();
177         }
178 
179         if (buttonDown & PAD_BUTTON_START)
180         {
181             if (play)
182             {
183                 THPPlayerStop();
184                 THPPlayerClose();
185                 MEMFreeToExpHeap(ExpHeap, buffer);
186             }
187 
188             break;
189         }
190 
191         if (dir & DEMO_STICK_RIGHT)
192         {
193             THPPlayerSkip();
194         }
195 
196         if (open)
197         {
198             // If playing back, release Stop and work area
199             if (play)
200             {
201                 THPPlayerStop();
202                 THPPlayerClose();
203                 MEMFreeToExpHeap(ExpHeap, buffer);
204                 play = FALSE;
205             }
206         }
207 
208         if (open && !play)
209         {
210             // Open THP file
211             if (THPPlayerOpen(fileName, onMemory) == FALSE)
212             {
213                 OSHalt("Failed to open the THP file\n");
214             }
215 
216             THPPlayerGetVideoInfo(&videoInfo);
217             THPPlayerGetAudioInfo(&audioInfo);
218 
219             x = (rmode->fbWidth   - videoInfo.xSize) / 2;
220             y = (rmode->efbHeight - videoInfo.ySize) / 2;
221 
222             // Set aside work area
223             size = THPPlayerCalcNeedMemory();
224             buffer = MEMAllocFromExpHeapEx(ExpHeap, size, 32);
225             if (!buffer)
226             {
227                 OSHalt("Cannot allocate the memory");
228             }
229 
230             // Set work area
231             THPPlayerSetBuffer(buffer);
232 
233             // Select randomly, if there are multiple audio tracks.
234             if (audioInfo.sndNumTracks != 1)
235             {
236                 audioTrack = (s32)(OSGetTick() % audioInfo.sndNumTracks);
237             }
238             else
239             {
240                 audioTrack = 0;
241             }
242 
243             // Preparation before playback
244             if (THPPlayerPrepare(0, THP_PLAY_LOOP, audioTrack) == FALSE)
245             {
246                 OSHalt("Failed to prepare\n");
247             }
248 
249             THPPlayerGetStreamAXPB(&left, &right);
250 
251             if (left)
252             {
253                 MIXSetInput(left, 0);
254                 MIXAuxAPreFader(left);
255                 MIXSetFader(left, -904);
256                 MIXSetAuxA(left, 0);
257             }
258 
259             if (right)
260             {
261                 MIXSetInput(right, 0);
262                 MIXAuxAPreFader(right);
263                 MIXSetFader(right, -904);
264                 MIXSetAuxA(right, 0);
265             }
266 
267             // Start playback
268             THPPlayerPlay();
269 
270             open = FALSE;
271             play = TRUE;
272         }
273     }
274 
275     THPPlayerQuit();
276 
277     VIWaitForRetrace();
278 
279     OSHalt("application end\n");
280 
281     return;
282 }
283 
MovieDoneRender(void)284 static void MovieDoneRender(void)
285 {
286     // Release used THP video data at same time as graphics processor
287     THPPlayerDrawDone();
288 
289     DEMODoneRender();
290 }
291 
CallbackAudioFrame(void)292 static void CallbackAudioFrame(void)
293 {
294     BOOL old;
295 
296     old = OSEnableInterrupts();
297 
298     MIXUpdateSettings();
299 
300     THPPlayerStreamUpdate();
301 
302     OSRestoreInterrupts(old);
303 }
304 
AXSetup(void)305 static void AXSetup(void)
306 {
307     void  *axBuffer;
308     void  *mixBuffer;
309 
310     axBuffer  = MEMAllocFromExpHeapEx(ExpHeap, AXGetMemorySize(AX_MAX_VOICES), 32);
311     mixBuffer = MEMAllocFromExpHeap(ExpHeap, MIXGetMemorySize(AX_MAX_VOICES));
312 
313     AXInitSpecifyMem(AX_MAX_VOICES, axBuffer); // initialize AX
314     MIXInitSpecifyMem(mixBuffer);              // initialize mixer
315 
316     AXRegisterCallback(CallbackAudioFrame);
317 
318     RevH.time           = 3.0f;
319     RevH.preDelay       = 0.1f;
320     RevH.damping        = 0.6f;
321     RevH.coloration     = 0.9f;
322     RevH.crosstalk      = 0.0f;
323     RevH.mix            = 0.65f;
324 
325     AXFXReverbHiInit(&RevH);
326 
327     AXRegisterAuxACallback((void *)&AXFXReverbHiCallback, (void*)&RevH);
328 }
329