1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
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/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( 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 |
|
---|
39 | nv::channel* fmod::audio::play_sound( nv::sound* a_sound )
|
---|
40 | {
|
---|
41 | FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
|
---|
42 | FMOD_SOUND* sample = (FMOD_SOUND*)( down_cast< fmod::sound >( a_sound )->m_sound );
|
---|
43 | FMOD_RESULT result = FMOD_System_PlaySound( system, FMOD_CHANNEL_FREE, sample, false, 0 );
|
---|
44 | if ( result != FMOD_OK )
|
---|
45 | {
|
---|
46 | NV_LOG( LOG_WARNING, "FMOD failed to play sound -- " << FMOD_ErrorString( result ) );
|
---|
47 | }
|
---|
48 | return nullptr;
|
---|
49 | }
|
---|
50 |
|
---|
51 | nv::sound* fmod::audio::load_sound( const std::string& a_path )
|
---|
52 | {
|
---|
53 | FMOD_SYSTEM* system = (FMOD_SYSTEM*)m_system;
|
---|
54 | FMOD_SOUND* sample;
|
---|
55 | FMOD_RESULT result = FMOD_System_CreateSound( system, a_path.c_str(), FMOD_DEFAULT, 0, &sample );
|
---|
56 | if ( result != FMOD_OK )
|
---|
57 | {
|
---|
58 | NV_LOG( LOG_ERROR, "FMOD failed to load sample '" << a_path << "' -- " << FMOD_ErrorString( result ) );
|
---|
59 | return nullptr;
|
---|
60 | }
|
---|
61 | return new fmod::sound( sample );
|
---|
62 | }
|
---|
63 |
|
---|
64 | void fmod::audio::update()
|
---|
65 | {
|
---|
66 | FMOD_System_Update( (FMOD_SYSTEM*)m_system );
|
---|
67 | // no-op
|
---|
68 | }
|
---|
69 |
|
---|
70 | fmod::audio::~audio()
|
---|
71 | {
|
---|
72 | FMOD_System_Release( (FMOD_SYSTEM*)m_system );
|
---|
73 | }
|
---|
74 |
|
---|
75 | fmod::sound::~sound()
|
---|
76 | {
|
---|
77 | FMOD_Sound_Release( (FMOD_SOUND*)m_sound );
|
---|
78 | }
|
---|