2005-01-09 15:49:43 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2005-2006 The ScummVM project
|
2005-01-09 15:49:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-01-09 15:49:43 +00:00
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2005-01-09 15:49:43 +00:00
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/stream.h"
|
|
|
|
|
|
|
|
#include "sound/audiostream.h"
|
|
|
|
#include "sound/mixer.h"
|
|
|
|
#include "sound/wave.h"
|
2005-05-03 22:12:23 +00:00
|
|
|
#include "sound/adpcm.h"
|
2005-01-09 15:49:43 +00:00
|
|
|
|
2005-10-21 02:00:12 +00:00
|
|
|
bool loadWAVFromStream(Common::SeekableReadStream &stream, int &size, int &rate, byte &flags, uint16 *wavType, int *blockAlign_) {
|
2005-01-09 15:49:43 +00:00
|
|
|
const uint32 initialPos = stream.pos();
|
|
|
|
byte buf[4+1];
|
|
|
|
|
|
|
|
buf[4] = 0;
|
|
|
|
|
|
|
|
stream.read(buf, 4);
|
|
|
|
if (memcmp(buf, "RIFF", 4) != 0) {
|
|
|
|
warning("getWavInfo: No 'RIFF' header");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 wavLength = stream.readUint32LE();
|
|
|
|
|
|
|
|
stream.read(buf, 4);
|
|
|
|
if (memcmp(buf, "WAVE", 4) != 0) {
|
|
|
|
warning("getWavInfo: No 'WAVE' header");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.read(buf, 4);
|
|
|
|
if (memcmp(buf, "fmt ", 4) != 0) {
|
|
|
|
warning("getWavInfo: No 'fmt' header");
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-01-09 15:49:43 +00:00
|
|
|
uint32 fmtLength = stream.readUint32LE();
|
|
|
|
if (fmtLength < 16) {
|
|
|
|
// A valid fmt chunk always contains at least 16 bytes
|
|
|
|
warning("getWavInfo: 'fmt' header is too short");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next comes the "type" field of the fmt header. Some typical
|
|
|
|
// values for it:
|
2005-05-03 22:12:23 +00:00
|
|
|
// 1 -> uncompressed PCM
|
|
|
|
// 17 -> IMA ADPCM compressed WAVE
|
2005-05-04 01:57:03 +00:00
|
|
|
// See <http://www.saettler.com/RIFFNEW/RIFFNEW.htm> for a more complete
|
2005-01-09 15:49:43 +00:00
|
|
|
// list of common WAVE compression formats...
|
|
|
|
uint16 type = stream.readUint16LE(); // == 1 for PCM data
|
|
|
|
uint16 numChannels = stream.readUint16LE(); // 1 for mono, 2 for stereo
|
|
|
|
uint32 samplesPerSec = stream.readUint32LE(); // in Hz
|
|
|
|
uint32 avgBytesPerSec = stream.readUint32LE(); // == SampleRate * NumChannels * BitsPerSample/8
|
|
|
|
|
|
|
|
uint16 blockAlign = stream.readUint16LE(); // == NumChannels * BitsPerSample/8
|
|
|
|
uint16 bitsPerSample = stream.readUint16LE(); // 8, 16 ...
|
|
|
|
// 8 bit data is unsigned, 16 bit data signed
|
|
|
|
|
2005-05-03 22:12:23 +00:00
|
|
|
|
|
|
|
if (wavType != 0)
|
|
|
|
*wavType = type;
|
2005-10-21 02:00:12 +00:00
|
|
|
|
|
|
|
if (blockAlign_ != 0)
|
|
|
|
*blockAlign_ = blockAlign;
|
2005-07-30 21:11:48 +00:00
|
|
|
#if 0
|
2005-01-09 15:49:43 +00:00
|
|
|
printf("WAVE information:\n");
|
|
|
|
printf(" total size: %d\n", wavLength);
|
|
|
|
printf(" fmt size: %d\n", fmtLength);
|
|
|
|
printf(" type: %d\n", type);
|
|
|
|
printf(" numChannels: %d\n", numChannels);
|
|
|
|
printf(" samplesPerSec: %d\n", samplesPerSec);
|
|
|
|
printf(" avgBytesPerSec: %d\n", avgBytesPerSec);
|
|
|
|
printf(" blockAlign: %d\n", blockAlign);
|
|
|
|
printf(" bitsPerSample: %d\n", bitsPerSample);
|
|
|
|
#endif
|
|
|
|
|
2005-05-03 22:12:23 +00:00
|
|
|
if (type != 1 && type != 17) {
|
|
|
|
warning("getWavInfo: only PCM or IMA ADPCM data is supported (type %d)", type);
|
2005-01-09 15:49:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockAlign != numChannels * bitsPerSample / 8) {
|
|
|
|
debug(0, "getWavInfo: blockAlign is invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avgBytesPerSec != samplesPerSec * blockAlign) {
|
|
|
|
debug(0, "getWavInfo: avgBytesPerSec is invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the return values.
|
|
|
|
rate = samplesPerSec;
|
|
|
|
|
2006-01-27 00:10:18 +00:00
|
|
|
flags = Audio::Mixer::FLAG_AUTOFREE;
|
2005-01-09 15:49:43 +00:00
|
|
|
if (bitsPerSample == 8) // 8 bit data is unsigned
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_UNSIGNED;
|
2005-01-11 08:30:28 +00:00
|
|
|
else if (bitsPerSample == 16) // 16 bit data is signed little endian
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= (Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_LITTLE_ENDIAN);
|
2005-05-04 01:57:03 +00:00
|
|
|
else if (bitsPerSample == 4 && type == 17) // IMA ADPCM compressed. We decompress it
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= (Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_LITTLE_ENDIAN);
|
2005-01-09 15:49:43 +00:00
|
|
|
else {
|
|
|
|
warning("getWavInfo: unsupported bitsPerSample %d", bitsPerSample);
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-01-09 15:49:43 +00:00
|
|
|
if (numChannels == 2)
|
2005-05-10 23:48:48 +00:00
|
|
|
flags |= Audio::Mixer::FLAG_STEREO;
|
2005-01-09 15:49:43 +00:00
|
|
|
else if (numChannels != 1) {
|
|
|
|
warning("getWavInfo: unsupported number of channels %d", numChannels);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's almost certainly a WAV file, but we still need to find its
|
|
|
|
// 'data' chunk.
|
|
|
|
|
|
|
|
// Skip over the rest of the fmt chunk.
|
|
|
|
int offset = fmtLength - 16;
|
|
|
|
|
|
|
|
do {
|
|
|
|
stream.seek(offset, SEEK_CUR);
|
|
|
|
if (stream.pos() >= initialPos + wavLength + 8) {
|
|
|
|
warning("getWavInfo: Can't find 'data' chunk");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stream.read(buf, 4);
|
|
|
|
offset = stream.readUint32LE();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf(" found a '%s' tag of size %d\n", buf, offset);
|
|
|
|
#endif
|
|
|
|
} while (memcmp(buf, "data", 4) != 0);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-01-09 15:49:43 +00:00
|
|
|
// Stream now points at 'offset' bytes of sample data...
|
|
|
|
size = offset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStream *makeWAVStream(Common::SeekableReadStream &stream) {
|
|
|
|
int size, rate;
|
|
|
|
byte flags;
|
2005-05-03 22:12:23 +00:00
|
|
|
uint16 type;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-03 22:12:23 +00:00
|
|
|
if (!loadWAVFromStream(stream, size, rate, flags, &type))
|
2005-01-09 15:49:43 +00:00
|
|
|
return 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-03 22:12:23 +00:00
|
|
|
if (type == 17) // IMA ADPCM
|
2005-10-19 04:59:18 +00:00
|
|
|
return new ADPCMInputStream(&stream, size, kADPCMIma, (flags & Audio::Mixer::FLAG_STEREO) ? 2 : 1);
|
2005-05-03 22:12:23 +00:00
|
|
|
|
2005-01-09 15:49:43 +00:00
|
|
|
byte *data = (byte *)malloc(size);
|
|
|
|
assert(data);
|
|
|
|
stream.read(data, size);
|
|
|
|
|
|
|
|
return makeLinearInputStream(rate, flags, data, size, 0, 0);
|
|
|
|
}
|