1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/* 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.
*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* Based on
* WebVenture (c) 2010, Sean Kasun
* https://github.com/mrkite/webventure, http://seancode.com/webventure/
*
* Used with explicit permission from the author
*/
#include "macventure/sound.h"
#include "audio/mixer.h"
#include "audio/audiostream.h"
#include "audio/decoders/raw.h"
namespace MacVenture {
// SoundManager
SoundManager::SoundManager(MacVentureEngine *engine, Audio::Mixer *mixer) {
_container = NULL;
Common::String filename = engine->getFilePath(kSoundPathID);
_container = new Container(filename);
_mixer = mixer;
debugC(1, kMVDebugSound, "Created sound manager with file %s", filename.c_str());
}
SoundManager::~SoundManager() {
if (_container)
delete _container;
Common::HashMap<ObjID, SoundAsset*>::iterator it;
Common::HashMap<ObjID, SoundAsset*>::iterator end = _assets.end();
for (it = _assets.begin(); it != end; it++) {
delete it->_value;
}
}
uint32 SoundManager::playSound(ObjID sound) {
ensureLoaded(sound);
_assets[sound]->play(_mixer, &_handle);
return _assets[sound]->getPlayLength();
}
void SoundManager::ensureLoaded(ObjID sound) {
if (!_assets.contains(sound))
_assets[sound] = new SoundAsset(_container, sound);
}
SoundAsset::SoundAsset(Container *container, ObjID id) :
_container(container), _id(id), _length(0), _frequency(1) {
if (_container->getItemByteSize(_id) == 0)
warning("Trying to load an empty sound asset (%d).", _id);
Common::SeekableReadStream *stream = _container->getItem(_id);
stream->seek(5, SEEK_SET);
SoundType type = (SoundType)stream->readByte();
debugC(2, kMVDebugSound, "Decoding sound of type %x", type);
switch(type) {
case kSound10:
decode10(stream);
break;
case kSound12:
decode12(stream);
break;
case kSound18:
decode18(stream);
break;
case kSound1a:
decode1a(stream);
break;
case kSound44:
decode44(stream);
break;
case kSound78:
decode78(stream);
break;
case kSound7e:
decode7e(stream);
break;
default:
warning("Unrecognized sound type: %x", type);
break;
}
delete stream;
}
SoundAsset::~SoundAsset() {
debugC(3, kMVDebugSound, "~SoundAsset(%d)", _id);
}
void SoundAsset::play(Audio::Mixer *mixer, Audio::SoundHandle *soundHandle) {
if (_data.size() == 0) {
return;
}
Audio::AudioStream *sound = Audio::makeRawStream(&_data.front(), _length, _frequency, Audio::FLAG_UNSIGNED, DisposeAfterUse::NO);
mixer->playStream(Audio::Mixer::kPlainSoundType, soundHandle, sound);
}
uint32 SoundAsset::getPlayLength() {
// Transform to milliseconds
return _length * 1000 / _frequency;
}
void SoundAsset::decode10(Common::SeekableReadStream *stream) {
warning("Decode sound 0x10 untested");
Common::Array<byte> wavtable;
stream->seek(0x198, SEEK_SET);
for (uint i = 0; i < 16; i++) {
wavtable.push_back(stream->readByte());
}
_length = stream->readUint32BE() * 2;
//Unused
stream->readUint16BE();
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
byte ch = 0;
for (uint i = 0; i < _length; i++) {
if (i & 1) {
ch >>= 4;
} else {
ch = stream->readByte();
}
_data.push_back(wavtable[ch & 0xf]);
}
}
void SoundAsset::decode12(Common::SeekableReadStream *stream) {
warning("Decode sound 0x12 untested");
stream->seek(0xc, SEEK_SET);
uint32 repeat = stream->readUint16BE();
stream->seek(0x34, SEEK_SET);
uint32 base = stream->readUint16BE() + 0x34;
stream->seek(base, SEEK_SET);
_length = stream->readUint32BE() - 6;
stream->readUint16BE();
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
stream->seek(0xe2, SEEK_SET);
// TODO: Possible source of bugs, the original just assigns the seek to the scales
uint32 scales = stream->pos() + 0xe2;
for (uint i = 0; i < repeat; i++) {
stream->seek(scales + i * 2, SEEK_SET);
uint32 scale = stream->readUint16BE();
stream->seek(base + 0xa, SEEK_SET);
for (uint j = 0; j < _length; j++) {
byte ch = stream->readByte();
if (ch & 0x80) {
ch -= 0x80;
uint32 env = ch * scale;
ch = (env >> 8) & 0xff;
if (ch & 0x80) {
ch = 0x7f;
}
ch += 0x80;
} else {
ch = (ch ^ 0xff) + 1;
ch -= 0x80;
uint32 env = ch * scale;
ch = (env >> 8) & 0xff;
if (ch & 0x80) {
ch = 0x7f;
}
ch += 0x80;
ch = (ch ^ 0xff) + 1;
}
_data.push_back(ch);
}
}
}
void SoundAsset::decode18(Common::SeekableReadStream *stream) {
warning("Decode sound 0x18 untested");
Common::Array<byte> wavtable;
stream->seek(0x252, SEEK_SET);
for (uint i = 0; i < 16; i++) {
wavtable.push_back(stream->readByte());
}
_length = stream->readUint32BE() * 2;
//Unused
stream->readUint16BE();
// TODO: It had `| 0` at the end of this line, possible source of bugs.
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
byte ch = 0;
for (uint i = 0; i < _length; i++) {
if (i & 1) {
ch >>= 4;
} else {
ch = stream->readByte();
}
_data.push_back(wavtable[ch & 0xf]);
}
}
void SoundAsset::decode1a(Common::SeekableReadStream *stream) {
warning("Decode sound 0x1a untested");
Common::Array<byte> wavtable;
stream->seek(0x220, SEEK_SET);
for (uint i = 0; i < 16; i++) {
wavtable.push_back(stream->readByte());
}
_length = stream->readUint32BE();
//Unused
stream->readUint16BE();
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
byte ch = 0;
for (uint i = 0; i < _length; i++) {
if (i & 1) {
ch >>= 4;
} else {
ch = stream->readByte();
}
_data.push_back(wavtable[ch & 0xf]);
}
}
void SoundAsset::decode44(Common::SeekableReadStream *stream) {
stream->seek(0x5e, SEEK_SET);
_length = stream->readUint32BE();
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
for (uint i = 0; i < _length; i++) {
_data.push_back(stream->readByte());
}
}
void SoundAsset::decode78(Common::SeekableReadStream *stream) {
Common::Array<byte> wavtable;
stream->seek(0xba, SEEK_SET);
for (uint i = 0; i < 16; i++) {
wavtable.push_back(stream->readByte());
}
//Unused
stream->readUint32BE();
_length = stream->readUint32BE();
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
byte ch = 0;
for (uint i = 0; i < _length; i++) {
if (i & 1) {
ch <<= 4;
} else {
ch = stream->readByte();
}
_data.push_back(wavtable[(ch >> 4) & 0xf]);
}
}
void SoundAsset::decode7e(Common::SeekableReadStream *stream) {
Common::Array<byte> wavtable;
stream->seek(0xc2, SEEK_SET);
for (uint i = 0; i < 16; i++) {
wavtable.push_back(stream->readByte());
}
//Unused
stream->readUint32BE();
_length = stream->readUint32BE();
_frequency = (stream->readUint32BE() * 22100 / 0x10000);
uint32 last = 0x80;
byte ch = 0;
for (uint i = 0; i < _length; i++) {
if (i & 1) {
ch <<= 4;
} else {
ch = stream->readByte();
}
last += wavtable[(ch >> 4) & 0xf];
_data.push_back(last & 0xff);
}
}
} //End of namespace MacVenture
|