source: trunk/src/fmod/fmod_audio.cc @ 491

Last change on this file since 491 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 4.3 KB
Line 
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
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        {
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
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        {
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
61nv::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
90nv::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
106void 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( static_cast<FMOD_SOUND*>( info->fmod_sound ) );
112                m_sounds.destroy( a_sound );
113        }
114}
115
116void nv::fmod::audio::set_orientation( vec3 forward, vec3 up )
117{
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( static_cast<FMOD_SYSTEM*>( m_system ), 0, 0, 0, &fmod_forward, &fmod_up );
128}
129
130void 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( static_cast<FMOD_SYSTEM*>( m_system ), 0, &fmod_position, 0, 0, 0 );
143        FMOD_System_Update( static_cast<FMOD_SYSTEM*>( m_system ) );
144}
145
146fmod::audio::~audio()
147{
148        while ( m_sounds.size() > 0 )
149                release( m_sounds.get_handle(0) );
150        FMOD_System_Release( static_cast<FMOD_SYSTEM*>( m_system ) );
151}
152
Note: See TracBrowser for help on using the repository browser.