aboutsummaryrefslogtreecommitdiff
path: root/engines/teenagent/music.cpp
blob: 795b8f7312f1c04038164c15cf65ca5f0c1a596b (plain)
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
/* 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.
 *
 */

#include "teenagent/music.h"
#include "teenagent/resources.h"
#include "teenagent/teenagent.h"

#include "common/debug.h"
#include "common/ptr.h"
#include "common/textconsole.h"

namespace TeenAgent {

static const uint32 noteToPeriod[3][12] = {
	{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}
};

MusicPlayer::MusicPlayer(TeenAgentEngine *vm) : Paula(false, 44100, 5000), _vm(vm), _id(0), _currRow(0) {
}

MusicPlayer::~MusicPlayer() {
	stop();
}

bool MusicPlayer::load(int id) {
	debugC(0, kDebugMusic, "MusicPlayer::load(%d)", id);
	Common::ScopedPtr<Common::SeekableReadStream> stream(_vm->res->mmm.getStream(id));
	if (!stream)
		return false;

	char header[4];
	stream->read(header, 4);
	// check header?

	Common::StackLock lock(_mutex);
	// Load the samples
	byte sampleCount = stream->readByte();

	debugC(0, kDebugMusic, "sampleCount = %d", sampleCount);

	for (byte currSample = 0; currSample < sampleCount; currSample++) {
		byte sample = stream->readByte();

		// Load the sample data
		byte sampleResource = ((sample >> 4) & 0x0f) * 10 + (sample & 0x0f);
		debugC(0, kDebugMusic, "currSample = %d, sample = 0x%02x, resource: %d", currSample, sample, sampleResource);
		uint32 sampleSize = _vm->res->sam_mmm.getSize(sampleResource);
		if (sampleSize == 0) {
			warning("load: invalid sample %d (0x%02x)", sample, sample);
			_samples[sample].clear();
			continue;
		}

		_samples[sample].resize(sampleSize);
		_vm->res->sam_mmm.read(sampleResource, _samples[sample].data, sampleSize);
	}

	// Load the music data
	_rows.clear();

	Row row;
	while (!stream->eos()) {

		byte cmd = stream->readByte();

		if (cmd < 0x40) {
			row.channels[0].note = cmd;
			row.channels[1].note = stream->readByte();
			row.channels[2].note = stream->readByte();
			_rows.push_back(row);
		} else if ((cmd & 0xf0) == 0x50) {
			byte sample = stream->readByte();
			debugC(1, kDebugMusic,  "%02x: set sample %02x", cmd, sample);
			row.channels[(cmd & 0x0f) - 1].sample = sample;
		} else if ((cmd & 0xf0) == 0x40) {
			byte vol = stream->readByte();
			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;
		} else {
			debugC(0, kDebugMusic, "unhandled music command %02x", cmd);
		}
	}
	_currRow = 0;
	_id = id;
	return true;
}

void MusicPlayer::start() {
	_currRow = 0;
	startPaula();
}

void MusicPlayer::stop() {
	stopPaula();
}

void MusicPlayer::interrupt() {
	if (_rows.empty())
		return;

	_currRow %= _rows.size();

	Row *row = &_rows[_currRow];
	for (int chn = 0; chn < 3; ++chn) {
		setChannelVolume(chn, row->channels[chn].volume);

		debugC(2, kDebugMusic, "row->channels[%d].volume = %d", chn, row->channels[chn].volume);

		byte sample = (row->channels[chn].sample);
		if (row->channels[chn].note != 0 && sample != 0) {

			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);

			byte note = row->channels[chn].note;
			if (_samples[sample].size == 0) {
				warning("interrupt: invalid sample %u (0x%02x)", sample, sample);
				continue;
			}

			setChannelData(chn, (const int8 *)_samples[sample].data, NULL, _samples[sample].size, 0);
			setChannelPeriod(chn, noteToPeriod[((note >> 4) & 0x0f) - 1][(note & 0x0f)]);
		}
	}

	debugC(2, kDebugMusic, "------------------------------------------------");

	++_currRow;
}

} // End of namespace TeenAgent