Make automated FSCommand invocation tests show player-side output.
[gnash.git] / libsound / EmbedSoundInst.h
blobde88ada462d10eadd03ba61744450a7ed6225af6
1 // EmbedSoundInst.h - instance of an embedded sound, for gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
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.
10 //
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.
15 //
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_EMBEDSOUNDINST_H
21 #define SOUND_EMBEDSOUNDINST_H
23 #include <cassert>
24 #include <cstdint> // For C99 int types
25 #include <limits>
27 #include "EmbedSound.h"
28 #include "LiveSound.h"
29 #include "SoundEnvelope.h"
31 // Forward declarations
32 namespace gnash {
33 namespace sound {
34 class EmbedSound;
36 namespace media {
37 class MediaHandler;
41 namespace gnash {
42 namespace sound {
44 /// Instance of a defined %sound (EmbedSound)
45 class EmbedSoundInst : public LiveSound
47 public:
49 /// Create an embedded %sound instance
51 /// @param def The definition of this sound (the immutable data)
52 /// @param mh The MediaHandler to use for on-demand decoding
53 /// @param inPoint Offset in output samples this instance should start
54 /// playing from. These are post-resampling samples (44100
55 /// for one second of samples).
56 /// @param outPoint Offset in output samples this instance should stop
57 /// playing at. These are post-resampling samples (44100
58 /// for one second of samples).
59 /// Use numeric_limits<unsigned int>::max() for never
60 /// @param envelopes SoundEnvelopes to apply to this sound. May be 0 for
61 /// none.
62 /// @param loopCount Number of times this instance should loop over the
63 /// defined sound. Note that every loop begins and ends
64 /// at the range given by inPoint and outPoint.
65 EmbedSoundInst(EmbedSound& def, media::MediaHandler& mh,
66 unsigned int inPoint, unsigned int outPoint,
67 const SoundEnvelopes* envelopes, int loopCount);
69 // See dox in sound_handler.h (InputStream)
70 virtual bool eof() const;
72 /// Unregister self from the associated EmbedSound
74 /// WARNING: must be thread-safe!
75 virtual ~EmbedSoundInst();
77 private:
79 virtual size_t checkEarlierEnd(size_t bytesAhead, size_t pos) const {
80 if (_outPoint < std::numeric_limits<unsigned long>::max()) {
81 const size_t toCustomEnd = _outPoint - pos;
82 return std::min(toCustomEnd, bytesAhead);
84 return bytesAhead;
87 virtual bool moreData();
89 /// Apply envelope-volume adjustments
91 /// Modified envelopes cursor (current_env)
92 ///
93 /// @param samples
94 /// The samples to apply envelopes to
95 ///
96 /// @param nSamples
97 /// Number of samples in the samples array.
98 /// (nSamples*2 bytes).
99 ///
100 /// @param firstSampleNum
101 /// Logical position of first sample in the array.
102 /// This number gives the sample position referred to
103 /// by SoundEnvelope objects.
105 /// @param env
106 /// SoundEnvelopes to apply.
107 void applyEnvelopes(std::int16_t* samples, unsigned int nSamples,
108 unsigned int firstSampleNum, const SoundEnvelopes& env);
110 bool reachedCustomEnd() const;
112 /// Return true if there's nothing more to decode
113 virtual bool decodingCompleted() const {
114 return (decodingPosition >= _soundDef.size());
117 /// Decode next input block
119 /// It's assumed !decodingCompleted()
120 virtual void decodeNextBlock();
122 /// Current decoding position in the encoded stream
123 unsigned long decodingPosition;
125 /// Numbers of loops: -1 means loop forever, 0 means play once.
126 /// For every loop completed, it is decremented.
127 long loopCount;
129 /// Offset in bytes to end playback at
130 /// Never if numeric_limits<unsigned long>::max()
131 const unsigned long _outPoint;
133 /// Sound envelopes for the current sound, which determine the volume level
134 /// from a given position. Only used with event sounds.
135 const SoundEnvelopes* envelopes;
137 /// Index of current envelope.
138 std::uint32_t current_env;
140 /// The encoded data
142 /// It is non-const because we deregister ourselves
143 /// from its container of playing instances on destruction
145 EmbedSound& _soundDef;
150 } // gnash.sound namespace
151 } // namespace gnash
153 #endif // SOUND_EMBEDSOUNDINST_H