Update with current status
[gnash.git] / libsound / LiveSound.cpp
blob843d719cceaf01303bceb83dc5e470cce43b2826
1 // LiveSound.cpp - 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.
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
21 #include "LiveSound.h"
23 #include <algorithm>
24 #include <cassert>
26 #include "log.h"
27 #include "SoundInfo.h"
28 #include "MediaHandler.h"
30 namespace gnash {
31 namespace sound {
33 LiveSound::LiveSound(media::MediaHandler& mh, const media::SoundInfo& info,
34 size_t inPoint)
36 _inPoint(inPoint * 4),
37 _playbackPosition(_inPoint),
38 _samplesFetched(0)
40 createDecoder(mh, info);
43 void
44 LiveSound::createDecoder(media::MediaHandler& mh, const media::SoundInfo& si)
47 media::AudioInfo info(si.getFormat(), si.getSampleRate(),
48 si.is16bit() ? 2 : 1, si.isStereo(), 0, media::CODEC_TYPE_FLASH);
50 _decoder.reset(mh.createAudioDecoder(info).release());
53 unsigned int
54 LiveSound::fetchSamples(std::int16_t* to, unsigned int nSamples)
56 unsigned int fetchedSamples = 0;
58 while (nSamples) {
59 unsigned int availableSamples = decodedSamplesAhead();
61 if (availableSamples) {
62 const std::int16_t* data = getDecodedData(_playbackPosition);
64 if (availableSamples >= nSamples) {
65 std::copy(data, data + nSamples, to);
66 fetchedSamples += nSamples;
68 // Update playback position (samples are 16bit)
69 _playbackPosition += nSamples * 2;
71 break; // fetched all
73 else {
74 // not enough decoded samples available:
75 // copy what we have and go on
76 std::copy(data, data + availableSamples, to);
77 fetchedSamples += availableSamples;
79 // Update playback position (samples are 16bit)
80 _playbackPosition += availableSamples * 2;
82 to += availableSamples;
83 nSamples -= availableSamples;
84 assert(nSamples);
88 // Get more data if it's ready. This could involve looping.
89 // Even if no data is available now, it can become available
90 // later.
91 if (!moreData()) break;
93 // We have looped.
94 if (!_samplesFetched) continue;
97 // update samples played
98 _samplesFetched += fetchedSamples;
100 return fetchedSamples;
104 } // sound namespace
105 } // namespace gnash