Gnash  0.8.11dev
sound_handler.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef SOUND_HANDLER_H
20 #define SOUND_HANDLER_H
21 
22 #ifdef HAVE_CONFIG_H
23 #include "gnashconfig.h"
24 #endif
25 
26 #include <atomic>
27 #include <limits>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
33 #include "dsodefs.h" // for DSOEXPORT
34 #include "SoundEnvelope.h" // for SoundEnvelopes typedef
35 #include "AuxStream.h" // for aux_streamer_ptr typedef
36 #include "WAVWriter.h"
37 
38 namespace gnash {
39  namespace media {
40  class MediaHandler;
41  class SoundInfo;
42  }
43  namespace sound {
44  class EmbedSound;
45  class StreamingSound;
46  class StreamingSoundData;
47  class InputStream;
48  }
49  class SimpleBuffer;
50 }
51 
52 namespace gnash {
53 
55 //
59 namespace sound {
60 
62 //
83 //
88 {
89 public:
90 
91  virtual ~sound_handler();
92 
94  //
97  typedef unsigned long StreamBlockId;
98 
102 
104  //
106  virtual void stop_all_sounds();
107 
111 
113  //
121  virtual int create_sound(std::unique_ptr<SimpleBuffer> data,
122  const media::SoundInfo& sinfo);
123 
125  //
130  //
132  virtual void stopEventSound(int sound_handle);
133 
135  //
136  virtual void stopAllEventSounds();
137 
139  //
142  //
144  virtual void delete_sound(int sound_handle);
145 
147  //
176  void startSound(int id, int loops, const SoundEnvelopes* env,
177  bool allowMultiple, unsigned int inPoint = 0,
178  unsigned int outPoint =
179  std::numeric_limits<unsigned int>::max());
180 
182  //
184  //
186  bool isSoundPlaying(int id) const;
187 
189  //
196  virtual void set_volume(int sound_handle, int volume);
197 
199  //
202  virtual unsigned int get_duration(int sound_handle) const;
203 
205  //
209  virtual unsigned int tell(int sound_handle) const;
210 
212  //
219  virtual int get_volume(int sound_handle) const;
220 
224 
225  virtual int createStreamingSound(const media::SoundInfo& sinfo);
226 
228  //
231  virtual void stopStreamingSound(int handle);
232 
247  virtual StreamBlockId addSoundBlock(SimpleBuffer data,
248  size_t sampleCount, int seekSamples, int streamId);
249 
251  //
253  //
260  virtual media::SoundInfo* get_sound_info(int handle) const;
261 
263  //
266  //
270  //
273  void playStream(int handle, StreamBlockId blockId);
274 
276  //
280  //
287  int getStreamBlock(int handle) const;
288 
292 
294  //
297  int getFinalVolume() const { return _volume; }
298 
300  //
303  void setFinalVolume(int v) { _volume = v; }
304 
308  //
313  virtual void reset();
314 
316  void mute();
317 
319  void unmute();
320 
322  //
324  bool is_muted() const;
325 
327  virtual void pause() { _paused=true; }
328 
330  virtual void unpause() { _paused=false; }
331 
333  bool isPaused() const { return _paused; }
334 
336  //
370  virtual InputStream* attach_aux_streamer(aux_streamer_ptr ptr, void* udata);
371 
373  //
377  //
382  virtual void unplugInputStream(InputStream* id);
383 
385  //
388  size_t numSoundsStarted() const { return _soundsStarted; }
389 
391  //
394  size_t numSoundsStopped() const { return _soundsStopped; }
395 
397  //
416  virtual void fetchSamples(std::int16_t* to, unsigned int nSamples);
417 
419  //
443  virtual void mix(std::int16_t* outSamples, std::int16_t* inSamples,
444  unsigned int nSamples, float volume);
445 
447  //
451  void setAudioDump(const std::string& wavefile);
452 
454  //
456  bool streamingSound() const;
457 
458 protected:
459 
461  :
462  _soundsStarted(0),
463  _soundsStopped(0),
464  _paused(false),
465  _muted(false),
466  _volume(100),
467  _mediaHandler(m)
468  {
469  }
470 
472  //
476  virtual void plugInputStream(std::unique_ptr<InputStream> in);
477 
479  virtual void unplugAllInputStreams();
480 
482  bool hasInputStreams() const;
483 
485  //
487  virtual void delete_all_sounds();
488 
489 private:
490 
492  size_t _soundsStarted;
493 
495  size_t _soundsStopped;
496 
498  bool _paused;
499 
501  std::atomic<bool> _muted;
502 
504  int _volume;
505 
506  typedef std::vector<EmbedSound*> Sounds;
507 
509  //
511  Sounds _sounds;
512 
513  typedef std::vector<StreamingSoundData*> StreamingSounds;
514 
516  //
518  StreamingSounds _streamingSounds;
519 
521  void stopEmbedSoundInstances(EmbedSound& def);
522 
524  void stopEmbedSoundInstances(StreamingSoundData& def);
525 
526  typedef std::set<InputStream*> InputStreams;
527 
529  //
531  InputStreams _inputStreams;
532 
533  media::MediaHandler* _mediaHandler;
534 
536  void unplugCompletedInputStreams();
537 
538  std::unique_ptr<WAVWriter> _wavWriter;
539 
540 };
541 
542 // TODO: move to appropriate specific sound handlers
543 
544 #ifdef SOUND_SDL
547 #elif defined(SOUND_AHI)
550 #elif defined(SOUND_MKIT)
553 #endif
554 
555 } // gnash.sound namespace
556 } // namespace gnash
557 
558 #endif // SOUND_HANDLER_H
559 
560 
561 // Local Variables:
562 // mode: C++
563 // indent-tabs-mode: t
564 // End:
sound_handler.h
gnash::sound::WAVWriter
WAV writer class.
Definition: WAVWriter.h:32
EmbedSound.h
gnash::sound::sound_handler::get_duration
virtual unsigned int get_duration(int sound_handle) const
Gets the duration in milliseconds of an event sound.
Definition: sound_handler.cpp:421
gnash::sound::sound_handler::stopStreamingSound
virtual void stopStreamingSound(int handle)
Remove scheduled requests to play the specified sound buffer slot.
Definition: sound_handler.cpp:268
MIX_MAXVOLUME
#define MIX_MAXVOLUME
Definition: sound_handler.cpp:77
gnash::sound::sound_handler::startSound
void startSound(int id, int loops, const SoundEnvelopes *env, bool allowMultiple, unsigned int inPoint=0, unsigned int outPoint=std::numeric_limits< unsigned int >::max())
Start playback of an event sound.
Definition: sound_handler.cpp:517
gnash::log_swferror
void log_swferror(StringType msg, Args... args)
Definition: log.h:325
gnash::media::SoundInfo::getSampleCount
unsigned long getSampleCount() const
Returns the samplecount of the sound.
Definition: SoundInfo.h:103
SimpleBuffer.h
gnash::sound::sound_handler::unplugAllInputStreams
virtual void unplugAllInputStreams()
Unplug all input streams.
Definition: sound_handler.cpp:636
gnash::sound::EmbedSound
Definition of an embedded sound.
Definition: EmbedSound.h:50
gnash::media::SoundInfo
Class containing information about an embedded sound definition.
Definition: SoundInfo.h:34
gnash::key::T
@ T
Definition: GnashKey.h:132
gnash::sound::sound_handler::unplugInputStream
virtual void unplugInputStream(InputStream *id)
Unplug an external InputStream from the mixer.
Definition: sound_handler.cpp:373
gnash::sound::sound_handler::stopEventSound
virtual void stopEventSound(int sound_handle)
Remove scheduled requests to play the specified sound buffer slot.
Definition: sound_handler.cpp:283
dsodefs.h
gnash::sound::sound_handler::fetchSamples
virtual void fetchSamples(std::int16_t *to, unsigned int nSamples)
Fetch mixed samples.
Definition: sound_handler.cpp:646
StreamingSound.h
gnash::sound::EmbedSound::soundinfo
media::SoundInfo soundinfo
Object holding information about the sound.
Definition: EmbedSound.h:171
gnash::sound::aux_streamer_ptr
unsigned int(* aux_streamer_ptr)(void *udata, std::int16_t *samples, unsigned int nSamples, bool &eof)
Definition: AuxStream.h:31
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
gnash::sound::sound_handler::streamingSound
bool streamingSound() const
Check if a streaming sound is playing.
Definition: sound_handler.cpp:719
gnash::key::i
@ i
Definition: GnashKey.h:155
LOG_ONCE
#define LOG_ONCE(x)
Definition: log.h:49
gnash::sound::StreamingSoundData::clearInstances
void clearInstances()
Drop all active sounds.
Definition: StreamingSoundData.cpp:63
gnash::sound::sound_handler::delete_sound
virtual void delete_sound(int sound_handle)
Discard the sound data for an embedded event sound.
Definition: sound_handler.cpp:198
gnash::sound::sound_handler::unmute
void unmute()
Call this to unmute audio.
Definition: sound_handler.cpp:805
_
#define _(String)
Definition: log.h:44
StreamingSoundData.h
gnash::sound::InputStream::eof
virtual bool eof() const =0
Return true if there'll be no more data to fetch.
MediaHandler.h
gnash::sound::EmbedSound::isPlaying
bool isPlaying() const
Are there known playing instances of this sound ?
Definition: EmbedSound.cpp:97
gnash::media::SoundInfo::getDelaySeek
std::int16_t getDelaySeek() const
Return the number of samples to seek forward or delay.
Definition: SoundInfo.h:109
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::sound::sound_handler::get_volume
virtual int get_volume(int sound_handle) const
Gets the volume for a given sound buffer slot.
Definition: sound_handler.cpp:241
gnash::sound::sound_handler::isPaused
bool isPaused() const
return true if audio is paused
Definition: sound_handler.h:333
gnash::key::m
@ m
Definition: GnashKey.h:159
SoundEnvelope.h
gnash::key::s
@ s
Definition: GnashKey.h:165
gnash::sound::sound_handler::mute
void mute()
Call this to mute audio.
Definition: sound_handler.cpp:798
gnash::media::SoundInfo::getSampleRate
unsigned long getSampleRate() const
Returns the samplerate of the sound.
Definition: SoundInfo.h:94
gnash::sound::sound_handler::createStreamingSound
virtual int createStreamingSound(const media::SoundInfo &sinfo)
Streaming sound functions:
Definition: sound_handler.cpp:442
gnash::sound::EmbedSound::createInstance
std::unique_ptr< EmbedSoundInst > createInstance(media::MediaHandler &mh, unsigned int inPoint, unsigned int outPoint, const SoundEnvelopes *envelopes, int loopCount)
Create an instance of this sound.
Definition: EmbedSound.cpp:60
gnash::sound::sound_handler::sound_handler
sound_handler(media::MediaHandler *m)
Definition: sound_handler.h:460
gnash::log_error
void log_error(StringType msg, Args... args)
Definition: log.h:283
volume
@ volume
Definition: klash_part.cpp:331
gnash::sound::sound_handler::stopAllEventSounds
virtual void stopAllEventSounds()
Stop all instances of all playing event sounds.
Definition: sound_handler.cpp:306
IF_VERBOSE_MALFORMED_SWF
#define IF_VERBOSE_MALFORMED_SWF(x)
Definition: log.h:404
gnash::media::MediaHandler
The MediaHandler class acts as a factory to provide parser and decoders.
Definition: MediaHandler.h:70
gnash::sound::StreamingSoundData::append
size_t append(SimpleBuffer data, size_t sampleCount, int seekSamples)
Append a sound data block.
Definition: StreamingSoundData.cpp:38
fill
VGPaint fill
Definition: testr_gtk.cpp:86
gnash::sound::sound_handler::getStreamBlock
int getStreamBlock(int handle) const
Get the identifier for the block playing in a specific stream.
Definition: sound_handler.cpp:730
WAVWriter.h
gnash::sound::sound_handler::setFinalVolume
void setFinalVolume(int v)
Set the volume to apply to mixed output.
Definition: sound_handler.h:303
gnash::sound::sound_handler::plugInputStream
virtual void plugInputStream(std::unique_ptr< InputStream > in)
Plug an InputStream to the mixer.
Definition: sound_handler.cpp:613
gnash::sound::sound_handler::attach_aux_streamer
virtual InputStream * attach_aux_streamer(aux_streamer_ptr ptr, void *udata)
Plug an external InputStream into the mixer.
Definition: sound_handler.cpp:820
gnash::sound::sound_handler::hasInputStreams
bool hasInputStreams() const
Does the mixer have input streams ?
Definition: sound_handler.cpp:785
gnash::sound::sound_handler::create_sound
virtual int create_sound(std::unique_ptr< SimpleBuffer > data, const media::SoundInfo &sinfo)
Event sound functions:
Definition: sound_handler.cpp:455
gnash::sound::sound_handler::reset
virtual void reset()
Discard all sound inputs (slots and aux streamers) and clear scheduling.
Definition: sound_handler.cpp:812
gnash::sound::sound_handler::is_muted
bool is_muted() const
Returns whether or not sound is muted.
Definition: sound_handler.cpp:791
gnash::sound::sound_handler::unpause
virtual void unpause()
gnash calls this to unpause audio
Definition: sound_handler.h:330
gnash::sound::sound_handler::pause
virtual void pause()
gnash calls this to pause audio
Definition: sound_handler.h:327
gnash::sound::create_sound_handler_mkit
sound_handler * create_sound_handler_mkit(media::MediaHandler *m)
Definition: sound_handler_mkit.cpp:269
gnash::sound::EmbedSound::firstPlayingInstance
EmbedSoundInst * firstPlayingInstance() const
Return the first created instance of this sound.
Definition: EmbedSound.cpp:111
gnash::sound::StreamingSoundData::getPlayingInstances
void getPlayingInstances(std::vector< InputStream * > &to) const
Append to the given vector all playing instances of this sound def.
Definition: StreamingSoundData.cpp:135
log.h
gnash::sound::swfToOutSamples
size_t swfToOutSamples(const media::SoundInfo &sinfo, size_t swfSamples, const size_t outRate=44100)
Convert SWF-specified number of samples to output number of samples.
Definition: SoundUtils.h:55
gnash::sound::sound_handler::getFinalVolume
int getFinalVolume() const
Sound output functions.
Definition: sound_handler.h:297
gnash::sound::sound_handler::mix
virtual void mix(std::int16_t *outSamples, std::int16_t *inSamples, unsigned int nSamples, float volume)
Mix nSamples from inSamples to outSamples, with given volume.
Definition: sound_handler.cpp:841
gnash::sound::sound_handler::numSoundsStarted
size_t numSoundsStarted() const
Special test-fuction. Reports how many times a sound has been started.
Definition: sound_handler.h:388
gnash::sound::sound_handler::tell
virtual unsigned int tell(int sound_handle) const
Gets the playhead position in milliseconds of an event sound.
Definition: sound_handler.cpp:398
gnash::sound::sound_handler
Sound mixer.
Definition: sound_handler.h:88
gnash::sound::sound_handler::numSoundsStopped
size_t numSoundsStopped() const
Special test-fuction. Reports how many times a sound has been stopped.
Definition: sound_handler.h:394
gnash::sound::sound_handler::setAudioDump
void setAudioDump(const std::string &wavefile)
Request to dump audio to the given filename.
Definition: sound_handler.cpp:701
gnashconfig.h
gnash::image::end
pixel_iterator< T > end(GnashImage &im)
Definition: ImageIterators.h:198
gnash::sound::sound_handler::StreamBlockId
unsigned long StreamBlockId
Identifier of a streaming sound block.
Definition: sound_handler.h:97
gnash::sound::SoundEnvelopes
std::vector< SoundEnvelope > SoundEnvelopes
A vector of SoundEnvelope objects.
Definition: SoundEnvelope.h:60
gnash::key::c
@ c
Definition: GnashKey.h:149
gnash::sound::create_sound_handler_aos4
sound_handler * create_sound_handler_aos4(media::MediaHandler *m)
Definition: sound_handler_ahi.cpp:259
gnash::sound::sound_handler::addSoundBlock
virtual StreamBlockId addSoundBlock(SimpleBuffer data, size_t sampleCount, int seekSamples, int streamId)
Definition: sound_handler.cpp:147
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::sound::create_sound_handler_sdl
sound_handler * create_sound_handler_sdl(media::MediaHandler *m)
Definition: sound_handler_sdl.cpp:234
gnash::sound::sound_handler::playStream
void playStream(int handle, StreamBlockId blockId)
Start playback of a streaming sound, if not playing already.
Definition: sound_handler.cpp:497
gnash::sound::sound_handler::isSoundPlaying
bool isSoundPlaying(int id) const
Check if an event sound is playing.
Definition: sound_handler.cpp:485
AuxStream.h
InputStream.h
gnash::sound::sound_handler::get_sound_info
virtual media::SoundInfo * get_sound_info(int handle) const
Returns a SoundInfo object for the sound with the given id.
Definition: sound_handler.cpp:258
gnash::sound::InputStream
A sound input stream.
Definition: InputStream.h:47
gnash::sound::sound_handler::~sound_handler
virtual ~sound_handler()
Definition: sound_handler.cpp:834
gnash::sound::sound_handler::stop_all_sounds
virtual void stop_all_sounds()
Mixed functions:
Definition: sound_handler.cpp:225
gnash::sound::EmbedSound::empty
bool empty() const
Is the data buffer empty ?
Definition: EmbedSound.h:74
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::media::SoundInfo::getFormat
audioCodecType getFormat() const
Returns the format of the sound.
Definition: SoundInfo.h:84
EmbedSoundInst.h
gnash::sound::InputStream::samplesFetched
virtual unsigned int samplesFetched() const =0
Return number of samples fetched from this stream.
gnash::sound::StreamingSound
Instance of a defined sound (StreamingSoundData)
Definition: StreamingSound.h:49
gnash::sound::sound_handler::set_volume
virtual void set_volume(int sound_handle, int volume)
Sets the volume for a given event sound.
Definition: sound_handler.cpp:250
gnash::MediaException
An exception from MediaHandler subsystem.
Definition: GnashException.h:50
gnash::sound::sound_handler::delete_all_sounds
virtual void delete_all_sounds()
Stop and delete all sounds.
Definition: sound_handler.cpp:169
test.v
v
Definition: test.py:11
data
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
gnash::key::e
@ e
Definition: GnashKey.h:151
gnash::SimpleBuffer
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
gnash::sound::AuxStream
Definition: AuxStream.h:34
gnash::sound::StreamingSoundData
Definition of an embedded sound.
Definition: StreamingSoundData.h:48
ADJUST_VOLUME
#define ADJUST_VOLUME(s, v)
Definition: sound_handler.cpp:78