Gnash 0.8.10dev
|
00001 // EmbedSoundInst.h - instance of an embedded sound, for gnash 00002 // 00003 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 00004 // 2011 Free Software Foundation, Inc 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 00020 #ifndef SOUND_EMBEDSOUNDINST_H 00021 #define SOUND_EMBEDSOUNDINST_H 00022 00023 #include "InputStream.h" // for inheritance 00024 #include "AudioDecoder.h" // for dtor visibility 00025 #include "SoundEnvelope.h" // for SoundEnvelopes typedef 00026 #include "SimpleBuffer.h" // for composition (decoded data) 00027 #include "EmbedSound.h" // for inlines 00028 #include "sound_handler.h" // for StreamBlockId typedef 00029 00030 #include <memory> 00031 #include <cassert> 00032 #include <boost/cstdint.hpp> // For C99 int types 00033 00034 00035 // Forward declarations 00036 namespace gnash { 00037 namespace sound { 00038 class EmbedSound; 00039 } 00040 namespace media { 00041 class MediaHandler; 00042 } 00043 } 00044 00045 namespace gnash { 00046 namespace sound { 00047 00049 // 00056 class EmbedSoundInst : public InputStream 00057 { 00058 public: 00059 00061 // 00091 EmbedSoundInst(EmbedSound& def, media::MediaHandler& mh, 00092 sound_handler::StreamBlockId blockId, 00093 unsigned int inPoint, 00094 unsigned int outPoint, 00095 const SoundEnvelopes* envelopes, 00096 unsigned int loopCount); 00097 00098 // See dox in sound_handler.h (InputStream) 00099 unsigned int fetchSamples(boost::int16_t* to, unsigned int nSamples); 00100 00101 // See dox in sound_handler.h (InputStream) 00102 unsigned int samplesFetched() const; 00103 00104 // See dox in sound_handler.h (InputStream) 00105 bool eof() const; 00106 00108 // 00111 ~EmbedSoundInst(); 00112 00113 private: 00114 00116 unsigned long decodingPosition; 00117 00119 unsigned long playbackPosition; 00120 00123 long loopCount; 00124 00127 unsigned long _inPoint; 00128 00131 unsigned long _outPoint; 00132 00135 const SoundEnvelopes* envelopes; 00136 00138 boost::uint32_t current_env; 00139 00141 unsigned long _samplesFetched; 00142 00144 const EmbedSound& getSoundData() { 00145 return _soundDef; 00146 } 00147 00149 // 00156 void appendDecodedData(boost::uint8_t* data, unsigned int size); 00157 00159 // 00166 void setDecodedData(boost::uint8_t* data, unsigned int size) 00167 { 00168 if ( ! _decodedData.get() ) 00169 { 00170 _decodedData.reset( new SimpleBuffer() ); 00171 } 00172 00173 _decodedData->resize(0); // shouldn't release memory 00174 _decodedData->append(data, size); 00175 delete [] data; // ownership transferred... 00176 } 00177 00178 size_t encodedDataSize() const 00179 { 00180 return _soundDef.size(); 00181 } 00182 00184 // 00204 void applyEnvelopes(boost::int16_t* samples, unsigned int nSamples, 00205 unsigned int firstSampleNum, 00206 const SoundEnvelopes& env); 00207 00209 // 00219 static void adjustVolume(boost::int16_t* samples, 00220 unsigned int nSamples, float volume); 00221 00224 // 00229 const boost::uint8_t* getEncodedData(unsigned long int pos); 00230 00233 unsigned int decodedSamplesAhead() const 00234 { 00235 unsigned int dds = decodedDataSize(); 00236 if ( dds <= playbackPosition ) return 0; 00237 unsigned int bytesAhead = dds - playbackPosition; 00238 assert(!(bytesAhead%2)); 00239 00240 if ( _outPoint < std::numeric_limits<unsigned long>::max() ) 00241 { 00242 unsigned int toCustomEnd = _outPoint-playbackPosition; 00243 if ( toCustomEnd < bytesAhead ) bytesAhead = toCustomEnd; 00244 } 00245 00246 unsigned int samplesAhead = bytesAhead/2; 00247 00248 return samplesAhead; 00249 } 00250 00251 bool reachedCustomEnd() const; 00252 00254 bool decodingCompleted() const 00255 { 00256 // example: 10 bytes of encoded data, decodingPosition 8 : more to decode 00257 // example: 10 bytes of encoded data, decodingPosition 10 : nothing more to decode 00258 00259 return ( decodingPosition >= encodedDataSize() ); 00260 } 00261 00262 00264 std::auto_ptr<media::AudioDecoder> _decoder; 00265 00267 // 00271 void createDecoder(media::MediaHandler& mediaHandler); 00272 00274 size_t decodedDataSize() const 00275 { 00276 if ( _decodedData.get() ) 00277 { 00278 return _decodedData->size(); 00279 } 00280 else return 0; 00281 } 00282 00286 // 00293 boost::int16_t* getDecodedData(unsigned long int pos); 00294 00296 // 00300 EmbedSound& _soundDef; 00301 00303 // 00307 std::auto_ptr<SimpleBuffer> _decodedData; 00308 00310 // 00313 void decodeNextBlock(); 00314 }; 00315 00316 00317 } // gnash.sound namespace 00318 } // namespace gnash 00319 00320 #endif // SOUND_EMBEDSOUNDINST_H