Changeset 330 for trunk/src/fmod


Ignore:
Timestamp:
09/02/14 08:45:01 (11 years ago)
Author:
epyon
Message:
  • implemented positional sound interface for audio
  • rudimentary implementations of positional sound for fmod and sdl
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/fmod/fmod_audio.cc

    r329 r330  
    2727                return;
    2828        }
    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 );
    3030        if ( result != FMOD_OK )
    3131        {
     
    3636}
    3737
    38 
    39 nv::channel fmod::audio::play_sound( nv::sound a_sound )
     38nv::channel nv::fmod::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ )
    4039{
    4140        sound_info* info = m_sounds.get( a_sound );
    4241        if ( info )
    4342        {
    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 );
    4747                if ( result != FMOD_OK )
    4848                {
    4949                        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 );
    5056                }
    5157        }
     
    5359}
    5460
     61nv::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
    5590nv::sound fmod::audio::load_sound( const std::string& a_path )
    5691{
    5792        FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
    5893        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 );
    6095        if ( fm_result != FMOD_OK )
    6196        {
     
    79114}
    80115
    81 void fmod::audio::update()
     116void nv::fmod::audio::set_orientation( vec3 forward, vec3 up )
    82117{
     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
     130void 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 );
    83143        FMOD_System_Update( (FMOD_SYSTEM*)m_system );
    84         // no-op
    85144}
    86145
Note: See TracChangeset for help on using the changeset viewer.