aboutsummaryrefslogtreecommitdiff
path: root/backends/midi/mt32/mt32.cpp
blob: 9faaa923ad7f622a361e73c3eb307fcbec19a331 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001-2004 The ScummVM project
 *
 * YM2612 tone generation code written by Tomoaki Hayasaka.
 * Used under the terms of the GNU General Public License.
 * Adpated to ScummVM by Jamieson Christian.
 *
 * 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 "backends/midi/emumidi.h"

#include "common/util.h"
#include "common/file.h"

#include "backends/midi/mt32/synth.h"

class MidiDriver_MT32 : public MidiDriver_Emulated {
private:
	CSynthMT32 *_synth;

	int _outputRate;

protected:
	void generate_samples(int16 *buf, int len);

public:
	MidiDriver_MT32(SoundMixer *mixer, const char *path);
	virtual ~MidiDriver_MT32();

	int open();
	void close();
	void send(uint32 b);
	uint32 property(int prop, uint32 param) { return 0; }

	void setPitchBendRange(byte channel, uint range) { }
	void sysEx(byte *msg, uint16 length);

	MidiChannel *allocateChannel() { return 0; }
	MidiChannel *getPercussionChannel() { return 0; }


	// AudioStream API
	bool isStereo() const { return true; }
	int getRate() const { return _outputRate; }
};


////////////////////////////////////////
//
// MidiDriver_MT32
//
////////////////////////////////////////


MidiDriver_MT32::MidiDriver_MT32(SoundMixer *mixer, const char *path)
	: MidiDriver_Emulated(mixer) {
	File fp;

	_synth = new CSynthMT32();
	File::addDefaultDirectory(path);

	_baseFreq = 1000;

	fp.open("waveforms.raw");

	if(!fp.isOpen()) {
		error("Unable to open waveforms.raw");
	}

	switch(fp.size()) {
	case 1410000:
		_outputRate = 32000;
		break;
	case 1944040:
		_outputRate = 44100;
		break;
	default:
		error("MT-32: Unknown waveforms.raw file sample rate");
	}
	fp.close();
}

MidiDriver_MT32::~MidiDriver_MT32() {
	delete _synth;
}

int MidiDriver_MT32::open() {
	SynthProperties prop;

	if (_isOpen)
		return MERR_ALREADY_OPEN;

	MidiDriver_Emulated::open();
	
	prop.SampleRate = getRate(); // 32000;
	prop.UseReverb = true;
	prop.UseDefault = true;
	//prop.RevType = 0;
	//prop.RevTime = 5;
	//prop.RevLevel = 3;

	_synth->ClassicOpen(prop);

	_mixer->setupPremix(this);

	return 0;
}

void MidiDriver_MT32::send(uint32 b) {
	_synth->PlayMsg(b);
}

void MidiDriver_MT32::sysEx(byte *msg, uint16 length) {
	_synth->PlaySysex(msg, length);
}

void MidiDriver_MT32::close() {
	if (!_isOpen)
		return;
	_isOpen = false;

	// Detach the premix callback handler
	_mixer->setupPremix(0);

	_synth->Close();
}

void MidiDriver_MT32::generate_samples(int16 *data, int len) {
	_synth->MT32_CallBack((uint8 *)data, len, _mixer->getMusicVolume());
}


////////////////////////////////////////
//
// MidiDriver_MT32 factory
//
////////////////////////////////////////

MidiDriver *MidiDriver_MT32_create(SoundMixer *mixer, const char *path) {
	return new MidiDriver_MT32(mixer, path);
}