TITANIC: Shifting wave file playback into CWaveFile class

This commit is contained in:
Paul Gilbert 2017-08-03 22:24:00 -04:00
parent a503af4959
commit 4649bbfce2
6 changed files with 39 additions and 29 deletions

View File

@ -209,11 +209,8 @@ void QMixer::qsWaveMixPump() {
SoundEntry &sound = channel._sounds.front();
if (sound._started && !_mixer->isSoundHandleActive(sound._soundHandle)) {
if (sound._loops == -1 || sound._loops-- > 0) {
// Need to loop the sound again
sound._waveFile->audioStream()->rewind();
_mixer->playStream(sound._waveFile->_soundType,
&sound._soundHandle, sound._waveFile->audioStream(),
-1, channel.getRawVolume(), 0, DisposeAfterUse::NO);
// Need to loop (replay) the sound again
sound._soundHandle = sound._waveFile->play(channel.getRawVolume());
} else {
// Sound is finished
if (sound._callback)
@ -234,10 +231,8 @@ void QMixer::qsWaveMixPump() {
if (channel._resetDistance)
channel._distance = 0.0;
// Calculate an effective volume based on distance of source
_mixer->playStream(sound._waveFile->_soundType,
&sound._soundHandle, sound._waveFile->audioStream(),
-1, channel.getRawVolume(), 0, DisposeAfterUse::NO);
// Play the wave
sound._soundHandle = sound._waveFile->play(channel.getRawVolume());
sound._started = true;
}
}

View File

@ -212,8 +212,9 @@ class QMixer {
byte getRawVolume() const;
};
private:
Audio::Mixer *_mixer;
Common::Array<ChannelEntry> _channels;
protected:
Audio::Mixer *_mixer;
public:
QMixer(Audio::Mixer *mixer);
virtual ~QMixer() {}

View File

@ -129,7 +129,6 @@ CWaveFile *CSound::loadSound(const CString &name) {
// Found it, so move it to the front of the list and return
_sounds.remove(soundItem);
_sounds.push_front(soundItem);
soundItem->_waveFile->reset();
return soundItem->_waveFile;
}
}

View File

@ -120,7 +120,7 @@ QSoundManager::~QSoundManager() {
}
CWaveFile *QSoundManager::loadSound(const CString &name) {
CWaveFile *waveFile = new CWaveFile();
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified sound
if (!waveFile->loadSound(name)) {
@ -132,7 +132,7 @@ CWaveFile *QSoundManager::loadSound(const CString &name) {
}
CWaveFile *QSoundManager::loadSpeech(CDialogueFile *dialogueFile, int speechId) {
CWaveFile *waveFile = new CWaveFile();
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified sound
if (!waveFile->loadSpeech(dialogueFile, speechId)) {
@ -144,7 +144,7 @@ CWaveFile *QSoundManager::loadSpeech(CDialogueFile *dialogueFile, int speechId)
}
CWaveFile *QSoundManager::loadMusic(const CString &name) {
CWaveFile *waveFile = new CWaveFile();
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified sound
if (!waveFile->loadMusic(name)) {
@ -156,7 +156,7 @@ CWaveFile *QSoundManager::loadMusic(const CString &name) {
}
CWaveFile *QSoundManager::loadMusic(CAudioBuffer *buffer, DisposeAfterUse::Flag disposeAfterUse) {
CWaveFile *waveFile = new CWaveFile();
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified audio buffer
if (!waveFile->loadMusic(buffer, disposeAfterUse)) {

View File

@ -64,7 +64,7 @@ bool AudioBufferStream::endOfData() const {
/*------------------------------------------------------------------------*/
CWaveFile::CWaveFile() : _audioStream(nullptr),
CWaveFile::CWaveFile(Audio::Mixer *mixer) : _mixer(mixer), _audioStream(nullptr),
_waveData(nullptr), _waveSize(0), _dataSize(0), _headerSize(0),
_rate(0), _flags(0), _wavType(0), _soundType(Audio::Mixer::kPlainSoundType) {
setup();
@ -181,9 +181,8 @@ Audio::SeekableAudioStream *CWaveFile::audioStream() {
// No stream yet, so create one and give it control of the raw wave data
assert(_waveData);
_audioStream = Audio::makeWAVStream(
new Common::MemoryReadStream(_waveData, _waveSize, DisposeAfterUse::YES),
new Common::MemoryReadStream(_waveData, _waveSize, DisposeAfterUse::NO),
DisposeAfterUse::YES);
_waveData = nullptr;
}
return _audioStream;
@ -194,10 +193,6 @@ uint CWaveFile::getFrequency() {
return audioStream()->getRate();
}
void CWaveFile::reset() {
audioStream()->rewind();
}
const int16 *CWaveFile::lock() {
enum { kWaveFormatPCM = 1 };
@ -220,4 +215,17 @@ void CWaveFile::unlock(const int16 *ptr) {
// No implementation needed in ScummVM
}
} // End of namespace Titanic z
Audio::SoundHandle CWaveFile::play(byte volume) {
// If there's a previous instance of the sound being played,
// stop in and free the old audio stream
if (_mixer->isSoundHandleActive(_soundHandle))
_mixer->stopHandle(_soundHandle);
Audio::SeekableAudioStream *stream = audioStream();
stream->rewind();
_mixer->playStream(_soundType, &_soundHandle, stream, -1,
volume, 0, DisposeAfterUse::NO);
return _soundHandle;
}
} // End of namespace Titanic

View File

@ -37,6 +37,7 @@ class QSoundManager;
class CWaveFile {
private:
Audio::Mixer *_mixer;
byte *_waveData;
int _waveSize;
int _dataSize;
@ -45,6 +46,7 @@ private:
byte _flags;
uint16 _wavType;
Audio::SeekableAudioStream *_audioStream;
Audio::SoundHandle _soundHandle;
private:
/**
* Handles setup of fields shared by the constructors
@ -55,6 +57,11 @@ private:
* Gets passed the raw data for the wave file
*/
void load(byte *data, uint dataSize);
/**
* Returns a ScummVM Audio Stream for playback purposes
*/
Audio::SeekableAudioStream *audioStream();
public:
Audio::Mixer::SoundType _soundType;
@ -63,7 +70,7 @@ public:
DisposeAfterUse::Flag _disposeAudioBuffer;
int _channel;
public:
CWaveFile();
CWaveFile(Audio::Mixer *mixer);
CWaveFile(QSoundManager *owner);
~CWaveFile();
@ -79,11 +86,6 @@ public:
*/
uint size() const { return _dataSize; }
/**
* Returns a ScummVM Audio Stream for playback purposes
*/
Audio::SeekableAudioStream *audioStream();
/**
* Tries to load the specified wave file sound
*/
@ -130,6 +132,11 @@ public:
* Unlock sound data after a prior call to lock
*/
void unlock(const int16 *ptr);
/**
* Plays the wave file
*/
Audio::SoundHandle play(byte volume);
};
} // End of namespace Titanic