1 // sound_handler_sdl.cpp: Sound handling using standard SDL
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 // Based on sound_handler_sdl.cpp by Thatcher Ulrich http://tulrich.com 2003
22 // which has been donated to the Public Domain.
24 #include "sound_handler_sdl.h"
25 #include "SoundInfo.h"
26 #include "EmbedSound.h"
27 #include "AuxStream.h" // for use..
28 #include "GnashSleep.h"
30 #include "log.h" // will import boost::format too
31 #include "GnashException.h" // for SoundException
34 #include <boost/scoped_array.hpp>
37 // Define this to get debugging call about pausing/unpausing audio
38 //#define GNASH_DEBUG_SDL_AUDIO_PAUSING
40 // Mixing and decoding debugging
41 //#define GNASH_DEBUG_MIXING
49 SDL_sound_handler::initAudio()
51 // NOTE: we open and close the audio card for the sole purpose
52 // of throwing an exception on error (unavailable audio
53 // card). Normally we'd want to open the audio card only
54 // when needed (it has a cost in number of wakeups).
58 // SDL can hang on windows if SDL_CloseAudio() is called immediately
59 // after SDL_OpenAudio(). It's evidently to do with threading, but
60 // internal to SDL. This is a tacky solution, but it's only windows.
69 SDL_sound_handler::openAudio()
71 if (_audioOpened
) return; // nothing to do
73 // This is our sound settings
74 audioSpec
.freq
= 44100;
76 // Each sample is a signed 16-bit audio in system-endian format
77 audioSpec
.format
= AUDIO_S16SYS
;
79 // We want to be pulling samples for 2 channels:
80 // {left,right},{left,right},...
81 audioSpec
.channels
= 2;
83 audioSpec
.callback
= SDL_sound_handler::sdl_audio_callback
;
85 audioSpec
.userdata
= this;
87 //512 - not enough for videostream
88 audioSpec
.samples
= 2048;
90 if (SDL_OpenAudio(&audioSpec
, NULL
) < 0) {
91 boost::format fmt
= boost::format(_("Couldn't open SDL audio: %s"))
93 throw SoundException(fmt
.str());
100 SDL_sound_handler::closeAudio()
103 _audioOpened
= false;
107 SDL_sound_handler::SDL_sound_handler(media::MediaHandler
* m
)
116 SDL_sound_handler::reset()
118 boost::mutex::scoped_lock
lock(_mutex
);
119 sound_handler::stop_all_sounds();
122 SDL_sound_handler::~SDL_sound_handler()
124 boost::mutex::scoped_lock
lock(_mutex
);
125 #ifdef GNASH_DEBUG_SDL_AUDIO_PAUSING
126 log_debug("Pausing SDL Audio on destruction");
132 // we already locked, so we call
133 // the base class (non-locking) deleter
136 unplugAllInputStreams();
144 SDL_sound_handler::create_sound(std::auto_ptr
<SimpleBuffer
> data
,
145 std::auto_ptr
<media::SoundInfo
> sinfo
)
147 boost::mutex::scoped_lock
lock(_mutex
);
148 return sound_handler::create_sound(data
, sinfo
);
151 sound_handler::StreamBlockId
152 SDL_sound_handler::addSoundBlock(unsigned char* data
,
153 unsigned int dataBytes
, unsigned int nSamples
,
157 boost::mutex::scoped_lock
lock(_mutex
);
158 return sound_handler::addSoundBlock(data
, dataBytes
, nSamples
, streamId
);
163 SDL_sound_handler::stop_sound(int soundHandle
)
165 boost::mutex::scoped_lock
lock(_mutex
);
166 sound_handler::stop_sound(soundHandle
);
171 SDL_sound_handler::delete_sound(int soundHandle
)
173 boost::mutex::scoped_lock
lock(_mutex
);
174 sound_handler::delete_sound(soundHandle
);
178 SDL_sound_handler::stop_all_sounds()
180 boost::mutex::scoped_lock
lock(_mutex
);
181 sound_handler::stop_all_sounds();
186 SDL_sound_handler::get_volume(int soundHandle
)
188 boost::mutex::scoped_lock
lock(_mutex
);
189 return sound_handler::get_volume(soundHandle
);
194 SDL_sound_handler::set_volume(int soundHandle
, int volume
)
196 boost::mutex::scoped_lock
lock(_mutex
);
197 sound_handler::set_volume(soundHandle
, volume
);
201 SDL_sound_handler::get_sound_info(int soundHandle
)
203 boost::mutex::scoped_lock
lock(_mutex
);
204 return sound_handler::get_sound_info(soundHandle
);
208 SDL_sound_handler::get_duration(int soundHandle
)
210 boost::mutex::scoped_lock
lock(_mutex
);
211 return sound_handler::get_duration(soundHandle
);
215 SDL_sound_handler::tell(int soundHandle
)
217 boost::mutex::scoped_lock
lock(_mutex
);
218 return sound_handler::tell(soundHandle
);
222 create_sound_handler_sdl(media::MediaHandler
* m
)
224 return new SDL_sound_handler(m
);
228 SDL_sound_handler::fetchSamples(boost::int16_t* to
, unsigned int nSamples
)
230 boost::mutex::scoped_lock
lock(_mutex
);
231 sound_handler::fetchSamples(to
, nSamples
);
233 // If nothing is left to play there is no reason to keep polling.
234 if ( ! hasInputStreams() )
236 #ifdef GNASH_DEBUG_SDL_AUDIO_PAUSING
237 log_debug("Pausing SDL Audio...");
243 // Callback invoked by the SDL audio thread.
245 SDL_sound_handler::sdl_audio_callback(void *udata
, Uint8
*buf
, int bufLenIn
)
248 log_error(_("Negative buffer length in sdl_audio_callback (%d)"),
254 log_error(_("Zero buffer length in sdl_audio_callback"));
258 unsigned int bufLen
= static_cast<unsigned int>(bufLenIn
);
259 boost::int16_t* samples
= reinterpret_cast<boost::int16_t*>(buf
);
261 // 16 bit per sample, 2 channels == 4 bytes per fetch ?
264 unsigned int nSamples
= bufLen
/2;
266 //log_debug("Fetching %d bytes (%d samples)", bufLen, nSamples);
268 // Get the soundhandler
269 SDL_sound_handler
* handler
= static_cast<SDL_sound_handler
*>(udata
);
270 handler
->fetchSamples(samples
, nSamples
);
274 SDL_sound_handler::mix(boost::int16_t* outSamples
, boost::int16_t* inSamples
,
275 unsigned int nSamples
, float volume
)
277 Uint8
* out
= reinterpret_cast<Uint8
*>(outSamples
);
278 Uint8
* in
= reinterpret_cast<Uint8
*>(inSamples
);
279 unsigned int nBytes
= nSamples
*2;
281 SDL_MixAudio(out
, in
, nBytes
, SDL_MIX_MAXVOLUME
*volume
);
285 SDL_sound_handler::plugInputStream(std::auto_ptr
<InputStream
> newStreamer
)
287 boost::mutex::scoped_lock
lock(_mutex
);
289 sound_handler::plugInputStream(newStreamer
);
291 { // TODO: this whole block should only be executed when adding
294 #ifdef GNASH_DEBUG_SDL_AUDIO_PAUSING
295 log_debug("Unpausing SDL Audio on inpust stream plug...");
297 openAudio(); // lazy sound card initialization
298 SDL_PauseAudio(0); // start polling data from us
303 SDL_sound_handler::mute()
305 boost::mutex::scoped_lock
lock(_mutedMutex
);
306 sound_handler::mute();
310 SDL_sound_handler::unmute()
312 boost::mutex::scoped_lock
lock(_mutedMutex
);
313 sound_handler::unmute();
317 SDL_sound_handler::is_muted() const
319 boost::mutex::scoped_lock
lock(_mutedMutex
);
320 return sound_handler::is_muted();
324 SDL_sound_handler::pause()
327 sound_handler::pause();
331 SDL_sound_handler::unpause()
333 if (hasInputStreams()) {
338 sound_handler::unpause();
341 } // gnash.sound namespace