- Timestamp:
- 09/02/14 08:45:01 (11 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/fmod/fmod_audio.cc
r329 r330 27 27 return; 28 28 } 29 result = FMOD_System_Init( system, 64, FMOD_ INIT_NORMAL, 0 );29 result = FMOD_System_Init( system, 64, FMOD_3D | FMOD_INIT_3D_RIGHTHANDED, 0 ); 30 30 if ( result != FMOD_OK ) 31 31 { … … 36 36 } 37 37 38 39 nv::channel fmod::audio::play_sound( nv::sound a_sound ) 38 nv::channel nv::fmod::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ ) 40 39 { 41 40 sound_info* info = m_sounds.get( a_sound ); 42 41 if ( info ) 43 42 { 44 FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system; 45 FMOD_SOUND* sample = (FMOD_SOUND*)( info->fmod_sound ); 46 FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, false, 0 ); 43 FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system; 44 FMOD_SOUND* sample = (FMOD_SOUND*)( info->fmod_sound ); 45 FMOD_CHANNEL* channel = nullptr; 46 FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, true, &channel ); 47 47 if ( result != FMOD_OK ) 48 48 { 49 49 NV_LOG( LOG_WARNING, "FMOD failed to play sound -- " << FMOD_ErrorString( result ) ); 50 } 51 else 52 { 53 FMOD_Channel_SetVolume( channel, volume ); 54 FMOD_Channel_SetPan( channel, pan ); 55 FMOD_Channel_SetPaused( channel, false ); 50 56 } 51 57 } … … 53 59 } 54 60 61 nv::channel nv::fmod::audio::play_sound( sound a_sound, vec3 position ) 62 { 63 sound_info* info = m_sounds.get( a_sound ); 64 if ( info ) 65 { 66 FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system; 67 FMOD_SOUND* sample = (FMOD_SOUND*)( info->fmod_sound ); 68 FMOD_CHANNEL* channel = nullptr; 69 FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, true, &channel ); 70 if ( result != FMOD_OK ) 71 { 72 NV_LOG( LOG_WARNING, "FMOD failed to play sound -- " << FMOD_ErrorString( result ) ); 73 } 74 else 75 { 76 FMOD_VECTOR fmod_position; 77 fmod_position.x = position.x; 78 fmod_position.y = position.y; 79 fmod_position.z = position.z; 80 FMOD_Channel_Set3DMinMaxDistance( channel, 1, 100000 ); 81 FMOD_Channel_Set3DAttributes( channel, &fmod_position, 0 ); 82 FMOD_Channel_SetPaused( channel, false ); 83 } 84 } 85 return channel(); 86 } 87 88 89 55 90 nv::sound fmod::audio::load_sound( const std::string& a_path ) 56 91 { 57 92 FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system; 58 93 FMOD_SOUND* sample; 59 FMOD_RESULT fm_result = FMOD_System_CreateSound( system, a_path.c_str(), FMOD_ DEFAULT, 0, &sample );94 FMOD_RESULT fm_result = FMOD_System_CreateSound( system, a_path.c_str(), FMOD_3D, 0, &sample ); 60 95 if ( fm_result != FMOD_OK ) 61 96 { … … 79 114 } 80 115 81 void fmod::audio::update()116 void nv::fmod::audio::set_orientation( vec3 forward, vec3 up ) 82 117 { 118 FMOD_VECTOR fmod_forward; 119 fmod_forward.x = forward.x; 120 fmod_forward.y = forward.y; 121 fmod_forward.z = forward.z; 122 FMOD_VECTOR fmod_up; 123 fmod_up.x = up.x; 124 fmod_up.y = up.y; 125 fmod_up.z = up.z; 126 // TODO: we also need to setup orientation! 127 FMOD_System_Set3DListenerAttributes( (FMOD_SYSTEM*)m_system, 0, 0, 0, &fmod_forward, &fmod_up ); 128 } 129 130 void fmod::audio::update( vec3 position ) 131 { 132 m_position = position; 133 FMOD_VECTOR fmod_position; 134 fmod_position.x = position.x; 135 fmod_position.y = position.y; 136 fmod_position.z = position.z; 137 // FMOD_VECTOR fmod_up; 138 // fmod_up.x = 0.0f; 139 // fmod_up.y = 0.0f; 140 // fmod_up.z = 0.0f; 141 // TODO: we also need to setup orientation! 142 FMOD_System_Set3DListenerAttributes( (FMOD_SYSTEM*)m_system, 0, &fmod_position, 0, 0, 0 ); 83 143 FMOD_System_Update( (FMOD_SYSTEM*)m_system ); 84 // no-op85 144 } 86 145 -
trunk/src/sdl/sdl_audio.cc
r329 r330 7 7 #include "nv/sdl/sdl_audio.hh" 8 8 9 #include <glm/gtx/vector_angle.hpp> 9 10 #include "nv/lib/sdl.hh" 10 11 #include "nv/lib/sdl_mixer.hh" … … 31 32 } 32 33 33 34 nv::channel sdl::audio::play_sound( sound a_sound ) 34 nv::channel nv::sdl::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ ) 35 35 { 36 36 sound_info* info = m_sounds.get( a_sound ); 37 37 if ( info ) 38 38 { 39 if ( Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0) == -1 ) 39 int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0); 40 if ( channel == -1 ) 40 41 { 41 42 NV_LOG( LOG_WARNING, "SDL_mixer failed to play -- " << Mix_GetError() ); 43 } 44 else 45 { 46 Mix_Volume( channel, int( volume * 128.0f ) ); 47 if ( pan != 0.0f) 48 { 49 uint8 right = (uint8)( (pan + 1.0f) * 127.0f ); 50 Mix_SetPanning( channel, 254-right, right ); 51 } 52 else 53 { 54 Mix_SetPanning( channel, 255, 255 ); 55 } 56 } 57 } 58 return channel(); 59 } 60 61 nv::channel nv::sdl::audio::play_sound( sound a_sound, vec3 position ) 62 { 63 sound_info* info = m_sounds.get( a_sound ); 64 if ( info ) 65 { 66 int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0); 67 if ( channel == -1 ) 68 { 69 NV_LOG( LOG_WARNING, "SDL_mixer failed to play -- " << Mix_GetError() ); 70 } 71 else 72 { 73 vec3 relative = position - m_position; 74 float angle = 0; 75 float distance = 0; 76 if ( relative != vec3() ) 77 { 78 angle = -glm::orientedAngle( m_forward, glm::normalize( relative ), m_up ); 79 distance = glm::clamp( 20.0f * glm::length( relative ), 0.0f, 255.0f ); 80 } 81 if ( angle < 0.0f ) angle += 360.0f; 82 Mix_SetPosition( channel, sint16(angle), uint8(distance) ); 42 83 } 43 84 } … … 47 88 nv::sound nv::sdl::audio::load_sound( const std::string& a_path ) 48 89 { 49 // TODO: this is a really w ierd error - if we remove this check, all hell gets loose90 // TODO: this is a really weird error - if we remove this check, all hell gets loose 50 91 if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr ) 51 92 { … … 64 105 } 65 106 66 void nv::sdl::audio::update( )107 void nv::sdl::audio::update( vec3 position ) 67 108 { 68 // no-op109 m_position = position; 69 110 } 70 111 … … 88 129 } 89 130 131 void nv::sdl::audio::set_orientation( vec3 forward, vec3 up ) 132 { 133 m_forward = forward; 134 m_up = up; 135 } 136
Note: See TracChangeset
for help on using the changeset viewer.