2009-09-03 20:59:17 +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.
|
2014-02-18 01:34:25 +00:00
|
|
|
*
|
2009-09-03 20:59:17 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:25 +00:00
|
|
|
*
|
2009-09-03 20:59:17 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-09-04 20:08:33 +00:00
|
|
|
#include "teenagent/music.h"
|
|
|
|
#include "teenagent/resources.h"
|
2012-07-07 12:46:28 +00:00
|
|
|
#include "teenagent/teenagent.h"
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/debug.h"
|
2010-03-20 20:37:36 +00:00
|
|
|
#include "common/ptr.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2009-09-03 20:59:17 +00:00
|
|
|
|
2009-09-04 20:08:33 +00:00
|
|
|
namespace TeenAgent {
|
2009-09-03 20:59:17 +00:00
|
|
|
|
2009-09-04 20:10:32 +00:00
|
|
|
static const uint32 noteToPeriod[3][12] = {
|
2009-09-03 20:59:17 +00:00
|
|
|
{855, 807, 761, 720, 678, 640, 604, 569, 537, 508, 480, 453},
|
|
|
|
{428, 404, 381, 360, 338, 320, 301, 285, 269, 254, 239, 226},
|
|
|
|
{214, 201, 189, 179, 170, 160, 151, 143, 135, 127, 120, 113}
|
|
|
|
};
|
|
|
|
|
2012-07-07 12:46:28 +00:00
|
|
|
MusicPlayer::MusicPlayer(TeenAgentEngine *vm) : Paula(false, 44100, 5000), _vm(vm), _id(0) {
|
2009-09-03 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MusicPlayer::~MusicPlayer() {
|
2012-08-02 12:01:37 +00:00
|
|
|
stop();
|
2009-09-03 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MusicPlayer::load(int id) {
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(0, kDebugMusic, "MusicPlayer::load(%d)", id);
|
2012-07-07 12:46:28 +00:00
|
|
|
Common::ScopedPtr<Common::SeekableReadStream> stream(_vm->res->mmm.getStream(id));
|
2010-03-20 20:37:36 +00:00
|
|
|
if (!stream)
|
2009-09-03 20:59:17 +00:00
|
|
|
return false;
|
2009-09-15 08:54:06 +00:00
|
|
|
|
2009-09-03 20:59:17 +00:00
|
|
|
char header[4];
|
|
|
|
stream->read(header, 4);
|
2012-07-12 20:03:47 +00:00
|
|
|
// check header?
|
2009-09-15 08:54:06 +00:00
|
|
|
|
2011-10-26 22:56:32 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2009-09-03 20:59:17 +00:00
|
|
|
// Load the samples
|
|
|
|
sampleCount = stream->readByte();
|
|
|
|
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(0, kDebugMusic, "sampleCount = %d", sampleCount);
|
2009-09-03 20:59:17 +00:00
|
|
|
|
|
|
|
for (byte currSample = 0; currSample < sampleCount; currSample++) {
|
|
|
|
byte sample = stream->readByte();
|
|
|
|
|
|
|
|
// Load the sample data
|
2012-07-12 20:03:47 +00:00
|
|
|
byte sampleResource = ((sample >> 4) & 0x0f) * 10 + (sample & 0x0f);
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(0, kDebugMusic, "currSample = %d, sample = 0x%02x, resource: %d", currSample, sample, sampleResource);
|
2012-07-07 12:46:28 +00:00
|
|
|
uint32 sampleSize = _vm->res->sam_mmm.getSize(sampleResource);
|
2010-03-20 15:24:16 +00:00
|
|
|
if (sampleSize == 0) {
|
2009-09-03 20:59:17 +00:00
|
|
|
warning("load: invalid sample %d (0x%02x)", sample, sample);
|
2010-01-07 12:01:33 +00:00
|
|
|
_samples[sample].clear();
|
2009-09-03 20:59:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-15 08:54:06 +00:00
|
|
|
|
2010-01-07 12:01:33 +00:00
|
|
|
_samples[sample].resize(sampleSize);
|
2012-07-07 12:46:28 +00:00
|
|
|
_vm->res->sam_mmm.read(sampleResource, _samples[sample].data, sampleSize);
|
2009-09-03 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the music data
|
|
|
|
_rows.clear();
|
2009-09-15 08:54:06 +00:00
|
|
|
|
2009-09-03 20:59:17 +00:00
|
|
|
Row row;
|
|
|
|
while (!stream->eos()) {
|
|
|
|
|
|
|
|
byte cmd = stream->readByte();
|
|
|
|
|
2010-01-08 11:05:59 +00:00
|
|
|
if (cmd < 0x40) {
|
2009-09-03 20:59:17 +00:00
|
|
|
row.channels[0].note = cmd;
|
|
|
|
row.channels[1].note = stream->readByte();
|
|
|
|
row.channels[2].note = stream->readByte();
|
|
|
|
_rows.push_back(row);
|
2012-07-12 20:03:47 +00:00
|
|
|
} else if ((cmd & 0xf0) == 0x50) {
|
2010-01-08 11:05:59 +00:00
|
|
|
byte sample = stream->readByte();
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(1, kDebugMusic, "%02x: set sample %02x", cmd, sample);
|
2012-07-12 20:03:47 +00:00
|
|
|
row.channels[(cmd & 0x0f) - 1].sample = sample;
|
|
|
|
} else if ((cmd & 0xf0) == 0x40) {
|
2010-01-08 11:05:59 +00:00
|
|
|
byte vol = stream->readByte();
|
2012-07-12 20:03:47 +00:00
|
|
|
debugC(1, kDebugMusic, "%02x: set volume %02x -> %02x", cmd, row.channels[(cmd & 0x0f) - 1].volume, vol);
|
|
|
|
// channel volume 0x40 * music volume 0x40 mixed with high bytes
|
|
|
|
row.channels[(cmd & 0x0f) - 1].volume = vol * 16;
|
2010-01-08 11:05:59 +00:00
|
|
|
} else {
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(0, kDebugMusic, "unhandled music command %02x", cmd);
|
2009-09-03 20:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_currRow = 0;
|
|
|
|
_id = id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::start() {
|
|
|
|
_currRow = 0;
|
|
|
|
startPaula();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::stop() {
|
|
|
|
stopPaula();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::interrupt() {
|
2009-12-12 18:07:20 +00:00
|
|
|
if (_rows.empty())
|
|
|
|
return;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-09-03 20:59:17 +00:00
|
|
|
_currRow %= _rows.size();
|
2009-09-15 08:54:06 +00:00
|
|
|
|
2009-09-03 20:59:17 +00:00
|
|
|
Row *row = &_rows[_currRow];
|
|
|
|
for (int chn = 0; chn < 3; ++chn) {
|
|
|
|
setChannelVolume(chn, row->channels[chn].volume);
|
|
|
|
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(2, kDebugMusic, "row->channels[%d].volume = %d", chn, row->channels[chn].volume);
|
2009-09-03 20:59:17 +00:00
|
|
|
|
|
|
|
byte sample = (row->channels[chn].sample);
|
|
|
|
if (row->channels[chn].note != 0 && sample != 0) {
|
|
|
|
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(2, kDebugMusic, "row->channels[%d].note = %d", chn, row->channels[chn].note);
|
|
|
|
debugC(2, kDebugMusic, "row->channels[%d].sample = %d", chn, row->channels[chn].sample);
|
2009-09-03 20:59:17 +00:00
|
|
|
|
|
|
|
byte note = row->channels[chn].note;
|
|
|
|
if (_samples[sample].size == 0) {
|
|
|
|
warning("interrupt: invalid sample %u (0x%02x)", sample, sample);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-23 00:05:05 +00:00
|
|
|
setChannelData(chn, (const int8 *)_samples[sample].data, NULL, _samples[sample].size, 0);
|
2012-07-12 20:03:47 +00:00
|
|
|
setChannelPeriod(chn, noteToPeriod[((note >> 4) & 0x0f) - 1][(note & 0x0f)]);
|
2009-09-03 20:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-07 23:36:05 +00:00
|
|
|
debugC(2, kDebugMusic, "------------------------------------------------");
|
2009-09-03 20:59:17 +00:00
|
|
|
|
|
|
|
++_currRow;
|
|
|
|
}
|
2009-09-04 20:08:33 +00:00
|
|
|
|
|
|
|
} // End of namespace TeenAgent
|