aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/seq/gm.cpp
blob: 13520b2eec22ddea50eccd5aae2e37e3a845404b (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
/* 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 <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
#include "../sequencer.h"
#include "../device.h"
#include "instrument-map.h"
#include <resource.h>

namespace Sci {

static midi_writer_t *writer = NULL;

static int
midi_gm_open(int patch_len, byte *data, int patch2_len, byte *data2, void *device) {
	sfx_instrument_map_t *instrument_map = sfx_instrument_map_load_sci(data, patch_len);

	if (!instrument_map) {
		fprintf(stderr, "[GM]  No GM instrument map found, trying MT-32 instrument map..\n");
		instrument_map = sfx_instrument_map_mt32_to_gm(data2, patch2_len);
	}

	writer = sfx_mapped_writer((midi_writer_t *) device, instrument_map);

	if (!writer)
		return SFX_ERROR;

	if (writer->reset_timer)
		writer->reset_timer(writer);

	return SFX_OK;
}

static int
midi_gm_close(void) {
	return SFX_OK;
}

static int
midi_gm_event(byte command, int argc, byte *argv) {
	byte data[4];

	assert(argc < 4);
	data[0] = command;
	memcpy(data + 1, argv, argc);

	writer->write(writer, data, argc + 1);

	return SFX_OK;
}

static int
midi_gm_delay(int ticks) {
	writer->delay(writer, ticks);

	return SFX_OK;
}

static int
midi_gm_reset_timer(GTimeVal ts) {
	writer->reset_timer(writer);

	return SFX_OK;
}

#define MIDI_MASTER_VOLUME_LEN 8

static int
midi_gm_volume(guint8 volume) {
	byte data[MIDI_MASTER_VOLUME_LEN] = {
		0xf0,
		0x7f,
		0x7f,
		0x04,
		0x01,
		volume,
		volume,
		0xf7
	};

	writer->write(writer, data, MIDI_MASTER_VOLUME_LEN);
	if (writer->flush)
		writer->flush(writer);

	return SFX_OK;
}

static int
midi_gm_allstop(void) {
	byte data[3] = { 0xb0,
	                 0x78, /* all sound off */
	                 0
	               };
	int i;

	/* All sound off on all channels */
	for (i = 0; i < 16; i++) {
		data[0] = 0xb0 | i;
		writer->write(writer, data, 3);
	}
	if (writer->flush)
		writer->flush(writer);

	return SFX_OK;
}

static int
midi_gm_reverb(int reverb) {
	byte data[3] = { 0xb0,
	                 91, /* set reverb */
	                 reverb
	               };
	int i;

	/* Set reverb on all channels */
	for (i = 0; i < 16; i++)
		if (i != 9) {
			data[0] = 0xb0 | i;
			writer->write(writer, data, 3);
		}
	if (writer->flush)
		writer->flush(writer);

	return SFX_OK;
}

static int
midi_gm_set_option(char *x, char *y) {
	return SFX_ERROR;
}

sfx_sequencer_t sfx_sequencer_gm = {
	"General MIDI",
	"0.1",
	SFX_DEVICE_MIDI,
	&midi_gm_set_option,
	&midi_gm_open,
	&midi_gm_close,
	&midi_gm_event,
	&midi_gm_delay,
	&midi_gm_reset_timer,
	&midi_gm_allstop,
	&midi_gm_volume,
	&midi_gm_reverb,
	004,	/* patch.004 */
	001,	/* patch.001 */
	0x01,	/* playflag */
	1,	/* do play rhythm */
	64,	/* max polyphony */
	0	/* no write-ahead needed inherently */
};

} // End of namespace Sci