1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/fmod/fmod_audio.hh"
|
---|
8 |
|
---|
9 | #include "nv/lib/fmod.hh"
|
---|
10 | #include "nv/core/logging.hh"
|
---|
11 |
|
---|
12 | using namespace nv;
|
---|
13 |
|
---|
14 | fmod::audio::audio()
|
---|
15 | {
|
---|
16 | m_system = nullptr;
|
---|
17 |
|
---|
18 | nv::load_fmod_library();
|
---|
19 |
|
---|
20 | FMOD_RESULT result;
|
---|
21 | FMOD_SYSTEM* system;
|
---|
22 |
|
---|
23 | result = FMOD_System_Create( &system );
|
---|
24 | if ( result != FMOD_OK )
|
---|
25 | {
|
---|
26 | NV_LOG_CRITICAL( "Failed to create FMOD System -- ", FMOD_ErrorString( result ) );
|
---|
27 | return;
|
---|
28 | }
|
---|
29 | result = FMOD_System_Init( system, 64, FMOD_3D | FMOD_INIT_3D_RIGHTHANDED, 0 );
|
---|
30 | if ( result != FMOD_OK )
|
---|
31 | {
|
---|
32 | NV_LOG_ERROR( "Failed to initialize FMOD System -- ", FMOD_ErrorString( result ) );
|
---|
33 | return;
|
---|
34 | }
|
---|
35 | m_system = system;
|
---|
36 | }
|
---|
37 |
|
---|
38 | nv::channel nv::fmod::audio::play_sound( sound a_sound, float volume, float pan /*= 0.0f */ )
|
---|
39 | {
|
---|
40 | sound_info* info = m_sounds.get( a_sound );
|
---|
41 | if ( info )
|
---|
42 | {
|
---|
43 | FMOD_SYSTEM* system = static_cast<FMOD_SYSTEM*>( m_system );
|
---|
44 | FMOD_SOUND* sample = static_cast<FMOD_SOUND*>( info->fmod_sound );
|
---|
45 | FMOD_CHANNEL* channel = nullptr;
|
---|
46 | FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, true, &channel );
|
---|
47 | if ( result != FMOD_OK )
|
---|
48 | {
|
---|
49 | NV_LOG_WARNING( "FMOD failed to play sound -- ", FMOD_ErrorString( result ) );
|
---|
50 | }
|
---|
51 | else
|
---|
52 | {
|
---|
53 | FMOD_Channel_SetVolume( channel, volume );
|
---|
54 | FMOD_Channel_SetPan( channel, pan );
|
---|
55 | FMOD_Channel_SetPaused( channel, false );
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return channel();
|
---|
59 | }
|
---|
60 |
|
---|
61 | nv::channel nv::fmod::audio::play_sound( sound a_sound, vec3 position )
|
---|
62 | {
|
---|
63 | sound_info* info = m_sounds.get( a_sound );
|
---|
64 | if ( info )
|
---|
65 | {
|
---|
66 | FMOD_SYSTEM* system = static_cast<FMOD_SYSTEM*>( m_system );
|
---|
67 | FMOD_SOUND* sample = static_cast<FMOD_SOUND*>( info->fmod_sound );
|
---|
68 | FMOD_CHANNEL* channel = nullptr;
|
---|
69 | FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, true, &channel );
|
---|
70 | if ( result != FMOD_OK )
|
---|
71 | {
|
---|
72 | NV_LOG_WARNING( "FMOD failed to play sound -- ", FMOD_ErrorString( result ) );
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | FMOD_VECTOR fmod_position;
|
---|
77 | fmod_position.x = position.x;
|
---|
78 | fmod_position.y = position.y;
|
---|
79 | fmod_position.z = position.z;
|
---|
80 | FMOD_Channel_Set3DMinMaxDistance( channel, 1, 100000 );
|
---|
81 | FMOD_Channel_Set3DAttributes( channel, &fmod_position, 0 );
|
---|
82 | FMOD_Channel_SetPaused( channel, false );
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return channel();
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | nv::sound fmod::audio::load_sound( const string_view& a_path )
|
---|
91 | {
|
---|
92 | FMOD_SYSTEM* system = static_cast<FMOD_SYSTEM*>( m_system );
|
---|
93 | FMOD_SOUND* sample;
|
---|
94 | FMOD_RESULT fm_result = FMOD_System_CreateSound( system, a_path.data(), FMOD_3D, 0, &sample );
|
---|
95 | if ( fm_result != FMOD_OK )
|
---|
96 | {
|
---|
97 | NV_LOG_ERROR( "FMOD failed to load sample '", a_path, "' -- ", FMOD_ErrorString( fm_result ) );
|
---|
98 | return sound();
|
---|
99 | }
|
---|
100 | sound result = m_sounds.create();
|
---|
101 | sound_info* info = m_sounds.get( result );
|
---|
102 | info->fmod_sound = sample;
|
---|
103 | return result;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void nv::fmod::audio::release( sound a_sound )
|
---|
107 | {
|
---|
108 | sound_info* info = m_sounds.get( a_sound );
|
---|
109 | release( info );
|
---|
110 | m_sounds.destroy( a_sound );
|
---|
111 | }
|
---|
112 |
|
---|
113 | void nv::fmod::audio::release( sound_info* info )
|
---|
114 | {
|
---|
115 | if ( info )
|
---|
116 | FMOD_Sound_Release( static_cast<FMOD_SOUND*>( info->fmod_sound ) );
|
---|
117 | }
|
---|
118 |
|
---|
119 | void nv::fmod::audio::set_orientation( vec3 forward, vec3 up )
|
---|
120 | {
|
---|
121 | FMOD_VECTOR fmod_forward;
|
---|
122 | fmod_forward.x = forward.x;
|
---|
123 | fmod_forward.y = forward.y;
|
---|
124 | fmod_forward.z = forward.z;
|
---|
125 | FMOD_VECTOR fmod_up;
|
---|
126 | fmod_up.x = up.x;
|
---|
127 | fmod_up.y = up.y;
|
---|
128 | fmod_up.z = up.z;
|
---|
129 | // TODO: we also need to setup orientation!
|
---|
130 | FMOD_System_Set3DListenerAttributes( static_cast<FMOD_SYSTEM*>( m_system ), 0, 0, 0, &fmod_forward, &fmod_up );
|
---|
131 | }
|
---|
132 |
|
---|
133 | void fmod::audio::update( vec3 position )
|
---|
134 | {
|
---|
135 | m_position = position;
|
---|
136 | FMOD_VECTOR fmod_position;
|
---|
137 | fmod_position.x = position.x;
|
---|
138 | fmod_position.y = position.y;
|
---|
139 | fmod_position.z = position.z;
|
---|
140 | // FMOD_VECTOR fmod_up;
|
---|
141 | // fmod_up.x = 0.0f;
|
---|
142 | // fmod_up.y = 0.0f;
|
---|
143 | // fmod_up.z = 0.0f;
|
---|
144 | // TODO: we also need to setup orientation!
|
---|
145 | FMOD_System_Set3DListenerAttributes( static_cast<FMOD_SYSTEM*>( m_system ), 0, &fmod_position, 0, 0, 0 );
|
---|
146 | FMOD_System_Update( static_cast<FMOD_SYSTEM*>( m_system ) );
|
---|
147 | }
|
---|
148 |
|
---|
149 | fmod::audio::~audio()
|
---|
150 | {
|
---|
151 | for ( auto& s : m_sounds )
|
---|
152 | release( &s );
|
---|
153 | m_sounds.clear();
|
---|
154 | FMOD_System_Release( static_cast<FMOD_SYSTEM*>( m_system ) );
|
---|
155 | }
|
---|
156 |
|
---|