aboutsummaryrefslogtreecommitdiff
path: root/audio/softsynth/eas.cpp
blob: d829e3b39af646aafc75a39f641a18550ec24740 (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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* 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.
 */

#include "common/scummsys.h"

#if defined(__ANDROID__)

#include <dlfcn.h>

#include "common/debug.h"
#include "common/endian.h"
#include "common/textconsole.h"
#include "common/error.h"
#include "common/file.h"
#include "common/config-manager.h"
#include "common/system.h"
#include "audio/audiostream.h"
#include "audio/mpu401.h"
#include "audio/musicplugin.h"
#include "audio/mixer.h"

//#define EAS_DUMPSTREAM

// NOTE:
// EAS's render function *only* accepts one mix buffer size. it's defined at
// compile time of the library and can be retrieved via EASLibConfig.bufSize
// (seen: 128 bytes).
// to avoid local intermediate buffers, this implementation insists on a fixed
// buffer size of the calling rate converter, which in return must be a
// multiple of EAS's. that may change if there're hickups because slower
// devices can't render fast enough

// from rate_arm.cpp
#define INTERMEDIATE_BUFFER_SIZE 512

// so far all android versions have the very same library version
#define EAS_LIBRARY "libsonivox.so"
#define EAS_KNOWNVERSION 0x03060a0e

#define EAS_REVERB 2
#define EAS_REVERB_BYPASS 0
#define EAS_REVERB_PRESET 1
#define EAS_REVERB_CHAMBER 2

class MidiDriver_EAS : public MidiDriver_MPU401, Audio::AudioStream {
public:
	MidiDriver_EAS();
	virtual ~MidiDriver_EAS();

	// MidiDriver
	virtual int open();
	virtual bool isOpen() const;
	virtual void close();
	virtual void send(uint32 b);
	virtual void sysEx(const byte *msg, uint16 length);
	virtual void setTimerCallback(void *timerParam,
								Common::TimerManager::TimerProc timerProc);
	virtual uint32 getBaseTempo();

	// AudioStream
	virtual int readBuffer(int16 *buffer, const int numSamples);
	virtual bool isStereo() const;
	virtual int getRate() const;
	virtual bool endOfData() const;

private:
	struct EASLibConfig {
		uint32 version;
		uint32 debug;
		int32 voices;
		int32 channels;
		int32 rate;
		int32 bufSize;
		uint32 filter;
		uint32 timeStamp;
		char *GUID;
	};

	struct EASFile {
		const char *path;
		int fd;
		long long offset;
		long long length;
	};

	typedef void * EASDataHandle;
	typedef void * EASHandle;

	typedef EASLibConfig *(*ConfigFunc)();
	typedef int32 (*InitFunc)(EASDataHandle *);
	typedef int32 (*ShutdownFunc)(EASDataHandle);
	typedef int32 (*LoadDLSFunc)(EASDataHandle, EASHandle, EASFile *);
	typedef int32 (*SetParameterFunc)(EASDataHandle, int32, int32, int32);
	typedef int32 (*SetVolumeFunc)(EASDataHandle, EASHandle, int32);
	typedef int32 (*OpenStreamFunc)(EASDataHandle, EASHandle *, EASHandle);
	typedef int32 (*WriteStreamFunc)(EASDataHandle, EASHandle, byte *, int32);
	typedef int32 (*CloseStreamFunc)(EASDataHandle, EASHandle);
	typedef int32 (*RenderFunc)(EASDataHandle, int16 *, int32, int32 *);

	template<typename T>
	void sym(T &t, const char *symbol) {
		union {
			void *v;
			T t;
		} u;

		assert(sizeof(u.v) == sizeof(u.t));

		u.v = dlsym(_dlHandle, symbol);

		if (!u.v)
			warning("couldn't resolve %s from " EAS_LIBRARY, symbol);

		t = u.t;
	}

	void *_dlHandle;

	ConfigFunc _configFunc;
	InitFunc _initFunc;
	ShutdownFunc _shutdownFunc;
	LoadDLSFunc _loadDLSFunc;
	SetParameterFunc _setParameterFunc;
	SetVolumeFunc _setVolumeFunc;
	OpenStreamFunc _openStreamFunc;
	WriteStreamFunc _writeStreamFunc;
	CloseStreamFunc _closeStreamFunc;
	RenderFunc _renderFunc;

	const EASLibConfig *_config;
	EASDataHandle _EASHandle;
	EASHandle _midiStream;

	Common::TimerManager::TimerProc _timerProc;
	void *_timerParam;
	uint32 _baseTempo;
	uint _rounds;
	Audio::SoundHandle _soundHandle;

	Common::DumpFile _dump;
};

MidiDriver_EAS::MidiDriver_EAS() :
	MidiDriver_MPU401(),
	_dlHandle(0),
	_configFunc(0),
	_initFunc(0),
	_shutdownFunc(0),
	_loadDLSFunc(0),
	_setParameterFunc(0),
	_setVolumeFunc(0),
	_openStreamFunc(0),
	_writeStreamFunc(0),
	_closeStreamFunc(0),
	_renderFunc(0),
	_config(0),
	_EASHandle(0),
	_midiStream(0),
	_timerProc(0),
	_timerParam(0),
	_baseTempo(0),
	_rounds(0),
	_soundHandle(),
	_dump() {
}

MidiDriver_EAS::~MidiDriver_EAS() {
}

int MidiDriver_EAS::open() {
	if (isOpen())
		return MERR_ALREADY_OPEN;

	_dlHandle = dlopen(EAS_LIBRARY, RTLD_LAZY);
	if (!_dlHandle) {
		warning("error opening " EAS_LIBRARY ": %s", dlerror());
		return MERR_DEVICE_NOT_AVAILABLE;
	}

	sym(_configFunc, "EAS_Config");
	if (!_configFunc) {
		close();
		return -1;
	}

	_config = _configFunc();
	if (!_config) {
		close();
		warning("error retrieving EAS library configuration");
		return -1;
	}

	if (_config->version != EAS_KNOWNVERSION) {
		close();
		warning("unknown EAS library version: 0x%08x", _config->version);
		return -1;
	}

	if (_config->channels > 2) {
		close();
		warning("unsupported number of EAS channels: %d", _config->channels);
		return -1;
	}

	// see note at top of this file
	if (INTERMEDIATE_BUFFER_SIZE % (_config->bufSize * _config->channels)) {
		close();
		warning("unsupported EAS buffer size: %d", _config->bufSize);
		return -1;
	}

	sym(_initFunc, "EAS_Init");
	sym(_shutdownFunc, "EAS_Shutdown");
	sym(_loadDLSFunc, "EAS_LoadDLSCollection");
	sym(_setParameterFunc, "EAS_SetParameter");
	sym(_setVolumeFunc, "EAS_SetVolume");
	sym(_openStreamFunc, "EAS_OpenMIDIStream");
	sym(_writeStreamFunc, "EAS_WriteMIDIStream");
	sym(_closeStreamFunc, "EAS_CloseMIDIStream");
	sym(_renderFunc, "EAS_Render");

	if (!_initFunc || !_shutdownFunc || !_loadDLSFunc || !_setParameterFunc ||
			!_openStreamFunc || !_writeStreamFunc || !_closeStreamFunc ||
			!_renderFunc) {
		close();
		return -1;
	}

	int32 res = _initFunc(&_EASHandle);
	if (res) {
		close();
		warning("error initializing the EAS library: %d", res);
		return -1;
	}

	res = _setParameterFunc(_EASHandle, EAS_REVERB, EAS_REVERB_PRESET,
							EAS_REVERB_CHAMBER);
	if (res)
		warning("error setting reverb preset: %d", res);

	res = _setParameterFunc(_EASHandle, EAS_REVERB, EAS_REVERB_BYPASS, 0);
	if (res)
		warning("error disabling reverb bypass: %d", res);

	// 90 is EAS's default, max is 100
	// so the option slider will only work from 0.1 to 1.1
	res = _setVolumeFunc(_EASHandle, 0, ConfMan.getInt("midi_gain") - 10);
	if (res)
		warning("error setting EAS master volume: %d", res);

	res = _openStreamFunc(_EASHandle, &_midiStream, 0);
	if (res) {
		close();
		warning("error opening EAS MIDI stream: %d", res);
		return -1;
	}

	// set the timer frequency to match a single buffer size
	_baseTempo = (1000000 * _config->bufSize) / _config->rate;

	// number of buffer fills per readBuffer()
	_rounds = INTERMEDIATE_BUFFER_SIZE / (_config->bufSize * _config->channels);

	debug("EAS initialized (voices:%d channels:%d rate:%d buffer:%d) "
			"tempo:%u rounds:%u", _config->voices, _config->channels,
			_config->rate, _config->bufSize, _baseTempo, _rounds);

	// TODO doesn't seem to work with midi streams?
	if (ConfMan.hasKey("soundfont")) {
		const Common::String dls = ConfMan.get("soundfont");

		debug("loading DLS file '%s'", dls.c_str());

		EASFile f;
		memset(&f, 0, sizeof(EASFile));
		f.path = dls.c_str();

		res = _loadDLSFunc(_EASHandle, 0, &f);
		if (res)
			warning("error loading DLS file '%s': %d", dls.c_str(), res);
		else
			debug("DLS file loaded");
	}

#ifdef EAS_DUMPSTREAM
	if (!_dump.open("/sdcard/eas.dump"))
		warning("error opening EAS dump file");
#endif

	g_system->getMixer()->playStream(Audio::Mixer::kMusicSoundType,
										&_soundHandle, this, -1,
										Audio::Mixer::kMaxChannelVolume, 0,
										DisposeAfterUse::NO, true);

	return 0;
}

bool MidiDriver_EAS::isOpen() const {
	return _dlHandle != 0;
}

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

	if (!isOpen())
		return;

	g_system->getMixer()->stopHandle(_soundHandle);

#ifdef EAS_DUMPSTREAM
	if (_dump.isOpen())
		_dump.close();
#endif

	// not pretty, but better than a mutex
	g_system->delayMillis((_baseTempo * _rounds) / 1000);

	if (_midiStream) {
		int32 res = _closeStreamFunc(_EASHandle, _midiStream);
		if (res)
			warning("error closing EAS MIDI stream: %d", res);

		_midiStream = 0;
	}

	if (_EASHandle) {
		int32 res = _shutdownFunc(_EASHandle);
		if (res)
			warning("error shutting down the EAS library: %d", res);

		_EASHandle = 0;
	}

	if (dlclose(_dlHandle))
		warning("error closing " EAS_LIBRARY ": %s", dlerror());

	_dlHandle = 0;
}

void MidiDriver_EAS::send(uint32 b) {
	byte buf[4];

	WRITE_LE_UINT32(buf, b);

	int32 len = 3;
	if ((buf[0] >> 4) == 0xC || (buf[0] >> 4) == 0xD)
		len = 2;

	int32 res = _writeStreamFunc(_EASHandle, _midiStream, buf, len);
	if (res)
		warning("error writing to EAS MIDI stream: %d", res);
}

void MidiDriver_EAS::sysEx(const byte *msg, uint16 length) {
	byte buf[266];

	assert(length + 2 <= ARRAYSIZE(buf));

	buf[0] = 0xF0;
	memcpy(buf + 1, msg, length);
	buf[length + 1] = 0xF7;

	int32 res = _writeStreamFunc(_EASHandle, _midiStream, buf, length + 2);
	if (res)
		warning("error writing to EAS MIDI stream: %d", res);
}

void MidiDriver_EAS::setTimerCallback(void *timerParam,
								Common::TimerManager::TimerProc timerProc) {
	_timerParam = timerParam;
	_timerProc = timerProc;
}

uint32 MidiDriver_EAS::getBaseTempo() {
	return _baseTempo;
}

int MidiDriver_EAS::readBuffer(int16 *buffer, const int numSamples) {
	// see note at top of this file
	assert(numSamples == INTERMEDIATE_BUFFER_SIZE);

	int32 res, c;

	for (uint i = 0; i < _rounds; ++i) {
		// pull in MIDI events for exactly one buffer size
		if (_timerProc)
			(*_timerProc)(_timerParam);

		// if there are no MIDI events, this just renders silence
		res = _renderFunc(_EASHandle, buffer, _config->bufSize, &c);
		if (res) {
			warning("error rendering EAS samples: %d", res);
			return -1;
		}

#ifdef EAS_DUMPSTREAM
		if (_dump.isOpen())
			_dump.write(buffer, c * _config->channels * 2);
#endif

		buffer += c * _config->channels;
	}

	return numSamples;
}

