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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001-2003 The ScummVM project
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*/
#include "stdafx.h"
#include "mpu401.h"
#include "base/engine.h" // for g_system
#include "common/util.h" // for ARRAYSIZE
void MidiChannel_MPU401::init(MidiDriver_MPU401 *owner, byte channel) {
_owner = owner;
_channel = channel;
_allocated = false;
}
MidiDriver *MidiChannel_MPU401::device() {
return _owner;
}
void MidiChannel_MPU401::send (uint32 b) {
_owner->send (b & 0xFFFFFFF0 | (_channel & 0xF));
}
void MidiChannel_MPU401::noteOff (byte note) {
_owner->send(note << 8 | 0x80 | _channel);
}
void MidiChannel_MPU401::noteOn(byte note, byte velocity) {
_owner->send (velocity << 16 | note << 8 | 0x90 | _channel);
}
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);
}
void MidiChannel_MPU401::pitchBendFactor(byte value) {
_owner->setPitchBendRange (_channel, value);
}
void MidiChannel_MPU401::sysEx_customInstrument(uint32 type, byte *instr) {
_owner->sysEx_customInstrument (_channel, type, instr);
}
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"
};
if ((uint) error_code >= ARRAYSIZE(midi_errors))
return "Unknown Error";
return midi_errors[error_code];
}
MidiDriver_MPU401::MidiDriver_MPU401() :
MidiDriver(),
_started_thread (false),
_mutex (0),
_timer_proc (0),
_timer_param (0),
_channel_mask (0xFFFF) // Permit all 16 channels by default
{
uint i;
for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
_midi_channels [i].init (this, i);
}
}
void MidiDriver_MPU401::close() {
if (_mutex) {
_started_thread = false;
g_system->lock_mutex (_mutex); // Wait for the timer thread to shutdown.
g_system->unlock_mutex (_mutex);
g_system->delete_mutex (_mutex);
}
int i;
for (i = 0; i < 16; ++i)
send (0x7B << 8 | 0xB0 | i);
}
uint32 MidiDriver_MPU401::property (int prop, uint32 param) {
switch (prop) {
case PROP_CHANNEL_MASK:
_channel_mask = param & 0xFFFF;
return 1;
}
return 0;
}
MidiChannel *MidiDriver_MPU401::allocateChannel() {
MidiChannel_MPU401 *chan;
uint i;
for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
if (i == 9 || !(_channel_mask & (1 << i)))
continue;
chan = &_midi_channels[i];
if (!chan->_allocated) {
chan->allocate();
return (chan);
}
}
return NULL;
}
void MidiDriver_MPU401::setTimerCallback (void *timer_param, TimerCallback timer_proc) {
if (!_timer_proc || !timer_proc) {
_timer_proc = timer_proc;
_timer_param = timer_param;
if (!_started_thread && timer_proc) {
// TODO: This is the only place in ScummVM where create_thread is
// being used. And it's used for a timer like thread. So if we
// could convert it to use the timer API instead, we could get
// rid of create_thread completely.
_mutex = g_system->create_mutex();
_started_thread = true;
g_system->create_thread(midi_driver_thread, this);
}
}
}
#if !defined(__MORPHOS__) && !defined(__PALM_OS__)
int MidiDriver_MPU401::midi_driver_thread(void *param) {
MidiDriver_MPU401 *mid = (MidiDriver_MPU401 *)param;
int old_time, cur_time;
// Grab the MidiDriver's mutex. When the MidiDriver
// shuts down, it will wait on that mutex until we've
// detected the shutdown and quit looping.
g_system->lock_mutex (mid->_mutex);
old_time = g_system->get_msecs();
while (mid->_started_thread) {
g_system->delay_msecs(10);
cur_time = g_system->get_msecs();
while (old_time < cur_time) {
old_time += 10;
if (mid->_timer_proc)
(*(mid->_timer_proc)) (mid->_timer_param);
}
}
g_system->unlock_mutex (mid->_mutex);
return 0;
}
#endif
|