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 "nv/stl/math.hh"
|
---|
10 | #include "nv/lib/sdl_mixer.hh"
|
---|
11 | #include "nv/core/logging.hh"
|
---|
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 | {
|
---|
22 | NV_LOG_CRITICAL( "SDL_AUDIO failed to load -- ", SDL_GetError() );
|
---|
23 | return;
|
---|
24 | }
|
---|
25 |
|
---|
26 | if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 )
|
---|
27 | {
|
---|
28 | NV_LOG_CRITICAL( "SDL_mixer failed to load -- ", Mix_GetError() );
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
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 | {
|
---|
38 | int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0);
|
---|
39 | if ( channel == -1 )
|
---|
40 | {
|
---|
41 | NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
|
---|
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 | }
|
---|
59 |
|
---|
60 | nv::channel nv::sdl::audio::play_sound( sound a_sound, vec3 position )
|
---|
61 | {
|
---|
62 | sound_info* info = m_sounds.get( a_sound );
|
---|
63 | if ( info )
|
---|
64 | {
|
---|
65 | int channel = Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0);
|
---|
66 | if ( channel == -1 )
|
---|
67 | {
|
---|
68 | NV_LOG_WARNING( "SDL_mixer failed to play -- ", Mix_GetError() );
|
---|
69 | }
|
---|
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 | }
|
---|
83 | }
|
---|
84 | return channel();
|
---|
85 | }
|
---|
86 |
|
---|
87 | nv::sound nv::sdl::audio::load_sound( const std::string& a_path )
|
---|
88 | {
|
---|
89 | // TODO: this is a really weird error - if we remove this check, all hell gets loose
|
---|
90 | if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr )
|
---|
91 | {
|
---|
92 | NV_LOG_ERROR( "SDL_mixer not loaded!" );
|
---|
93 | }
|
---|
94 | Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromFile(a_path.c_str(), "rb"), 1);
|
---|
95 | if ( sample == nullptr )
|
---|
96 | {
|
---|
97 | NV_LOG_ERROR( "SDL_mixer failed to load sample '", a_path, "' -- ", Mix_GetError() );
|
---|
98 | return sound();
|
---|
99 | }
|
---|
100 | sound result = m_sounds.create();
|
---|
101 | sound_info* info = m_sounds.get( result );
|
---|
102 | info->sdl_sound = sample;
|
---|
103 | return result;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void nv::sdl::audio::update( vec3 position )
|
---|
107 | {
|
---|
108 | m_position = position;
|
---|
109 | }
|
---|
110 |
|
---|
111 | nv::sdl::audio::~audio()
|
---|
112 | {
|
---|
113 | while ( m_sounds.size() > 0 )
|
---|
114 | release( m_sounds.get_handle(0) );
|
---|
115 | Mix_CloseAudio();
|
---|
116 | // TODO: should we do it here?
|
---|
117 | SDL_Quit();
|
---|
118 | }
|
---|
119 |
|
---|
120 | void nv::sdl::audio::release( sound a_sound )
|
---|
121 | {
|
---|
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 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | void nv::sdl::audio::set_orientation( vec3 forward, vec3 up )
|
---|
131 | {
|
---|
132 | m_forward = forward;
|
---|
133 | m_up = up;
|
---|
134 | }
|
---|
135 |
|
---|