[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[194] | 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/fmod/fmod_audio.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/lib/fmod.hh"
|
---|
[319] | 10 | #include "nv/core/logging.hh"
|
---|
[194] | 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 | {
|
---|
[365] | 26 | NV_LOG_CRITICAL( "Failed to create FMOD System -- ", FMOD_ErrorString( result ) );
|
---|
[194] | 27 | return;
|
---|
| 28 | }
|
---|
[330] | 29 | result = FMOD_System_Init( system, 64, FMOD_3D | FMOD_INIT_3D_RIGHTHANDED, 0 );
|
---|
[194] | 30 | if ( result != FMOD_OK )
|
---|
| 31 | {
|
---|
[365] | 32 | NV_LOG_ERROR( "Failed to initialize FMOD System -- ", FMOD_ErrorString( result ) );
|
---|
[194] | 33 | return;
|
---|
| 34 | }
|
---|
| 35 | m_system = system;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[330] | 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 = (FMOD_SYSTEM*)m_system;
|
---|
| 44 | FMOD_SOUND* sample = (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 | {
|
---|
[365] | 49 | NV_LOG_WARNING( "FMOD failed to play sound -- ", FMOD_ErrorString( result ) );
|
---|
[330] | 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 | }
|
---|
[194] | 60 |
|
---|
[330] | 61 | nv::channel nv::fmod::audio::play_sound( sound a_sound, vec3 position )
|
---|
[194] | 62 | {
|
---|
[329] | 63 | sound_info* info = m_sounds.get( a_sound );
|
---|
| 64 | if ( info )
|
---|
[194] | 65 | {
|
---|
[330] | 66 | FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
|
---|
| 67 | FMOD_SOUND* sample = (FMOD_SOUND*)( info->fmod_sound );
|
---|
| 68 | FMOD_CHANNEL* channel = nullptr;
|
---|
| 69 | FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, true, &channel );
|
---|
[329] | 70 | if ( result != FMOD_OK )
|
---|
| 71 | {
|
---|
[365] | 72 | NV_LOG_WARNING( "FMOD failed to play sound -- ", FMOD_ErrorString( result ) );
|
---|
[329] | 73 | }
|
---|
[330] | 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 | }
|
---|
[194] | 84 | }
|
---|
[329] | 85 | return channel();
|
---|
[194] | 86 | }
|
---|
| 87 |
|
---|
[330] | 88 |
|
---|
| 89 |
|
---|
[329] | 90 | nv::sound fmod::audio::load_sound( const std::string& a_path )
|
---|
[194] | 91 | {
|
---|
| 92 | FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
|
---|
| 93 | FMOD_SOUND* sample;
|
---|
[330] | 94 | FMOD_RESULT fm_result = FMOD_System_CreateSound( system, a_path.c_str(), FMOD_3D, 0, &sample );
|
---|
[329] | 95 | if ( fm_result != FMOD_OK )
|
---|
[194] | 96 | {
|
---|
[365] | 97 | NV_LOG_ERROR( "FMOD failed to load sample '", a_path, "' -- ", FMOD_ErrorString( fm_result ) );
|
---|
[329] | 98 | return sound();
|
---|
[194] | 99 | }
|
---|
[329] | 100 | sound result = m_sounds.create();
|
---|
| 101 | sound_info* info = m_sounds.get( result );
|
---|
| 102 | info->fmod_sound = sample;
|
---|
| 103 | return result;
|
---|
[194] | 104 | }
|
---|
| 105 |
|
---|
[329] | 106 | void nv::fmod::audio::release( sound a_sound )
|
---|
| 107 | {
|
---|
| 108 | sound_info* info = m_sounds.get( a_sound );
|
---|
| 109 | if ( info )
|
---|
| 110 | {
|
---|
| 111 | FMOD_Sound_Release( (FMOD_SOUND*)info->fmod_sound );
|
---|
| 112 | m_sounds.destroy( a_sound );
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[330] | 116 | void nv::fmod::audio::set_orientation( vec3 forward, vec3 up )
|
---|
[194] | 117 | {
|
---|
[330] | 118 | FMOD_VECTOR fmod_forward;
|
---|
| 119 | fmod_forward.x = forward.x;
|
---|
| 120 | fmod_forward.y = forward.y;
|
---|
| 121 | fmod_forward.z = forward.z;
|
---|
| 122 | FMOD_VECTOR fmod_up;
|
---|
| 123 | fmod_up.x = up.x;
|
---|
| 124 | fmod_up.y = up.y;
|
---|
| 125 | fmod_up.z = up.z;
|
---|
| 126 | // TODO: we also need to setup orientation!
|
---|
| 127 | FMOD_System_Set3DListenerAttributes( (FMOD_SYSTEM*)m_system, 0, 0, 0, &fmod_forward, &fmod_up );
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void fmod::audio::update( vec3 position )
|
---|
| 131 | {
|
---|
| 132 | m_position = position;
|
---|
| 133 | FMOD_VECTOR fmod_position;
|
---|
| 134 | fmod_position.x = position.x;
|
---|
| 135 | fmod_position.y = position.y;
|
---|
| 136 | fmod_position.z = position.z;
|
---|
| 137 | // FMOD_VECTOR fmod_up;
|
---|
| 138 | // fmod_up.x = 0.0f;
|
---|
| 139 | // fmod_up.y = 0.0f;
|
---|
| 140 | // fmod_up.z = 0.0f;
|
---|
| 141 | // TODO: we also need to setup orientation!
|
---|
| 142 | FMOD_System_Set3DListenerAttributes( (FMOD_SYSTEM*)m_system, 0, &fmod_position, 0, 0, 0 );
|
---|
[194] | 143 | FMOD_System_Update( (FMOD_SYSTEM*)m_system );
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | fmod::audio::~audio()
|
---|
| 147 | {
|
---|
[329] | 148 | while ( m_sounds.size() > 0 )
|
---|
| 149 | release( m_sounds.get_handle(0) );
|
---|
[194] | 150 | FMOD_System_Release( (FMOD_SYSTEM*)m_system );
|
---|
| 151 | }
|
---|
| 152 |
|
---|