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