aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/softseq/SN76496.cpp
blob: f128be2d008f4bfc172c029745260d4fe76c9a3b (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* 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$
 *
 */

/* Tandy/PCJr sequencer for FreeSCI */

#include "sci/sfx/softseq.h"
#include "sci/include/sci_midi.h"

namespace Sci {

#define FREQUENCY 44100
#define CHANNELS_NR 3
#define VOLUME_SHIFT 3

static int global_volume = 100; /* Base volume */
static int volumes[CHANNELS_NR] = { 100, 100, 100 };
static int notes[CHANNELS_NR] = {0, 0, 0}; /* Current halftone, or 0 if off */
static int freq_count[CHANNELS_NR] = {0, 0, 0};
static int channel_assigner = 0;
static int channels_assigned = 0;
static int chan_nrs[CHANNELS_NR] = { -1, -1, -1};

extern sfx_softseq_t sfx_softseq_pcspeaker;
/* Forward-declare the sequencer we are defining here */


static int
SN76496_set_option(sfx_softseq_t *self, const char *name, const char *value) {
	return SFX_ERROR;
}

static int
SN76496_init(sfx_softseq_t *self, byte *patch, int patch_len, byte *patch2,
             int patch2_len) {
	return SFX_OK;
}

static void
SN76496_exit(sfx_softseq_t *self) {
}

static void
SN76496_event(sfx_softseq_t *self, byte command, int argc, byte *argv) {
	int i;
	int chan = -1;
#if 0
	fprintf(stderr, "Note [%02x : %02x %02x]\n", command,  argc ? argv[0] : 0, (argc > 1) ? argv[1] : 0);
#endif
	if ((command & 0xe0) == 0x80) {
		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) {
					chan = i;
					break;
				}
		} else {
			/* 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: */
			chan = channel_assigner;
			/* Round-ropin iterate channel assigner: */
			channel_assigner = (channel_assigner + 1) % CHANNELS_NR;
		}
	}
#if 0
	fprintf(stderr, " --> %d [%04x], {%d,%d,%d}@%d\n", chan,
	        channels_assigned, chan_nrs[0], chan_nrs[1], chan_nrs[2], channel_assigner);
#endif

	switch (command & 0xf0) {

	case 0x80:
		if (argv[0] == notes[chan])
			notes[chan] = 0;
		break;

	case 0x90:
		if (!argv[1]) {
			if (argv[chan] == notes[chan])
				notes[chan] = 0;
		} else {
			notes[chan] = argv[0];
			volumes[chan] = argv[1];
		}
		break;

	case 0xb0:
		if (argv[1] == SCI_MIDI_CHANNEL_NOTES_OFF)
			notes[chan] = 0;
		break;


	default:
#if DEBUG
		fprintf(stderr, "[SFX:PCM-PC] Unused MIDI command %02x %02x %02x\n", command, argc ? argv[0] : 0, (argc > 1) ? argv[1] : 0);
#endif
		break; /* ignore */
	}
}

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

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
SN76496_poll(sfx_softseq_t *self, byte *dest, int len) {
	gint16 *buf = (gint16 *) dest;
	int i;
	int chan;
	int freq[CHANNELS_NR];

	for (chan = 0; chan < CHANNELS_NR; chan++)
		freq[chan] = get_freq(notes[chan]);

	for (i = 0; i < len; i++) {
		int 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;
				}
			}
		buf[i] = result;
	}

}

void
SN76496_allstop(sfx_softseq_t *self) {
	int i;
	for (i = 0; i < CHANNELS_NR; i++)
		notes[i] = 0;
}

void
SN76496_volume(sfx_softseq_t *self, int new_volume) {
	global_volume = new_volume;
}


sfx_softseq_t sfx_softseq_SN76496 = {
	"SN76496",
	"0.1",
	SN76496_set_option,
	SN76496_init,
	SN76496_exit,
	SN76496_volume,
	SN76496_event,
	SN76496_poll,
	SN76496_allstop,
	NULL,
	SFX_SEQ_PATCHFILE_NONE,
	SFX_SEQ_PATCHFILE_NONE,
	0x10,  /* Tandy/PCJr channels */
	0, /* No rhythm channel */
	3, /* # of voices */
	{FREQUENCY, SFX_PCM_MONO, SFX_PCM_FORMAT_S16_NATIVE}
};

} // End of namespace Sci