1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     HoldSoundApp.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Revision:$
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include "HoldSoundApp.h"
19 #include "simple.csid"
20 
21 namespace
22 {
23 
24 const s32 SOUND_THREAD_PRIORITY = 4;
25 const s32 LOAD_THREAD_PRIORITY = 3;
26 const s32 SOUND_HEAP_SIZE = 1 * 1024 * 1024;
27 const char SOUND_ARC_PATH[] = NW_SND_DEMO_PATH_PREFIX "simple.bcsar";
28 const char DEMO_TITLE[] = "HoldSound";
29 
30 }
31 
OnInitialize()32 void HoldSoundApp::OnInitialize()
33 {
34     InitializeSoundSystem();
35 
36     // サウンドデータのロード
37     if ( ! m_DataManager.LoadData( SEQ_MARIOKART, &m_Heap ) )
38     {
39         NW_ASSERTMSG( false, "LoadData(SEQ_MARIOKART) failed." );
40     }
41     if ( ! m_DataManager.LoadData( SE_YOSHI, &m_Heap ) )
42     {
43         NW_ASSERTMSG( false, "LoadData(SE_YOSHI) failed." );
44     }
45 }
46 
InitializeSoundSystem()47 void HoldSoundApp::InitializeSoundSystem()
48 {
49     // サウンドシステムの初期化
50     {
51         nw::snd::SoundSystem::SoundSystemParam param;
52         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
53         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
54 
55         nw::snd::SoundSystem::Initialize(
56                 param,
57                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
58                 workMemSize );
59     }
60 
61     // サウンドアーカイブの初期化
62     if ( ! m_Archive.Open( SOUND_ARC_PATH ) )
63     {
64         NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", SOUND_ARC_PATH );
65     }
66 
67     // INFO ブロックのロード
68     {
69         u32 infoBlockSize = m_Archive.GetHeaderSize();
70         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
71         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
72         {
73             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH );
74         }
75     }
76 
77     // サウンドデータマネージャーの初期化
78     {
79         u32 setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
80         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
81         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
82     }
83 
84     // サウンドアーカイブプレイヤーの初期化
85     {
86         u32 setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
87         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
88         u32 setupStrmBufferSize =
89             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
90         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
91         bool result = m_ArchivePlayer.Initialize(
92                 &m_Archive,
93                 &m_DataManager,
94                 m_pMemoryForSoundArchivePlayer, setupSize,
95                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
96         NW_ASSERT( result );
97     }
98 
99     // サウンドヒープの構築
100     {
101         m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE );
102         bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE );
103         NW_ASSERT( result );
104     }
105 }
106 
OnFinalize()107 void HoldSoundApp::OnFinalize()
108 {
109     nw::snd::SoundSystem::Finalize();
110 
111     MemFree( m_pMemoryForSoundSystem );
112     MemFree( m_pMemoryForInfoBlock );
113     MemFree( m_pMemoryForSoundDataManager );
114     MemFree( m_pMemoryForSoundArchivePlayer );
115     MemFree( m_pMemoryForSoundHeap );
116     MemFree( m_pMemoryForStreamBuffer );
117 }
118 
OnDrawUpLCD(nw::font::TextWriter & writer)119 void HoldSoundApp::OnDrawUpLCD( nw::font::TextWriter& writer )
120 {
121     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE);
122 
123     writer.Print("    -- usage --\n\n");
124     writer.Print("    [A] Hold Sequence Sound\n");
125     writer.Print("    [X] Hold Wave Sound\n");
126     writer.Print("    [Y] Hold Stream Sound\n");
127 }
128 
OnDrawDownLCD(nw::font::TextWriter & writer)129 void HoldSoundApp::OnDrawDownLCD( nw::font::TextWriter& writer )
130 {
131 #ifdef NW_PLATFORM_CTRWIN
132     (void)writer;
133 #else
134 #if 0
135     writer.Printf("ChannelStatus\n\n");
136 
137     char str[37]; str[0] = '\0';
138     for ( int i = 0; i < 36; i++ )
139     {
140         nw::os::SNPrintf( str, 36, "%s%d",
141             str,
142             nw::snd::internal::HardwareChannelManager::IsChannelPlay( i )
143         );
144         if ( i % 8 == 7 ) nw::os::SNPrintf( str, 36, "%s ", str );
145     }
146     writer.Printf("%s\n", str);
147 #else
148 #endif
149     (void)writer;
150 #endif
151 }
152 
OnUpdatePad(nw::demo::Pad & pad)153 void HoldSoundApp::OnUpdatePad( nw::demo::Pad& pad )
154 {
155     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_A ) )
156     {
157         bool result = m_ArchivePlayer.HoldSound( &m_Handle[0], SEQ_MARIOKART ).IsSuccess();
158         // NN_LOG("[SEQ] SEQ_MARIOKART ... (%d)\n", result);
159     }
160 
161     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_X ) )
162     {
163         bool result = m_ArchivePlayer.HoldSound( &m_Handle[1], SE_YOSHI ).IsSuccess();
164         // NN_LOG("[WSD] SE_YOSHI ... (%d)\n", result);
165     }
166 
167     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_Y ) )
168     {
169         bool result = m_ArchivePlayer.HoldSound( &m_Handle[2], STRM_MARIOKART ).IsSuccess();
170         // NN_LOG("[STRM] STRM_MARIOKART ... (%d)\n", result );
171     }
172 }
173 
OnUpdate()174 void HoldSoundApp::OnUpdate()
175 {
176     m_ArchivePlayer.Update();
177 }
178