1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - demos.TWL - DSP - g711
3   File:     main.c
4 
5   Copyright 2007-2009 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   $Date:: 2010-05-17#$
14   $Rev: 11335 $
15   $Author: kitase_hirotake $
16  *---------------------------------------------------------------------------*/
17 
18 
19 #include <twl.h>
20 #include <twl/dsp.h>
21 #include <twl/dsp/common/g711.h>
22 
23 #include <DEMO.h>
24 
25 
26 /*---------------------------------------------------------------------------*/
27 /* Functions */
28 
29 /*---------------------------------------------------------------------------*
30   Name:         TwlMain
31 
32   Description:  Main
33 
34   Arguments:    None.
35 
36   Returns:      None.
37  *---------------------------------------------------------------------------*/
TwlMain(void)38 void TwlMain(void)
39 {
40     // OS initialization
41     OS_Init();
42     OS_InitTick();
43     OS_InitAlarm();
44     (void)OS_EnableIrq();
45     (void)OS_EnableInterrupts();
46     FS_Init(FS_DMA_NOT_USE);
47     SNDEX_Init();
48 
49     // When in NITRO mode, stopped by Panic
50     DEMOCheckRunOnTWL();
51 
52     // Initialize screen display
53     DEMOInitCommon();
54     DEMOInitVRAM();
55     DEMOInitDisplayBitmap();
56     DEMOHookConsole();
57     DEMOSetBitmapTextColor(GX_RGBA(31, 31, 0, 1));
58     DEMOSetBitmapGroundColor(DEMO_RGB_CLEAR);
59     DEMOStartDisplay();
60 
61     // Load the DSP components
62     {
63         FSFile  file[1];
64         int     slotB;
65         int     slotC;
66         // In this sample, all of the WRAM is allocated for the DSP, but for actual use, you would only need as many slots as were necessary.
67         //
68         (void)MI_FreeWram_B(MI_WRAM_ARM9);
69         (void)MI_CancelWram_B(MI_WRAM_ARM9);
70         (void)MI_FreeWram_C(MI_WRAM_ARM9);
71         (void)MI_CancelWram_C(MI_WRAM_ARM9);
72         (void)MI_FreeWram_B( MI_WRAM_ARM7 );
73         (void)MI_CancelWram_B( MI_WRAM_ARM7 );
74         (void)MI_FreeWram_C( MI_WRAM_ARM7 );
75         (void)MI_CancelWram_C( MI_WRAM_ARM7 );
76         slotB = 0xFF;
77         slotC = 0xFF;
78         // For simplification, this sample uses files in static memory.
79         DSP_OpenStaticComponentG711(file);
80         if (!DSP_LoadG711(file, slotB, slotC))
81         {
82             OS_TPanic("failed to load G.711 DSP-component! (lack of WRAM-B/C)");
83         }
84     }
85 
86     // Confirm that encryption and decryption will work correctly with G.711.
87     {
88         enum { sample_wave_max = 512 * 1024 };
89         static u8   encbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
90         static u8   srcbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
91         static u8   dstbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
92         u32         length;
93         // Begin by preparing a PCM waveform for converting.
94         {
95             FSFile  file[1];
96             u32     chunk;
97             FS_InitFile(file);
98             if (!FS_OpenFileEx(file, "rom:/fanfare.32.wav", FS_FILEMODE_R) ||
99                 !FS_SeekFile(file, 0x24, FS_SEEK_SET) ||
100                 (FS_ReadFile(file, &chunk, 4) != 4) ||
101                 (chunk != (('d'<<0)|('a'<<8)|('t'<<16) |('a'<<24))) ||
102                 (FS_ReadFile(file, &length, 4) != 4) ||
103                 (FS_ReadFile(file, srcbuf, (s32)length) != length))
104             {
105                 OS_TPanic("cannot prepare sample waveform!");
106             }
107             (void)FS_CloseFile(file);
108             DC_StoreRange(srcbuf, length);
109         }
110         // Standardize units to sample counts.
111         length /= sizeof(u16);
112         // Encrypt the resulting PCM waveform in A-law format.
113         DSP_EncodeG711(encbuf, srcbuf, length, DSP_AUDIO_CODEC_MODE_G711_ALAW);
114         // Wait for completion here.
115         DSP_WaitForG711();
116         // Decrypt the resulting A-law format to a PCM waveform.
117         DSP_DecodeG711(dstbuf, encbuf, length, DSP_AUDIO_CODEC_MODE_G711_ALAW);
118         // Wait for completion here.
119         DSP_WaitForG711();
120 
121         // Press the A button to see whether the sound will play. This is not directly related to G.711, however.
122         (void)SNDEX_SetDSPMixRate(0);
123         for (;;)
124         {
125             DEMOReadKey();
126             if (DEMO_IS_TRIG(PAD_BUTTON_A))
127             {
128                 DSP_PlaySound(srcbuf, length*2, FALSE); // Because 'length' is a sample count, correct it to bytes
129             }
130             DEMO_DrawFlip();
131             OS_WaitVBlankIntr();
132         }
133     }
134 
135     // Unload the DSP components if they are no longer needed.
136     DSP_UnloadG711();
137 
138     // Because the sample is so simplistic, there's nothing to display.
139     OS_Terminate();
140 }
141