bool MidiDriver_EAS::isStereo() const {
	return _config->channels == 2;
}

int MidiDriver_EAS::getRate() const {
	return _config->rate;
}

bool MidiDriver_EAS::endOfData() const {
	return false;
}

class EASMusicPlugin : public MusicPluginObject {
public:
	EASMusicPlugin();
	virtual ~EASMusicPlugin();

	const char *getName() const;
	const char *getId() const;
	MusicDevices getDevices() const;
	Common::Error createInstance(MidiDriver **mididriver,
									MidiDriver::DeviceHandle = 0) const;
};

EASMusicPlugin::EASMusicPlugin() {
}

EASMusicPlugin::~EASMusicPlugin() {
}

const char *EASMusicPlugin::getName() const {
	return "Embedded Audio Synthesis";
}

const char *EASMusicPlugin::getId() const {
	return "eas";
}

MusicDevices EASMusicPlugin::getDevices() const {
	MusicDevices devices;
	devices.push_back(MusicDevice(this, "", MT_GM));

	return devices;
}

Common::Error EASMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle) const {
	*mididriver = new MidiDriver_EAS();

	return Common::kNoError;
}

//#if PLUGIN_ENABLED_DYNAMIC(EAS)
	//REGISTER_PLUGIN_DYNAMIC(EAS, PLUGIN_TYPE_MUSIC, EASMusicPlugin);
//#else
	REGISTER_PLUGIN_STATIC(EAS, PLUGIN_TYPE_MUSIC, EASMusicPlugin);
//#endif

#endif