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/sdl/sdl_audio.hh"
|
---|
8 |
|
---|
9 | #include "nv/lib/sdl.hh"
|
---|
10 | #include "nv/lib/sdl_mixer.hh"
|
---|
11 | #include "nv/core/logging.hh"
|
---|
12 |
|
---|
13 | using namespace nv;
|
---|
14 |
|
---|
15 | sdl::audio::audio()
|
---|
16 | {
|
---|
17 | nv::load_sdl_library();
|
---|
18 | nv::load_sdl_mixer_library();
|
---|
19 |
|
---|
20 | if ( SDL_Init( SDL_INIT_AUDIO ) == -1 )
|
---|
21 | {
|
---|
22 | NV_LOG( LOG_CRITICAL, "SDL_AUDIO failed to load -- " << SDL_GetError() );
|
---|
23 | return;
|
---|
24 | }
|
---|
25 |
|
---|
26 | if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 )
|
---|
27 | {
|
---|
28 | NV_LOG( LOG_CRITICAL, "SDL_mixer failed to load -- " << Mix_GetError() );
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | nv::channel* sdl::audio::play_sound( nv::sound* a_sound )
|
---|
35 | {
|
---|
36 | if ( Mix_PlayChannel(-1, (Mix_Chunk*)( down_cast< sdl::sound >( a_sound )->m_sound), 0) == -1 )
|
---|
37 | {
|
---|
38 | NV_LOG( LOG_WARNING, "SDL_mixer failed to play -- " << Mix_GetError() );
|
---|
39 | }
|
---|
40 | return nullptr;
|
---|
41 | }
|
---|
42 |
|
---|
43 | nv::sound* nv::sdl::audio::load_sound( const std::string& a_path )
|
---|
44 | {
|
---|
45 | Mix_Chunk *sample = Mix_LoadWAV( a_path.c_str() );
|
---|
46 | if ( sample == nullptr )
|
---|
47 | {
|
---|
48 | NV_LOG( LOG_ERROR, "SDL_mixer failed to load sample '" << a_path << "' -- " << Mix_GetError() );
|
---|
49 | return nullptr;
|
---|
50 | }
|
---|
51 | return new sdl::sound( sample );
|
---|
52 | }
|
---|
53 |
|
---|
54 | void nv::sdl::audio::update()
|
---|
55 | {
|
---|
56 | // no-op
|
---|
57 | }
|
---|
58 |
|
---|
59 | nv::sdl::audio::~audio()
|
---|
60 | {
|
---|
61 | Mix_CloseAudio();
|
---|
62 | // TODO: should we do it here?
|
---|
63 | SDL_Quit();
|
---|
64 | }
|
---|
65 |
|
---|
66 | nv::sdl::sound::~sound()
|
---|
67 | {
|
---|
68 | Mix_FreeChunk( (Mix_Chunk*)m_sound );
|
---|
69 | }
|
---|