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:
A sound input stream.
Definition: InputStream.h:47
size_t numSoundsStarted() const
Special test-fuction. Reports how many times a sound has been started.
Definition: sound_handler.h:388
Definition: klash_part.cpp:331
Class containing information about an embedded sound definition.
Definition: SoundInfo.h:34
Definition of an embedded sound.
Definition: EmbedSound.h:49
int getFinalVolume() const
Sound output functions.
Definition: sound_handler.h:297
sound_handler * create_sound_handler_mkit(media::MediaHandler *m)
Definition: sound_handler_mkit.cpp:269
unsigned int(* aux_streamer_ptr)(void *udata, std::int16_t *samples, unsigned int nSamples, bool &eof)
Definition: AuxStream.h:31
v
Definition: test.py:11
sound_handler * create_sound_handler_sdl(media::MediaHandler *m)
Definition: sound_handler_sdl.cpp:234
Sound mixer.
Definition: sound_handler.h:87
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
size_t numSoundsStopped() const
Special test-fuction. Reports how many times a sound has been stopped.
Definition: sound_handler.h:394
Definition of an embedded sound.
Definition: StreamingSoundData.h:47
The MediaHandler class acts as a factory to provide parser and decoders.
Definition: MediaHandler.h:69
sound_handler * create_sound_handler_aos4(media::MediaHandler *m)
Definition: sound_handler_ahi.cpp:259
sound_handler(media::MediaHandler *m)
Definition: sound_handler.h:460
#define DSOEXPORT
Definition: dsodefs.h:55
void setFinalVolume(int v)
Set the volume to apply to mixed output.
Definition: sound_handler.h:303
std::vector< SoundEnvelope > SoundEnvelopes
A vector of SoundEnvelope objects.
Definition: SoundEnvelope.h:60
unsigned long StreamBlockId
Identifier of a streaming sound block.
Definition: sound_handler.h:97
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
Definition: GnashKey.h:159
virtual void unpause()
gnash calls this to unpause audio
Definition: sound_handler.h:330
bool isPaused() const
return true if audio is paused
Definition: sound_handler.h:333
virtual void pause()
gnash calls this to pause audio
Definition: sound_handler.h:327