/*---------------------------------------------------------------------------* Project: NintendoWare File: Sound3dApp.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Revision:$ *---------------------------------------------------------------------------*/ #include "precompiled.h" #include "Sound3dApp.h" #include "simple.csid" namespace { const s32 SOUND_THREAD_PRIORITY = 4; const s32 LOAD_THREAD_PRIORITY = 3; const s32 SOUND_HEAP_SIZE = 1 * 1024 * 1024; const char SOUND_ARC_PATH[] = NW_SND_DEMO_PATH_PREFIX "simple.bcsar"; const char DEMO_TITLE[] = "Sound3d"; const f32 CONTROL_PAD_STEP = 0.2f; } void Sound3dApp::OnInitialize() { InitializeSoundSystem(); // サウンドデータのロード if ( ! m_DataManager.LoadData( SE_SQUARE, &m_Heap ) ) { NW_ASSERTMSG( false, "LoadData(SE_SQUARE) failed." ); } } void Sound3dApp::InitializeSoundSystem() { // サウンドシステムの初期化 { nw::snd::SoundSystem::SoundSystemParam param; size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param ); m_pMemoryForSoundSystem = MemAlloc( workMemSize ); nw::snd::SoundSystem::Initialize( param, reinterpret_cast( m_pMemoryForSoundSystem ), workMemSize ); } // サウンドアーカイブの初期化 if ( ! m_Archive.Open( SOUND_ARC_PATH ) ) { NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", SOUND_ARC_PATH ); } // INFO ブロックのロード { size_t infoBlockSize = m_Archive.GetHeaderSize(); m_pMemoryForInfoBlock = MemAlloc( infoBlockSize ); if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) ) { NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH ); } } // サウンドデータマネージャーの初期化 { size_t setupSize = m_DataManager.GetRequiredMemSize( &m_Archive ); m_pMemoryForSoundDataManager = MemAlloc( setupSize ); m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize ); } // サウンドアーカイブプレイヤーの初期化 { size_t setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive ); m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 ); size_t setupStrmBufferSize = m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive ); m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 ); bool result = m_ArchivePlayer.Initialize( &m_Archive, &m_DataManager, m_pMemoryForSoundArchivePlayer, setupSize, m_pMemoryForStreamBuffer, setupStrmBufferSize ); NW_ASSERT( result ); } // サウンドヒープの構築 { m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE ); bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE ); NW_ASSERT( result ); } // 3D サウンドマネージャーの初期化 { size_t setupSize = m_3dManager.GetRequiredMemSize( &m_Archive ); m_pMemoryFor3dManager = MemAlloc( setupSize ); m_3dManager.Initialize( &m_Archive, m_pMemoryFor3dManager, setupSize ); m_3dManager.SetMaxPriorityReduction( 32 ); m_3dManager.SetSonicVelocity( 340.0f / 60 ); } // 3D サウンドリスナーの初期化 { m_3dManager.AddListener( &m_3dListener ); CalcListenerMatrix( &m_ListenerMtx ); m_3dListener.SetMatrix( m_ListenerMtx ); m_3dListener.SetMaxVolumeDistance( 5.0f ); m_3dListener.SetUnitDistance( 5.0f ); m_3dListener.SetInteriorSize( 5.0f ); } // 3D サウンドアクターの初期化 { m_3dActor.Initialize( m_ArchivePlayer, m_3dManager ); m_ActorPos = nw::math::VEC3::Zero(); m_3dActor.SetPosition( m_ActorPos ); } } void Sound3dApp::CalcListenerMatrix( nw::math::MTX34* mtx ) { const nw::math::VEC3 pos( 0.0f, 0.0f, -3.0f ); // リスナーの位置 const nw::math::VEC3 upVec( 0.0f, 1.0f, 0.0f ); // リスナーのUp方向ベクトル const f32 degree = 0.0f; // リスナーの向き // リスナーの方向ベクトル const nw::math::VEC3 direction( -nw::math::SinDeg( degree ), 0.0f, -nw::math::CosDeg( degree ) ); nw::math::VEC3 target = pos + direction; // リスナー行列生成 nw::math::MTX34LookAt( mtx, &pos, &upVec, &target ); } void Sound3dApp::OnFinalize() { nw::snd::SoundSystem::Finalize(); MemFree( m_pMemoryForSoundSystem ); MemFree( m_pMemoryForInfoBlock ); MemFree( m_pMemoryForSoundDataManager ); MemFree( m_pMemoryForSoundArchivePlayer ); MemFree( m_pMemoryForSoundHeap ); MemFree( m_pMemoryForStreamBuffer ); } void Sound3dApp::OnDrawUpLCD( nw::font::TextWriter& writer ) { writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE); writer.Print (" -- usage --\n\n"); writer.Print (" [A] Play Sound (Doppler OFF)\n"); writer.Print (" [X] Play Sound (Doppler ON)\n"); writer.Print (" [B] Reset Actor Position\n"); writer.Print (" [LEFT/RIGHT] Move Actor Position.x\n"); writer.Print (" [UP/DOWN] Move Actor Position.z\n\n\n"); writer.Print (" -- status ---\n\n"); writer.Printf(" Actor x:% 2.2f\n", m_ActorPos.x); writer.Printf(" z:% 2.2f\n", m_ActorPos.z); } void Sound3dApp::OnDrawDownLCD( nw::font::TextWriter& writer ) { (void)writer; } void Sound3dApp::OnUpdatePad( nw::demo::Pad& pad ) { if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_A ) ) { // ドップラーファクター 0 のサウンド m_3dActor.HoldSound( &m_Handle, SE_SQUARE ); } if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_X ) ) { // ドップラーファクター非 0 のサウンド m_3dActor.HoldSound( &m_Handle, SE_3D_DOPPLER ); } // 3D サウンドアクター座標をリセット if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) ) { m_ActorPos.x = 0.0f; m_ActorPos.z = 0.0f; // 位置が飛ぶ時は、ResetPosition関数を呼び出す必要がある m_3dActor.ResetPosition(); } // 3D サウンドアクターの座標変更 if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_UP ) ) { m_ActorPos.z += -CONTROL_PAD_STEP; } if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_DOWN ) ) { m_ActorPos.z += CONTROL_PAD_STEP; } if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_LEFT ) ) { m_ActorPos.x += -CONTROL_PAD_STEP; } if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_RIGHT ) ) { m_ActorPos.x += CONTROL_PAD_STEP; } #if 0 if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_L ) || pad.IsButtonPress( nw::demo::Pad::BUTTON_R ) ) { f32 l, r = 0.0f; if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_L ) ) { l = 1.0f; } if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_R ) ) { r = 1.0f; } double distance = std::sqrt( m_ActorPos.x * m_ActorPos.x + m_ActorPos.z + m_ActorPos.z ); double angle = 0.0f; if ( m_ActorPos.x != 0.0f ) { angle = std::atan( m_ActorPos.z / m_ActorPos.x ); if ( m_ActorPos.x < 0 ) angle += nw::math::F_PI; } else { angle = ( m_ActorPos.z > 0 ) ? nw::math::F_PI / 2.0f : -nw::math::F_PI / 2.0f; } angle -= l / 360.0f * 2 * nw::math::F_PI * 2; angle += r / 360.0f * 2 * nw::math::F_PI * 2; m_ActorPos.x = static_cast( std::cos( angle ) * distance ); m_ActorPos.z = static_cast( std::sin( angle ) * distance ); } #endif } void Sound3dApp::OnUpdate() { m_3dActor.SetPosition( m_ActorPos ); m_ArchivePlayer.Update(); }