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:: 2009-09-24#$
14   $Rev: 11063 $
15   $Author: okubata_ryoma $
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         slotB = 0xFF;
73         slotC = 0xFF;
74         // For simplification, this sample uses files in static memory.
75         DSP_OpenStaticComponentG711(file);
76         if (!DSP_LoadG711(file, slotB, slotC))
77         {
78             OS_TPanic("failed to load G.711 DSP-component! (lack of WRAM-B/C)");
79         }
80     }
81 
82     // Confirm that encryption and decryption will work correctly with G.711.
83     {
84         enum { sample_wave_max = 512 * 1024 };
85         static u8   encbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
86         static u8   srcbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
87         static u8   dstbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
88         u32         length;
89         // Begin by preparing a PCM waveform for converting.
90         {
91             FSFile  file[1];
92             u32     chunk;
93             FS_InitFile(file);
94             if (!FS_OpenFileEx(file, "rom:/fanfare.32.wav", FS_FILEMODE_R) ||
95                 !FS_SeekFile(file, 0x24, FS_SEEK_SET) ||
96                 (FS_ReadFile(file, &chunk, 4) != 4) ||
97                 (chunk != (('d'<<0)|('a'<<8)|('t'<<16) |('a'<<24))) ||
98                 (FS_ReadFile(file, &length, 4) != 4) ||
99                 (FS_ReadFile(file, srcbuf, (s32)length) != length))
100             {
101                 OS_TPanic("cannot prepare sample waveform!");
102             }
103             (void)FS_CloseFile(file);
104             DC_StoreRange(srcbuf, length);
105         }
106         // Standardize units to sample counts.
107         length /= sizeof(u16);
108         // Encrypt the resulting PCM waveform in A-law format.
109         DSP_EncodeG711(encbuf, srcbuf, length, DSP_AUDIO_CODEC_MODE_G711_ALAW);
110         // Wait for completion here.
111         DSP_WaitForG711();
112         // Decrypt the resulting A-law format to a PCM waveform.
113         DSP_DecodeG711(dstbuf, encbuf, length, DSP_AUDIO_CODEC_MODE_G711_ALAW);
114         // Wait for completion here.
115         DSP_WaitForG711();
116 
117         // Press the A button to see whether the sound will play. This is not directly related to G.711, however.
118         (void)SNDEX_SetDSPMixRate(0);
119         for (;;)
120         {
121             DEMOReadKey();
122             if (DEMO_IS_TRIG(PAD_BUTTON_A))
123             {
124                 DSP_PlaySound(srcbuf, length*2, FALSE); // Because 'length' is a sample count, correct it to bytes
125             }
126             DEMO_DrawFlip();
127             OS_WaitVBlankIntr();
128         }
129     }
130 
131     // Unload the DSP components if they are no longer needed.
132     DSP_UnloadG711();
133 
134     // Because the sample is so simplistic, there's nothing to display.
135     OS_Terminate();
136 }
137