1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "sound/mpu401.h"
26 #include "common/system.h"
27 #include "common/timer.h"
28 #include "common/util.h" // for ARRAYSIZE
30 void MidiChannel_MPU401::init(MidiDriver
*owner
, byte channel
) {
36 bool MidiChannel_MPU401::allocate() {
39 return (_allocated
= true);
42 MidiDriver
*MidiChannel_MPU401::device() {
46 void MidiChannel_MPU401::send(uint32 b
) {
47 _owner
->send((b
& 0xFFFFFFF0) | (_channel
& 0xF));
50 void MidiChannel_MPU401::noteOff(byte note
) {
51 _owner
->send(note
<< 8 | 0x80 | _channel
);
54 void MidiChannel_MPU401::noteOn(byte note
, byte velocity
) {
55 _owner
->send(velocity
<< 16 | note
<< 8 | 0x90 | _channel
);
58 void MidiChannel_MPU401::programChange(byte program
) {
59 _owner
->send(program
<< 8 | 0xC0 | _channel
);
62 void MidiChannel_MPU401::pitchBend(int16 bend
) {
63 _owner
->send((((bend
+ 0x2000) >> 7) & 0x7F) << 16 | ((bend
+ 0x2000) & 0x7F) << 8 | 0xE0 | _channel
);
66 void MidiChannel_MPU401::controlChange(byte control
, byte value
) {
67 _owner
->send(value
<< 16 | control
<< 8 | 0xB0 | _channel
);
70 void MidiChannel_MPU401::pitchBendFactor(byte value
) {
71 _owner
->setPitchBendRange(_channel
, value
);
74 void MidiChannel_MPU401::sysEx_customInstrument(uint32 type
, const byte
*instr
) {
75 _owner
->sysEx_customInstrument(_channel
, type
, instr
);
78 const char *MidiDriver::getErrorName(int error_code
) {
79 static const char *const midi_errors
[] = {
82 "Streaming not available",
83 "Device not available",
87 if ((uint
)error_code
>= ARRAYSIZE(midi_errors
))
88 return "Unknown Error";
89 return midi_errors
[error_code
];
92 MidiDriver_MPU401::MidiDriver_MPU401() :
95 _channel_mask(0xFFFF) // Permit all 16 channels by default
99 for (i
= 0; i
< ARRAYSIZE(_midi_channels
); ++i
) {
100 _midi_channels
[i
].init(this, i
);
104 void MidiDriver_MPU401::close() {
106 g_system
->getTimerManager()->removeTimerProc(_timer_proc
);
108 for (int i
= 0; i
< 16; ++i
)
109 send(0x7B << 8 | 0xB0 | i
);
112 uint32
MidiDriver_MPU401::property(int prop
, uint32 param
) {
114 case PROP_CHANNEL_MASK
:
115 _channel_mask
= param
& 0xFFFF;
122 MidiChannel
*MidiDriver_MPU401::allocateChannel() {
123 MidiChannel_MPU401
*chan
;
126 for (i
= 0; i
< ARRAYSIZE(_midi_channels
); ++i
) {
127 if (i
== 9 || !(_channel_mask
& (1 << i
)))
129 chan
= &_midi_channels
[i
];
130 if (chan
->allocate()) {
137 void MidiDriver_MPU401::setTimerCallback(void *timer_param
, Common::TimerManager::TimerProc timer_proc
) {
138 if (!_timer_proc
|| !timer_proc
) {
140 g_system
->getTimerManager()->removeTimerProc(_timer_proc
);
141 _timer_proc
= timer_proc
;
143 g_system
->getTimerManager()->installTimerProc(timer_proc
, 10000, timer_param
);