source: trunk/src/sdl/sdl_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.1 KB
RevLine 
[319]1// Copyright (C) 2012-2014 ChaosForge Ltd
[202]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"
[319]11#include "nv/core/logging.hh"
[202]12
13using namespace nv;
14
15sdl::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
[329]34nv::channel sdl::audio::play_sound( sound a_sound )
[202]35{
[329]36        sound_info* info = m_sounds.get( a_sound );
37        if ( info )
[202]38        {
[329]39                if ( Mix_PlayChannel(-1, (Mix_Chunk*)( info->sdl_sound), 0) == -1 )
40                {
41                        NV_LOG( LOG_WARNING, "SDL_mixer failed to play -- " << Mix_GetError() );
42                }
[202]43        }
[329]44        return channel();
[202]45}
46
[329]47nv::sound nv::sdl::audio::load_sound( const std::string& a_path )
[202]48{
[322]49        // TODO: this is a really wierd error - if we remove this check, all hell gets loose
50        if ( Mix_LoadWAV_RW == nullptr || SDL_RWFromFile == nullptr )
51        {
52                NV_LOG( LOG_ERROR, "SDL_mixer not loaded!" );
53        }
54        Mix_Chunk *sample = Mix_LoadWAV_RW(SDL_RWFromFile(a_path.c_str(), "rb"), 1);
[202]55        if ( sample == nullptr )
56        {
57                NV_LOG( LOG_ERROR, "SDL_mixer failed to load sample '" << a_path << "' -- " << Mix_GetError() );
[329]58                return sound();
[202]59        }
[329]60        sound result = m_sounds.create();
61        sound_info* info = m_sounds.get( result );
62        info->sdl_sound = sample;
63        return result;
[202]64}
65
66void nv::sdl::audio::update()
67{
68        // no-op
69}
70
71nv::sdl::audio::~audio()
72{
[329]73        while ( m_sounds.size() > 0 )
74                release( m_sounds.get_handle(0) );
[202]75        Mix_CloseAudio();
76        // TODO: should we do it here?
77        SDL_Quit();
78}
79
[329]80void nv::sdl::audio::release( sound a_sound )
[202]81{
[329]82        sound_info* info = m_sounds.get( a_sound );
83        if ( info )
84        {
85                Mix_FreeChunk( (Mix_Chunk*)info->sdl_sound );
86                m_sounds.destroy( a_sound );
87        }
[202]88}
[329]89
Note: See TracBrowser for help on using the repository browser.