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

Last change on this file since 329 was 329, checked in by epyon, 11 years ago
  • audio now based on handles
File size: 2.3 KB
Line 
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/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( LOG_CRITICAL, "Failed to create FMOD System -- " << FMOD_ErrorString( result ) );
27                return;
28        }
29        result = FMOD_System_Init( system, 64, FMOD_INIT_NORMAL, 0 );
30        if ( result != FMOD_OK )
31        {
32                NV_LOG( LOG_ERROR, "Failed to initialize FMOD System -- " << FMOD_ErrorString( result ) );
33                return;
34        }
35        m_system = system;
36}
37
38
39nv::channel fmod::audio::play_sound( nv::sound a_sound )
40{
41        sound_info* info = m_sounds.get( a_sound );
42        if ( info )
43        {
44                FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
45                FMOD_SOUND* sample  = (FMOD_SOUND*)( info->fmod_sound );
46                FMOD_RESULT result  = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, false, 0 );
47                if ( result != FMOD_OK )
48                {
49                        NV_LOG( LOG_WARNING, "FMOD failed to play sound -- " << FMOD_ErrorString( result ) );
50                }
51        }
52        return channel();
53}
54
55nv::sound fmod::audio::load_sound( const std::string& a_path )
56{
57        FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
58        FMOD_SOUND* sample;
59        FMOD_RESULT fm_result = FMOD_System_CreateSound( system, a_path.c_str(), FMOD_DEFAULT, 0, &sample );
60        if ( fm_result != FMOD_OK )
61        {
62                NV_LOG( LOG_ERROR, "FMOD failed to load sample '" << a_path << "' -- " << FMOD_ErrorString( fm_result ) );
63                return sound();
64        }
65        sound result = m_sounds.create();
66        sound_info* info = m_sounds.get( result );
67        info->fmod_sound = sample;
68        return result;
69}
70
71void nv::fmod::audio::release( sound a_sound )
72{
73        sound_info* info = m_sounds.get( a_sound );
74        if ( info )
75        {
76                FMOD_Sound_Release( (FMOD_SOUND*)info->fmod_sound );
77                m_sounds.destroy( a_sound );
78        }
79}
80
81void fmod::audio::update()
82{
83        FMOD_System_Update( (FMOD_SYSTEM*)m_system );
84        // no-op
85}
86
87fmod::audio::~audio()
88{
89        while ( m_sounds.size() > 0 )
90                release( m_sounds.get_handle(0) );
91        FMOD_System_Release( (FMOD_SYSTEM*)m_system );
92}
93
Note: See TracBrowser for help on using the repository browser.