aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/softseq/pcspeaker.cpp
blob: 3a05c7bef98a97c9926320ef49a0bb0a6ca95fd4 (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
/* 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$
 *
 */

/* PC speaker software sequencer for SCI */

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

namespace Sci {

#define FREQUENCY 94020

static int volume = 0x0600;
static int note = 0; /* Current halftone, or 0 if off */
static int freq_count = 0;

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


static Common::Error sps_set_option(sfx_softseq_t *self, const char *name, const char *value) {
	return Common::kUnknownError;
}

static Common::Error sps_init(sfx_softseq_t *self, byte *patch, int patch_len, byte *patch2,
	int patch2_len) {
	return Common::kNoError;
}

static void sps_exit(sfx_softseq_t *self) {
}

static void sps_event(sfx_softseq_t *self, byte command, int argc, byte *argv) {
#if 0
	fprintf(stderr, "Note [%02x : %02x %02x]\n", command,  argc ? argv[0] : 0, (argc > 1) ? argv[1] : 0);
#endif

	switch (command & 0xf0) {

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

	case 0x90:
		if (!argv[1]) {
			if (argv[0] == note)
				note = 0;
		} else
			note = argv[0];
		/* Ignore velocity otherwise; we just use the global one */
		break;

	case 0xb0:
		if (argv[1] == SCI_MIDI_CHANNEL_NOTES_OFF)
			note = 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 const 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
};


void sps_poll(sfx_softseq_t *self, byte *dest, int len) {
	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));
	int16 *buf = (int16 *) dest;

	int i;
	for (i = 0; i < len; i++) {
		if (note) {
			freq_count += freq;
			while (freq_count >= (FREQUENCY << 1))
				freq_count -= (FREQUENCY << 1);

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

}

void sps_volume(sfx_softseq_t *self, int new_volume) {
	volume = new_volume << 4;
}

void sps_allstop(sfx_softseq_t *self) {
	note = 0;
}

sfx_softseq_t sfx_softseq_pcspeaker = {
	"pc-speaker",
	"0.3",
	sps_set_option,
	sps_init,
	sps_exit,
	sps_volume,
	sps_event,
	sps_poll,
	sps_allstop,
	NULL,
	SFX_SEQ_PATCHFILE_NONE,
	SFX_SEQ_PATCHFILE_NONE,
	0x20,  /* PC speaker channel only */
	0, /* No rhythm channel */
	1, /* # of voices */
	{FREQUENCY, SFX_PCM_MONO, SFX_PCM_FORMAT_S16_NATIVE}
};

} // End of namespace Sci