2008-07-23 09:02:47 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// FIXME: This code is taken from MADE and may need more work (e.g. setVolume).
|
|
|
|
|
|
|
|
// MIDI and digital music class
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/mididrv.h"
|
|
|
|
#include "audio/midiparser.h"
|
|
|
|
#include "audio/decoders/adpcm.h"
|
2010-11-08 23:07:42 +00:00
|
|
|
|
|
|
|
#include "backends/audiocd/audiocd.h"
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
#include "common/config-manager.h"
|
|
|
|
#include "common/file.h"
|
2010-11-19 17:03:07 +00:00
|
|
|
#include "common/memstream.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2011-04-13 10:24:34 +00:00
|
|
|
#include "tinsel/adpcm.h"
|
2008-07-23 09:02:47 +00:00
|
|
|
#include "tinsel/config.h"
|
|
|
|
#include "tinsel/sound.h"
|
|
|
|
#include "tinsel/music.h"
|
2008-12-01 20:35:36 +00:00
|
|
|
#include "tinsel/handle.h"
|
|
|
|
#include "tinsel/sysvar.h"
|
|
|
|
|
2009-02-02 18:32:36 +00:00
|
|
|
enum {
|
|
|
|
MUSIC_JUMP = -1,
|
|
|
|
MUSIC_END = -2,
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-02-02 18:32:36 +00:00
|
|
|
BLMAGIC = -3458,
|
|
|
|
|
|
|
|
DIM_SPEED = 8
|
|
|
|
};
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
namespace Tinsel {
|
|
|
|
|
|
|
|
//--------------------------- Midi data -------------------------------------
|
|
|
|
|
|
|
|
// sound buffer structure used for MIDI data and samples
|
|
|
|
struct SOUND_BUFFER {
|
|
|
|
uint8 *pDat; // pointer to actual buffer
|
|
|
|
uint32 size; // size of the buffer
|
|
|
|
};
|
|
|
|
|
2010-11-16 09:53:55 +00:00
|
|
|
// FIXME: Avoid non-const global vars
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
// MIDI buffer
|
2012-02-22 22:14:29 +00:00
|
|
|
static SOUND_BUFFER g_midiBuffer = { 0, 0 };
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
static SCNHANDLE g_currentMidi = 0;
|
|
|
|
static bool g_currentLoop = false;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2010-08-24 12:27:22 +00:00
|
|
|
// We allocate 155 entries because that's the maximum, used in the SCN version
|
2012-02-22 22:14:29 +00:00
|
|
|
static SCNHANDLE g_midiOffsets[155];
|
2010-08-24 09:46:17 +00:00
|
|
|
|
2009-11-09 22:19:54 +00:00
|
|
|
static const int enhancedAudioGRAVersion[] = {
|
2009-01-12 14:56:29 +00:00
|
|
|
1, 2, 1, 1, 3, 3, 4, 4, 5, 6, // 1-10
|
|
|
|
1, 7, 8, 9, 10, 3, 11, 11, 12, 13, // 11-20
|
|
|
|
13, 13, 13, 13, 14, 13, 13, 15, 16, 17, // 21-30
|
|
|
|
15, 18, 19, 20, 338, 21, 21, 22, 22, 23, // 31-40
|
|
|
|
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, // 41-50
|
|
|
|
34, 35, 35, 36, 37, 38, 39, 39, 39, 39, // 51-60
|
|
|
|
40, 39, 41, 41, 42, 43, 42, 44, 45, 41, // 61-70
|
|
|
|
46, 48, 47, 48, 49, 50, 51, 52, 53, 54, // 71-80
|
|
|
|
55, 56, 57, 58, 59, 60, 61, 62, 63, 61, // 81-90
|
|
|
|
64, 65, 66, 67, 68, 69, 70, 68, 71, 72, // 91-100
|
|
|
|
73, 74, 75, 12, 76, 77, 78, 79, 80, 4, // 101-110
|
|
|
|
81, 82, 83, 82, 81, 84, 85, 86, 87, 88, // 111-120
|
|
|
|
89, 90, 88, 2, 2, 2, 2, 2, 2, 60, // 121-130
|
|
|
|
91, 92, 93, 94, 94, 95, 96, 52, 4, 97, // 131-140
|
|
|
|
98, 99, 99 // 141-143
|
|
|
|
};
|
|
|
|
|
2009-11-09 22:19:54 +00:00
|
|
|
static const int enhancedAudioSCNVersion[] = {
|
2009-01-12 14:56:29 +00:00
|
|
|
301, 302, 2, 1, 1, 301, 302, 3, 3, 4, // 1-10
|
|
|
|
4, 5, 6, 1, 7, 8, 9, 10, 8, 11, // 11-20
|
|
|
|
11, 12, 13, 13, 13, 13, 13, 14, 13, 13, // 21-30
|
|
|
|
15, 16, 17, 15, 18, 19, 20, 338, 21, 21, // 31-40
|
|
|
|
341, 342, 22, 22, 23, 24, 25, 26, 27, 28, // 41-50
|
|
|
|
29, 30, 31, 32, 33, 34, 35, 35, 36, 37, // 51-60
|
|
|
|
38, 39, 39, 39, 39, 40, 39, 41, 41, 42, // 61-70
|
|
|
|
43, 42, 44, 45, 41, 46, 48, 47, 48, 49, // 71-80
|
|
|
|
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, // 81-90
|
|
|
|
60, 61, 62, 63, 61, 64, 65, 66, 67, 68, // 91-100
|
|
|
|
69, 70, 68, 71, 72, 73, 74, 75, 12, 76, // 101-110
|
|
|
|
77, 78, 79, 80, 4, 4, 82, 83, 77, 4, // 111-120
|
|
|
|
84, 85, 86, 3124, 88, 89, 90, 88, 2, 2, // 121-130
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 131-140
|
2010-08-24 09:47:04 +00:00
|
|
|
3142, 91, 92, 93, 94, 94, 95, 96, 52, 4, // 141-150
|
2009-03-07 19:24:31 +00:00
|
|
|
97, 98, 99, 99 // 151-154
|
2008-07-23 09:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int GetTrackNumber(SCNHANDLE hMidi) {
|
2012-02-22 22:14:29 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(g_midiOffsets); i++)
|
|
|
|
if (g_midiOffsets[i] == hMidi)
|
2010-08-24 12:27:22 +00:00
|
|
|
return i;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCNHANDLE GetTrackOffset(int trackNumber) {
|
2012-02-22 22:14:29 +00:00
|
|
|
assert(trackNumber < ARRAYSIZE(g_midiOffsets));
|
|
|
|
return g_midiOffsets[trackNumber];
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays the specified MIDI sequence through the sound driver.
|
|
|
|
* @param dwFileOffset File offset of MIDI sequence data
|
|
|
|
* @param bLoop Whether to loop the sequence
|
|
|
|
*/
|
|
|
|
bool PlayMidiSequence(uint32 dwFileOffset, bool bLoop) {
|
2012-02-22 22:14:29 +00:00
|
|
|
g_currentMidi = dwFileOffset;
|
|
|
|
g_currentLoop = bLoop;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-07-15 14:37:55 +00:00
|
|
|
bool mute = false;
|
|
|
|
if (ConfMan.hasKey("mute"))
|
|
|
|
mute = ConfMan.getBool("mute");
|
2010-09-11 05:28:54 +00:00
|
|
|
|
2012-07-15 14:37:55 +00:00
|
|
|
SetMidiVolume(mute ? 0 : _vm->_config->_musicVolume);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
// the index and length of the last tune loaded
|
|
|
|
uint32 dwSeqLen = 0; // length of the sequence
|
|
|
|
|
2009-01-12 15:31:50 +00:00
|
|
|
// Support for external music from the music enhancement project
|
|
|
|
if (_vm->getFeatures() & GF_ENHANCED_AUDIO_SUPPORT) {
|
|
|
|
int trackNumber = GetTrackNumber(dwFileOffset);
|
2011-02-24 12:51:05 +00:00
|
|
|
// Track 8 has been removed in the German CD re-release "Neon Edition"
|
|
|
|
if ((_vm->getFeatures() & GF_ALT_MIDI) && trackNumber >= 8)
|
|
|
|
trackNumber++;
|
|
|
|
|
2009-01-12 15:31:50 +00:00
|
|
|
int track = 0;
|
|
|
|
if (trackNumber >= 0) {
|
2011-02-24 12:51:05 +00:00
|
|
|
if (_vm->getFeatures() & GF_SCNFILES)
|
2009-01-12 15:31:50 +00:00
|
|
|
track = enhancedAudioSCNVersion[trackNumber];
|
|
|
|
else
|
|
|
|
track = enhancedAudioGRAVersion[trackNumber];
|
|
|
|
|
|
|
|
if (track > 0) {
|
|
|
|
StopMidi();
|
|
|
|
|
2009-07-11 08:16:55 +00:00
|
|
|
// StopMidi resets these fields, so set them again
|
2012-02-22 22:14:29 +00:00
|
|
|
g_currentMidi = dwFileOffset;
|
|
|
|
g_currentLoop = bLoop;
|
2009-07-11 08:16:55 +00:00
|
|
|
|
2009-01-24 01:29:52 +00:00
|
|
|
// try to play track, but don't fall back to a true CD
|
2010-06-15 04:13:12 +00:00
|
|
|
g_system->getAudioCDManager()->play(track, bLoop ? -1 : 1, 0, 0, true);
|
2009-01-12 15:31:50 +00:00
|
|
|
|
|
|
|
// Check if an enhanced audio track is being played.
|
|
|
|
// If it is, stop here and don't load a MIDI track
|
2010-06-15 04:13:12 +00:00
|
|
|
if (g_system->getAudioCDManager()->isPlaying()) {
|
2009-01-12 15:31:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warning("Unknown MIDI offset %d", dwFileOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 13:55:36 +00:00
|
|
|
if (dwFileOffset == 0)
|
|
|
|
return true;
|
|
|
|
|
2012-07-08 22:31:25 +00:00
|
|
|
Common::File midiStream;
|
|
|
|
|
|
|
|
// open MIDI sequence file in binary mode
|
|
|
|
if (!midiStream.open(MIDI_FILE))
|
|
|
|
error(CANNOT_FIND_FILE, MIDI_FILE);
|
|
|
|
|
|
|
|
// move to correct position in the file
|
|
|
|
midiStream.seek(dwFileOffset, SEEK_SET);
|
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
if (TinselV1Mac) {
|
|
|
|
// The Macintosh version of DW1 uses raw PCM for music
|
|
|
|
dwSeqLen = midiStream.readUint32BE();
|
|
|
|
_vm->_sound->playDW1MacMusic(midiStream, dwSeqLen);
|
|
|
|
} else {
|
|
|
|
dwSeqLen = midiStream.readUint32LE();
|
|
|
|
|
|
|
|
// make sure buffer is large enough for this sequence
|
|
|
|
assert(dwSeqLen > 0 && dwSeqLen <= g_midiBuffer.size);
|
|
|
|
|
|
|
|
// stop any currently playing tune
|
|
|
|
_vm->_midiMusic->stop();
|
|
|
|
|
|
|
|
// read the sequence. This needs to be read again before playSEQ() is
|
|
|
|
// called even if the music is restarting, as playSEQ() reads the file
|
|
|
|
// name off the buffer itself. However, that function adds SMF headers
|
|
|
|
// to the buffer, thus if it's read again, the SMF headers will be read
|
|
|
|
// and the filename will always be 'MThd'.
|
|
|
|
if (midiStream.read(g_midiBuffer.pDat, dwSeqLen) != dwSeqLen)
|
|
|
|
error(FILE_IS_CORRUPT, MIDI_FILE);
|
|
|
|
|
|
|
|
// WORKAROUND for bug #2820054 "DW1: No intro music at first start on Wii",
|
|
|
|
// which actually affects all ports, since it's specific to the GRA version.
|
|
|
|
//
|
|
|
|
// The GRA version does not seem to set the channel volume at all for the first
|
|
|
|
// intro track, thus we need to do that here. We only initialize the channels
|
|
|
|
// used in that sequence. And we are using 127 as default channel volume.
|
|
|
|
//
|
|
|
|
// Only in the GRA version dwFileOffset can be "38888", just to be sure, we
|
|
|
|
// check for the SCN files feature flag not being set though.
|
|
|
|
if (_vm->getGameID() == GID_DW1 && dwFileOffset == 38888 && !(_vm->getFeatures() & GF_SCNFILES)) {
|
|
|
|
_vm->_midiMusic->send(0x7F07B0 | 3);
|
|
|
|
_vm->_midiMusic->send(0x7F07B0 | 5);
|
|
|
|
_vm->_midiMusic->send(0x7F07B0 | 8);
|
|
|
|
_vm->_midiMusic->send(0x7F07B0 | 10);
|
|
|
|
_vm->_midiMusic->send(0x7F07B0 | 13);
|
|
|
|
}
|
2012-07-08 22:31:25 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
_vm->_midiMusic->playMIDI(dwSeqLen, bLoop);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
midiStream.close();
|
2012-07-08 22:31:25 +00:00
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE if a Midi tune is currently playing.
|
|
|
|
*/
|
2009-11-02 21:54:57 +00:00
|
|
|
bool MidiPlaying() {
|
2009-01-12 14:56:29 +00:00
|
|
|
if (_vm->getFeatures() & GF_ENHANCED_AUDIO_SUPPORT) {
|
2010-06-15 04:13:12 +00:00
|
|
|
if (g_system->getAudioCDManager()->isPlaying())
|
2009-01-12 14:56:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-01 20:35:36 +00:00
|
|
|
return _vm->_midiMusic->isPlaying();
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops any currently playing midi.
|
|
|
|
*/
|
2009-11-02 21:54:57 +00:00
|
|
|
bool StopMidi() {
|
2012-02-22 22:14:29 +00:00
|
|
|
g_currentMidi = 0;
|
|
|
|
g_currentLoop = false;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2009-01-12 14:56:29 +00:00
|
|
|
if (_vm->getFeatures() & GF_ENHANCED_AUDIO_SUPPORT) {
|
2010-06-15 04:13:12 +00:00
|
|
|
g_system->getAudioCDManager()->stop();
|
2009-01-12 14:56:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
_vm->_midiMusic->stop();
|
2008-07-23 09:02:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the volume of the MIDI music.
|
|
|
|
*/
|
|
|
|
int GetMidiVolume() {
|
2009-11-02 21:56:29 +00:00
|
|
|
return _vm->_config->_musicVolume;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the volume of the MIDI music.
|
|
|
|
* @param vol New volume - 0..MAXMIDIVOL
|
|
|
|
*/
|
2009-11-09 22:19:54 +00:00
|
|
|
void SetMidiVolume(int vol) {
|
2008-09-19 13:58:09 +00:00
|
|
|
assert(vol >= 0 && vol <= Audio::Mixer::kMaxChannelVolume);
|
2012-07-15 14:37:55 +00:00
|
|
|
_vm->_midiMusic->setVolume(vol);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens and inits all MIDI sequence files.
|
|
|
|
*/
|
2009-11-02 21:54:57 +00:00
|
|
|
void OpenMidiFiles() {
|
2008-07-23 09:02:47 +00:00
|
|
|
Common::File midiStream;
|
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
if (TinselV0) {
|
|
|
|
// The early demo version of DW1 doesn't have MIDI
|
|
|
|
} else if (TinselV2) {
|
|
|
|
// DW2 uses a different music mechanism
|
|
|
|
} else if (TinselV1Mac) {
|
|
|
|
// open MIDI sequence file in binary mode
|
|
|
|
if (!midiStream.open(MIDI_FILE))
|
|
|
|
error(CANNOT_FIND_FILE, MIDI_FILE);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
uint32 curTrack = 1;
|
|
|
|
uint32 songLength = 0;
|
|
|
|
int32 fileSize = midiStream.size();
|
2012-12-01 19:53:33 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
// Init
|
|
|
|
for (int i = 0; i < ARRAYSIZE(g_midiOffsets); i++)
|
|
|
|
g_midiOffsets[i] = 0;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
midiStream.skip(4); // skip file header
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
while (!midiStream.eos() && !midiStream.err() && midiStream.pos() != fileSize) {
|
|
|
|
assert(curTrack < ARRAYSIZE(g_midiOffsets));
|
|
|
|
g_midiOffsets[curTrack] = midiStream.pos();
|
|
|
|
//debug("%d: %d", curTrack, g_midiOffsets[curTrack]);
|
|
|
|
|
|
|
|
songLength = midiStream.readUint32BE();
|
|
|
|
midiStream.skip(songLength);
|
|
|
|
|
|
|
|
curTrack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
midiStream.close();
|
|
|
|
} else {
|
|
|
|
if (g_midiBuffer.pDat)
|
|
|
|
// already allocated
|
|
|
|
return;
|
|
|
|
|
|
|
|
// open MIDI sequence file in binary mode
|
|
|
|
if (!midiStream.open(MIDI_FILE))
|
|
|
|
error(CANNOT_FIND_FILE, MIDI_FILE);
|
|
|
|
|
|
|
|
// get length of the largest sequence
|
|
|
|
g_midiBuffer.size = midiStream.readUint32LE();
|
|
|
|
if (midiStream.eos() || midiStream.err())
|
|
|
|
error(FILE_IS_CORRUPT, MIDI_FILE);
|
|
|
|
|
|
|
|
if (g_midiBuffer.size) {
|
|
|
|
// allocate a buffer big enough for the largest MIDI sequence
|
|
|
|
if ((g_midiBuffer.pDat = (uint8 *)malloc(g_midiBuffer.size)) != NULL) {
|
|
|
|
// clear out the buffer
|
|
|
|
memset(g_midiBuffer.pDat, 0, g_midiBuffer.size);
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
// Now scan through the contents of the MIDI file to find the offset
|
|
|
|
// of each individual track, in order to create a mapping from MIDI
|
|
|
|
// offset to track number, for the enhanced MIDI soundtrack.
|
|
|
|
// The first song is always at position 4. The subsequent ones are
|
|
|
|
// calculated dynamically.
|
|
|
|
uint32 curOffset = 4;
|
|
|
|
uint32 curTrack = 0;
|
|
|
|
uint32 songLength = 0;
|
2010-08-24 12:27:22 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
// Init
|
|
|
|
for (int i = 0; i < ARRAYSIZE(g_midiOffsets); i++)
|
|
|
|
g_midiOffsets[i] = 0;
|
2010-08-24 12:45:19 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
while (!midiStream.eos() && !midiStream.err()) {
|
|
|
|
if (curOffset + (4 * curTrack) >= (uint32)midiStream.size())
|
|
|
|
break;
|
2010-08-24 13:31:44 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
assert(curTrack < ARRAYSIZE(g_midiOffsets));
|
|
|
|
g_midiOffsets[curTrack] = curOffset + (4 * curTrack);
|
|
|
|
//debug("%d: %d", curTrack, midiOffsets[curTrack]);
|
2010-08-24 12:27:22 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
songLength = midiStream.readUint32LE();
|
|
|
|
curOffset += songLength;
|
|
|
|
midiStream.skip(songLength);
|
2010-08-24 12:27:22 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
curTrack++;
|
|
|
|
}
|
2010-08-24 12:27:22 +00:00
|
|
|
|
2013-04-12 16:28:20 +00:00
|
|
|
midiStream.close();
|
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteMidiBuffer() {
|
2012-02-22 22:14:29 +00:00
|
|
|
free(g_midiBuffer.pDat);
|
|
|
|
g_midiBuffer.pDat = NULL;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 15:41:46 +00:00
|
|
|
MidiMusicPlayer::MidiMusicPlayer() {
|
2011-03-28 16:06:24 +00:00
|
|
|
MidiPlayer::createDriver();
|
2011-03-23 15:14:39 +00:00
|
|
|
|
|
|
|
int ret = _driver->open();
|
|
|
|
if (ret == 0) {
|
2011-03-25 13:10:02 +00:00
|
|
|
if (_nativeMT32)
|
2011-03-23 15:14:39 +00:00
|
|
|
_driver->sendMT32Reset();
|
|
|
|
else
|
|
|
|
_driver->sendGMReset();
|
|
|
|
|
2011-03-25 13:10:02 +00:00
|
|
|
_driver->setTimerCallback(this, &timerCallback);
|
2011-03-23 15:14:39 +00:00
|
|
|
}
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
void MidiMusicPlayer::setVolume(int volume) {
|
2008-07-23 09:02:47 +00:00
|
|
|
_vm->_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, volume);
|
|
|
|
|
2011-03-24 15:41:46 +00:00
|
|
|
Audio::MidiPlayer::setVolume(volume);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
void MidiMusicPlayer::send(uint32 b) {
|
2011-03-24 15:41:46 +00:00
|
|
|
Audio::MidiPlayer::send(b);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2011-03-24 15:41:46 +00:00
|
|
|
byte channel = (byte)(b & 0x0F);
|
|
|
|
if (_channelsTable[channel]) {
|
2008-07-23 09:02:47 +00:00
|
|
|
if ((b & 0xFFF0) == 0x0079B0) {
|
|
|
|
// We've just Reset All Controllers, so we need to
|
|
|
|
// re-adjust the volume. Otherwise, volume is reset to
|
|
|
|
// default whenever the music changes.
|
2011-03-24 15:41:46 +00:00
|
|
|
_channelsTable[channel]->send(0x000007B0 | (((_channelsVolume[channel] * _masterVolume) / 255) << 16) | channel);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 16:18:10 +00:00
|
|
|
void MidiMusicPlayer::playMIDI(uint32 size, bool loop) {
|
2011-03-25 12:00:37 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
if (_isPlaying)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stop();
|
|
|
|
|
2012-05-03 16:18:10 +00:00
|
|
|
if (TinselV1PSX)
|
|
|
|
playSEQ(size, loop);
|
|
|
|
else
|
|
|
|
playXMIDI(size, loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiMusicPlayer::playXMIDI(uint32 size, bool loop) {
|
2008-07-23 09:02:47 +00:00
|
|
|
// It seems like not all music (the main menu music, for instance) set
|
|
|
|
// all the instruments explicitly. That means the music will sound
|
|
|
|
// different, depending on which music played before it. This appears
|
|
|
|
// to be a genuine glitch in the original. For consistency, reset all
|
|
|
|
// instruments to the default one (piano).
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
_driver->send(0xC0 | i, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load XMID resource data
|
|
|
|
|
2011-03-25 12:00:37 +00:00
|
|
|
MidiParser *parser = MidiParser::createParser_XMIDI();
|
2012-05-03 16:18:10 +00:00
|
|
|
if (parser->loadMusic(g_midiBuffer.pDat, size)) {
|
|
|
|
parser->setTrack(0);
|
|
|
|
parser->setMidiDriver(this);
|
|
|
|
parser->setTimerRate(getBaseTempo());
|
|
|
|
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
|
|
|
|
parser->property(MidiParser::mpSendSustainOffOnNotesOff, 1);
|
|
|
|
|
|
|
|
_parser = parser;
|
|
|
|
|
|
|
|
_isLooping = loop;
|
|
|
|
_isPlaying = true;
|
|
|
|
} else {
|
|
|
|
delete parser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiMusicPlayer::playSEQ(uint32 size, bool loop) {
|
|
|
|
// MIDI.DAT holds the file names in DW1 PSX
|
|
|
|
Common::String baseName((char *)g_midiBuffer.pDat, size);
|
|
|
|
Common::String seqName = baseName + ".SEQ";
|
|
|
|
|
|
|
|
// TODO: Load the instrument bank (<baseName>.VB and <baseName>.VH)
|
|
|
|
|
|
|
|
Common::File seqFile;
|
|
|
|
if (!seqFile.open(seqName))
|
|
|
|
error("Failed to open SEQ file '%s'", seqName.c_str());
|
|
|
|
|
|
|
|
if (seqFile.readUint32LE() != MKTAG('S', 'E', 'Q', 'p'))
|
|
|
|
error("Failed to find SEQp tag");
|
|
|
|
|
|
|
|
// Make sure we don't have a SEP file (with multiple SEQ's inside)
|
|
|
|
if (seqFile.readUint32BE() != 1)
|
|
|
|
error("Can only play SEQ files, not SEP");
|
|
|
|
|
|
|
|
uint16 ppqn = seqFile.readUint16BE();
|
|
|
|
uint32 tempo = seqFile.readUint16BE() << 8;
|
|
|
|
tempo |= seqFile.readByte();
|
|
|
|
/* uint16 beat = */ seqFile.readUint16BE();
|
|
|
|
|
|
|
|
// SEQ is directly based on SMF and we'll use that to our advantage here
|
|
|
|
// and convert to SMF and then use the SMF MidiParser.
|
|
|
|
|
|
|
|
// Calculate the SMF size we'll need
|
|
|
|
uint32 dataSize = seqFile.size() - 15;
|
|
|
|
uint32 actualSize = dataSize + 7 + 22;
|
|
|
|
|
|
|
|
// Resize the buffer if necessary
|
|
|
|
if (g_midiBuffer.size < actualSize) {
|
|
|
|
g_midiBuffer.pDat = (byte *)realloc(g_midiBuffer.pDat, actualSize);
|
|
|
|
assert(g_midiBuffer.pDat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now construct the header
|
|
|
|
WRITE_BE_UINT32(g_midiBuffer.pDat, MKTAG('M', 'T', 'h', 'd'));
|
|
|
|
WRITE_BE_UINT32(g_midiBuffer.pDat + 4, 6); // header size
|
|
|
|
WRITE_BE_UINT16(g_midiBuffer.pDat + 8, 0); // type 0
|
|
|
|
WRITE_BE_UINT16(g_midiBuffer.pDat + 10, 1); // one track
|
|
|
|
WRITE_BE_UINT16(g_midiBuffer.pDat + 12, ppqn);
|
|
|
|
WRITE_BE_UINT32(g_midiBuffer.pDat + 14, MKTAG('M', 'T', 'r', 'k'));
|
|
|
|
WRITE_BE_UINT32(g_midiBuffer.pDat + 18, dataSize + 7); // SEQ data size + tempo change event size
|
|
|
|
|
|
|
|
// Add in a fake tempo change event
|
|
|
|
WRITE_BE_UINT32(g_midiBuffer.pDat + 22, 0x00FF5103); // no delta, meta event, tempo change, param size = 3
|
|
|
|
WRITE_BE_UINT16(g_midiBuffer.pDat + 26, tempo >> 8);
|
|
|
|
g_midiBuffer.pDat[28] = tempo & 0xFF;
|
|
|
|
|
|
|
|
// Now copy in the rest of the events
|
|
|
|
seqFile.read(g_midiBuffer.pDat + 29, dataSize);
|
|
|
|
seqFile.close();
|
|
|
|
|
|
|
|
MidiParser *parser = MidiParser::createParser_SMF();
|
|
|
|
if (parser->loadMusic(g_midiBuffer.pDat, actualSize)) {
|
2008-07-23 09:02:47 +00:00
|
|
|
parser->setTrack(0);
|
|
|
|
parser->setMidiDriver(this);
|
|
|
|
parser->setTimerRate(getBaseTempo());
|
|
|
|
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
|
2011-02-02 23:27:59 +00:00
|
|
|
parser->property(MidiParser::mpSendSustainOffOnNotesOff, 1);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
_parser = parser;
|
|
|
|
|
2011-03-24 15:41:46 +00:00
|
|
|
_isLooping = loop;
|
2008-07-23 09:02:47 +00:00
|
|
|
_isPlaying = true;
|
2011-03-25 12:00:37 +00:00
|
|
|
} else {
|
|
|
|
delete parser;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
void MidiMusicPlayer::pause() {
|
2008-07-23 09:02:47 +00:00
|
|
|
setVolume(-1);
|
|
|
|
_isPlaying = false;
|
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
void MidiMusicPlayer::resume() {
|
2008-07-23 09:02:47 +00:00
|
|
|
setVolume(GetMidiVolume());
|
|
|
|
_isPlaying = true;
|
|
|
|
}
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
PCMMusicPlayer::PCMMusicPlayer() {
|
|
|
|
_silenceSamples = 0;
|
|
|
|
|
|
|
|
_curChunk = 0;
|
|
|
|
_state = S_IDLE;
|
|
|
|
_mState = S_IDLE;
|
|
|
|
_scriptNum = -1;
|
|
|
|
_scriptIndex = 0;
|
|
|
|
_forcePlay = false;
|
|
|
|
|
|
|
|
_volume = 255;
|
|
|
|
_dimmed = false;
|
|
|
|
_dimmedTinsel = false;
|
|
|
|
_dimIteration = 0;
|
|
|
|
|
|
|
|
_fadeOutVolume = 0;
|
|
|
|
_fadeOutIteration = 0;
|
|
|
|
|
|
|
|
_end = true;
|
|
|
|
|
2010-04-12 09:14:17 +00:00
|
|
|
_vm->_mixer->playStream(Audio::Mixer::kMusicSoundType,
|
2010-01-08 22:09:43 +00:00
|
|
|
&_handle, this, -1, _volume, 0, DisposeAfterUse::NO, true);
|
2008-12-01 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PCMMusicPlayer::~PCMMusicPlayer() {
|
|
|
|
_vm->_mixer->stopHandle(_handle);
|
2011-11-14 21:54:34 +00:00
|
|
|
delete _curChunk;
|
2008-12-01 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::startPlay(int id) {
|
2011-04-12 14:06:33 +00:00
|
|
|
if (_filename.empty())
|
2008-12-01 20:35:36 +00:00
|
|
|
return;
|
|
|
|
|
2011-04-12 14:06:33 +00:00
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "Playing PCM music %s, index %d", _filename.c_str(), id);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
Common::StackLock slock(_mutex);
|
|
|
|
|
|
|
|
stop();
|
|
|
|
|
|
|
|
_scriptNum = id;
|
|
|
|
_scriptIndex = 1;
|
|
|
|
_state = S_NEW;
|
|
|
|
|
|
|
|
play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::stopPlay() {
|
|
|
|
Common::StackLock slock(_mutex);
|
|
|
|
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PCMMusicPlayer::readBuffer(int16 *buffer, const int numSamples) {
|
|
|
|
Common::StackLock slock(_mutex);
|
|
|
|
|
|
|
|
if (!_curChunk && ((_state == S_IDLE) || (_state == S_STOP)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int samplesLeft = numSamples;
|
|
|
|
|
|
|
|
while (samplesLeft > 0) {
|
|
|
|
if (_silenceSamples > 0) {
|
|
|
|
int n = MIN(_silenceSamples, samplesLeft);
|
|
|
|
|
|
|
|
memset(buffer, 0, n);
|
|
|
|
|
|
|
|
buffer += n;
|
|
|
|
_silenceSamples -= n;
|
|
|
|
samplesLeft -= n;
|
|
|
|
|
|
|
|
} else if (_curChunk &&
|
|
|
|
((_state == S_MID) || (_state == S_NEXT) || (_state == S_NEW))) {
|
|
|
|
int n = _curChunk->readBuffer(buffer, samplesLeft);
|
|
|
|
|
|
|
|
buffer += n;
|
|
|
|
samplesLeft -= n;
|
|
|
|
|
|
|
|
if (_curChunk->endOfData()) {
|
|
|
|
_state = S_END1;
|
|
|
|
|
|
|
|
delete _curChunk;
|
|
|
|
_curChunk = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (!getNextChunk())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (numSamples - samplesLeft);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCMMusicPlayer::isPlaying() const {
|
|
|
|
return ((_state != S_IDLE) && (_state != S_STOP));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCMMusicPlayer::isDimmed() const {
|
|
|
|
return _dimmed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::getTunePlaying(void *voidPtr, int length) {
|
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "getTunePlaying");
|
|
|
|
|
|
|
|
assert(length == (3 * sizeof(int32)));
|
|
|
|
|
|
|
|
int32 *p = (int32 *) voidPtr;
|
|
|
|
|
|
|
|
_mState = _state;
|
|
|
|
|
|
|
|
p[0] = (int32) _mState;
|
|
|
|
p[1] = _scriptNum;
|
|
|
|
p[2] = _scriptIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::restoreThatTune(void *voidPtr) {
|
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "restoreThatTune");
|
|
|
|
|
|
|
|
int32 *p = (int32 *) voidPtr;
|
|
|
|
|
|
|
|
_mState = (State) p[0];
|
|
|
|
_scriptNum = p[1];
|
|
|
|
_scriptIndex = p[2];
|
|
|
|
|
|
|
|
if (_mState != S_IDLE)
|
|
|
|
_state = S_NEW;
|
|
|
|
|
|
|
|
delete _curChunk;
|
|
|
|
_curChunk = 0;
|
|
|
|
|
|
|
|
_end = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::setMusicSceneDetails(SCNHANDLE hScript,
|
|
|
|
SCNHANDLE hSegment, const char *fileName) {
|
|
|
|
|
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
|
|
|
stop();
|
|
|
|
|
|
|
|
debugC(DEBUG_INTERMEDIATE, kTinselDebugMusic, "Setting music scene details: %s", fileName);
|
|
|
|
|
|
|
|
_hScript = hScript;
|
|
|
|
_hSegment = hSegment;
|
2011-04-12 14:06:33 +00:00
|
|
|
_filename = fileName;
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
// Start scene with music not dimmed
|
|
|
|
_dimmed = false;
|
|
|
|
_dimmedTinsel = false;
|
|
|
|
_dimIteration = 0;
|
|
|
|
setVol(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::setVolume(int volume) {
|
|
|
|
assert((volume >= 0) && (volume <= 100));
|
|
|
|
|
|
|
|
_dimmed = false;
|
|
|
|
setVol((volume * 255) / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::setVol(uint8 volume) {
|
|
|
|
_volume = volume;
|
|
|
|
|
|
|
|
_vm->_mixer->setChannelVolume(_handle, _volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCMMusicPlayer::getMusicTinselDimmed() const {
|
|
|
|
return _dimmedTinsel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::dim(bool bTinselDim) {
|
|
|
|
if (_dimmed || (_volume == 0) ||
|
|
|
|
(_state == S_IDLE) || !_curChunk || (SysVar(SV_MUSICDIMFACTOR) == 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_dimmed = true;
|
|
|
|
if (bTinselDim)
|
|
|
|
_dimmedTinsel = true;
|
|
|
|
|
|
|
|
_dimmedVolume = _volume - (_volume / SysVar(SV_MUSICDIMFACTOR));
|
|
|
|
|
|
|
|
// Iterate down, negative iteration
|
|
|
|
if (!_dimIteration)
|
|
|
|
_dimPosition = _volume;
|
|
|
|
_dimIteration = (_dimmedVolume - _volume)/DIM_SPEED;
|
|
|
|
|
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "Dimming music from %d to %d, steps %d", _dimPosition, _dimmedVolume, _dimIteration);
|
|
|
|
|
|
|
|
// And SFX
|
|
|
|
if (SysVar(SYS_SceneFxDimFactor))
|
|
|
|
_vm->_sound->setSFXVolumes(255 - 255/SysVar(SYS_SceneFxDimFactor));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::unDim(bool bTinselUnDim) {
|
|
|
|
if (!_dimmed || (_dimmedTinsel && !bTinselUnDim))
|
|
|
|
return; // not dimmed
|
|
|
|
|
|
|
|
_dimmed = _dimmedTinsel = false;
|
|
|
|
|
|
|
|
if ((_volume == 0) || (_state == S_IDLE) || !_curChunk)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Iterate up, positive iteration
|
|
|
|
if (!_dimIteration)
|
|
|
|
_dimPosition = _dimmedVolume;
|
|
|
|
_dimIteration = (_volume - _dimmedVolume)/DIM_SPEED;
|
|
|
|
|
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "UnDimming music from %d to %d, steps %d", _dimPosition, _volume, _dimIteration);
|
|
|
|
|
|
|
|
// And SFX
|
|
|
|
_vm->_sound->setSFXVolumes(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::dimIteration() {
|
|
|
|
if (_dimIteration != 0)
|
|
|
|
{
|
|
|
|
_dimPosition += _dimIteration;
|
|
|
|
if (_dimPosition >= _volume)
|
|
|
|
{
|
|
|
|
_dimPosition = _volume;
|
|
|
|
_dimIteration = 0;
|
|
|
|
}
|
|
|
|
else if (_dimPosition <= _dimmedVolume)
|
|
|
|
{
|
|
|
|
_dimPosition = _dimmedVolume;
|
|
|
|
_dimIteration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_vm->_mixer->setChannelVolume(_handle, _dimPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::startFadeOut(int ticks) {
|
|
|
|
if ((_volume == 0) || (_state == S_IDLE) || !_curChunk)
|
|
|
|
return;
|
|
|
|
|
|
|
|
debugC(DEBUG_INTERMEDIATE, kTinselDebugMusic, "Fading out music...");
|
|
|
|
|
|
|
|
if (_dimmed) {
|
|
|
|
// Start from dimmed volume and go from there
|
|
|
|
_dimmed = false;
|
|
|
|
_fadeOutVolume = _volume - _volume/SysVar(SV_MUSICDIMFACTOR);
|
|
|
|
} else
|
|
|
|
_fadeOutVolume = _volume;
|
|
|
|
|
|
|
|
assert(ticks != 0);
|
|
|
|
_fadeOutIteration = _fadeOutVolume / ticks;
|
|
|
|
|
|
|
|
fadeOutIteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::fadeOutIteration() {
|
|
|
|
if ((_volume == 0) || (_state == S_IDLE) || !_curChunk)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_fadeOutVolume = CLIP<int>(_fadeOutVolume -= _fadeOutIteration, 0, 255);
|
|
|
|
|
|
|
|
_vm->_mixer->setChannelVolume(_handle, _fadeOutVolume);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCMMusicPlayer::getNextChunk() {
|
|
|
|
MusicSegment *musicSegments;
|
|
|
|
int32 *script, *scriptBuffer;
|
|
|
|
int id;
|
|
|
|
int snum;
|
|
|
|
uint32 sampleOffset, sampleLength, sampleCLength;
|
2009-01-27 00:41:41 +00:00
|
|
|
Common::File file;
|
2008-12-01 20:35:36 +00:00
|
|
|
byte *buffer;
|
2010-11-19 17:03:07 +00:00
|
|
|
Common::SeekableReadStream *sampleStream;
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
switch (_state) {
|
|
|
|
case S_NEW:
|
|
|
|
case S_NEXT:
|
|
|
|
_forcePlay = false;
|
|
|
|
|
2009-01-27 00:41:41 +00:00
|
|
|
script = scriptBuffer = (int32 *)LockMem(_hScript);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
// Set parameters for this chunk of music
|
|
|
|
id = _scriptNum;
|
2009-01-29 05:26:12 +00:00
|
|
|
while (id--)
|
2012-12-11 00:56:48 +00:00
|
|
|
script = scriptBuffer + READ_32(script);
|
|
|
|
snum = FROM_32(script[_scriptIndex++]);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-01-27 00:41:41 +00:00
|
|
|
if (snum == MUSIC_JUMP || snum == MUSIC_END) {
|
2008-12-01 20:35:36 +00:00
|
|
|
// Let usual code sort it out!
|
|
|
|
_scriptIndex--; // Undo increment
|
|
|
|
_forcePlay = true; // Force a Play
|
|
|
|
_state = S_END1; // 'Goto' S_END1
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
musicSegments = (MusicSegment *) LockMem(_hSegment);
|
|
|
|
|
2012-12-11 00:56:48 +00:00
|
|
|
assert(FROM_32(musicSegments[snum].numChannels) == 1);
|
|
|
|
assert(FROM_32(musicSegments[snum].bitsPerSample) == 16);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2012-12-11 00:56:48 +00:00
|
|
|
sampleOffset = FROM_32(musicSegments[snum].sampleOffset);
|
|
|
|
sampleLength = FROM_32(musicSegments[snum].sampleLength);
|
2008-12-01 20:35:36 +00:00
|
|
|
sampleCLength = (((sampleLength + 63) & ~63)*33)/64;
|
|
|
|
|
2011-04-12 14:06:33 +00:00
|
|
|
if (!file.open(_filename))
|
|
|
|
error(CANNOT_FIND_FILE, _filename.c_str());
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-01-27 00:41:41 +00:00
|
|
|
file.seek(sampleOffset);
|
2009-12-15 08:18:57 +00:00
|
|
|
if (file.eos() || file.err() || (uint32)file.pos() != sampleOffset)
|
2011-04-12 14:06:33 +00:00
|
|
|
error(FILE_IS_CORRUPT, _filename.c_str());
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
buffer = (byte *) malloc(sampleCLength);
|
|
|
|
assert(buffer);
|
|
|
|
|
|
|
|
// read all of the sample
|
2009-01-27 00:41:41 +00:00
|
|
|
if (file.read(buffer, sampleCLength) != sampleCLength)
|
2011-04-12 14:06:33 +00:00
|
|
|
error(FILE_IS_CORRUPT, _filename.c_str());
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-03-19 19:17:29 +00:00
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "Creating ADPCM music chunk with size %d, "
|
|
|
|
"offset %d (script %d.%d)", sampleCLength, sampleOffset,
|
|
|
|
_scriptNum, _scriptIndex - 1);
|
|
|
|
|
2010-01-08 22:07:35 +00:00
|
|
|
sampleStream = new Common::MemoryReadStream(buffer, sampleCLength, DisposeAfterUse::YES);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
delete _curChunk;
|
2011-04-13 10:24:34 +00:00
|
|
|
_curChunk = new Tinsel8_ADPCMStream(sampleStream, DisposeAfterUse::YES, sampleCLength,
|
|
|
|
22050, 1, 32);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
|
|
|
_state = S_MID;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case S_END1:
|
2009-03-19 19:17:29 +00:00
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "Music reached state S_END1 (script %d.%d)",
|
|
|
|
_scriptNum, _scriptIndex);
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
script = scriptBuffer = (int32 *) LockMem(_hScript);
|
|
|
|
|
|
|
|
id = _scriptNum;
|
2009-01-29 05:26:12 +00:00
|
|
|
while (id--)
|
2012-12-11 00:56:48 +00:00
|
|
|
script = scriptBuffer + READ_32(script);
|
|
|
|
snum = FROM_32(script[_scriptIndex]);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-02-02 18:32:36 +00:00
|
|
|
if (snum == MUSIC_END) {
|
2008-12-01 20:35:36 +00:00
|
|
|
_state = S_END2;
|
2009-02-02 18:32:36 +00:00
|
|
|
} else {
|
|
|
|
if (snum == MUSIC_JUMP)
|
2012-12-11 00:56:48 +00:00
|
|
|
_scriptIndex = FROM_32(script[_scriptIndex+1]);
|
2008-12-01 20:35:36 +00:00
|
|
|
|
2009-02-02 18:32:36 +00:00
|
|
|
_state = _forcePlay ? S_NEW : S_NEXT;
|
2008-12-01 20:35:36 +00:00
|
|
|
_forcePlay = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case S_END2:
|
2009-03-19 19:17:29 +00:00
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "Music reached state S_END2 (script %d.%d)",
|
|
|
|
_scriptNum, _scriptIndex);
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
_silenceSamples = 11025; // Half a second of silence
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case S_END3:
|
2009-03-19 19:17:29 +00:00
|
|
|
debugC(DEBUG_DETAILED, kTinselDebugMusic, "Music reached state S_END3 (script %d.%d)",
|
|
|
|
_scriptNum, _scriptIndex);
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
stop();
|
|
|
|
_state = S_IDLE;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case S_IDLE:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::play() {
|
|
|
|
if (_curChunk)
|
|
|
|
return;
|
|
|
|
if (_scriptNum == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_end = false;
|
|
|
|
|
|
|
|
getNextChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCMMusicPlayer::stop() {
|
|
|
|
delete _curChunk;
|
|
|
|
_curChunk = 0;
|
|
|
|
_scriptNum = -1;
|
|
|
|
_state = S_IDLE;
|
|
|
|
_mState = S_IDLE;
|
|
|
|
|
|
|
|
_end = true;
|
|
|
|
}
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
void CurrentMidiFacts(SCNHANDLE *pMidi, bool *pLoop) {
|
2012-02-22 22:14:29 +00:00
|
|
|
*pMidi = g_currentMidi;
|
|
|
|
*pLoop = g_currentLoop;
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RestoreMidiFacts(SCNHANDLE Midi, bool Loop) {
|
|
|
|
StopMidi();
|
|
|
|
|
2012-02-22 22:14:29 +00:00
|
|
|
g_currentMidi = Midi;
|
|
|
|
g_currentLoop = Loop;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2012-07-15 14:37:55 +00:00
|
|
|
bool mute = false;
|
|
|
|
if (ConfMan.hasKey("mute"))
|
|
|
|
mute = ConfMan.getBool("mute");
|
2010-09-11 05:28:54 +00:00
|
|
|
|
2012-07-15 14:37:55 +00:00
|
|
|
PlayMidiSequence(g_currentMidi, true);
|
|
|
|
SetMidiVolume(mute ? 0 : _vm->_config->_musicVolume);
|
2008-07-23 09:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Dumps all of the game's music in external XMIDI *.xmi files
|
|
|
|
void dumpMusic() {
|
|
|
|
Common::File midiFile;
|
2008-07-29 16:09:10 +00:00
|
|
|
Common::DumpFile outFile;
|
2008-07-23 09:02:47 +00:00
|
|
|
char outName[20];
|
|
|
|
midiFile.open(MIDI_FILE);
|
|
|
|
int outFileSize = 0;
|
|
|
|
char buffer[20000];
|
|
|
|
|
2010-08-24 12:45:19 +00:00
|
|
|
const int total = 155; // maximum (SCN version)
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < total; i++) {
|
2010-08-24 12:45:19 +00:00
|
|
|
if (midiOffsets[i] == 0)
|
|
|
|
break;
|
|
|
|
|
2008-07-23 09:02:47 +00:00
|
|
|
sprintf(outName, "track%03d.xmi", i + 1);
|
2008-07-29 16:09:10 +00:00
|
|
|
outFile.open(outName);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2010-08-24 12:45:19 +00:00
|
|
|
if (i < total - 1)
|
|
|
|
outFileSize = midiOffsets[i + 1] - midiOffsets[i] - 4;
|
|
|
|
else
|
|
|
|
outFileSize = midiFile.size() - midiOffsets[i] - 4;
|
2008-07-23 09:02:47 +00:00
|
|
|
|
2010-08-24 12:45:19 +00:00
|
|
|
midiFile.seek(midiOffsets[i] + 4, SEEK_SET);
|
2008-07-23 09:02:47 +00:00
|
|
|
|
|
|
|
assert(outFileSize < 20000);
|
|
|
|
midiFile.read(buffer, outFileSize);
|
|
|
|
outFile.write(buffer, outFileSize);
|
|
|
|
|
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
midiFile.close();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-01 20:35:36 +00:00
|
|
|
} // End of namespace Tinsel
|