ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

mpu401.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "audio/mpu401.h"
00024 #include "common/system.h"
00025 #include "common/timer.h"
00026 #include "common/util.h"    // for ARRAYSIZE
00027 
00028 void MidiChannel_MPU401::init(MidiDriver *owner, byte channel) {
00029     _owner = owner;
00030     _channel = channel;
00031     _allocated = false;
00032 }
00033 
00034 bool MidiChannel_MPU401::allocate() {
00035     if (_allocated)
00036         return false;
00037 
00038     _allocated = true;
00039 
00040     return true;
00041 }
00042 
00043 MidiDriver *MidiChannel_MPU401::device() {
00044     return _owner;
00045 }
00046 
00047 void MidiChannel_MPU401::send(uint32 b) {
00048     _owner->send((b & 0xFFFFFFF0) | (_channel & 0xF));
00049 }
00050 
00051 void MidiChannel_MPU401::noteOff(byte note) {
00052     _owner->send(note << 8 | 0x80 | _channel);
00053 }
00054 
00055 void MidiChannel_MPU401::noteOn(byte note, byte velocity) {
00056     _owner->send(velocity << 16 | note << 8 | 0x90 | _channel);
00057 }
00058 
00059 void MidiChannel_MPU401::programChange(byte program) {
00060     _owner->send(program << 8 | 0xC0 | _channel);
00061 }
00062 
00063 void MidiChannel_MPU401::pitchBend(int16 bend) {
00064     _owner->send((((bend + 0x2000) >> 7) & 0x7F) << 16 | ((bend + 0x2000) & 0x7F) << 8 | 0xE0 | _channel);
00065 }
00066 
00067 void MidiChannel_MPU401::controlChange(byte control, byte value) {
00068     _owner->send(value << 16 | control << 8 | 0xB0 | _channel);
00069 }
00070 
00071 void MidiChannel_MPU401::pitchBendFactor(byte value) {
00072     _owner->setPitchBendRange(_channel, value);
00073 }
00074 
00075 void MidiChannel_MPU401::sysEx_customInstrument(uint32 type, const byte *instr) {
00076     _owner->sysEx_customInstrument(_channel, type, instr);
00077 }
00078 
00079 const char *MidiDriver::getErrorName(int error_code) {
00080     static const char *const midi_errors[] = {
00081         "No error",
00082         "Cannot connect",
00083         "Streaming not available",
00084         "Device not available",
00085         "Driver already open"
00086     };
00087 
00088     if ((uint)error_code >= ARRAYSIZE(midi_errors))
00089         return "Unknown Error";
00090     return midi_errors[error_code];
00091 }
00092 
00093 MidiDriver_MPU401::MidiDriver_MPU401() :
00094     MidiDriver(),
00095     _timer_proc(0),
00096     _channel_mask(0xFFFF) // Permit all 16 channels by default
00097 {
00098 
00099     uint i;
00100     for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
00101         _midi_channels[i].init(this, i);
00102     }
00103 }
00104 
00105 MidiDriver_MPU401::~MidiDriver_MPU401() {
00106 }
00107 
00108 void MidiDriver_MPU401::close() {
00109     if (_timer_proc) {
00110         g_system->getTimerManager()->removeTimerProc(_timer_proc);
00111         _timer_proc = 0;
00112     }
00113     if (isOpen()) {
00114         for (int i = 0; i < 16; ++i)
00115             send(0x7B << 8 | 0xB0 | i);
00116     }
00117 }
00118 
00119 uint32 MidiDriver_MPU401::property(int prop, uint32 param) {
00120     switch (prop) {
00121     case PROP_CHANNEL_MASK:
00122         _channel_mask = param & 0xFFFF;
00123         return 1;
00124     }
00125 
00126     return 0;
00127 }
00128 
00129 MidiChannel *MidiDriver_MPU401::allocateChannel() {
00130     MidiChannel_MPU401 *chan;
00131     uint i;
00132 
00133     for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) {
00134         if (i == 9 || !(_channel_mask & (1 << i)))
00135             continue;
00136         chan = &_midi_channels[i];
00137         if (chan->allocate()) {
00138             return chan;
00139         }
00140     }
00141     return NULL;
00142 }
00143 
00144 void MidiDriver_MPU401::setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) {
00145     if (!_timer_proc || !timer_proc) {
00146         if (_timer_proc)
00147             g_system->getTimerManager()->removeTimerProc(_timer_proc);
00148         _timer_proc = timer_proc;
00149         if (timer_proc)
00150             g_system->getTimerManager()->installTimerProc(timer_proc, 10000, timer_param, "MPU401");
00151     }
00152 }


Generated on Sat Feb 16 2019 05:00:59 for ResidualVM by doxygen 1.7.1
curved edge   curved edge