1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     playerHeapApp.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 "playerHeapApp.h"
21 #include "playerHeap.csid"
22 
23 namespace
24 {
25 /*
26 
27     定数
28 
29  */
30 const s32 SOUND_THREAD_PRIORITY = 4;
31 const s32 LOAD_THREAD_PRIORITY = 3;
32 const s32 SOUND_HEAP_SIZE = 1 * 1024 * 1024;
33 const char SOUND_ARC_PATH[] = NW_SND_DEMO_PATH_PREFIX "playerHeap.bcsar";
34 const char DEMO_TITLE[] = "playerHeap";
35 
36 } // anonymous namespace
37 
38 
39 
40 /*
41 
42     PlayerHeapApp クラス定義
43 
44  */
PlayerHeapApp()45 PlayerHeapApp::PlayerHeapApp()
46 {
47 }
48 
OnInitialize()49 void PlayerHeapApp::OnInitialize()
50 {
51     InitializeSoundSystem();
52 
53     // SoundDataManager を使った明示的なデータロードを行わない
54     // (StartSound を呼び出すと、サウンドデータロードスレッドにて、
55     //  プレイヤーヒープに必要なデータがロードされます)
56 }
57 
InitializeSoundSystem()58 void PlayerHeapApp::InitializeSoundSystem()
59 {
60     // サウンドシステムの初期化
61     {
62         nw::snd::SoundSystem::SoundSystemParam param;
63         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
64         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
65 
66         nw::snd::SoundSystem::Initialize(
67                 param,
68                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
69                 workMemSize );
70     }
71 
72     // サウンドアーカイブ
73     if ( ! m_Archive.Open( SOUND_ARC_PATH ) )
74     {
75         NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", SOUND_ARC_PATH );
76     }
77 
78     // INFO ブロックのロード
79     {
80         u32 infoBlockSize = m_Archive.GetHeaderSize();
81         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
82         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
83         {
84             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH );
85         }
86     }
87 
88     // STRING ブロックのロード
89     {
90         u32 stringBlockSize = m_Archive.GetLabelStringDataSize();
91         m_pMemoryForStringBlock = MemAlloc( stringBlockSize );
92         if ( ! m_Archive.LoadLabelStringData( m_pMemoryForStringBlock, stringBlockSize ) )
93         {
94             NW_ASSERTMSG( 0, "cannot load stringBlock(%s)", SOUND_ARC_PATH );
95         }
96     }
97 
98     // サウンドデータマネージャーの初期化
99     {
100         u32 setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
101         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
102         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
103     }
104 
105     // サウンドアーカイブプレイヤーの初期化
106     {
107         u32 setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
108         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
109         u32 setupStrmBufferSize =
110             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
111         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
112         bool result = m_ArchivePlayer.Initialize(
113                 &m_Archive,
114                 &m_DataManager,
115                 nw::ut::RoundUp( m_pMemoryForSoundArchivePlayer, 32 ), setupSize,
116                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
117         NW_ASSERT( result );
118     }
119 }
120 
OnFinalize()121 void PlayerHeapApp::OnFinalize()
122 {
123     nw::snd::SoundSystem::Finalize();
124 
125     MemFree( m_pMemoryForSoundSystem );
126     MemFree( m_pMemoryForInfoBlock );
127     MemFree( m_pMemoryForStringBlock );
128     MemFree( m_pMemoryForSoundDataManager );
129     MemFree( m_pMemoryForSoundArchivePlayer );
130     MemFree( m_pMemoryForStreamBuffer );
131 }
132 
OnDrawUpLCD(nw::font::TextWriter & writer)133 void PlayerHeapApp::OnDrawUpLCD( nw::font::TextWriter& writer )
134 {
135     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE);
136 
137     writer.Print("    -- usage --\n\n");
138     writer.Print("    [A] Play SEQ\n");
139     writer.Print("    [X] Play WSD\n");
140     writer.Print("    [B] Stop Sound\n\n");
141 }
142 
OnDrawDownLCD(nw::font::TextWriter &)143 void PlayerHeapApp::OnDrawDownLCD( nw::font::TextWriter& )
144 {
145     // なにも書かない
146 }
147 
OnUpdatePad(nw::demo::Pad & pad)148 void PlayerHeapApp::OnUpdatePad( nw::demo::Pad& pad )
149 {
150     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_A ) )
151     {
152         m_Handle.Stop( 0 );
153         bool result = m_ArchivePlayer.StartSound( &m_Handle, SEQ_YOSHI ).IsSuccess();
154         NN_LOG("SEQ_YOSHI ... (%d)\n", result);
155     }
156     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_X ) )
157     {
158         m_Handle.Stop( 0 );
159         bool result = m_ArchivePlayer.StartSound( &m_Handle, WSD_WIHAHO ).IsSuccess();
160         NN_LOG("WSD_WIHAHO ... (%d)\n", result);
161     }
162     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) )
163     {
164         m_Handle.Stop( 3 );
165     }
166 }
167 
OnUpdate()168 void PlayerHeapApp::OnUpdate()
169 {
170     m_ArchivePlayer.Update();
171 }
172