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

Last change on this file since 365 was 365, checked in by epyon, 10 years ago
  • overhaul of logging: no longer stream operated no longer using STL no memory allocation shorthand macros fast file and console I/O
File size: 3.3 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
[330]9#include <glm/gtx/vector_angle.hpp>
[202]10#include "nv/lib/sdl.hh"
11#include "nv/lib/sdl_mixer.hh"
[319]12#include "nv/core/logging.hh"
[202]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        {
[365]23                NV_LOG_CRITICAL( "SDL_AUDIO failed to load -- ", SDL_GetError() );
[202]24                return;
25        }
26
27        if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 )
28        {
[365]29                NV_LOG_CRITICAL( "SDL_mixer failed to load -- ", Mix_GetError() );
[202]30                return;
31        }
32}
33
[330]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                {
[365]42                        NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
[330]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}
[202]60
[330]61nv::channel nv::sdl::audio::play_sound( sound a_sound, vec3 position )
[202]62{
[329]63        sound_info* info = m_sounds.get( a_sound );
64        if ( info )
[202]65        {
[330]66                int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0);
67                if ( channel == -1 )
[329]68                {
[365]69                        NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
[329]70                }
[330]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                }
[202]84        }
[329]85        return channel();
[202]86}
87
[329]88nv::sound nv::sdl::audio::load_sound( const std::string& a_path )
[202]89{
[330]90        // TODO: this is a really weird error - if we remove this check, all hell gets loose
[322]91        if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr )
92        {
[365]93                NV_LOG_ERROR( "SDL_mixer not loaded!" );
[322]94        }
95        Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromFile(a_path.c_str(), "rb"), 1);
[202]96        if ( sample == nullptr )
97        {
[365]98                NV_LOG_ERROR( "SDL_mixer failed to load sample '", a_path, "' -- ", Mix_GetError() );
[329]99                return sound();
[202]100        }
[329]101        sound result = m_sounds.create();
102        sound_info* info = m_sounds.get( result );
103        info->sdl_sound = sample;
104        return result;
[202]105}
106
[330]107void nv::sdl::audio::update( vec3 position )
[202]108{
[330]109        m_position = position;
[202]110}
111
112nv::sdl::audio::~audio()
113{
[329]114        while ( m_sounds.size() > 0 )
115                release( m_sounds.get_handle(0) );
[202]116        Mix_CloseAudio();
117        // TODO: should we do it here?
118        SDL_Quit();
119}
120
[329]121void nv::sdl::audio::release( sound a_sound )
[202]122{
[329]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        }
[202]129}
[329]130
[330]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.