source: trunk/src/sdl/sdl_audio.cc @ 338

Last change on this file since 338 was 330, checked in by epyon, 11 years ago
  • implemented positional sound interface for audio
  • rudimentary implementations of positional sound for fmod and sdl
File size: 3.3 KB
Line 
1// Copyright (C) 2012-2014 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/sdl/sdl_audio.hh"
8
9#include <glm/gtx/vector_angle.hpp>
10#include "nv/lib/sdl.hh"
11#include "nv/lib/sdl_mixer.hh"
12#include "nv/core/logging.hh"
13
14using namespace nv;
15
16sdl::audio::audio()
17{
18        nv::load_sdl_library();
19        nv::load_sdl_mixer_library();
20
21        if ( SDL_Init( SDL_INIT_AUDIO ) == -1 )
22        {
23                NV_LOG( LOG_CRITICAL, "SDL_AUDIO failed to load -- " << SDL_GetError() );
24                return;
25        }
26
27        if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 )
28        {
29                NV_LOG( LOG_CRITICAL, "SDL_mixer failed to load -- " << Mix_GetError() );
30                return;
31        }
32}
33
34nv::channel nv::sdl::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ )
35{
36        sound_info* info = m_sounds.get( a_sound );
37        if ( info )
38        {
39                int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0);
40                if ( channel == -1 )
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
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) );
83                }
84        }
85        return channel();
86}
87
88nv::sound nv::sdl::audio::load_sound( const std::string& a_path )
89{
90        // TODO: this is a really weird error - if we remove this check, all hell gets loose
91        if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr )
92        {
93                NV_LOG( LOG_ERROR, "SDL_mixer not loaded!" );
94        }
95        Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromFile(a_path.c_str(), "rb"), 1);
96        if ( sample == nullptr )
97        {
98                NV_LOG( LOG_ERROR, "SDL_mixer failed to load sample '" << a_path << "' -- " << Mix_GetError() );
99                return sound();
100        }
101        sound result = m_sounds.create();
102        sound_info* info = m_sounds.get( result );
103        info->sdl_sound = sample;
104        return result;
105}
106
107void nv::sdl::audio::update( vec3 position )
108{
109        m_position = position;
110}
111
112nv::sdl::audio::~audio()
113{
114        while ( m_sounds.size() > 0 )
115                release( m_sounds.get_handle(0) );
116        Mix_CloseAudio();
117        // TODO: should we do it here?
118        SDL_Quit();
119}
120
121void nv::sdl::audio::release( sound a_sound )
122{
123        sound_info* info = m_sounds.get( a_sound );
124        if ( info )
125        {
126                Mix_FreeChunk( (Mix_Chunk*)info->sdl_sound );
127                m_sounds.destroy( a_sound );
128        }
129}
130
131void nv::sdl::audio::set_orientation( vec3 forward, vec3 up )
132{
133        m_forward = forward;
134        m_up      = up;
135}
136
Note: See TracBrowser for help on using the repository browser.