aboutsummaryrefslogtreecommitdiff
path: root/engines/sky/music/adlibmusic.cpp
blob: c13d6150ec07bbfadf0757793fffaa423ce7ae3d (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
/* 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 "common/endian.h"
#include "common/textconsole.h"

#include "sky/music/adlibmusic.h"
#include "sky/music/adlibchannel.h"
#include "audio/mixer.h"
#include "sky/sky.h"

namespace Sky {

AdLibMusic::AdLibMusic(Audio::Mixer *pMixer, Disk *pDisk) : MusicBase(pMixer, pDisk) {
	_driverFileBase = 60202;
	_sampleRate = pMixer->getOutputRate();

	_opl = OPL::Config::create();
	if (!_opl || !_opl->init())
		error("Failed to create OPL");

	_opl->start(new Common::Functor0Mem<void, AdLibMusic>(this, &AdLibMusic::onTimer), 50);
	_mixer->playStream(Audio::Mixer::kMusicSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);
}

AdLibMusic::~AdLibMusic() {
	_mixer->stopHandle(_soundHandle);
	delete _opl;
}

int AdLibMusic::readBuffer(int16 *data, const int numSamples) {
	return _opl->readBuffer(data, numSamples);
}

void AdLibMusic::onTimer() {
	if (_musicData != NULL)
		pollMusic();
}

void AdLibMusic::setupPointers() {
	if (SkyEngine::_systemVars.gameVersion == 109) {
		// disk demo uses a different AdLib driver version, some offsets have changed
		//_musicDataLoc = (_musicData[0x11CC] << 8) | _musicData[0x11CB];
		//_initSequence = _musicData + 0xEC8;

		_musicDataLoc = READ_LE_UINT16(_musicData + 0x1200);
		_initSequence = _musicData + 0xEFB;
	} else if (SkyEngine::_systemVars.gameVersion == 267) {
		_musicDataLoc = READ_LE_UINT16(_musicData + 0x11F7);
		_initSequence = _musicData + 0xE87;
	} else {
		_musicDataLoc = READ_LE_UINT16(_musicData + 0x1201);
		_initSequence = _musicData + 0xE91;
	}
}

void AdLibMusic::setupChannels(uint8 *channelData) {
	_numberOfChannels = channelData[0];
	channelData++;
	for (uint8 cnt = 0; cnt < _numberOfChannels; cnt++) {
		uint16 chDataStart = READ_LE_UINT16((uint16 *)channelData + cnt) + _musicDataLoc;
		_channels[cnt] = new AdLibChannel(_opl, _musicData, chDataStart);
	}
}

void AdLibMusic::startDriver() {
	uint16 cnt = 0;
	while (_initSequence[cnt] || _initSequence[cnt + 1]) {
		_opl->writeReg(_initSequence[cnt], _initSequence[cnt + 1]);
		cnt += 2;
	}
}

void AdLibMusic::setVolume(uint16 param) {
	_musicVolume = param;
	// FIXME: This is bad. There's no real volume control here.
	_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, 2 * param);
}

bool AdLibMusic::isStereo() const {
	return false;
}

bool AdLibMusic::endOfData() const {
	return false;
}

int AdLibMusic::getRate() const {
	return _sampleRate;
}

} // End of namespace Sky