2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-01-30 05:25:17 +00:00
|
|
|
#include "common/debug.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2005-01-09 15:49:43 +00:00
|
|
|
#include "common/stream.h"
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/decoders/wave.h"
|
|
|
|
#include "audio/decoders/adpcm.h"
|
|
|
|
#include "audio/decoders/raw.h"
|
2005-01-09 15:49:43 +00:00
|
|
|
|
2006-04-29 22:33:31 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
2005-10-21 02:00:12 +00:00
|
|
|
bool loadWAVFromStream(Common::SeekableReadStream &stream, int &size, int &rate, byte &flags, uint16 *wavType, int *blockAlign_) {
|
2008-09-13 16:51:46 +00:00
|
|
|
const int32 initialPos = stream.pos();
|
2005-01-09 15:49:43 +00:00
|
|
|
byte buf[4+1];
|
|
|
|
|
|
|
|
buf[4] = 0;
|
|
|
|
|
|
|
|
stream.read(buf, 4);
|
|
|
|
if (memcmp(buf, "RIFF", 4) != 0) {
|
|
|
|
warning("getWavInfo: No 'RIFF' header");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
int32 wavLength = stream.readUint32LE();
|
2005-01-09 15:49:43 +00:00
|
|
|
|
|
|
|
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
|
2010-11-01 20:41:03 +00:00
|
|
|
debug("WAVE information:");
|
|
|
|
debug(" total size: %d", wavLength);
|
|
|
|
debug(" fmt size: %d", fmtLength);
|
|
|
|
debug(" type: %d", type);
|
|
|
|
debug(" numChannels: %d", numChannels);
|
|
|
|
debug(" samplesPerSec: %d", samplesPerSec);
|
|
|
|
debug(" avgBytesPerSec: %d", avgBytesPerSec);
|
|
|
|
debug(" blockAlign: %d", blockAlign);
|
|
|
|
debug(" bitsPerSample: %d", bitsPerSample);
|
2005-01-09 15:49:43 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-22 03:00:21 +00:00
|
|
|
if (type != 1 && type != 2 && type != 17) {
|
|
|
|
warning("getWavInfo: only PCM, MS ADPCM or IMA ADPCM data is supported (type %d)", type);
|
2005-01-09 15:49:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-04-22 03:00:21 +00:00
|
|
|
if (blockAlign != numChannels * bitsPerSample / 8 && type != 2) {
|
2005-01-09 15:49:43 +00:00
|
|
|
debug(0, "getWavInfo: blockAlign is invalid");
|
|
|
|
}
|
|
|
|
|
2006-04-22 03:00:21 +00:00
|
|
|
if (avgBytesPerSec != samplesPerSec * blockAlign && type != 2) {
|
2005-01-09 15:49:43 +00:00
|
|
|
debug(0, "getWavInfo: avgBytesPerSec is invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the return values.
|
|
|
|
rate = samplesPerSec;
|
|
|
|
|
2006-03-19 05:15:49 +00:00
|
|
|
flags = 0;
|
2005-01-09 15:49:43 +00:00
|
|
|
if (bitsPerSample == 8) // 8 bit data is unsigned
|
2010-01-19 22:30:33 +00:00
|
|
|
flags |= Audio::FLAG_UNSIGNED;
|
2005-01-11 08:30:28 +00:00
|
|
|
else if (bitsPerSample == 16) // 16 bit data is signed little endian
|
2010-01-19 22:30:33 +00:00
|
|
|
flags |= (Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN);
|
2009-10-14 22:37:05 +00:00
|
|
|
else if (bitsPerSample == 4 && (type == 2 || type == 17))
|
2010-01-19 22:30:33 +00:00
|
|
|
flags |= Audio::FLAG_16BITS;
|
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)
|
2010-01-19 22:30:33 +00:00
|
|
|
flags |= Audio::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
|
2010-11-01 20:41:03 +00:00
|
|
|
debug(" found a '%s' tag of size %d", buf, offset);
|
2005-01-09 15:49:43 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2010-01-16 21:36:08 +00:00
|
|
|
RewindableAudioStream *makeWAVStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) {
|
2005-01-09 15:49:43 +00:00
|
|
|
int size, rate;
|
2009-10-14 22:37:05 +00:00
|
|
|
byte flags;
|
2005-05-03 22:12:23 +00:00
|
|
|
uint16 type;
|
2006-04-22 03:00:21 +00:00
|
|
|
int blockAlign;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2009-01-27 00:42:43 +00:00
|
|
|
if (!loadWAVFromStream(*stream, size, rate, flags, &type, &blockAlign)) {
|
2010-01-16 21:36:08 +00:00
|
|
|
if (disposeAfterUse == DisposeAfterUse::YES)
|
2009-01-27 00:42:43 +00:00
|
|
|
delete stream;
|
2005-01-09 15:49:43 +00:00
|
|
|
return 0;
|
2009-01-27 00:42:43 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2010-01-07 16:18:03 +00:00
|
|
|
if (type == 17) // MS IMA ADPCM
|
2010-01-19 22:30:33 +00:00
|
|
|
return makeADPCMStream(stream, disposeAfterUse, size, Audio::kADPCMMSIma, rate, (flags & Audio::FLAG_STEREO) ? 2 : 1, blockAlign);
|
2010-01-07 16:18:03 +00:00
|
|
|
else if (type == 2) // MS ADPCM
|
2010-01-19 22:30:33 +00:00
|
|
|
return makeADPCMStream(stream, disposeAfterUse, size, Audio::kADPCMMS, rate, (flags & Audio::FLAG_STEREO) ? 2 : 1, blockAlign);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2012-02-08 08:39:15 +00:00
|
|
|
// Raw PCM, make sure the last packet is complete
|
|
|
|
uint sampleSize = (flags & Audio::FLAG_16BITS ? 2 : 1) * (flags & Audio::FLAG_STEREO ? 2 : 1);
|
|
|
|
if (size % sampleSize != 0) {
|
|
|
|
warning("makeWAVStream: Trying to play a WAVE file with an incomplete PCM packet");
|
|
|
|
size &= ~(sampleSize - 1);
|
|
|
|
}
|
|
|
|
|
2009-10-14 22:37:05 +00:00
|
|
|
// Raw PCM. Just read everything at once.
|
|
|
|
// TODO: More elegant would be to wrap the stream.
|
|
|
|
byte *data = (byte *)malloc(size);
|
|
|
|
assert(data);
|
|
|
|
stream->read(data, size);
|
2005-01-09 15:49:43 +00:00
|
|
|
|
2010-01-16 21:36:08 +00:00
|
|
|
if (disposeAfterUse == DisposeAfterUse::YES)
|
2009-01-27 03:13:13 +00:00
|
|
|
delete stream;
|
2009-01-27 00:42:43 +00:00
|
|
|
|
2010-01-30 15:28:07 +00:00
|
|
|
return makeRawStream(data, size, rate, flags);
|
2005-01-09 15:49:43 +00:00
|
|
|
}
|
2006-04-29 22:33:31 +00:00
|
|
|
|
|
|
|
} // End of namespace Audio
|