aboutsummaryrefslogtreecommitdiff
path: root/queen/music.cpp
blob: 2561b58c18be055aec90dc30f2957af18a952a7d (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003-2004 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "queen/music.h"
#include "queen/queen.h"
#include "queen/resource.h"

#include "sound/midiparser.h"

namespace Queen {

	MusicPlayer::MusicPlayer(MidiDriver *driver, byte *data, uint32 size) : _driver(driver), _isPlaying(false), _looping(false), _volume(255), _queuePos(0), _musicData(data), _musicDataSize(size) {
		memset(_channel, 0, sizeof(_channel));
		queueClear();
		_lastSong = 0;
		_parser = MidiParser::createParser_SMF();
		_parser->setMidiDriver(this);
		_parser->setTimerRate(_driver->getBaseTempo());
		
		_numSongs = READ_LE_UINT16(_musicData);
		this->open();
	}
	
	MusicPlayer::~MusicPlayer() {
		_driver->setTimerCallback(NULL, NULL);
		_parser->unloadMusic();
		this->close();
		delete _parser;
	}
	
	bool MusicPlayer::queueSong(uint16 songNum) {
		uint8 emptySlots = 0;
		for (int i = 0; i < MUSIC_QUEUE_SIZE; i++)
			if (!_songQueue[i])
				emptySlots++;
		
		if (!emptySlots)
			return false;
			
		_songQueue[MUSIC_QUEUE_SIZE - emptySlots] = songNum;
		return true;
	}
	
	void MusicPlayer::queueClear() {
		_lastSong = _songQueue[0];
		_queuePos = 0;
		memset(_songQueue, 0, sizeof(_songQueue));
	}
	
	int MusicPlayer::open() {
		// Don't ever call open without first setting the output driver!
		if (!_driver)
			return 255;
			
		int ret = _driver->open();
		if (ret)
			return ret;
		_driver->setTimerCallback(this, &onTimer);
		return 0;
	}
	
	void MusicPlayer::close() {
		stopMusic();
		if (_driver)
			_driver->close();
		_driver = 0;
	}
	
	void MusicPlayer::send(uint32 b) {
		byte channel = (byte)(b & 0x0F);
		if ((b & 0xFFF0) == 0x07B0) {
			// Adjust volume changes by master volume
			byte volume = (byte)((b >> 16) & 0x7F);
			_channelVolume[channel] = volume;
			//volume = volume * _masterVolume / 255;
			b = (b & 0xFF00FFFF) | (volume << 16);
		} 
		else if ((b & 0xFFF0) == 0x007BB0) {
			//Only respond to All Notes Off if this channel
			//has currently been allocated
			if (_channel[b & 0x0F])
				return;
		}
		
		
		if (!_channel[channel])
			_channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
		
		if (_channel[channel])
			_channel[channel]->send(b);
	}
	
	void MusicPlayer::metaEvent(byte type, byte *data, uint16 length) {
		//Only thing we care about is End of Track.
		if (type != 0x2F)
			return;
		
		if (_looping || _songQueue[1]) 
			playMusic();
		else
			stopMusic();
	}
	
	void MusicPlayer::onTimer(void *refCon) {
		MusicPlayer *music = (MusicPlayer *)refCon;
		if (music->_isPlaying)
			music->_parser->onTimer();
	}
	
	void MusicPlayer::playMusic() {
		if (!_queuePos && !_songQueue[_queuePos]) {
			debug(5, "MusicPlayer::playMusic - Music queue is empty!");
			return;
		}
		
		uint16 songNum = _songQueue[_queuePos];

		//Special type
		//2000: (songNum + 1) - repeat music from previous queue
		if (songNum == 1999) {
			songNum = _lastSong;
			queueClear();
			queueSong(songNum);
		}
		
		_parser->loadMusic(_musicData + songOffset(songNum), songLength(songNum));
		_parser->setTrack(0);	
		//debug(0, "Playing song %d [queue position: %d]", songNum, _queuePos);
		_isPlaying = true;
		queueUpdatePos();
	}
	
	void MusicPlayer::queueUpdatePos() {
		if (_queuePos < (MUSIC_QUEUE_SIZE - 1) && _songQueue[_queuePos + 1])
			_queuePos++;
		else
			_queuePos = 0;
	}
	
	void MusicPlayer::stopMusic() {
		_isPlaying = false;
		_parser->unloadMusic();
	}

	uint32 MusicPlayer::songOffset(uint16 songNum) {
		uint16 offsLo = READ_LE_UINT16(_musicData + (songNum * 4) + 2);
		uint16 offsHi = READ_LE_UINT16(_musicData + (songNum * 4) + 4);
		return (offsHi << 4) | offsLo;
	}

	uint32 MusicPlayer::songLength(uint16 songNum) {
		if (songNum < _numSongs)
			return (songOffset(songNum + 1) - songOffset(songNum));
		return (_musicDataSize - songOffset(songNum));
	}

	Music::Music(MidiDriver *driver, QueenEngine *vm) {
		if (vm->resource()->isDemo()) {
			_musicData = vm->resource()->loadFile("AQ8.RL", 0, NULL);
			_musicDataSize = vm->resource()->fileSize("AQ8.RL");
		} else {
			_musicData = vm->resource()->loadFile("AQ.RL", 0, NULL);
			_musicDataSize = vm->resource()->fileSize("AQ.RL");
		}
		
		_player = new MusicPlayer(driver, _musicData, _musicDataSize);
	}

	Music::~Music() {
		delete _player;
		delete[] _musicData;	
	}

	bool Music::queueSong(uint16 songNum) {
		// Work around bug in Roland music, note that these numbers are 'one-off'
		// from the original code
		if (/*isRoland && */ songNum == 88 || songNum == 89)
			songNum = 62;
			
		return _player->queueSong(songNum);
	}
	
	void Music::playSong(uint16 songNum) {
		_player->queueClear();
		_player->queueSong(songNum);
		_player->playMusic();				
	}

	void Music::stopSong() {
		return _player->stopMusic();
	}
	
} // End of namespace Queen