Gnash  0.8.10
sound_handler.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef SOUND_HANDLER_H
00020 #define SOUND_HANDLER_H
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "gnashconfig.h"
00024 #endif
00025 
00026 #include <string>
00027 #include <vector>
00028 #include <memory>
00029 #include <limits>
00030 #include <set>
00031 #include <boost/scoped_ptr.hpp>
00032 
00033 #include "dsodefs.h" // for DSOEXPORT
00034 #include "MediaHandler.h" // for inlined ctor
00035 #include "SoundEnvelope.h" // for SoundEnvelopes typedef
00036 #include "AuxStream.h" // for aux_streamer_ptr typedef
00037 #include "WAVWriter.h" // for dtor visibility 
00038 
00039 namespace gnash {
00040     namespace media {
00041         class SoundInfo;
00042     }
00043     namespace sound {
00044         class EmbedSound;
00045         class StreamingSound;
00046         class StreamingSoundData;
00047         class InputStream;
00048     }
00049     class SimpleBuffer;
00050 }
00051 
00052 namespace gnash {
00053 
00055 //
00059 namespace sound {
00060 
00062 //
00083 //
00087 class DSOEXPORT sound_handler
00088 {
00089 public:
00090 
00091     virtual ~sound_handler();
00092 
00094     //
00097     typedef unsigned long StreamBlockId;
00098 
00102 
00104     //
00106     virtual void stop_all_sounds();
00107 
00111     
00113     //
00121     virtual int create_sound(std::auto_ptr<SimpleBuffer> data,
00122             const media::SoundInfo& sinfo);
00123         
00125     //
00130     //
00132     virtual void stopEventSound(int sound_handle);
00133 
00135     //
00136     virtual void stopAllEventSounds();
00137 
00139     //
00142     //
00144     virtual void delete_sound(int sound_handle);
00145 
00147     //
00176     void startSound(int id, int loops, const SoundEnvelopes* env,
00177                    bool allowMultiple, unsigned int inPoint = 0,
00178                    unsigned int outPoint = 
00179                    std::numeric_limits<unsigned int>::max());
00180 
00182     //
00184     //
00186     bool isSoundPlaying(int id) const;
00187     
00189     //
00196     virtual void set_volume(int sound_handle, int volume);
00197     
00199     //
00202     virtual unsigned int get_duration(int sound_handle) const;
00203 
00205     //
00209     virtual unsigned int tell(int sound_handle) const;
00210 
00212     //
00219     virtual int get_volume(int sound_handle) const;
00220 
00224 
00225     virtual int createStreamingSound(const media::SoundInfo& sinfo);
00226 
00228     //
00231     virtual void stopStreamingSound(int handle);
00232 
00247     virtual StreamBlockId addSoundBlock(std::auto_ptr<SimpleBuffer> data,
00248                size_t sampleCount, int seekSamples, int streamId);
00249 
00251     //
00253     //
00260     virtual media::SoundInfo* get_sound_info(int handle) const;
00261 
00263     //
00266     //
00270     //
00273     void playStream(int handle, StreamBlockId blockId);
00274 
00276     //
00280     //
00287     int getStreamBlock(int handle) const;
00288 
00292 
00294     //
00297     int getFinalVolume() const { return _volume; }
00298 
00300     //
00303     void setFinalVolume(int v) { _volume = v; }
00304 
00308     //
00313     virtual void reset();
00314         
00316     virtual void mute();
00317 
00319     virtual void unmute();
00320 
00322     //
00324     virtual bool is_muted() const;
00325 
00327     virtual void pause() { _paused=true; }
00328 
00330     virtual void unpause() { _paused=false; }
00331 
00333     bool isPaused() const { return _paused; }
00334 
00336     //
00370     virtual InputStream* attach_aux_streamer(aux_streamer_ptr ptr, void* udata);
00371 
00373     //
00377     //
00382     virtual void unplugInputStream(InputStream* id);
00383 
00385     //
00388     size_t numSoundsStarted() const { return _soundsStarted; }
00389 
00391     //
00394     size_t numSoundsStopped() const { return _soundsStopped; }
00395 
00397     //
00416     virtual void fetchSamples(boost::int16_t* to, unsigned int nSamples);
00417 
00419     //
00443     virtual void mix(boost::int16_t* outSamples, boost::int16_t* inSamples,
00444                 unsigned int nSamples, float volume) = 0;
00445 
00447     //
00451     void setAudioDump(const std::string& wavefile);
00452 
00454     //
00456     bool streamingSound() const;
00457 
00458 protected:
00459 
00460     sound_handler(media::MediaHandler* m)
00461         :
00462         _soundsStarted(0),
00463         _soundsStopped(0),
00464         _paused(false),
00465         _muted(false),
00466         _volume(100),
00467         _mediaHandler(m)
00468     {
00469     }
00470 
00472     //
00476     virtual void plugInputStream(std::auto_ptr<InputStream> in);
00477 
00479     virtual void unplugAllInputStreams();
00480 
00482     bool hasInputStreams() const;
00483 
00485     //
00487     virtual void delete_all_sounds();
00488 
00489 private:
00490 
00492     size_t _soundsStarted;
00493 
00495     size_t _soundsStopped;
00496 
00498     bool _paused;
00499 
00501     bool _muted;
00502 
00504     int _volume;
00505 
00506     typedef std::vector<EmbedSound*> Sounds;
00507 
00509     //
00511     Sounds  _sounds;
00512 
00513     typedef std::vector<StreamingSoundData*> StreamingSounds;
00514 
00516     //
00518     StreamingSounds _streamingSounds;
00519 
00521     void stopEmbedSoundInstances(EmbedSound& def);
00522 
00524     void stopEmbedSoundInstances(StreamingSoundData& def);
00525 
00526     typedef std::set<InputStream*> InputStreams;
00527 
00529     //
00531     InputStreams _inputStreams;
00532 
00533     media::MediaHandler* _mediaHandler;
00534 
00536     void unplugCompletedInputStreams();
00537 
00538     boost::scoped_ptr<WAVWriter> _wavWriter;
00539 
00540 };
00541 
00542 // TODO: move to appropriate specific sound handlers
00543 
00544 #ifdef SOUND_SDL
00545 
00546 DSOEXPORT sound_handler* create_sound_handler_sdl(media::MediaHandler* m);
00547 #elif defined(SOUND_AHI)
00548 
00549 DSOEXPORT sound_handler* create_sound_handler_aos4(media::MediaHandler* m);
00550 #elif defined(SOUND_MKIT)
00551 
00552 DSOEXPORT sound_handler* create_sound_handler_mkit(media::MediaHandler* m);
00553 #endif
00554 
00555 } // gnash.sound namespace 
00556 } // namespace gnash
00557 
00558 #endif // SOUND_HANDLER_H
00559 
00560 
00561 // Local Variables:
00562 // mode: C++
00563 // indent-tabs-mode: t
00564 // End: