File: seq.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (395 lines) | stat: -rw-r--r-- 10,593 bytes parent folder | download | duplicates (2)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Raw output support by Michael Pearce
 * Alsa support by Nicolas Noble <nicolas@nobis-crew.org> copied from
 * both the QuickTime support and (vkeybd https://web.archive.org/web/20070629122111/http://www.alsa-project.org/~iwai/alsa.html)
 */

// Disable symbol overrides so that we can use system headers.
#define FORBIDDEN_SYMBOL_ALLOW_ALL

#include "common/scummsys.h"

#if defined(USE_SEQ_MIDI)

#include "common/error.h"
#include "common/textconsole.h"
#include "common/util.h"
#include "audio/musicplugin.h"
#include "audio/mpu401.h"

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

////////////////////////////////////////
//
// Unix dev/sequencer driver
//
////////////////////////////////////////

#define SEQ_MIDIPUTC 5

class MidiDriver_SEQ : public MidiDriver_MPU401 {
public:
	MidiDriver_SEQ(int port, bool isSynth);
	int open() override;
	bool isOpen() const override { return _isOpen; }
	void close() override;
	void send(uint32 b) override;
	void sysEx(const byte *msg, uint16 length) override;

	static const char *getDeviceName();

private:
	bool _isSynth;
	bool _isOpen;
	int _device;
	int _port;
};

MidiDriver_SEQ::MidiDriver_SEQ(int port, bool isSynth) {
	_isOpen = false;
	_isSynth = isSynth;
	_device = -1;
	_port = port;
}

int MidiDriver_SEQ::open() {
	if (_isOpen)
		return MERR_ALREADY_OPEN;

	const char *deviceName = getDeviceName();

	_isOpen = true;
	_device = ::open(deviceName, O_RDWR, 0);

	if (_device < 0) {
		warning("Cannot open rawmidi device %s - using /dev/null (no music will be heard)", deviceName);
		_device = (::open(("/dev/null"), O_RDWR, 0));
		if (_device < 0)
			error("Cannot open /dev/null to dump midi output");
	}

	return 0;
}

void MidiDriver_SEQ::close() {
	MidiDriver_MPU401::close();

	if (_isOpen)
		::close(_device);
	_isOpen = false;
}

void MidiDriver_SEQ::send(uint32 b) {
	midiDriverCommonSend(b);

	unsigned char buf[256];
	int position = 0;

	if (_isSynth) {
		switch (b & 0xf0) {
		case 0x80:
		case 0x90:
		case 0xa0:
			buf[position++] = EV_CHN_VOICE;
			buf[position++] = _port;
			buf[position++] = (unsigned char)(b & 0xf0);
			buf[position++] = (unsigned char)(b & 0x0f);
			buf[position++] = (unsigned char)((b >> 8) & 0xff);
			buf[position++] = (unsigned char)((b >> 16) & 0xff);
			buf[position++] = 0;
			buf[position++] = 0;
			break;
		case 0xc0:	// Program change
		case 0xd0:	// Channel pressure
			buf[position++] = EV_CHN_COMMON;
			buf[position++] = _port;
			buf[position++] = (unsigned char)(b & 0xf0);
			buf[position++] = (unsigned char)(b & 0x0f);
			buf[position++] = (unsigned char)((b >> 8) & 0xff);
			buf[position++] = 0;
			buf[position++] = 0;
			buf[position++] = 0;
			break;
		case 0xb0:	// Control change
			buf[position++] = EV_CHN_COMMON;
			buf[position++] = _port;
			buf[position++] = (unsigned char)(b & 0xf0);
			buf[position++] = (unsigned char)(b & 0x0f);
			buf[position++] = (unsigned char)((b >> 8) & 0xff);
			buf[position++] = 0;

			// TODO/FIXME?: Main volume control in the soundcard.h macros is value*16383/100, expression is value*128, and pan is (value+128)/2
			// Not sure how those translate to the scales we're using here.
			*reinterpret_cast<uint16 *>(buf + position) = static_cast<uint16>((b >> 16) & 0xffff);
			position += 2;
			break;
		case 0xe0: // Pitch bend
			buf[position++] = EV_CHN_COMMON;
			buf[position++] = _port;
			buf[position++] = (unsigned char)(b & 0xf0);
			buf[position++] = (unsigned char)(b & 0x0f);
			buf[position++] = 0;
			buf[position++] = 0;

			*reinterpret_cast<uint16 *>(buf + position) = static_cast<uint16>((b >> 8) & 0xffff);
			position += 2;
			break;
		default:
			warning("MidiDriver_SEQ::send: unknown: %08x", (int)b);
			break;

		}
	} else {
		switch (b & 0xF0) {
		case 0x80:
		case 0x90:
		case 0xA0:
		case 0xB0:
		case 0xE0:
			buf[position++] = SEQ_MIDIPUTC;
			buf[position++] = (unsigned char)b;
			buf[position++] = _port;
			buf[position++] = 0;
			buf[position++] = SEQ_MIDIPUTC;
			buf[position++] = (unsigned char)((b >> 8) & 0x7F);
			buf[position++] = _port;
			buf[position++] = 0;
			buf[position++] = SEQ_MIDIPUTC;
			buf[position++] = (unsigned char)((b >> 16) & 0x7F);
			buf[position++] = _port;
			buf[position++] = 0;
			break;
		case 0xC0:
		case 0xD0:
			buf[position++] = SEQ_MIDIPUTC;
			buf[position++] = (unsigned char)b;
			buf[position++] = _port;
			buf[position++] = 0;
			buf[position++] = SEQ_MIDIPUTC;
			buf[position++] = (unsigned char)((b >> 8) & 0x7F);
			buf[position++] = _port;
			buf[position++] = 0;
			break;
		default:
			warning("MidiDriver_SEQ::send: unknown: %08x", (int)b);
			break;
		}
	}

	if (write(_device, buf, position) == -1)
		warning("MidiDriver_SEQ::send: write failed (%s)", strerror(errno));
}

void MidiDriver_SEQ::sysEx(const byte *msg, uint16 length) {
	unsigned char buf [266*4];
	int position = 0;
	const byte *chr = msg;

	assert(length + 2 <= 266);

	midiDriverCommonSysEx(msg, length);

	if (_isSynth) {
		int chunksRequired = (length + 7) / 6;

		assert(chunksRequired * 8 <= static_cast<int>(sizeof(buf)));

		for (int i = 0; i < chunksRequired; i++) {
			int chunkStart = i * 6;

			buf[position++] = EV_SYSEX;
			buf[position++] = _port;

			for (int j = 0; j < 6; j++) {
				int pos = chunkStart + j - 1;
				if (pos < 0)
					buf[position++] = 0xf0;
				else if (pos < length)
					buf[position++] = msg[pos];
				else if (pos == length)
					buf[position++] = 0x7f;
				else
					buf[position++] = 0xff;
			}
		}
	} else {
		buf[position++] = SEQ_MIDIPUTC;
		buf[position++] = 0xF0;
		buf[position++] = _port;
		buf[position++] = 0;
		for (; length; --length, ++chr) {
			buf[position++] = SEQ_MIDIPUTC;
			buf[position++] = (unsigned char)*chr & 0x7F;
			buf[position++] = _port;
			buf[position++] = 0;
		}
		buf[position++] = SEQ_MIDIPUTC;
		buf[position++] = 0xF7;
		buf[position++] = _port;
		buf[position++] = 0;
	}

	if (write(_device, buf, position) == -1)
		warning("MidiDriver_SEQ::send: write failed (%s)", strerror(errno));
}


const char *MidiDriver_SEQ::getDeviceName() {
	const char *devName = getenv("SCUMMVM_MIDI");

	if (devName)
		return devName;
	else
		return "/dev/sequencer";
}

// Plugin interface

class SeqMusicPlugin : public MusicPluginObject {
public:
	const char *getName() const {
		return "SEQ";
	}

	const char *getId() const {
		return "seq";
	}

	MusicDevices getDevices() const;
	Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const;
	bool checkDevice(MidiDriver::DeviceHandle hdl, int checkFlags, bool quiet) const;

private:
	void addMidiDevices(int deviceFD, MusicDevices &devices, Common::Array<int> *portIDs) const;
	void addSynthDevices(int deviceFD, MusicDevices &devices, Common::Array<int> *portIDs) const;
};

MusicDevices SeqMusicPlugin::getDevices() const {
	MusicDevices devices;

	int deviceFD = ::open(MidiDriver_SEQ::getDeviceName(), O_RDWR, 0);
	if (deviceFD >= 0) {
		addMidiDevices(deviceFD, devices, nullptr);
		addSynthDevices(deviceFD, devices, nullptr);
		::close(deviceFD);
	}

	return devices;
}

void SeqMusicPlugin::addMidiDevices(int deviceFD, MusicDevices &devices, Common::Array<int> *portIDs) const {
	int midiDeviceCount = 0;
	if (ioctl(deviceFD, SNDCTL_SEQ_NRMIDIS, &midiDeviceCount) == 0) {
		for (int i = 0; i < midiDeviceCount; i++) {
			midi_info midiInfo;
			midiInfo.device = i;
			if (ioctl(deviceFD, SNDCTL_MIDI_INFO, &midiInfo) == 0) {
				devices.push_back(MusicDevice(this, midiInfo.name, MT_GM));	// dev_type is unimplemented so we just assume GM
				if (portIDs)
					portIDs->push_back(i);
			}
		}
	}
}

void SeqMusicPlugin::addSynthDevices(int deviceFD, MusicDevices &devices, Common::Array<int> *portIDs) const {
	int synthDeviceCount = 0;
	if (ioctl(deviceFD, SNDCTL_SEQ_NRSYNTHS, &synthDeviceCount) == 0) {
		for (int i = 0; i < synthDeviceCount; i++) {
			synth_info synthInfo;
			synthInfo.device = i;
			if (ioctl(deviceFD, SNDCTL_SYNTH_ID, &synthInfo) == 0) {
				MusicType musicType = MT_GM;

				if (synthInfo.synth_type == SYNTH_TYPE_FM)
					musicType = MT_ADLIB;

				devices.push_back(MusicDevice(this, synthInfo.name, musicType)); // dev_type is unimplemented so we just assume GM
				if (portIDs)
					portIDs->push_back(i);
			}
		}
	}
}

Common::Error SeqMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle dev) const {
	int port = 0;
	bool isSynth = false;
	bool found = false;

	if (dev) {
		Common::String deviceIDString = MidiDriver::getDeviceString(dev, MidiDriver::kDeviceId);

		MusicDevices devices;
		Common::Array<int> ports;
		int firstSynthIndex = 0;

		int deviceFD = ::open(MidiDriver_SEQ::getDeviceName(), O_RDONLY, 0);
		if (deviceFD >= 0) {
			addMidiDevices(deviceFD, devices, &ports);

			firstSynthIndex = static_cast<int>(ports.size());

			addSynthDevices(deviceFD, devices, &ports);

			::close(deviceFD);
		} else {
			warning("Device enumeration failed when creating device");
		}

		int devIndex = 0;
		for (MusicDevices::iterator d = devices.begin(); d != devices.end(); d++) {
			if (d->getCompleteId().equals(deviceIDString)) {
				found = true;
				isSynth = (devIndex >= firstSynthIndex);
				port = ports[devIndex];
				break;
			}
			devIndex++;
		}
	}

	if (found) {
		*mididriver = new MidiDriver_SEQ(port, isSynth);
		return Common::kNoError;
	}

	return Common::kAudioDeviceInitFailed;
}

bool SeqMusicPlugin::checkDevice(MidiDriver::DeviceHandle hdl, int checkFlags, bool quiet) const {
	return true;
}

//#if PLUGIN_ENABLED_DYNAMIC(SEQ)
	//REGISTER_PLUGIN_DYNAMIC(SEQ, PLUGIN_TYPE_MUSIC, SeqMusicPlugin);
//#else
	REGISTER_PLUGIN_STATIC(SEQ, PLUGIN_TYPE_MUSIC, SeqMusicPlugin);
//#endif

#endif