[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
[202] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[202] | 6 |
|
---|
| 7 | #include "nv/sdl/sdl_audio.hh"
|
---|
| 8 |
|
---|
[368] | 9 | #include "nv/stl/math.hh"
|
---|
[202] | 10 | #include "nv/lib/sdl_mixer.hh"
|
---|
[319] | 11 | #include "nv/core/logging.hh"
|
---|
[202] | 12 |
|
---|
| 13 | using namespace nv;
|
---|
| 14 |
|
---|
| 15 | sdl::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] | 33 | nv::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 | {
|
---|
[406] | 38 | int channel = Mix_PlayChannel(-1, static_cast<Mix_Chunk*>( info->sdl_sound ), 0);
|
---|
[330] | 39 | if ( channel == -1 )
|
---|
| 40 | {
|
---|
[365] | 41 | NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
|
---|
[330] | 42 | }
|
---|
| 43 | else
|
---|
| 44 | {
|
---|
[406] | 45 | Mix_Volume( channel, static_cast<int>( volume * 128.0f ) );
|
---|
[330] | 46 | if ( pan != 0.0f)
|
---|
| 47 | {
|
---|
[406] | 48 | uint8 right = static_cast<uint8>( (pan + 1.0f) * 127.0f );
|
---|
[330] | 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] | 60 | nv::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 | {
|
---|
[406] | 65 | int channel = Mix_PlayChannel(-1, static_cast<Mix_Chunk*>( info->sdl_sound ), 0);
|
---|
[330] | 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 | {
|
---|
[454] | 77 | angle = math::degrees( -math::oriented_angle( m_forward, math::normalize( relative ), m_up ) );
|
---|
| 78 | distance = math::clamp( 20.0f * math::length( relative ), 0.0f, 255.0f );
|
---|
[330] | 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 |
|
---|
[399] | 87 | nv::sound nv::sdl::audio::load_sound( const string_view& 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 | }
|
---|
[392] | 94 | Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromFile(a_path.data(), "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] | 106 | void nv::sdl::audio::update( vec3 position )
|
---|
[202] | 107 | {
|
---|
[330] | 108 | m_position = position;
|
---|
[202] | 109 | }
|
---|
| 110 |
|
---|
| 111 | nv::sdl::audio::~audio()
|
---|
| 112 | {
|
---|
[543] | 113 | for ( auto& s : m_sounds )
|
---|
| 114 | release( &s );
|
---|
[202] | 115 | Mix_CloseAudio();
|
---|
| 116 | // TODO: should we do it here?
|
---|
| 117 | SDL_Quit();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[329] | 120 | void nv::sdl::audio::release( sound a_sound )
|
---|
[202] | 121 | {
|
---|
[329] | 122 | sound_info* info = m_sounds.get( a_sound );
|
---|
[543] | 123 | release( info );
|
---|
| 124 | m_sounds.destroy( a_sound );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void nv::sdl::audio::release( sound_info* info )
|
---|
| 128 | {
|
---|
[329] | 129 | if ( info )
|
---|
[406] | 130 | Mix_FreeChunk( static_cast<Mix_Chunk*>( info->sdl_sound ) );
|
---|
[202] | 131 | }
|
---|
[329] | 132 |
|
---|
[330] | 133 | void nv::sdl::audio::set_orientation( vec3 forward, vec3 up )
|
---|
| 134 | {
|
---|
| 135 | m_forward = forward;
|
---|
| 136 | m_up = up;
|
---|
| 137 | }
|
---|
| 138 |
|
---|