| 1 | /* ScummVM - Graphic Adventure Engine |
| 2 | * |
| 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. |
| 6 | * |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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. |
| 20 | * |
| 21 | * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/trunk/engines/scumm/player_v1.cpp $ |
| 22 | * $Id: player_v1.cpp 46143 2009-11-26 00:31:19Z Kirben $ |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include "engines/engine.h" |
| 29 | #include "scumm/player_appleII.h" |
| 30 | #include "scumm/scumm.h" |
| 31 | |
| 32 | // CPU_CLOCK according to AppleWin |
| 33 | static const double CPU_CLOCK = 1020484.5; // ~ 1.02 MHz |
| 34 | |
| 35 | namespace Scumm { |
| 36 | |
| 37 | Player_AppleII::Player_AppleII(ScummEngine *scumm, Audio::Mixer *mixer) { |
| 38 | _speakerState = 0; |
| 39 | _soundNr = 0; |
| 40 | |
| 41 | _mixer = mixer; |
| 42 | _sampleRate = _mixer->getOutputRate(); |
| 43 | _vm = scumm; |
| 44 | |
| 45 | setMusicVolume(255); |
| 46 | |
| 47 | _mixer->playStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true); |
| 48 | } |
| 49 | |
| 50 | Player_AppleII::~Player_AppleII() { |
| 51 | _mixer->stopHandle(_soundHandle); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | void logSounds() { |
| 56 | for (nr = 1; nr < 70; ++nr) { |
| 57 | switch (nr) { |
| 58 | // non-existing or invalid |
| 59 | case 3: case 4: case 5: case 49: case 51: case 68: |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | byte *data = _vm->getResourceAddress(rtSound, nr); |
| 64 | if (data) { |
| 65 | size_t size = (data[1] << 8) | data[0]; |
| 66 | |
| 67 | std::stringstream s; |
| 68 | s << "d:/msound/sound" << nr << ".snd"; |
| 69 | FILE *f = fopen(s.str().c_str(), "wb"); |
| 70 | fwrite(data, 1, size, f); |
| 71 | fclose(f); |
| 72 | printf("sound: %d\n", nr); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | */ |
| 77 | |
| 78 | void Player_AppleII::startSound(int nr) { |
| 79 | Common::StackLock lock(_mutex); |
| 80 | |
| 81 | _soundNr = nr; |
| 82 | _buffer.clear(); |
| 83 | |
| 84 | byte *data = _vm->getResourceAddress(rtSound, nr); |
| 85 | assert(data); |
| 86 | |
| 87 | byte *ptr1 = data + 4; |
| 88 | |
| 89 | int type = ptr1[0]; |
| 90 | if (type == 0) |
| 91 | return; |
| 92 | int loop = ptr1[1]; |
| 93 | assert(loop > 0); |
| 94 | ptr1 += 2; |
| 95 | |
| 96 | debug(4, "startSound %d: type %d, loop %d", |
| 97 | nr, type, loop); |
| 98 | |
| 99 | do { |
| 100 | switch (type) { |
| 101 | case 1: // freq up/down |
| 102 | soundFunc1(ptr1); |
| 103 | break; |
| 104 | case 2: // symmetric wave (~) |
| 105 | soundFunc2(ptr1); |
| 106 | break; |
| 107 | case 3: // asymmetric wave (__-) |
| 108 | soundFunc3(ptr1); |
| 109 | break; |
| 110 | case 4: // polyphone (2 voices) |
| 111 | soundFunc4(ptr1); |
| 112 | break; |
| 113 | case 5: // periodic noise |
| 114 | soundFunc5(ptr1); |
| 115 | break; |
| 116 | } |
| 117 | --loop; |
| 118 | } while (loop > 0); |
| 119 | } |
| 120 | |
| 121 | void Player_AppleII::stopAllSounds() { |
| 122 | Common::StackLock lock(_mutex); |
| 123 | _buffer.clear(); |
| 124 | } |
| 125 | |
| 126 | void Player_AppleII::stopSound(int nr) { |
| 127 | Common::StackLock lock(_mutex); |
| 128 | if (_soundNr == nr) { |
| 129 | _buffer.clear(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | int Player_AppleII::getSoundStatus(int nr) const { |
| 134 | Common::StackLock lock(_mutex); |
| 135 | return (_buffer.availableSize() > 0 ? 1 : 0); |
| 136 | } |
| 137 | |
| 138 | int Player_AppleII::getMusicTimer() { |
| 139 | /* Apple-II sounds are synchronous -> no music timer */ |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int Player_AppleII::readBuffer(int16 *buffer, const int numSamples) { |
| 144 | Common::StackLock lock(_mutex); |
| 145 | return _buffer.read((byte*)buffer, numSamples * 2) / 2; |
| 146 | } |
| 147 | |
| 148 | /************************************ |
| 149 | * Apple-II sound-resource parser |
| 150 | ************************************/ |
| 151 | |
| 152 | // toggle speaker on/off |
| 153 | void Player_AppleII::speakerToggle() { |
| 154 | _speakerState ^= 0x1; |
| 155 | } |
| 156 | |
| 157 | void Player_AppleII::generateSamples(int cycles) { |
| 158 | // sampleDiff is used to compensate fractional samples |
| 159 | static double sampleDiff = 0; |
| 160 | double fSamples = (double)cycles / CPU_CLOCK * _sampleRate + sampleDiff; |
| 161 | int samples = (int)(fSamples + 0.5); |
| 162 | sampleDiff = fSamples - samples; |
| 163 | |
| 164 | float vol = (float)_maxvol / 255; |
| 165 | int16 value = vol * (_speakerState ? 32767 : -32767); |
| 166 | for (int i = 0; i < samples; ++i) |
| 167 | _buffer.write(&value, sizeof(value)); |
| 168 | } |
| 169 | |
| 170 | void Player_AppleII::wait(int interval, int count /*y*/) { |
| 171 | assert(count > 0); // 0 == 256? |
| 172 | assert(interval > 0); // 0 == 256? |
| 173 | generateSamples(11 + count*(8 + 5 * interval)); |
| 174 | } |
| 175 | |
| 176 | void Player_AppleII::_soundFunc1(int interval /*a*/, int count /*y*/) { // D076 |
| 177 | assert(interval > 0); // 0 == 256? |
| 178 | assert(count > 0); // 0 == 256? |
| 179 | |
| 180 | for (; count >= 0; --count) { |
| 181 | speakerToggle(); |
| 182 | generateSamples(17 + 5 * interval); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void Player_AppleII::soundFunc1(const byte *params) { // D085 |
| 187 | int delta = params[0]; |
| 188 | int count = params[1]; |
| 189 | byte interval = params[2]; // must be byte ("interval < delta" possible) |
| 190 | int limit = params[3]; |
| 191 | bool decInterval = (params[4] >= 0x40); |
| 192 | |
| 193 | if (decInterval) { |
| 194 | do { |
| 195 | _soundFunc1(interval, count); |
| 196 | interval -= delta; |
| 197 | } while (interval >= limit); |
| 198 | } else { |
| 199 | do { |
| 200 | _soundFunc1(interval, count); |
| 201 | interval += delta; |
| 202 | } while (interval < limit); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void Player_AppleII::_soundFunc2(int interval /*a*/, int count) { // D0EF |
| 207 | if (interval == 0xFE) { |
| 208 | wait(interval, 10); |
| 209 | } else { |
| 210 | assert(count > 0); // 0 == 256? |
| 211 | assert(interval > 0); // 0 == 256? |
| 212 | |
| 213 | int a = (interval >> 3) + count; |
| 214 | for (int y = a; y > 0; --y) { |
| 215 | generateSamples(1292 - 5*interval); |
| 216 | speakerToggle(); |
| 217 | |
| 218 | generateSamples(1287 - 5*interval); |
| 219 | speakerToggle(); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void Player_AppleII::soundFunc2(const byte *params) { // D0D6 |
| 225 | for (int pos = 1; pos < 256; ++pos) { |
| 226 | byte interval = params[pos]; |
| 227 | if (interval == 0xFF) |
| 228 | return; |
| 229 | _soundFunc2(interval, params[0] /*, LD12F=interval*/); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void Player_AppleII::_soundFunc3(int interval /*a*/, int count /*LD12D*/) { // D14B |
| 234 | if (interval == 0xFE) { |
| 235 | wait(interval, 70); |
| 236 | } else { |
| 237 | assert(interval > 0); // 0 == 256? |
| 238 | assert(count > 0); // 0 == 256? |
| 239 | |
| 240 | for (int y = count; y > 0; --y) { |
| 241 | generateSamples(1289 - 5*interval); |
| 242 | speakerToggle(); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void Player_AppleII::soundFunc3(const byte *params) { // D132 |
| 248 | for (int pos = 1; pos < 256; ++pos) { |
| 249 | byte interval = params[pos]; |
| 250 | if (interval == 0xFF) |
| 251 | return; |
| 252 | _soundFunc3(interval, params[0]); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void Player_AppleII::_soundFunc4(byte param0, byte param1, byte param2) { // D1A2 |
| 257 | uint16 count = (-param2 << 8) | 0x3; |
| 258 | byte bitmask1 = 0x3; |
| 259 | byte bitmask2 = 0x3; |
| 260 | |
| 261 | byte updateInterval2 = param0; |
| 262 | if (updateInterval2 == 0) |
| 263 | bitmask2 = 0x0; |
| 264 | |
| 265 | byte updateInterval1 = param1; |
| 266 | if (updateInterval1 == 0) { |
| 267 | bitmask1 = 0x0; |
| 268 | if (bitmask2 != 0) { |
| 269 | bitmask1 = bitmask2; |
| 270 | bitmask2 = 0; |
| 271 | updateInterval1 = updateInterval2; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | byte speakerShiftReg = 0; |
| 276 | static byte updateRemain1 = 80; |
| 277 | static byte updateRemain2 = 10; |
| 278 | |
| 279 | while (true) { |
| 280 | --updateRemain1; |
| 281 | if (updateRemain1 == 0) { |
| 282 | updateRemain1 = updateInterval1; |
| 283 | speakerShiftReg ^= bitmask1; |
| 284 | } |
| 285 | |
| 286 | --updateRemain2; |
| 287 | if (updateRemain2 == 0) { |
| 288 | updateRemain2 = updateInterval2; |
| 289 | if (updateRemain1 != 0) { |
| 290 | speakerShiftReg ^= bitmask2; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (speakerShiftReg & 0x1) |
| 295 | speakerToggle(); |
| 296 | speakerShiftReg >>= 1; |
| 297 | generateSamples(40); |
| 298 | |
| 299 | ++count; |
| 300 | if (count == 0) { |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void Player_AppleII::soundFunc4(const byte *params) { // D170 |
| 307 | while (params[0] != 0x01) { |
| 308 | _soundFunc4(params[0], params[1], params[2]); |
| 309 | params += 3; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void Player_AppleII::_soundFunc5(int interval /*a*/, int count) { // D270 |
| 314 | assert(count > 0); // 0 == 256? |
| 315 | if (interval == 0) |
| 316 | interval = 256; |
| 317 | |
| 318 | for (int i = count; i > 0; --i) { |
| 319 | generateSamples(10 + 5*interval); |
| 320 | speakerToggle(); |
| 321 | |
| 322 | generateSamples(5 + 5*interval); |
| 323 | speakerToggle(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // LD000[loc] ^ LD00A[loc] |
| 328 | static const byte noiseTable[256] = { |
| 329 | 0x65, 0x1b, 0xda, 0x11, 0x61, 0xe5, 0x77, 0x57, 0x92, 0xc8, 0x51, 0x1c, 0xd4, 0x91, 0x62, 0x63, |
| 330 | 0x00, 0x38, 0x57, 0xd5, 0x18, 0xd8, 0xdc, 0x40, 0x03, 0x86, 0xd3, 0x2f, 0x10, 0x11, 0xd8, 0x3c, |
| 331 | 0xbe, 0x00, 0x19, 0xc5, 0xd2, 0xc3, 0xca, 0x34, 0x00, 0x28, 0xbf, 0xb9, 0x18, 0x20, 0x01, 0xcc, |
| 332 | 0xda, 0x08, 0xbc, 0x75, 0x7c, 0xb0, 0x8d, 0xe0, 0x09, 0x18, 0xbf, 0x5d, 0xe9, 0x8c, 0x75, 0x64, |
| 333 | 0xe5, 0xb5, 0x5d, 0xe0, 0xb7, 0x7d, 0xe9, 0x8c, 0x55, 0x65, 0xc5, 0xb5, 0x5d, 0xd8, 0x09, 0x0d, |
| 334 | 0x64, 0xf0, 0xf0, 0x08, 0x63, 0x03, 0x00, 0x55, 0x35, 0xc0, 0x00, 0x20, 0x74, 0xa5, 0x1e, 0xe3, |
| 335 | 0x00, 0x06, 0x3c, 0x52, 0xd1, 0x70, 0xd0, 0x57, 0x02, 0xf0, 0x00, 0xb6, 0xfc, 0x02, 0x11, 0x9a, |
| 336 | 0x3b, 0xc8, 0x38, 0xdf, 0x1a, 0xb0, 0xd1, 0xb8, 0xd0, 0x18, 0x8a, 0x4a, 0xea, 0x1b, 0x12, 0x5d, |
| 337 | 0x29, 0x58, 0xd8, 0x43, 0xb8, 0x2d, 0xd2, 0x61, 0x10, 0x3c, 0x0c, 0x5d, 0x1b, 0x61, 0x10, 0x3c, |
| 338 | 0x0a, 0x5d, 0x1d, 0x61, 0x10, 0x3c, 0x0b, 0x19, 0x88, 0x21, 0xc0, 0x21, 0x07, 0x00, 0x65, 0x62, |
| 339 | 0x08, 0xe9, 0x36, 0x40, 0x20, 0x41, 0x06, 0x00, 0x20, 0x00, 0x00, 0xed, 0xa3, 0x00, 0x88, 0x06, |
| 340 | 0x98, 0x01, 0x5d, 0x7f, 0x02, 0x1d, 0x78, 0x03, 0x60, 0xcb, 0x3a, 0x01, 0xbd, 0x78, 0x02, 0x5d, |
| 341 | 0x7e, 0x03, 0x1d, 0xf5, 0xa6, 0x40, 0x81, 0xb4, 0xd0, 0x8d, 0xd3, 0xd0, 0x6d, 0xd5, 0x61, 0x48, |
| 342 | 0x61, 0x4d, 0xd1, 0xc8, 0xb1, 0xd8, 0x69, 0xff, 0x61, 0xd9, 0xed, 0xa0, 0xfe, 0x19, 0x91, 0x37, |
| 343 | 0x19, 0x37, 0x00, 0xf1, 0x00, 0x01, 0x1f, 0x00, 0xad, 0xc1, 0x01, 0x01, 0x2e, 0x00, 0x40, 0xc6, |
| 344 | 0x7a, 0x9b, 0x95, 0x43, 0xfc, 0x18, 0xd2, 0x9e, 0x2a, 0x5a, 0x4b, 0x2a, 0xb6, 0x87, 0x30, 0x6c |
| 345 | }; |
| 346 | |
| 347 | byte /*a*/ Player_AppleII::noise() { // D261 |
| 348 | static int pos = 0; // initial value? |
| 349 | byte result = noiseTable[pos]; |
| 350 | pos = (pos + 1) % 256; |
| 351 | return result; |
| 352 | } |
| 353 | |
| 354 | void Player_AppleII::soundFunc5(const byte *params) { // D222 |
| 355 | const byte noiseMask[] = { |
| 356 | 0x3F, 0x3F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0x0F, 0x0F |
| 357 | }; |
| 358 | |
| 359 | int param0 = params[0]; |
| 360 | assert(param0 > 0); |
| 361 | for (int i = 0; i < 10; ++i) { |
| 362 | int count = param0; |
| 363 | do { |
| 364 | _soundFunc5(noise() & noiseMask[i], 1); |
| 365 | --count; |
| 366 | } while (count > 0); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | } // End of namespace Scumm |