source: trunk/src/fmod/fmod_audio.cc

Last change on this file was 543, checked in by epyon, 8 years ago
  • fixes in handle store
  • removal of get_handle
  • indices instead of handles
  • efficient handle store clears
File size: 4.3 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[194]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.
[194]6
7#include "nv/fmod/fmod_audio.hh"
8
9#include "nv/lib/fmod.hh"
[319]10#include "nv/core/logging.hh"
[194]11
12using namespace nv;
13
14fmod::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]38nv::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        {
[406]43                FMOD_SYSTEM* system   = static_cast<FMOD_SYSTEM*>( m_system );
44                FMOD_SOUND* sample    = static_cast<FMOD_SOUND*>( info->fmod_sound );
[330]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]61nv::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        {
[406]66                FMOD_SYSTEM* system   = static_cast<FMOD_SYSTEM*>( m_system );
67                FMOD_SOUND* sample    = static_cast<FMOD_SOUND*>( info->fmod_sound );
[330]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
[399]90nv::sound fmod::audio::load_sound( const string_view& a_path )
[194]91{
[406]92        FMOD_SYSTEM* system = static_cast<FMOD_SYSTEM*>( m_system );
[194]93        FMOD_SOUND* sample;
[392]94        FMOD_RESULT fm_result = FMOD_System_CreateSound( system, a_path.data(), 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]106void nv::fmod::audio::release( sound a_sound )
107{
108        sound_info* info = m_sounds.get( a_sound );
[543]109        release( info );
110        m_sounds.destroy( a_sound );
111}
112
113void nv::fmod::audio::release( sound_info* info )
114{
[329]115        if ( info )
[406]116                FMOD_Sound_Release( static_cast<FMOD_SOUND*>( info->fmod_sound ) );
[329]117}
118
[330]119void nv::fmod::audio::set_orientation( vec3 forward, vec3 up )
[194]120{
[330]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!
[406]130        FMOD_System_Set3DListenerAttributes( static_cast<FMOD_SYSTEM*>( m_system ), 0, 0, 0, &fmod_forward, &fmod_up );
[330]131}
132
133void 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!
[406]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 ) );
[194]147}
148
149fmod::audio::~audio()
150{
[543]151        for ( auto& s : m_sounds )
152                release( &s );
153        m_sounds.clear();
[406]154        FMOD_System_Release( static_cast<FMOD_SYSTEM*>( m_system ) );
[194]155}
156
Note: See TracBrowser for help on using the repository browser.