aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/softseq/pcjr.cpp
blob: a80a188751d6b8940704aab79a5ec6685210b2fa (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "sci/sfx/softseq/mididriver.h"
#include "sci/sfx/softseq/pcjr.h"

namespace Sci {

#define FREQUENCY 44100
#define VOLUME_SHIFT 3

#define BASE_NOTE 129	// A10
#define BASE_OCTAVE 10	// A10, as I said

const static int freq_table[12] = { // A4 is 440Hz, halftone map is x |-> ** 2^(x/12)
	28160, // A10
	29834,
	31608,
	33488,
	35479,
	37589,
	39824,
	42192,
	44701,
	47359,
	50175,
	53159
};

static inline int get_freq(int note) {
	int halftone_delta = note - BASE_NOTE;
	int oct_diff = ((halftone_delta + BASE_OCTAVE * 12) / 12) - BASE_OCTAVE;
	int halftone_index = (halftone_delta + (12 * 100)) % 12 ;
	int freq = (!note) ? 0 : freq_table[halftone_index] / (1 << (-oct_diff));

	return freq;
}

void MidiDriver_PCJr::send(uint32 b) {
	byte command = b & 0xff;
	byte op1 = (b >> 8) & 0xff;
	byte op2 = (b >> 16) & 0xff;
	int i;
	int mapped_chan = -1;
	int chan_nr = command & 0xf;

	// First, test for channel having been assigned already
	if (_channels_assigned & (1 << chan_nr)) {
		// Already assigned this channel number:
		for (i = 0; i < _channels_nr; i++)
			if (_chan_nrs[i] == chan_nr) {
				mapped_chan = i;
				break;
			}
	} else if ((command & 0xe0) == 0x80) {
		// Assign new channel round-robin

		// Mark channel as unused:
		if (_chan_nrs[_channel_assigner] >= 0)
			_channels_assigned &= ~(1 << _chan_nrs[_channel_assigner]);

		// Remember channel:
		_chan_nrs[_channel_assigner] = chan_nr;
		// Mark channel as used
		_channels_assigned |= (1 << _chan_nrs[_channel_assigner]);

		// Save channel for use later in this call:
		mapped_chan = _channel_assigner;
		// Round-ropin iterate channel assigner:
		_channel_assigner = (_channel_assigner + 1) % _channels_nr;
	}

	if (mapped_chan == -1)
		return;

	switch (command & 0xf0) {

	case 0x80:
		if (op1 == _notes[mapped_chan])
			_notes[mapped_chan] = 0;
		break;

	case 0x90:
		if (!op2) {
			if (op1 == _notes[mapped_chan])
				_notes[mapped_chan] = 0;
		} else {
			_notes[mapped_chan] = op1;
			_volumes[mapped_chan] = op2;
		}
		break;

	case 0xb0:
		if ((op1 == SCI_MIDI_CHANNEL_NOTES_OFF) || (op1 == SCI_MIDI_CHANNEL_SOUND_OFF))
			_notes[mapped_chan] = 0;
		break;

	default:
		debug(2, "Unused MIDI command %02x %02x %02x", command, op1, op2);
		break; /* ignore */
	}
}

void MidiDriver_PCJr::generateSamples(int16 *data, int len) {
	int i;
	int chan;
	int freq[kMaxChannels];

	for (chan = 0; chan < _channels_nr; chan++)
		freq[chan] = get_freq(_notes[chan]);

	for (i = 0; i < len; i++) {
		int16 result = 0;

		for (chan = 0; chan < _channels_nr; chan++)
			if (_notes[chan]) {
				int volume = (_global_volume * _volumes[chan])
				             >> VOLUME_SHIFT;

				_freq_count[chan] += freq[chan];
				while (_freq_count[chan] >= (FREQUENCY << 1))
					_freq_count[chan] -= (FREQUENCY << 1);

				if (_freq_count[chan] - freq[chan] < 0) {
					/* Unclean rising edge */
					int l = volume << 1;
					result += -volume + (l * _freq_count[chan]) / freq[chan];
				} else if (_freq_count[chan] >= FREQUENCY
				           && _freq_count[chan] - freq[chan] < FREQUENCY) {
					/* Unclean falling edge */
					int l = volume << 1;
					result += volume - (l * (_freq_count[chan] - FREQUENCY)) / freq[chan];
				} else {
					if (_freq_count[chan] < FREQUENCY)
						result += volume;
					else
						result += -volume;
				}
			}
		data[i] = result;
	}
}

int MidiDriver_PCJr::open(int channels) {
	if (_isOpen)
		return MERR_ALREADY_OPEN;

	if (channels > kMaxChannels)
		return -1;

	_channels_nr = channels;
	_global_volume = 100;
	for (int i = 0; i < _channels_nr; i++) {
		_volumes[i] = 100;
		_notes[i] = 0;
		_freq_count[i] = 0;
		_chan_nrs[i] = -1;
	}
	_channel_assigner = 0;
	_channels_assigned = 0;

	MidiDriver_Emulated::open();

	_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_mixerSoundHandle, this, -1);

	return 0;
}

void MidiDriver_PCJr::close() {
	_mixer->stopHandle(_mixerSoundHandle);
}

int MidiPlayer_PCJr::getPlayMask(SciVersion soundVersion) {
	if (soundVersion == SCI_VERSION_0_EARLY)
		return 0x10; // FIXME: Not correct

	return 0x10;
}

int MidiPlayer_PCSpeaker::getPlayMask(SciVersion soundVersion) {
	if (soundVersion == SCI_VERSION_0_EARLY)
		return 0x02;

	return 0x20;
}

} // End of namespace Sci