2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2006-05-23 23:43:52 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2006-05-23 23:43:52 +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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-08-09 17:44:07 +00:00
|
|
|
#include "common/md5.h"
|
|
|
|
#include "common/config-manager.h"
|
2010-05-04 11:56:52 +00:00
|
|
|
#include "common/fs.h"
|
2010-03-18 15:07:11 +00:00
|
|
|
#include "common/random.h"
|
2010-03-18 15:09:24 +00:00
|
|
|
#include "common/str-array.h"
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
#include "sound/mididrv.h"
|
|
|
|
|
2006-05-23 23:43:52 +00:00
|
|
|
#include "agi/agi.h"
|
|
|
|
|
2010-06-15 10:33:57 +00:00
|
|
|
#include "agi/sound_2gs.h"
|
2010-06-15 10:34:45 +00:00
|
|
|
#include "agi/sound_midi.h"
|
2010-06-15 10:33:57 +00:00
|
|
|
|
2006-05-23 23:43:52 +00:00
|
|
|
namespace Agi {
|
|
|
|
|
|
|
|
#define USE_INTERPOLATION
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
//
|
|
|
|
// TODO: add support for variable sampling rate in the output device
|
|
|
|
//
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
AgiSound *AgiSound::createFromRawResource(uint8 *data, uint32 len, int resnum, SoundMgr &manager, int soundemu) {
|
2009-01-03 14:05:57 +00:00
|
|
|
if (data == NULL || len < 2) // Check for too small resource or no resource at all
|
|
|
|
return NULL;
|
2007-08-15 22:00:31 +00:00
|
|
|
uint16 type = READ_LE_UINT16(data);
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
switch (type) { // Create a sound object based on the type
|
2009-05-24 15:17:42 +00:00
|
|
|
case AGI_SOUND_SAMPLE:
|
2009-01-03 14:05:57 +00:00
|
|
|
return new IIgsSample(data, len, resnum, manager);
|
2009-05-24 15:17:42 +00:00
|
|
|
case AGI_SOUND_MIDI:
|
2010-06-15 10:34:45 +00:00
|
|
|
return new IIgsMidi(data, len, resnum, manager);
|
2009-05-24 15:17:42 +00:00
|
|
|
case AGI_SOUND_4CHN:
|
2010-06-15 10:34:45 +00:00
|
|
|
if (soundemu == SOUND_EMU_MIDI) {
|
|
|
|
return new MIDISound(data, len, resnum, manager);
|
|
|
|
} else {
|
|
|
|
return new PCjrSound(data, len, resnum, manager);
|
|
|
|
}
|
2007-08-15 22:00:31 +00:00
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
warning("Sound resource (%d) has unknown type (0x%04x). Not using the sound", resnum, type);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PCjrSound::PCjrSound(uint8 *data, uint32 len, int resnum, SoundMgr &manager) : AgiSound(manager) {
|
|
|
|
_data = data; // Save the resource pointer
|
|
|
|
_len = len; // Save the resource's length
|
|
|
|
_type = READ_LE_UINT16(data); // Read sound resource's type
|
|
|
|
_isValid = (_type == AGI_SOUND_4CHN) && (_data != NULL) && (_len >= 2);
|
|
|
|
|
|
|
|
if (!_isValid) // Check for errors
|
|
|
|
warning("Error creating PCjr 4-channel sound from resource %d (Type %d, length %d)", resnum, _type, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8 *PCjrSound::getVoicePointer(uint voiceNum) {
|
2007-08-16 18:42:28 +00:00
|
|
|
assert(voiceNum < 4);
|
2007-08-15 22:00:31 +00:00
|
|
|
uint16 voiceStartOffset = READ_LE_UINT16(_data + voiceNum * 2);
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
return _data + voiceStartOffset;
|
|
|
|
}
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
static const int16 waveformRamp[WAVEFORM_SIZE] = {
|
2006-05-23 23:43:52 +00:00
|
|
|
0, 8, 16, 24, 32, 40, 48, 56,
|
|
|
|
64, 72, 80, 88, 96, 104, 112, 120,
|
|
|
|
128, 136, 144, 152, 160, 168, 176, 184,
|
|
|
|
192, 200, 208, 216, 224, 232, 240, 255,
|
|
|
|
0, -248, -240, -232, -224, -216, -208, -200,
|
|
|
|
-192, -184, -176, -168, -160, -152, -144, -136,
|
|
|
|
-128, -120, -112, -104, -96, -88, -80, -72,
|
2009-06-06 17:39:13 +00:00
|
|
|
-64, -56, -48, -40, -32, -24, -16, -8 // Ramp up
|
2006-05-23 23:43:52 +00:00
|
|
|
};
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
static const int16 waveformSquare[WAVEFORM_SIZE] = {
|
2006-05-23 23:43:52 +00:00
|
|
|
255, 230, 220, 220, 220, 220, 220, 220,
|
|
|
|
220, 220, 220, 220, 220, 220, 220, 220,
|
|
|
|
220, 220, 220, 220, 220, 220, 220, 220,
|
|
|
|
220, 220, 220, 220, 220, 220, 220, 110,
|
|
|
|
-255, -230, -220, -220, -220, -220, -220, -220,
|
|
|
|
-220, -220, -220, -220, -220, -220, -220, -220,
|
|
|
|
-220, -220, -220, -220, -220, -220, -220, -220,
|
2009-06-06 17:39:13 +00:00
|
|
|
-220, -220, -220, -110, 0, 0, 0, 0 // Square
|
2006-05-23 23:43:52 +00:00
|
|
|
};
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
static const int16 waveformMac[WAVEFORM_SIZE] = {
|
2006-05-23 23:43:52 +00:00
|
|
|
45, 110, 135, 161, 167, 173, 175, 176,
|
|
|
|
156, 137, 123, 110, 91, 72, 35, -2,
|
|
|
|
-60, -118, -142, -165, -170, -176, -177, -179,
|
|
|
|
-177, -176, -164, -152, -117, -82, -17, 47,
|
|
|
|
92, 137, 151, 166, 170, 173, 171, 169,
|
|
|
|
151, 133, 116, 100, 72, 43, -7, -57,
|
|
|
|
-99, -141, -156, -170, -174, -177, -178, -179,
|
|
|
|
-175, -172, -165, -159, -137, -114, -67, -19
|
|
|
|
};
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
static const uint16 period[] = {
|
2006-05-23 23:43:52 +00:00
|
|
|
1024, 1085, 1149, 1218, 1290, 1367,
|
|
|
|
1448, 1534, 1625, 1722, 1825, 1933
|
|
|
|
};
|
|
|
|
|
2007-09-03 12:23:50 +00:00
|
|
|
#if 0
|
2007-01-16 12:40:51 +00:00
|
|
|
static int noteToPeriod(int note) {
|
2006-05-23 23:43:52 +00:00
|
|
|
return 10 * (period[note % 12] >> (note / 12 - 3));
|
|
|
|
}
|
2007-09-03 12:23:50 +00:00
|
|
|
#endif
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::unloadSound(int resnum) {
|
|
|
|
if (_vm->_game.dirSound[resnum].flags & RES_LOADED) {
|
2007-08-15 22:00:31 +00:00
|
|
|
if (_vm->_game.sounds[resnum]->isPlaying()) {
|
|
|
|
_vm->_game.sounds[resnum]->stop();
|
2007-09-19 08:40:12 +00:00
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
// Release the sound resource's data
|
|
|
|
delete _vm->_game.sounds[resnum];
|
|
|
|
_vm->_game.sounds[resnum] = NULL;
|
2007-01-16 12:40:51 +00:00
|
|
|
_vm->_game.dirSound[resnum].flags &= ~RES_LOADED;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::startSound(int resnum, int flag) {
|
2007-08-19 20:48:11 +00:00
|
|
|
int i;
|
2010-06-15 10:35:10 +00:00
|
|
|
AgiSoundEmuType type;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
if (_vm->_game.sounds[resnum] != NULL && _vm->_game.sounds[resnum]->isPlaying())
|
2006-05-23 23:43:52 +00:00
|
|
|
return;
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
stopSound();
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
if (_vm->_game.sounds[resnum] == NULL) // Is this needed at all?
|
2006-05-23 23:43:52 +00:00
|
|
|
return;
|
|
|
|
|
2010-06-15 10:35:10 +00:00
|
|
|
type = (AgiSoundEmuType)_vm->_game.sounds[resnum]->type();
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
if (type != AGI_SOUND_SAMPLE && type != AGI_SOUND_MIDI && type != AGI_SOUND_4CHN)
|
|
|
|
return;
|
|
|
|
|
2007-08-15 22:00:31 +00:00
|
|
|
_vm->_game.sounds[resnum]->play();
|
2007-08-19 20:48:11 +00:00
|
|
|
_playingSound = resnum;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-06-17 23:16:21 +00:00
|
|
|
debugC(3, kDebugLevelSound, "startSound(resnum = %d, flag = %d) type = %d", resnum, flag, type);
|
2007-08-18 12:41:24 +00:00
|
|
|
|
2006-05-23 23:43:52 +00:00
|
|
|
switch (type) {
|
2007-08-18 12:41:24 +00:00
|
|
|
case AGI_SOUND_SAMPLE: {
|
2007-08-19 20:48:11 +00:00
|
|
|
IIgsSample *sampleRes = (IIgsSample *) _vm->_game.sounds[_playingSound];
|
2010-06-15 10:33:57 +00:00
|
|
|
_gsSound->playSampleSound(sampleRes->getHeader(), sampleRes->getSample());
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
2007-08-18 12:41:24 +00:00
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
case AGI_SOUND_MIDI:
|
2008-04-21 04:04:24 +00:00
|
|
|
((IIgsMidi *) _vm->_game.sounds[_playingSound])->rewind();
|
2007-08-19 20:48:11 +00:00
|
|
|
break;
|
2006-05-23 23:43:52 +00:00
|
|
|
case AGI_SOUND_4CHN:
|
2010-06-15 10:34:45 +00:00
|
|
|
if (_vm->_soundemu == SOUND_EMU_MIDI) {
|
|
|
|
_musicPlayer->playMIDI((MIDISound *)_vm->_game.sounds[resnum]);
|
|
|
|
} else {
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
PCjrSound *pcjrSound = (PCjrSound *) _vm->_game.sounds[resnum];
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
// Initialize channel info
|
|
|
|
for (i = 0; i < NUM_CHANNELS; i++) {
|
|
|
|
_chn[i].type = type;
|
|
|
|
_chn[i].flags = AGI_SOUND_LOOP;
|
|
|
|
|
|
|
|
if (_env) {
|
|
|
|
_chn[i].flags |= AGI_SOUND_ENVELOPE;
|
|
|
|
_chn[i].adsr = AGI_SOUND_ENV_ATTACK;
|
|
|
|
}
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
_chn[i].ins = _waveform;
|
|
|
|
_chn[i].size = WAVEFORM_SIZE;
|
|
|
|
_chn[i].ptr = pcjrSound->getVoicePointer(i % 4);
|
|
|
|
_chn[i].timer = 0;
|
|
|
|
_chn[i].vol = 0;
|
|
|
|
_chn[i].end = 0;
|
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
memset(_sndBuffer, 0, BUFFER_SIZE << 1);
|
|
|
|
_endflag = flag;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// Nat Budin reports that the flag should be reset when sound starts
|
2007-08-19 20:48:11 +00:00
|
|
|
_vm->setflag(_endflag, false);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::stopSound() {
|
2006-05-23 23:43:52 +00:00
|
|
|
int i;
|
|
|
|
|
2009-06-13 22:48:16 +00:00
|
|
|
debugC(3, kDebugLevelSound, "stopSound() --> %d", _playingSound);
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
_endflag = -1;
|
2007-08-18 12:41:24 +00:00
|
|
|
if (_vm->_soundemu != SOUND_EMU_APPLE2GS) {
|
|
|
|
for (i = 0; i < NUM_CHANNELS; i++)
|
|
|
|
stopNote(i);
|
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_playingSound != -1) {
|
2009-06-13 22:48:16 +00:00
|
|
|
if (_vm->_game.sounds[_playingSound]) // sanity checking
|
|
|
|
_vm->_game.sounds[_playingSound]->stop();
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-08-18 12:41:24 +00:00
|
|
|
if (_vm->_soundemu == SOUND_EMU_APPLE2GS) {
|
2010-06-15 10:33:57 +00:00
|
|
|
_gsSound->stopSounds();
|
2007-08-18 12:41:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
if (_vm->_soundemu == SOUND_EMU_MIDI) {
|
|
|
|
_musicPlayer->stop();
|
|
|
|
}
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
_playingSound = -1;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
2010-06-15 10:35:42 +00:00
|
|
|
|
|
|
|
if (_endflag != -1)
|
|
|
|
_vm->setflag(_endflag, true);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
int SoundMgr::initSound() {
|
2006-05-23 23:43:52 +00:00
|
|
|
int r = -1;
|
|
|
|
|
2008-08-13 11:57:48 +00:00
|
|
|
memset(_sndBuffer, 0, BUFFER_SIZE << 1);
|
2007-08-19 20:48:11 +00:00
|
|
|
_env = false;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-02-13 15:27:36 +00:00
|
|
|
switch (_vm->_soundemu) {
|
2006-05-23 23:43:52 +00:00
|
|
|
case SOUND_EMU_NONE:
|
2007-08-19 20:48:11 +00:00
|
|
|
_waveform = waveformRamp;
|
|
|
|
_env = true;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
case SOUND_EMU_AMIGA:
|
|
|
|
case SOUND_EMU_PC:
|
2007-08-19 20:48:11 +00:00
|
|
|
_waveform = waveformSquare;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
case SOUND_EMU_MAC:
|
2007-08-19 20:48:11 +00:00
|
|
|
_waveform = waveformMac;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
2007-08-17 13:10:57 +00:00
|
|
|
case SOUND_EMU_APPLE2GS:
|
2008-10-13 19:17:51 +00:00
|
|
|
_disabledMidi = !loadInstruments();
|
2007-08-17 13:10:57 +00:00
|
|
|
break;
|
2009-06-17 23:16:21 +00:00
|
|
|
case SOUND_EMU_COCO3:
|
|
|
|
break;
|
2010-06-15 10:34:45 +00:00
|
|
|
case SOUND_EMU_MIDI:
|
|
|
|
break;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
report("Initializing sound:\n");
|
|
|
|
|
|
|
|
report("sound: envelopes ");
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_env) {
|
2006-05-23 23:43:52 +00:00
|
|
|
report("enabled (decay=%d, sustain=%d)\n", ENV_DECAY, ENV_SUSTAIN);
|
|
|
|
} else {
|
|
|
|
report("disabled\n");
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:34:45 +00:00
|
|
|
if (_vm->_soundemu != SOUND_EMU_MIDI)
|
|
|
|
_mixer->playStream(Audio::Mixer::kMusicSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);
|
2007-01-27 05:23:56 +00:00
|
|
|
|
2006-05-23 23:43:52 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::deinitSound() {
|
2006-05-23 23:43:52 +00:00
|
|
|
debugC(3, kDebugLevelSound, "()");
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->stopHandle(_soundHandle);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::stopNote(int i) {
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[i].adsr = AGI_SOUND_ENV_RELEASE;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-10-20 12:22:30 +00:00
|
|
|
if (_useChorus) {
|
2009-06-06 17:39:13 +00:00
|
|
|
// Stop chorus ;)
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[i].type == AGI_SOUND_4CHN &&
|
2007-08-15 16:31:15 +00:00
|
|
|
_vm->_soundemu == SOUND_EMU_NONE && i < 3) {
|
|
|
|
stopNote(i + 4);
|
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::playNote(int i, int freq, int vol) {
|
|
|
|
if (!_vm->getflag(fSoundOn))
|
2006-05-23 23:43:52 +00:00
|
|
|
vol = 0;
|
2007-02-13 15:27:36 +00:00
|
|
|
else if (vol && _vm->_soundemu == SOUND_EMU_PC)
|
2006-05-23 23:43:52 +00:00
|
|
|
vol = 160;
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[i].phase = 0;
|
|
|
|
_chn[i].freq = freq;
|
|
|
|
_chn[i].vol = vol;
|
|
|
|
_chn[i].env = 0x10000;
|
|
|
|
_chn[i].adsr = AGI_SOUND_ENV_ATTACK;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-10-20 12:22:30 +00:00
|
|
|
if (_useChorus) {
|
2009-06-06 17:39:13 +00:00
|
|
|
// Add chorus ;)
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[i].type == AGI_SOUND_4CHN &&
|
2007-08-15 16:31:15 +00:00
|
|
|
_vm->_soundemu == SOUND_EMU_NONE && i < 3) {
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2007-08-15 16:31:15 +00:00
|
|
|
int newfreq = freq * 1007 / 1000;
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2007-08-15 16:31:15 +00:00
|
|
|
if (freq == newfreq)
|
|
|
|
newfreq++;
|
2009-06-06 17:39:13 +00:00
|
|
|
|
2007-08-15 16:31:15 +00:00
|
|
|
playNote(i + 4, newfreq, vol * 2 / 3);
|
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:16:21 +00:00
|
|
|
static int cocoFrequencies[] = {
|
|
|
|
130, 138, 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
|
|
|
|
261, 277, 293, 311, 329, 349, 369, 391, 415, 440, 466, 493,
|
|
|
|
523, 554, 587, 622, 659, 698, 739, 783, 830, 880, 932, 987,
|
|
|
|
1046, 1108, 1174, 1244, 1318, 1396, 1479, 1567, 1661, 1760, 1864, 1975,
|
|
|
|
2093, 2217, 2349, 2489, 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951
|
|
|
|
};
|
|
|
|
|
|
|
|
void SoundMgr::playCoCoSound() {
|
2009-06-19 07:56:30 +00:00
|
|
|
int i = 0;
|
2009-06-17 23:16:21 +00:00
|
|
|
CoCoNote note;
|
|
|
|
|
|
|
|
do {
|
|
|
|
note.read(_chn[i].ptr);
|
|
|
|
|
|
|
|
if (note.freq != 0xff) {
|
|
|
|
playNote(0, cocoFrequencies[note.freq], note.volume);
|
|
|
|
|
|
|
|
uint32 start_time = _vm->_system->getMillis();
|
|
|
|
|
|
|
|
while (_vm->_system->getMillis() < start_time + note.duration) {
|
|
|
|
_vm->_system->updateScreen();
|
|
|
|
|
|
|
|
_vm->_system->delayMillis(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (note.freq != 0xff);
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::playAgiSound() {
|
2007-08-15 15:55:38 +00:00
|
|
|
int i;
|
|
|
|
AgiNote note;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
_playing = false;
|
|
|
|
for (i = 0; i < (_vm->_soundemu == SOUND_EMU_PC ? 1 : 4); i++) {
|
|
|
|
_playing |= !_chn[i].end;
|
|
|
|
note.read(_chn[i].ptr); // Read a single note (Doesn't advance the pointer)
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[i].end)
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if ((--_chn[i].timer) <= 0) {
|
2007-01-16 12:40:51 +00:00
|
|
|
stopNote(i);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-15 15:55:38 +00:00
|
|
|
if (note.freqDiv != 0) {
|
|
|
|
int volume = (note.attenuation == 0x0F) ? 0 : (0xFF - note.attenuation * 2);
|
|
|
|
playNote(i, note.freqDiv * 10, volume);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[i].timer = note.duration;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[i].timer == 0xffff) {
|
|
|
|
_chn[i].end = 1;
|
|
|
|
_chn[i].vol = 0;
|
|
|
|
_chn[i].env = 0;
|
2007-08-15 16:31:15 +00:00
|
|
|
|
2009-10-20 12:22:30 +00:00
|
|
|
if (_useChorus) {
|
2009-06-06 17:39:13 +00:00
|
|
|
// chorus
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[i].type == AGI_SOUND_4CHN && _vm->_soundemu == SOUND_EMU_NONE && i < 3) {
|
|
|
|
_chn[i + 4].vol = 0;
|
|
|
|
_chn[i + 4].env = 0;
|
2007-08-15 16:31:15 +00:00
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[i].ptr += 5; // Advance the pointer to the next note data (5 bytes per note)
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
void SoundMgr::playSound() {
|
2006-05-23 23:43:52 +00:00
|
|
|
int i;
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_endflag == -1)
|
2006-05-23 23:43:52 +00:00
|
|
|
return;
|
|
|
|
|
2007-08-18 12:41:24 +00:00
|
|
|
if (_vm->_soundemu == SOUND_EMU_APPLE2GS) {
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_playingSound != -1) {
|
|
|
|
if (_vm->_game.sounds[_playingSound]->type() == AGI_SOUND_MIDI) {
|
2007-08-19 23:57:12 +00:00
|
|
|
playMidiSound();
|
2009-05-31 10:02:16 +00:00
|
|
|
//warning("playSound: Trying to play an Apple IIGS MIDI sound. Not yet implemented");
|
2007-08-19 20:48:11 +00:00
|
|
|
} else if (_vm->_game.sounds[_playingSound]->type() == AGI_SOUND_SAMPLE) {
|
2007-08-18 12:41:24 +00:00
|
|
|
//debugC(3, kDebugLevelSound, "playSound: Trying to play an Apple IIGS sample");
|
|
|
|
playSampleSound();
|
|
|
|
}
|
|
|
|
}
|
2009-06-17 23:16:21 +00:00
|
|
|
} else if (_vm->_soundemu == SOUND_EMU_COCO3) {
|
|
|
|
playCoCoSound();
|
2007-08-18 12:41:24 +00:00
|
|
|
} else {
|
|
|
|
//debugC(3, kDebugLevelSound, "playSound: Trying to play a PCjr 4-channel sound");
|
2007-01-16 12:40:51 +00:00
|
|
|
playAgiSound();
|
2007-08-18 12:41:24 +00:00
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (!_playing) {
|
2007-08-18 12:41:24 +00:00
|
|
|
if (_vm->_soundemu != SOUND_EMU_APPLE2GS) {
|
2009-01-29 05:26:12 +00:00
|
|
|
for (i = 0; i < NUM_CHANNELS; _chn[i++].vol = 0)
|
|
|
|
;
|
2007-08-18 12:41:24 +00:00
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_endflag != -1)
|
|
|
|
_vm->setflag(_endflag, true);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_playingSound != -1)
|
|
|
|
_vm->_game.sounds[_playingSound]->stop();
|
|
|
|
_playingSound = -1;
|
|
|
|
_endflag = -1;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
uint32 SoundMgr::mixSound() {
|
2006-05-23 23:43:52 +00:00
|
|
|
register int i, p;
|
2007-08-19 20:48:11 +00:00
|
|
|
const int16 *src;
|
2006-05-23 23:43:52 +00:00
|
|
|
int c, b, m;
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
memset(_sndBuffer, 0, BUFFER_SIZE << 1);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 23:28:38 +00:00
|
|
|
if (!_playing || _playingSound == -1)
|
|
|
|
return BUFFER_SIZE;
|
|
|
|
|
2007-08-18 12:41:24 +00:00
|
|
|
// Handle Apple IIGS sound mixing here
|
2008-04-17 17:18:43 +00:00
|
|
|
// TODO: Implement playing both waves in an oscillator
|
|
|
|
// TODO: Implement swap-mode in an oscillator
|
2007-08-19 23:33:22 +00:00
|
|
|
if (_vm->_soundemu == SOUND_EMU_APPLE2GS) {
|
2010-06-15 10:33:57 +00:00
|
|
|
for (uint midiChan = 0; midiChan < _gsSound->_midiChannels.size(); midiChan++) {
|
|
|
|
for (uint gsChan = 0; gsChan < _gsSound->_midiChannels[midiChan]._gsChannels.size(); gsChan++) {
|
|
|
|
IIgsChannelInfo &channel = _gsSound->_midiChannels[midiChan]._gsChannels[gsChan];
|
2008-04-17 17:18:43 +00:00
|
|
|
if (channel.playing()) { // Only mix in actively playing channels
|
|
|
|
// Frequency multiplier was 1076.0 based on tests made with MESS 0.117.
|
|
|
|
// Tests made with KEGS32 averaged the multiplier to around 1045.
|
|
|
|
// So this is a guess but maybe it's 1046.5... i.e. C6's frequency?
|
|
|
|
double hertz = C6_FREQ * pow(SEMITONE, fracToDouble(channel.note));
|
|
|
|
channel.posAdd = doubleToFrac(hertz / getRate());
|
|
|
|
channel.vol = doubleToFrac(fracToDouble(channel.envVol) * fracToDouble(channel.chanVol) / 127.0);
|
|
|
|
double tempVol = fracToDouble(channel.vol)/127.0;
|
|
|
|
for (i = 0; i < IIGS_BUFFER_SIZE; i++) {
|
|
|
|
b = channel.relocatedSample[fracToInt(channel.pos)];
|
|
|
|
// TODO: Find out what volume/amplification setting is loud enough
|
|
|
|
// but still doesn't clip when playing many channels on it.
|
|
|
|
_sndBuffer[i] += (int16) (b * tempVol * 256/4);
|
|
|
|
channel.pos += channel.posAdd;
|
|
|
|
|
|
|
|
if (channel.pos >= intToFrac(channel.size)) {
|
|
|
|
if (channel.loop) {
|
|
|
|
// Don't divide by zero on zero length samples
|
|
|
|
channel.pos %= intToFrac(channel.size + (channel.size == 0));
|
|
|
|
// Probably we should loop the envelope too
|
|
|
|
channel.envSeg = 0;
|
|
|
|
channel.envVol = channel.startEnvVol;
|
|
|
|
} else {
|
|
|
|
channel.pos = channel.chanVol = 0;
|
|
|
|
channel.end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-18 12:41:24 +00:00
|
|
|
|
2008-04-17 17:18:43 +00:00
|
|
|
if (channel.envSeg < ENVELOPE_SEGMENT_COUNT) {
|
|
|
|
const IIgsEnvelopeSegment &seg = channel.ins->env.seg[channel.envSeg];
|
2008-04-21 04:04:24 +00:00
|
|
|
// I currently assume enveloping works with the same speed as the MIDI
|
|
|
|
// (i.e. with 1/60ths of a second ticks).
|
|
|
|
// TODO: Check if enveloping really works with the same speed as MIDI
|
|
|
|
frac_t envVolDelta = doubleToFrac(seg.inc/256.0);
|
2008-04-17 17:18:43 +00:00
|
|
|
if (intToFrac(seg.bp) >= channel.envVol) {
|
|
|
|
channel.envVol += envVolDelta;
|
|
|
|
if (channel.envVol >= intToFrac(seg.bp)) {
|
|
|
|
channel.envVol = intToFrac(seg.bp);
|
|
|
|
channel.envSeg += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
channel.envVol -= envVolDelta;
|
|
|
|
if (channel.envVol <= intToFrac(seg.bp)) {
|
|
|
|
channel.envVol = intToFrac(seg.bp);
|
|
|
|
channel.envSeg += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-18 12:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 10:33:57 +00:00
|
|
|
_gsSound->removeStoppedSounds();
|
2007-08-18 12:41:24 +00:00
|
|
|
return IIGS_BUFFER_SIZE;
|
2009-06-06 17:39:13 +00:00
|
|
|
} // else ...
|
2007-08-18 12:41:24 +00:00
|
|
|
|
|
|
|
// Handle PCjr 4-channel sound mixing here
|
2006-05-23 23:43:52 +00:00
|
|
|
for (c = 0; c < NUM_CHANNELS; c++) {
|
2007-08-19 20:48:11 +00:00
|
|
|
if (!_chn[c].vol)
|
2006-05-23 23:43:52 +00:00
|
|
|
continue;
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
m = _chn[c].flags & AGI_SOUND_ENVELOPE ?
|
|
|
|
_chn[c].vol * _chn[c].env >> 16 : _chn[c].vol;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[c].type != AGI_SOUND_4CHN || c != 3) {
|
|
|
|
src = _chn[c].ins;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
p = _chn[c].phase;
|
2006-05-23 23:43:52 +00:00
|
|
|
for (i = 0; i < BUFFER_SIZE; i++) {
|
|
|
|
b = src[p >> 8];
|
|
|
|
#ifdef USE_INTERPOLATION
|
2007-08-19 20:48:11 +00:00
|
|
|
b += ((src[((p >> 8) + 1) % _chn[c].size] - src[p >> 8]) * (p & 0xff)) >> 8;
|
2006-05-23 23:43:52 +00:00
|
|
|
#endif
|
2007-08-19 20:48:11 +00:00
|
|
|
_sndBuffer[i] += (b * m) >> 4;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
p += (uint32) 118600 *4 / _chn[c].freq;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2007-09-11 09:20:39 +00:00
|
|
|
// FIXME: Fingolfin asks: why is there a FIXME here? Please either clarify what
|
|
|
|
// needs fixing, or remove it!
|
2009-06-06 17:39:13 +00:00
|
|
|
// FIXME
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[c].flags & AGI_SOUND_LOOP) {
|
|
|
|
p %= _chn[c].size << 8;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-08-19 20:48:11 +00:00
|
|
|
if (p >= _chn[c].size << 8) {
|
|
|
|
p = _chn[c].vol = 0;
|
|
|
|
_chn[c].end = 1;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[c].phase = p;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2009-06-06 17:39:13 +00:00
|
|
|
// Add white noise
|
2006-05-23 23:43:52 +00:00
|
|
|
for (i = 0; i < BUFFER_SIZE; i++) {
|
2006-12-06 19:27:02 +00:00
|
|
|
b = _vm->_rnd->getRandomNumber(255) - 128;
|
2007-08-19 20:48:11 +00:00
|
|
|
_sndBuffer[i] += (b * m) >> 4;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-19 20:48:11 +00:00
|
|
|
switch (_chn[c].adsr) {
|
2006-05-23 23:43:52 +00:00
|
|
|
case AGI_SOUND_ENV_ATTACK:
|
2009-06-06 17:39:13 +00:00
|
|
|
// not implemented
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[c].adsr = AGI_SOUND_ENV_DECAY;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
case AGI_SOUND_ENV_DECAY:
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[c].env > _chn[c].vol * ENV_SUSTAIN + ENV_DECAY) {
|
|
|
|
_chn[c].env -= ENV_DECAY;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[c].env = _chn[c].vol * ENV_SUSTAIN;
|
|
|
|
_chn[c].adsr = AGI_SOUND_ENV_SUSTAIN;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AGI_SOUND_ENV_SUSTAIN:
|
|
|
|
break;
|
|
|
|
case AGI_SOUND_ENV_RELEASE:
|
2007-08-19 20:48:11 +00:00
|
|
|
if (_chn[c].env >= ENV_RELEASE) {
|
|
|
|
_chn[c].env -= ENV_RELEASE;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-08-19 20:48:11 +00:00
|
|
|
_chn[c].env = 0;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2007-08-13 14:06:30 +00:00
|
|
|
/**
|
2008-04-08 14:45:42 +00:00
|
|
|
* Convert sample from 8-bit unsigned to 8-bit signed format.
|
2007-08-13 14:06:30 +00:00
|
|
|
* @param source Source stream containing the 8-bit unsigned sample data.
|
2008-04-08 14:45:42 +00:00
|
|
|
* @param dest Destination buffer for the 8-bit signed sample data.
|
2007-08-13 14:06:30 +00:00
|
|
|
* @param length Length of the sample data to be converted.
|
|
|
|
*/
|
2008-04-08 14:45:42 +00:00
|
|
|
bool SoundMgr::convertWave(Common::SeekableReadStream &source, int8 *dest, uint length) {
|
|
|
|
// Convert the wave from 8-bit unsigned to 8-bit signed format
|
2007-08-13 14:06:30 +00:00
|
|
|
for (uint i = 0; i < length; i++)
|
2008-04-08 14:45:42 +00:00
|
|
|
dest[i] = (int8) ((int) source.readByte() - 128);
|
2009-07-14 23:03:40 +00:00
|
|
|
return !(source.eos() || source.err());
|
2007-08-13 14:06:30 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 11:57:48 +00:00
|
|
|
void SoundMgr::fillAudio(void *udata, int16 *stream, uint len) {
|
2006-12-06 19:27:02 +00:00
|
|
|
SoundMgr *soundMgr = (SoundMgr *)udata;
|
2006-05-23 23:43:52 +00:00
|
|
|
uint32 p = 0;
|
2009-05-10 20:42:46 +00:00
|
|
|
|
|
|
|
// current number of audio bytes in _sndBuffer
|
|
|
|
static uint32 data_available = 0;
|
|
|
|
// offset of start of audio bytes in _sndBuffer
|
|
|
|
static uint32 data_offset = 0;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
len <<= 2;
|
|
|
|
|
2006-05-24 19:51:37 +00:00
|
|
|
debugC(5, kDebugLevelSound, "(%p, %p, %d)", (void *)udata, (void *)stream, len);
|
2009-05-10 20:42:46 +00:00
|
|
|
|
|
|
|
while (len > data_available) {
|
|
|
|
memcpy((uint8 *)stream + p, (uint8*)_sndBuffer + data_offset, data_available);
|
|
|
|
p += data_available;
|
|
|
|
len -= data_available;
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
soundMgr->playSound();
|
2009-05-10 20:42:46 +00:00
|
|
|
data_available = soundMgr->mixSound() << 1;
|
|
|
|
data_offset = 0;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
2009-05-10 20:42:46 +00:00
|
|
|
|
|
|
|
memcpy((uint8 *)stream + p, (uint8*)_sndBuffer + data_offset, len);
|
|
|
|
data_offset += len;
|
|
|
|
data_available -= len;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 11:36:45 +00:00
|
|
|
SoundMgr::SoundMgr(AgiBase *agi, Audio::Mixer *pMixer) : _chn() {
|
2006-12-06 19:27:02 +00:00
|
|
|
_vm = agi;
|
2006-05-23 23:43:52 +00:00
|
|
|
_mixer = pMixer;
|
|
|
|
_sampleRate = pMixer->getOutputRate();
|
2007-08-19 20:48:11 +00:00
|
|
|
_endflag = -1;
|
|
|
|
_playingSound = -1;
|
2008-08-13 11:57:48 +00:00
|
|
|
_env = false;
|
|
|
|
_playing = false;
|
|
|
|
_sndBuffer = (int16 *)calloc(2, BUFFER_SIZE);
|
2007-08-19 20:48:11 +00:00
|
|
|
_waveform = 0;
|
2008-10-13 19:17:51 +00:00
|
|
|
_disabledMidi = false;
|
2009-10-20 12:22:30 +00:00
|
|
|
_useChorus = true; // FIXME: Currently always true?
|
2010-06-15 10:34:45 +00:00
|
|
|
_midiDriver = 0;
|
2010-06-15 10:33:57 +00:00
|
|
|
|
|
|
|
_gsSound = new IIgsSoundMgr;
|
2010-06-15 10:34:45 +00:00
|
|
|
|
|
|
|
if (_vm->_soundemu == SOUND_EMU_MIDI) {
|
|
|
|
MidiDriverType midiDriver = MidiDriver::detectMusicDriver(MDT_MIDI | MDT_ADLIB);
|
|
|
|
|
|
|
|
_midiDriver = MidiDriver::createMidi(midiDriver);
|
|
|
|
_musicPlayer = new MusicPlayer(_midiDriver, this);
|
|
|
|
}
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
void SoundMgr::premixerCall(int16 *data, uint len) {
|
2010-06-15 10:34:45 +00:00
|
|
|
if (_vm->_soundemu != SOUND_EMU_MIDI)
|
|
|
|
fillAudio(this, data, len);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
void SoundMgr::setVolume(uint8 volume) {
|
2006-05-23 23:43:52 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
SoundMgr::~SoundMgr() {
|
2008-08-13 11:57:48 +00:00
|
|
|
free(_sndBuffer);
|
2010-06-15 10:33:57 +00:00
|
|
|
delete _gsSound;
|
2010-06-15 10:34:45 +00:00
|
|
|
|
|
|
|
delete _musicPlayer;
|
|
|
|
delete _midiDriver;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2006-05-24 14:00:08 +00:00
|
|
|
} // End of namespace Agi
|