1
/* ScummVM - Graphic Adventure Engine
3
* ScummVM is the legal property of its developers, whose names
4
* are too numerous to list here. Please refer to the COPYRIGHT
5
* file distributed with this source distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
* The code in this file is based on information found at
28
* http://www.borg.com/~jglatt/tech/aiff.htm
30
* We currently only implement uncompressed AIFF. If we ever need AIFF-C, SoX
31
* (http://sox.sourceforge.net) may be a good place to start from.
34
#include "common/endian.h"
35
#include "common/stream.h"
36
#include "common/textconsole.h"
38
#include "audio/decoders/aiff.h"
39
#include "audio/decoders/raw.h"
43
uint32 readExtended(Common::SeekableReadStream &stream) {
44
// The sample rate is stored as an "80 bit IEEE Standard 754 floating
45
// point number (Standard Apple Numeric Environment [SANE] data type
54
mantissa = READ_BE_UINT32(buf + 2);
62
if (last & 0x00000001)
68
bool loadAIFFFromStream(Common::SeekableReadStream &stream, int &size, int &rate, byte &flags) {
72
if (memcmp(buf, "FORM", 4) != 0) {
73
warning("loadAIFFFromStream: No 'FORM' header");
77
stream.readUint32BE();
79
// This could be AIFC, but we don't handle that case.
82
if (memcmp(buf, "AIFF", 4) != 0) {
83
warning("loadAIFFFromStream: No 'AIFF' header");
87
// From here on, we only care about the COMM and SSND chunks, which are
88
// the only required chunks.
90
bool foundCOMM = false;
91
bool foundSSND = false;
93
uint16 numChannels = 0, bitsPerSample = 0;
94
uint32 numSampleFrames = 0, offset = 0, blockSize = 0, soundOffset = 0;
96
while (!(foundCOMM && foundSSND) && !stream.err() && !stream.eos()) {
100
length = stream.readUint32BE();
103
if (memcmp(buf, "COMM", 4) == 0) {
105
numChannels = stream.readUint16BE();
106
numSampleFrames = stream.readUint32BE();
107
bitsPerSample = stream.readUint16BE();
108
rate = readExtended(stream);
109
size = numSampleFrames * numChannels * (bitsPerSample / 8);
110
} else if (memcmp(buf, "SSND", 4) == 0) {
112
offset = stream.readUint32BE();
113
blockSize = stream.readUint32BE();
114
soundOffset = stream.pos();
117
stream.seek(pos + length);
121
warning("loadAIFFFromStream: Cound not find 'COMM' chunk");
126
warning("loadAIFFFromStream: Cound not find 'SSND' chunk");
130
// We only implement a subset of the AIFF standard.
132
if (numChannels < 1 || numChannels > 2) {
133
warning("loadAIFFFromStream: Only 1 or 2 channels are supported, not %d", numChannels);
137
if (bitsPerSample != 8 && bitsPerSample != 16) {
138
warning("loadAIFFFromStream: Only 8 or 16 bits per sample are supported, not %d", bitsPerSample);
142
if (offset != 0 || blockSize != 0) {
143
warning("loadAIFFFromStream: Block-aligned data is not supported");
147
// Samples are always signed, and big endian.
150
if (bitsPerSample == 16)
151
flags |= Audio::FLAG_16BITS;
152
if (numChannels == 2)
153
flags |= Audio::FLAG_STEREO;
155
stream.seek(soundOffset);
157
// Stream now points at the sample data
162
SeekableAudioStream *makeAIFFStream(Common::SeekableReadStream *stream,
163
DisposeAfterUse::Flag disposeAfterUse) {
167
if (!loadAIFFFromStream(*stream, size, rate, flags)) {
168
if (disposeAfterUse == DisposeAfterUse::YES)
173
data = (byte *)malloc(size);
175
stream->read(data, size);
177
if (disposeAfterUse == DisposeAfterUse::YES)
180
// Since we allocated our own buffer for the data, we must specify DisposeAfterUse::YES.
181
return makeRawStream(data, size, rate, flags);
184
} // End of namespace Audio