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

Last change on this file since 367 was 367, checked in by epyon, 10 years ago
  • fixed compilation and warnings on gcc
  • slowly removing/merging external includes
File size: 3.2 KB
RevLine 
[319]1// Copyright (C) 2012-2014 ChaosForge Ltd
[202]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
[367]9#include "nv/core/math.hh"
[202]10#include "nv/lib/sdl_mixer.hh"
[319]11#include "nv/core/logging.hh"
[202]12
13using namespace nv;
14
15sdl::audio::audio()
16{
17        nv::load_sdl_library();
18        nv::load_sdl_mixer_library();
19
20        if ( SDL_Init( SDL_INIT_AUDIO ) == -1 )
21        {
[365]22                NV_LOG_CRITICAL( "SDL_AUDIO failed to load -- ", SDL_GetError() );
[202]23                return;
24        }
25
26        if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 )
27        {
[365]28                NV_LOG_CRITICAL( "SDL_mixer failed to load -- ", Mix_GetError() );
[202]29                return;
30        }
31}
32
[330]33nv::channel nv::sdl::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ )
34{
35        sound_info* info = m_sounds.get( a_sound );
36        if ( info )
37        {
38                int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0);
39                if ( channel == -1 )
40                {
[365]41                        NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
[330]42                }
43                else
44                {
45                        Mix_Volume( channel, int( volume * 128.0f ) );
46                        if ( pan != 0.0f)
47                        {
48                                uint8 right = (uint8)( (pan + 1.0f) * 127.0f );
49                                Mix_SetPanning( channel, 254-right, right );
50                        }
51                        else
52                        {
53                                Mix_SetPanning( channel, 255, 255 );
54                        }
55                }
56        }
57        return channel();
58}
[202]59
[330]60nv::channel nv::sdl::audio::play_sound( sound a_sound, vec3 position )
[202]61{
[329]62        sound_info* info = m_sounds.get( a_sound );
63        if ( info )
[202]64        {
[330]65                int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0);
66                if ( channel == -1 )
[329]67                {
[365]68                        NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
[329]69                }
[330]70                else
71                {
72                        vec3 relative = position - m_position;
73                        float angle = 0;
74                        float distance = 0;
75                        if ( relative != vec3() )
76                        {
77                                angle = -glm::orientedAngle( m_forward, glm::normalize( relative ), m_up );
78                                distance = glm::clamp( 20.0f * glm::length( relative ), 0.0f, 255.0f );
79                        }
80                        if ( angle < 0.0f ) angle += 360.0f;
81                        Mix_SetPosition( channel, sint16(angle), uint8(distance) );
82                }
[202]83        }
[329]84        return channel();
[202]85}
86
[329]87nv::sound nv::sdl::audio::load_sound( const std::string& a_path )
[202]88{
[330]89        // TODO: this is a really weird error - if we remove this check, all hell gets loose
[322]90        if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr )
91        {
[365]92                NV_LOG_ERROR( "SDL_mixer not loaded!" );
[322]93        }
94        Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromFile(a_path.c_str(), "rb"), 1);
[202]95        if ( sample == nullptr )
96        {
[365]97                NV_LOG_ERROR( "SDL_mixer failed to load sample '", a_path, "' -- ", Mix_GetError() );
[329]98                return sound();
[202]99        }
[329]100        sound result = m_sounds.create();
101        sound_info* info = m_sounds.get( result );
102        info->sdl_sound = sample;
103        return result;
[202]104}
105
[330]106void nv::sdl::audio::update( vec3 position )
[202]107{
[330]108        m_position = position;
[202]109}
110
111nv::sdl::audio::~audio()
112{
[329]113        while ( m_sounds.size() > 0 )
114                release( m_sounds.get_handle(0) );
[202]115        Mix_CloseAudio();
116        // TODO: should we do it here?
117        SDL_Quit();
118}
119
[329]120void nv::sdl::audio::release( sound a_sound )
[202]121{
[329]122        sound_info* info = m_sounds.get( a_sound );
123        if ( info )
124        {
125                Mix_FreeChunk( (Mix_Chunk*)info->sdl_sound );
126                m_sounds.destroy( a_sound );
127        }
[202]128}
[329]129
[330]130void nv::sdl::audio::set_orientation( vec3 forward, vec3 up )
131{
132        m_forward = forward;
133        m_up      = up;
134}
135
Note: See TracBrowser for help on using the repository browser.