1 // EmbedSound.h - embedded sound definition, for gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // 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
20 #ifndef SOUND_EMBEDSOUND_H
21 #define SOUND_EMBEDSOUND_H
24 #include <memory> // for unique_ptr (composition)
30 #include "SimpleBuffer.h" // for composition
31 #include "SoundInfo.h" // for composition
32 #include "SoundEnvelope.h" // for SoundEnvelopes define
34 // Forward declarations
48 /// Definition of an embedded sound
53 /// Vector containing the active instances of this sounds being played
55 /// NOTE: This class does NOT own the active sounds
56 typedef std::list
<EmbedSoundInst
*> Instances
;
58 /// Construct a sound with given data, info and volume.
60 /// @param data The encoded sound data.
61 /// @param info encoding info
62 /// @param volume initial volume (0..100). Optional, defaults to 100.
63 EmbedSound(std::unique_ptr
<SimpleBuffer
> data
, media::SoundInfo info
,
68 /// Return size of the data buffer
73 /// Is the data buffer empty ?
78 /// Return a pointer to the underlying buffer
79 const std::uint8_t* data() const {
83 /// Return a pointer to an offset in the underlying buffer
85 /// @param pos The offset value.
86 /// An assertion will fail if pos > size()
88 const std::uint8_t* data(size_t pos
) const {
89 assert(pos
< _buf
->size());
90 return _buf
->data()+pos
;
93 /// Are there known playing instances of this sound ?
95 /// Locks _soundInstancesMutex
97 bool isPlaying() const;
99 /// Return number of playing instances of this sound
101 /// Locks _soundInstancesMutex
103 size_t numPlayingInstances() const;
105 /// Append to the given vector all playing instances of this sound def
106 void getPlayingInstances(std::vector
<InputStream
*>& to
) const;
108 /// Return the first created instance of this sound
110 /// Locks _soundInstancesMutex
112 EmbedSoundInst
* firstPlayingInstance() const;
114 /// Create an instance of this sound
116 /// The returned instance ownership is transferred
119 /// The MediaHandler to use for on-demand decoding
122 /// Offset in output samples this instance should start
123 /// playing from. These are post-resampling samples from
124 /// the start of the specified blockId.
128 /// Offset in output samples this instance should stop
129 /// playing at. These are post-resampling samples from
130 /// the start of the specified blockId.
133 /// SoundEnvelopes to apply to this sound. May be 0 for none.
136 /// Number of times this instance should loop over the defined sound.
137 /// @todo document if every loop starts at secsOffset !
138 /// Locks the _soundInstancesMutex when pushing to it
140 std::unique_ptr
<EmbedSoundInst
> createInstance(media::MediaHandler
& mh
,
141 unsigned int inPoint
, unsigned int outPoint
,
142 const SoundEnvelopes
* envelopes
, int loopCount
);
144 /// Drop all active sounds
146 /// Locks _soundInstancesMutex
147 void clearInstances();
149 /// Drop an active sound (by iterator)
151 /// Does *NOT* lock the _soundInstancesMutex
153 /// @return iterator after the one being erased
154 Instances::iterator
eraseActiveSound(Instances::iterator i
);
156 /// Drop an active sound (by pointer)
158 /// @param inst The active sound instance to unregister
160 /// This is intended to be called by EmbedSoundInst
161 /// destructor, which may be called by a separate thread
162 /// so MUST be thread-safe
164 /// Does lock the _soundInstancesMutex
166 /// @todo make private and mark EmbedSoundInst as friend ?
168 void eraseActiveSound(EmbedSoundInst
* inst
);
170 /// Object holding information about the sound
171 media::SoundInfo soundinfo
;
173 /// Volume for AS-sounds, range: 0-100.
174 /// It's the SWF range that is represented here.
179 /// The undecoded data
180 std::unique_ptr
<SimpleBuffer
> _buf
;
182 /// Playing instances of this sound definition
184 /// Multithread access to this member is protected
185 /// by the _soundInstancesMutex mutex
186 Instances _soundInstances
;
188 /// Mutex protecting access to _soundInstances
190 mutable std::mutex _soundInstancesMutex
;
193 } // gnash.sound namespace
196 #endif // SOUND_EMBEDSOUND_H