Made a separate function out of the code that retrieves information about

an in-memory WAV file. At the moment it's only used in one place, which is
a bit silly, but I hope to use it for the cutscene player to figure out
when to start the lead-out music.

(To do that I'll need to know how long the cutscene is, though. I haven't
looked into how to find that out yet.)

svn-id: r12424
This commit is contained in:
Torbjörn Andersson 2004-01-16 08:16:23 +00:00
parent 006ef5e01f
commit 673862dbfd
2 changed files with 56 additions and 15 deletions

View File

@ -242,6 +242,44 @@ bool MusicHandle::endOfData(void) const {
return (!_streaming || _filePos >= _fileEnd);
}
/**
* Retrieve information about an in-memory WAV file.
* @param data The WAV data
* @param wavInfo Pointer to the WavInfo structure to fill with information.
* @return True if the data appears to be a WAV file, otherwise false.
*/
bool Sound::getWavInfo(uint8 *data, WavInfo *wavInfo) {
if (READ_UINT32(data) != MKID('RIFF')) {
warning("getWavInfo: No 'RIFF' header");
return false;
}
if (READ_UINT32(data + 8) != MKID('WAVE')) {
warning("getWavInfo: No 'WAVE' header");
return false;
}
if (READ_UINT32(data + 12) != MKID('fmt ')) {
warning("getWavInfo: No 'fmt' header");
return false;
}
wavInfo->channels = READ_LE_UINT16(data + 22);
wavInfo->rate = READ_LE_UINT16(data + 24);
data += READ_LE_UINT32(data + 16) + 20;
if (READ_UINT32(data) != MKID('data')) {
warning("getWavInfo: No 'data' header");
return false;
}
wavInfo->samples = READ_LE_UINT32(data + 4);
wavInfo->data = data + 8;
return true;
}
/**
* Mutes/Unmutes the music.
* @param mute If mute is false, restore the volume to the last set master
@ -1026,32 +1064,26 @@ int32 Sound::openFx(int32 id, uint8 *data) {
}
}
if (READ_UINT32(data) != MKID('RIFF') || READ_UINT32(data + 8) != MKID('WAVE') || READ_UINT32(data + 12) != MKID('fmt ')) {
warning("openFx: Not a valid WAV file");
return RDERR_INVALIDWAV;
}
_fx[fxi]._id = id;
_fx[fxi]._flags = SoundMixer::FLAG_16BITS | SoundMixer::FLAG_LITTLE_ENDIAN;
if (READ_LE_UINT16(data + 22) == 2)
_fx[fxi]._flags |= SoundMixer::FLAG_STEREO;
WavInfo wavInfo;
_fx[fxi]._rate = READ_LE_UINT16(data + 24);
data += READ_LE_UINT32(data + 16) + 20;
if (READ_UINT32(data) != MKID('data')) {
warning("openFx: WAV file has no 'data' chunk");
if (!getWavInfo(data, &wavInfo)) {
warning("openFx: Not a valida WAV file");
return RDERR_INVALIDWAV;
}
_fx[fxi]._bufSize = READ_LE_UINT32(data + 4);
if (wavInfo.channels == 2)
_fx[fxi]._flags |= SoundMixer::FLAG_STEREO;
_fx[fxi]._rate = wavInfo.rate;
_fx[fxi]._bufSize = wavInfo.samples;
// Fill the speech buffer with data
free(_fx[fxi]._buf);
_fx[fxi]._buf = (uint16 *) malloc(_fx[fxi]._bufSize);
memcpy(_fx[fxi]._buf, data + 8, _fx[fxi]._bufSize);
memcpy(_fx[fxi]._buf, wavInfo.data, _fx[fxi]._bufSize);
return RD_OK;
}

View File

@ -33,6 +33,13 @@ namespace Sword2 {
extern void sword2_sound_handler(void *refCon);
struct WavInfo {
uint8 channels;
uint16 rate;
uint32 samples;
uint8 *data;
};
struct FxHandle {
int32 _id;
bool _paused;
@ -113,6 +120,8 @@ public:
void fxServer(int16 *data, uint len);
void buildPanTable(bool reverse);
bool getWavInfo(uint8 *data, WavInfo *wavInfo);
void muteMusic(bool mute);
bool isMusicMute(void);
void setMusicVolume(uint8 vol);