diff options
Diffstat (limited to 'sword2')
-rw-r--r-- | sword2/driver/d_sound.cpp | 62 | ||||
-rw-r--r-- | sword2/driver/d_sound.h | 9 |
2 files changed, 56 insertions, 15 deletions
diff --git a/sword2/driver/d_sound.cpp b/sword2/driver/d_sound.cpp index 2f4253b85b..41d7630b07 100644 --- a/sword2/driver/d_sound.cpp +++ b/sword2/driver/d_sound.cpp @@ -243,6 +243,44 @@ bool MusicHandle::endOfData(void) const { } /** + * 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 * level. Otherwise the music is muted (volume 0). @@ -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; - - _fx[fxi]._rate = READ_LE_UINT16(data + 24); - - data += READ_LE_UINT32(data + 16) + 20; + WavInfo wavInfo; - 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; } diff --git a/sword2/driver/d_sound.h b/sword2/driver/d_sound.h index 1dd61407a7..8612bbefb0 100644 --- a/sword2/driver/d_sound.h +++ b/sword2/driver/d_sound.h @@ -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); |