Changeset 330 for trunk/src/sdl


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/sdl/sdl_audio.cc

    r329 r330  
    77#include "nv/sdl/sdl_audio.hh"
    88
     9#include <glm/gtx/vector_angle.hpp>
    910#include "nv/lib/sdl.hh"
    1011#include "nv/lib/sdl_mixer.hh"
     
    3132}
    3233
    33 
    34 nv::channel sdl::audio::play_sound( sound a_sound )
     34nv::channel nv::sdl::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ )
    3535{
    3636        sound_info* info = m_sounds.get( a_sound );
    3737        if ( info )
    3838        {
    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 )
    4041                {
    4142                        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
     61nv::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) );
    4283                }
    4384        }
     
    4788nv::sound nv::sdl::audio::load_sound( const std::string& a_path )
    4889{
    49         // TODO: this is a really wierd error - if we remove this check, all hell gets loose
     90        // TODO: this is a really weird error - if we remove this check, all hell gets loose
    5091        if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr )
    5192        {
     
    64105}
    65106
    66 void nv::sdl::audio::update()
     107void nv::sdl::audio::update( vec3 position )
    67108{
    68         // no-op
     109        m_position = position;
    69110}
    70111
     
    88129}
    89130
     131void 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.