1 // LiveSound.cpp - instance of an embedded sound, 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
21 #include "LiveSound.h"
27 #include "SoundInfo.h"
28 #include "MediaHandler.h"
33 LiveSound::LiveSound(media::MediaHandler
& mh
, const media::SoundInfo
& info
,
36 _inPoint(inPoint
* 4),
37 _playbackPosition(_inPoint
),
40 createDecoder(mh
, info
);
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());
54 LiveSound::fetchSamples(std::int16_t* to
, unsigned int nSamples
)
56 unsigned int fetchedSamples
= 0;
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;
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
;
88 // Get more data if it's ready. This could involve looping.
89 // Even if no data is available now, it can become available
91 if (!moreData()) break;
94 if (!_samplesFetched
) continue;
97 // update samples played
98 _samplesFetched
+= fetchedSamples
;
100 return fetchedSamples
;