1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - demos.TWL - DSP - g711
3   File:     main.c
4 
5   Copyright 2007-2008 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-02-12#$
14   $Rev: 10018 $
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     // Initialize screen display
50     DEMOInitCommon();
51     DEMOInitVRAM();
52     DEMOInitDisplayBitmap();
53     DEMOHookConsole();
54     DEMOSetBitmapTextColor(GX_RGBA(31, 31, 0, 1));
55     DEMOSetBitmapGroundColor(DEMO_RGB_CLEAR);
56     DEMOStartDisplay();
57 
58     // Load the DSP components
59     {
60         FSFile  file[1];
61         int     slotB;
62         int     slotC;
63         // 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
64         //
65         (void)MI_FreeWram_B(MI_WRAM_ARM9);
66         (void)MI_CancelWram_B(MI_WRAM_ARM9);
67         (void)MI_FreeWram_C(MI_WRAM_ARM9);
68         (void)MI_CancelWram_C(MI_WRAM_ARM9);
69         slotB = 0xFF;
70         slotC = 0xFF;
71         // For simplification, this sample uses files in static memory
72         DSP_OpenStaticComponentG711(file);
73         if (!DSP_LoadG711(file, slotB, slotC))
74         {
75             OS_TPanic("failed to load G.711 DSP-component! (lack of WRAM-B/C)");
76         }
77     }
78 
79     // Confirm that encryption and decryption will work correctly with G.711
80     {
81         enum { sample_wave_max = 512 * 1024 };
82         static u8   encbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
83         static u8   srcbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
84         static u8   dstbuf[sample_wave_max] ATTRIBUTE_ALIGN(4);
85         u32         length;
86         // Begin by preparing a PCM waveform for converting
87         {
88             FSFile  file[1];
89             u32     chunk;
90             FS_InitFile(file);
91             if (!FS_OpenFileEx(file, "rom:/fanfare.32.wav", FS_FILEMODE_R) ||
92                 !FS_SeekFile(file, 0x24, FS_SEEK_SET) ||
93                 (FS_ReadFile(file, &chunk, 4) != 4) ||
94                 (chunk != (('d'<<0)|('a'<<8)|('t'<<16) |('a'<<24))) ||
95                 (FS_ReadFile(file, &length, 4) != 4) ||
96                 (FS_ReadFile(file, srcbuf, (s32)length) != length))
97             {
98                 OS_TPanic("cannot prepare sample waveform!");
99             }
100             (void)FS_CloseFile(file);
101             DC_StoreRange(srcbuf, length);
102         }
103         // Standardize units to number of samples
104         length /= sizeof(u16);
105         // Encrypt the resulting PCM waveform in A-law format
106         DSP_EncodeG711(encbuf, srcbuf, length, DSP_AUDIO_CODEC_MODE_G711_ALAW);
107         // Wait for completion here
108         DSP_WaitForG711();
109         // Decrypt the resulting A-law format to a PCM waveform
110         DSP_DecodeG711(dstbuf, encbuf, length, DSP_AUDIO_CODEC_MODE_G711_ALAW);
111         // Wait for completion here
112         DSP_WaitForG711();
113 
114         // Press the A button to see whether the sound will play. This is not directly related to G.711, however.
115         (void)SNDEX_SetDSPMixRate(0);
116         for (;;)
117         {
118             DEMOReadKey();
119             if (DEMO_IS_TRIG(PAD_BUTTON_A))
120             {
121                 DSP_PlaySound(srcbuf, length*2, FALSE); // Because length is the number of samples, correct it to bytes
122             }
123             DEMO_DrawFlip();
124             OS_WaitVBlankIntr();
125         }
126     }
127 
128     // Unload the DSP components if they are no longer needed
129     DSP_UnloadG711();
130 
131     // Because the sample is so simplistic, there's nothing to display
132     OS_Terminate();
133 }
134