2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2002-12-08 20:19:01 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2002-12-08 20:19:01 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/mpu401.h"
|
2006-10-21 12:44:10 +00:00
|
|
|
#include "common/system.h"
|
2003-09-26 14:05:33 +00:00
|
|
|
#include "common/timer.h"
|
2002-12-08 20:19:01 +00:00
|
|
|
#include "common/util.h" // for ARRAYSIZE
|
|
|
|
|
2004-11-04 21:34:17 +00:00
|
|
|
void MidiChannel_MPU401::init(MidiDriver *owner, byte channel) {
|
2002-12-08 20:19:01 +00:00
|
|
|
_owner = owner;
|
|
|
|
_channel = channel;
|
|
|
|
_allocated = false;
|
|
|
|
}
|
|
|
|
|
2004-11-04 21:34:17 +00:00
|
|
|
bool MidiChannel_MPU401::allocate() {
|
|
|
|
if (_allocated)
|
|
|
|
return false;
|
2011-06-04 15:38:06 +00:00
|
|
|
|
|
|
|
_allocated = true;
|
|
|
|
|
|
|
|
return true;
|
2004-11-04 21:34:17 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 18:30:44 +00:00
|
|
|
MidiDriver *MidiChannel_MPU401::device() {
|
|
|
|
return _owner;
|
|
|
|
}
|
|
|
|
|
2006-05-12 12:03:36 +00:00
|
|
|
void MidiChannel_MPU401::send(uint32 b) {
|
2008-11-29 18:01:16 +00:00
|
|
|
_owner->send((b & 0xFFFFFFF0) | (_channel & 0xF));
|
2003-08-08 11:54:24 +00:00
|
|
|
}
|
|
|
|
|
2006-05-12 12:03:36 +00:00
|
|
|
void MidiChannel_MPU401::noteOff(byte note) {
|
2003-03-06 18:30:44 +00:00
|
|
|
_owner->send(note << 8 | 0x80 | _channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiChannel_MPU401::noteOn(byte note, byte velocity) {
|
2003-09-28 21:08:48 +00:00
|
|
|
_owner->send(velocity << 16 | note << 8 | 0x90 | _channel);
|
2003-03-06 18:30:44 +00:00
|
|
|
}
|
2002-12-08 20:19:01 +00:00
|
|
|
|
2003-03-06 18:30:44 +00:00
|
|
|
void MidiChannel_MPU401::programChange(byte program) {
|
|
|
|
_owner->send(program << 8 | 0xC0 | _channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiChannel_MPU401::pitchBend(int16 bend) {
|
|
|
|
_owner->send((((bend + 0x2000) >> 7) & 0x7F) << 16 | ((bend + 0x2000) & 0x7F) << 8 | 0xE0 | _channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiChannel_MPU401::controlChange(byte control, byte value) {
|
|
|
|
_owner->send(value << 16 | control << 8 | 0xB0 | _channel);
|
|
|
|
}
|
2002-12-08 20:19:01 +00:00
|
|
|
|
2003-03-06 18:30:44 +00:00
|
|
|
void MidiChannel_MPU401::pitchBendFactor(byte value) {
|
2003-09-28 21:08:48 +00:00
|
|
|
_owner->setPitchBendRange(_channel, value);
|
2003-03-06 18:30:44 +00:00
|
|
|
}
|
|
|
|
|
2006-02-27 01:59:07 +00:00
|
|
|
void MidiChannel_MPU401::sysEx_customInstrument(uint32 type, const byte *instr) {
|
2003-09-28 21:08:48 +00:00
|
|
|
_owner->sysEx_customInstrument(_channel, type, instr);
|
2003-03-06 18:30:44 +00:00
|
|
|
}
|
|
|
|
|
2003-05-17 14:27:58 +00:00
|
|
|
const char *MidiDriver::getErrorName(int error_code) {
|
|
|
|
static const char *const midi_errors[] = {
|
|
|
|
"No error",
|
|
|
|
"Cannot connect",
|
|
|
|
"Streaming not available",
|
|
|
|
"Device not available",
|
|
|
|
"Driver already open"
|
|
|
|
};
|
|
|
|
|
2006-05-12 12:03:36 +00:00
|
|
|
if ((uint)error_code >= ARRAYSIZE(midi_errors))
|
2003-05-17 14:27:58 +00:00
|
|
|
return "Unknown Error";
|
|
|
|
return midi_errors[error_code];
|
|
|
|
}
|
|
|
|
|
2003-06-11 05:54:46 +00:00
|
|
|
MidiDriver_MPU401::MidiDriver_MPU401() :
|
|
|
|
MidiDriver(),
|
2006-05-12 12:03:36 +00:00
|
|
|
_timer_proc(0),
|
|
|
|
_channel_mask(0xFFFF) // Permit all 16 channels by default
|
2003-06-11 05:54:46 +00:00
|
|
|
{
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-06-11 05:54:46 +00:00
|
|
|
uint i;
|
2002-12-08 20:19:01 +00:00
|
|
|
for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
|
2006-05-12 12:03:36 +00:00
|
|
|
_midi_channels[i].init(this, i);
|
2002-12-08 20:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-21 21:50:21 +00:00
|
|
|
MidiDriver_MPU401::~MidiDriver_MPU401() {
|
|
|
|
}
|
|
|
|
|
2003-06-11 05:54:46 +00:00
|
|
|
void MidiDriver_MPU401::close() {
|
2011-03-21 14:42:17 +00:00
|
|
|
if (_timer_proc) {
|
2006-10-21 12:44:10 +00:00
|
|
|
g_system->getTimerManager()->removeTimerProc(_timer_proc);
|
2011-03-21 14:42:17 +00:00
|
|
|
_timer_proc = 0;
|
|
|
|
}
|
|
|
|
if (isOpen()) {
|
|
|
|
for (int i = 0; i < 16; ++i)
|
|
|
|
send(0x7B << 8 | 0xB0 | i);
|
|
|
|
}
|
2003-06-11 05:54:46 +00:00
|
|
|
}
|
|
|
|
|
2006-05-12 12:03:36 +00:00
|
|
|
uint32 MidiDriver_MPU401::property(int prop, uint32 param) {
|
2003-09-25 22:32:05 +00:00
|
|
|
switch (prop) {
|
2006-08-01 14:05:18 +00:00
|
|
|
case PROP_CHANNEL_MASK:
|
|
|
|
_channel_mask = param & 0xFFFF;
|
|
|
|
return 1;
|
2003-09-25 22:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-06 18:30:44 +00:00
|
|
|
MidiChannel *MidiDriver_MPU401::allocateChannel() {
|
2002-12-08 20:19:01 +00:00
|
|
|
MidiChannel_MPU401 *chan;
|
|
|
|
uint i;
|
2003-03-06 18:30:44 +00:00
|
|
|
|
2002-12-08 20:19:01 +00:00
|
|
|
for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
|
2003-09-25 22:32:05 +00:00
|
|
|
if (i == 9 || !(_channel_mask & (1 << i)))
|
2003-03-06 18:30:44 +00:00
|
|
|
continue;
|
2002-12-08 20:19:01 +00:00
|
|
|
chan = &_midi_channels[i];
|
2004-11-04 21:34:17 +00:00
|
|
|
if (chan->allocate()) {
|
2003-09-28 21:08:48 +00:00
|
|
|
return chan;
|
2002-12-08 20:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-06-24 09:34:49 +00:00
|
|
|
void MidiDriver_MPU401::setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) {
|
2002-12-08 20:19:01 +00:00
|
|
|
if (!_timer_proc || !timer_proc) {
|
2003-09-26 14:05:33 +00:00
|
|
|
if (_timer_proc)
|
2006-10-21 12:44:10 +00:00
|
|
|
g_system->getTimerManager()->removeTimerProc(_timer_proc);
|
2003-06-11 05:54:46 +00:00
|
|
|
_timer_proc = timer_proc;
|
2003-09-26 14:05:33 +00:00
|
|
|
if (timer_proc)
|
2011-08-05 09:15:20 +00:00
|
|
|
g_system->getTimerManager()->installTimerProc(timer_proc, 10000, timer_param, "MPU401");
|
2002-12-08 20:19:01 +00:00
|
|
|
}
|
|
|
|
}
|