diff options
author | Max Horn | 2002-12-08 20:19:01 +0000 |
---|---|---|
committer | Max Horn | 2002-12-08 20:19:01 +0000 |
commit | 0ac3214fd581e6782b09dcffe220eed2bc09c3dd (patch) | |
tree | 29d8aebd2981ab4a28aee1f9da26327fa02744f1 | |
parent | 3a9ab200921fc3a71ef8c30e73fd82f78a0eba08 (diff) | |
download | scummvm-rg350-0ac3214fd581e6782b09dcffe220eed2bc09c3dd.tar.gz scummvm-rg350-0ac3214fd581e6782b09dcffe220eed2bc09c3dd.tar.bz2 scummvm-rg350-0ac3214fd581e6782b09dcffe220eed2bc09c3dd.zip |
started to split out the midi drivers
svn-id: r5889
-rw-r--r-- | backends/midi/adlib.cpp | 10 | ||||
-rw-r--r-- | backends/midi/coreaudio.cpp | 133 | ||||
-rw-r--r-- | backends/midi/null.cpp | 43 | ||||
-rw-r--r-- | backends/midi/quicktime.cpp | 260 | ||||
-rw-r--r-- | backends/module.mk | 5 | ||||
-rw-r--r-- | sound/mididrv.cpp | 562 | ||||
-rw-r--r-- | sound/module.mk | 3 | ||||
-rw-r--r-- | sound/mpu401.cpp | 104 | ||||
-rw-r--r-- | sound/mpu401.h | 96 |
9 files changed, 659 insertions, 557 deletions
diff --git a/backends/midi/adlib.cpp b/backends/midi/adlib.cpp index f19596ffd6..affa88afd8 100644 --- a/backends/midi/adlib.cpp +++ b/backends/midi/adlib.cpp @@ -1,5 +1,4 @@ /* ScummVM - Scumm Interpreter - * Copyright (C) 2001 Ludvig Strigeus * Copyright (C) 2001/2002 The ScummVM project * * This program is free software; you can redistribute it and/or @@ -19,15 +18,6 @@ * $Header$ */ -/* - * Timidity support by Lionel Ulmer <lionel.ulmer@free.fr> - * QuickTime support by Florent Boudet <flobo@ifrance.com> - * Raw output support by Michael Pearce - * MorphOS support by Ruediger Hanke - * Alsa support by Nicolas Noble <nicolas@nobis-crew.org> copied from - * both the QuickTime support and (vkeybd http://www.alsa-project.org/~iwai/alsa.html) - */ - #include "stdafx.h" #include "sound/mididrv.h" #include "sound/fmopl.h" diff --git a/backends/midi/coreaudio.cpp b/backends/midi/coreaudio.cpp new file mode 100644 index 0000000000..00cc81e4f5 --- /dev/null +++ b/backends/midi/coreaudio.cpp @@ -0,0 +1,133 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2001/2002 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$ + */ + +#ifdef MACOSX + +#include "sound/mpu401.h" + +#include <AudioUnit/AudioUnit.h> + + +// FIXME - the following disables reverb support in the QuickTime / CoreAudio +// midi backends. For some reasons, reverb will suck away a *lot* of CPU time. +// Until we know for sure what is causing this and if there is a better way to +// fix the problem, we just disable all reverb for these backends. +#define COREAUDIO_REVERB_HACK + + +/* CoreAudio MIDI driver + * Based on code by Benjamin W. Zale + * Extended by Max Horn + */ +class MidiDriver_CORE : public MidiDriver_MPU401 { +public: + MidiDriver_CORE() : au_MusicDevice(0), au_output(0) { } + int open(int mode); + void close(); + void send(uint32 b); + void pause(bool p) { } + void set_stream_callback(void *param, StreamCallback *sc) { } + +private: + AudioUnit au_MusicDevice; + AudioUnit au_output; + + int _mode; +}; + + +int MidiDriver_CORE::open(int mode) +{ + if (au_output != NULL) + return MERR_ALREADY_OPEN; + + if (mode == MO_STREAMING) + return MERR_STREAMING_NOT_AVAILABLE; + + _mode = mode; + + int err; + AudioUnitConnection auconnect; + ComponentDescription compdesc; + Component compid; + + // Open the Music Device + compdesc.componentType = kAudioUnitComponentType; + compdesc.componentSubType = kAudioUnitSubType_MusicDevice; + compdesc.componentManufacturer = kAudioUnitID_DLSSynth; + compdesc.componentFlags = 0; + compdesc.componentFlagsMask = 0; + compid = FindNextComponent(NULL, &compdesc); + au_MusicDevice = (AudioUnit) OpenComponent(compid); + + // open the output unit + au_output = (AudioUnit) OpenDefaultComponent(kAudioUnitComponentType, kAudioUnitSubType_Output); + + // connect the units + auconnect.sourceAudioUnit = au_MusicDevice; + auconnect.sourceOutputNumber = 0; + auconnect.destInputNumber = 0; + err = + AudioUnitSetProperty(au_output, kAudioUnitProperty_MakeConnection, kAudioUnitScope_Input, 0, + (void *)&auconnect, sizeof(AudioUnitConnection)); + + // initialize the units + AudioUnitInitialize(au_MusicDevice); + AudioUnitInitialize(au_output); + + // start the output + AudioOutputUnitStart(au_output); + + return 0; +} + +void MidiDriver_CORE::close() +{ + // Stop the output + AudioOutputUnitStop(au_output); + + // Cleanup + CloseComponent(au_output); + CloseComponent(au_MusicDevice); + + _mode = 0; +} + +void MidiDriver_CORE::send(uint32 b) +{ + unsigned char first_byte, seccond_byte, status_byte; + status_byte = (b & 0x000000FF); + first_byte = (b & 0x0000FF00) >> 8; + seccond_byte = (b & 0x00FF0000) >> 16; + +#ifdef COREAUDIO_REVERB_HACK + if ((status_byte&0xF0) == 0xB0 && first_byte == 0x5b) + return; +#endif + + MusicDeviceMIDIEvent(au_MusicDevice, status_byte, first_byte, seccond_byte, 0); +} + +MidiDriver *MidiDriver_CORE_create() +{ + return new MidiDriver_CORE(); +} + +#endif // MACOSX diff --git a/backends/midi/null.cpp b/backends/midi/null.cpp new file mode 100644 index 0000000000..ea54c9826a --- /dev/null +++ b/backends/midi/null.cpp @@ -0,0 +1,43 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2001/2002 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 "sound/mpu401.h" +#include "common/engine.h" // for warning/error/debug + +/* NULL driver */ +class MidiDriver_NULL : public MidiDriver_MPU401 { +public: + int open(int mode); + void close() { } + void send(uint32 b) { } + void pause(bool p) { } + void set_stream_callback(void *param, StreamCallback *sc) { } +}; + +int MidiDriver_NULL::open(int mode) +{ + warning("Music not enabled - MIDI support selected with no MIDI driver available. Try Adlib"); + return 0; +} + +MidiDriver *MidiDriver_NULL_create() +{ + return new MidiDriver_NULL(); +} diff --git a/backends/midi/quicktime.cpp b/backends/midi/quicktime.cpp new file mode 100644 index 0000000000..f08f3e2d79 --- /dev/null +++ b/backends/midi/quicktime.cpp @@ -0,0 +1,260 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2001/2002 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$ + */ + +#if defined(MACOSX) || defined(macintosh) + + +#include "sound/mpu401.h" +#include "common/engine.h" // for warning/error/debug + +#if defined(MACOSX) +#include <QuickTime/QuickTimeComponents.h> +#include <QuickTime/QuickTimeMusic.h> +#else +#include <QuickTimeComponents.h> +#include <QuickTimeMusic.h> +#endif + + +// FIXME - the following disables reverb support in the QuickTime / CoreAudio +// midi backends. For some reasons, reverb will suck away a *lot* of CPU time. +// Until we know for sure what is causing this and if there is a better way to +// fix the problem, we just disable all reverb for these backends. +#define COREAUDIO_REVERB_HACK + + +/* QuickTime MIDI driver + * Original QuickTime support by Florent Boudet <flobo@ifrance.com> + * Modified by Max Horn + */ +class MidiDriver_QT : public MidiDriver_MPU401 { +public: + int open(int mode); + void close(); + void send(uint32 b); + void pause(bool p) { } + void set_stream_callback(void *param, StreamCallback *sc); + void setPitchBendRange (byte channel, uint range); + +private: + NoteAllocator qtNoteAllocator; + NoteChannel qtNoteChannel[16]; + NoteRequest simpleNoteRequest; + + StreamCallback *_stream_proc; + void *_stream_param; + int _mode; + + // Pitch bend tracking. Necessary since QTMA handles + // pitch bending so differently from MPU401. + uint16 _pitchbend [16]; + byte _pitchbend_range [16]; +}; + +void MidiDriver_QT::set_stream_callback(void *param, StreamCallback *sc) +{ + _stream_param = param; + _stream_proc = sc; +} + +int MidiDriver_QT::open(int mode) +{ + ComponentResult qtErr = noErr; + int i; + + qtNoteAllocator = NULL; + + if (mode == MO_STREAMING) + return MERR_STREAMING_NOT_AVAILABLE; + + _mode = mode; + + for (i = 0; i < 15; i++) + qtNoteChannel[i] = NULL; + + qtNoteAllocator = OpenDefaultComponent(kNoteAllocatorComponentType, 0); + if (qtNoteAllocator == NULL) + goto bail; + + simpleNoteRequest.info.flags = 0; + *(short *)(&simpleNoteRequest.info.polyphony) = EndianS16_NtoB(15); // simultaneous tones + *(Fixed *) (&simpleNoteRequest.info.typicalPolyphony) = EndianU32_NtoB(0x00010000); + + qtErr = NAStuffToneDescription(qtNoteAllocator, 1, &simpleNoteRequest.tone); + if (qtErr != noErr) + goto bail; + + for (i = 0; i < 15; i++) { + qtErr = NANewNoteChannel(qtNoteAllocator, &simpleNoteRequest, &(qtNoteChannel[i])); + if ((qtErr != noErr) || (qtNoteChannel == NULL)) + goto bail; + } + return 0; + +bail: + error("Init QT failed %x %x %d\n", (int)qtNoteAllocator, (int)qtNoteChannel, (int)qtErr); + for (i = 0; i < 15; i++) { + if (qtNoteChannel[i] != NULL) + NADisposeNoteChannel(qtNoteAllocator, qtNoteChannel[i]); + qtNoteChannel[i] = NULL; + } + + if (qtNoteAllocator != NULL) { + CloseComponent(qtNoteAllocator); + qtNoteAllocator = NULL; + } + + return MERR_DEVICE_NOT_AVAILABLE; +} + +void MidiDriver_QT::close() +{ + _mode = 0; + + for (int i = 0; i < 15; i++) { + if (qtNoteChannel[i] != NULL) + NADisposeNoteChannel(qtNoteAllocator, qtNoteChannel[i]); + qtNoteChannel[i] = NULL; + } + + if (qtNoteAllocator != NULL) { + CloseComponent(qtNoteAllocator); + qtNoteAllocator = NULL; + } +} + +void MidiDriver_QT::send(uint32 b) +{ + if (_mode != MO_SIMPLE) + error("MidiDriver_QT:send called but driver is not in simple mode"); + + MusicMIDIPacket midPacket; + unsigned char *midiCmd = midPacket.data; + midPacket.length = 3; + midiCmd[3] = (b & 0xFF000000) >> 24; + midiCmd[2] = (b & 0x00FF0000) >> 16; + midiCmd[1] = (b & 0x0000FF00) >> 8; + midiCmd[0] = (b & 0x000000FF); + + unsigned char chanID = midiCmd[0] & 0x0F; + switch (midiCmd[0] & 0xF0) { + case 0x80: // Note off + NAPlayNote(qtNoteAllocator, qtNoteChannel[chanID], midiCmd[1], 0); + break; + + case 0x90: // Note on + NAPlayNote(qtNoteAllocator, qtNoteChannel[chanID], midiCmd[1], midiCmd[2]); + break; + + case 0xB0: // Effect + switch (midiCmd[1]) { + case 0x01: // Modulation + NASetController(qtNoteAllocator, qtNoteChannel[chanID], + kControllerModulationWheel, midiCmd[2] << 8); + break; + + case 0x07: // Volume + NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerVolume, midiCmd[2] * 300); + break; + + case 0x0A: // Pan + NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerPan, + (midiCmd[2] << 1) + 0xFF); + break; + + case 0x40: // Sustain on/off + NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerSustain, midiCmd[2]); + break; + + case 0x5b: // ext effect depth +#if !defined(COREAUDIO_REVERB_HACK) + NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerReverb, midiCmd[2] << 8); +#endif + break; + + case 0x5d: // chorus depth + NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerChorus, midiCmd[2] << 8); + break; + + case 0x7b: // mode message all notes off + for (int i = 0; i < 128; i++) + NAPlayNote(qtNoteAllocator, qtNoteChannel[chanID], i, 0); + break; + case 0x64: + case 0x65: + case 0x06: + case 0x26: + // pitch bend changes - ignore those for now + break; + + case 0x12: // Occurs in Scumm games + case 0x79: // Occurs in Simon1 + // What are these ?!? Ignore it for now + break; + + default: + // Error: Unknown MIDI effect: 007f76b3 + warning("Unknown MIDI effect: %08x", (int)b); + break; + } + break; + + case 0xC0: // Program change + NASetInstrumentNumber(qtNoteAllocator, qtNoteChannel[chanID], midiCmd[1] + 1); + break; + + case 0xE0:{ // Pitch bend + // QuickTime specifies pitchbend in semitones, using 8.8 fixed point values; + // but iMuse sends us the pitch bend data as 0-16383. which has to be mapped + // to +/- 12 semitones. Based on this, we first center the input data, then + // multiply it by a factor. If all was right, the factor would be 3/8, but for + // mysterious reasons the actual factor we have to use is more like 1/32 or 3/64. + // Maybe the QT docs are right, and + _pitchbend[chanID] = ((uint16) midiCmd[1] | (uint16) (midiCmd[2] << 7)); + long theBend = ((long) _pitchbend[chanID] - 0x2000) * _pitchbend_range[chanID] / 32; + + NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerPitchBend, theBend); + } + break; + + default: + error("Unknown Command: %08x", (int)b); + NASendMIDI(qtNoteAllocator, qtNoteChannel[chanID], &midPacket); + break; + } +} + +void MidiDriver_QT::setPitchBendRange (byte channel, uint range) +{ + if (_pitchbend_range[channel] == range) + return; + _pitchbend_range[channel] = range; + + long theBend = _pitchbend[channel]; + theBend = (theBend - 0x2000) * range / 32; + NASetController(qtNoteAllocator, qtNoteChannel[channel], kControllerPitchBend, theBend); +} + +MidiDriver *MidiDriver_QT_create() +{ + return new MidiDriver_QT(); +} + +#endif // MACOSX || macintosh diff --git a/backends/module.mk b/backends/module.mk index 03fa22b42f..4e36c8cd2a 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -4,7 +4,10 @@ MODULE_OBJS = \ backends/fs/posix/posix-fs.o \ backends/fs/morphos/abox-fs.o \ backends/fs/windows/windows-fs.o \ - backends/midi/adlib.o + backends/midi/adlib.o \ + backends/midi/coreaudio.o \ + backends/midi/null.o \ + backends/midi/quicktime.o # Include common rules include common.rules diff --git a/sound/mididrv.cpp b/sound/mididrv.cpp index ce379a51c6..c13cb8e0f5 100644 --- a/sound/mididrv.cpp +++ b/sound/mididrv.cpp @@ -20,8 +20,6 @@ */ /* - * Timidity support by Lionel Ulmer <lionel.ulmer@free.fr> - * QuickTime support by Florent Boudet <flobo@ifrance.com> * Raw output support by Michael Pearce * MorphOS support by Ruediger Hanke * Alsa support by Nicolas Noble <nicolas@nobis-crew.org> copied from @@ -30,170 +28,35 @@ #include "stdafx.h" #include "mididrv.h" -#include "fmopl.h" -#include "mixer.h" +#include "mpu401.h" #include "common/engine.h" // for warning/error/debug -#include "common/util.h" +#include "common/util.h" // for ARRAYSIZE -//////////////////////////////////////// -// -// Common MPU401 implementation methods -// -//////////////////////////////////////// - -typedef void TimerCallback (void *); - -class MidiDriver_MPU401; - -class MidiChannel_MPU401 : public MidiChannel { - friend class MidiDriver_MPU401; - -private: - MidiDriver_MPU401 *_owner; - bool _allocated; - byte _channel; - - void init (MidiDriver_MPU401 *owner, byte channel); - void allocate() { _allocated = true; } - -public: - void release() { _allocated = false; } - - // Regular messages - void noteOff (byte note); - void noteOn (byte note, byte velocity); - void programChange (byte program); - void pitchBend (int16 bend); - - // Control Change messages - void controlChange (byte control, byte value); - void modulationWheel (byte value) { controlChange (1, value); } - void volume (byte value) { controlChange (7, value); } - void panPosition (byte value) { controlChange (10, value); } - void pitchBendFactor (byte value); - void detune (byte value) { controlChange (17, value); } - void priority (byte value) { controlChange (18, value); } - void sustain (bool value) { controlChange (64, value ? 1 : 0); } - void effectLevel (byte value) { controlChange (91, value); } - void chorusLevel (byte value) { controlChange (93, value); } - void allNotesOff() { controlChange (123, 0); } - - // SysEx messages - void sysEx_customInstrument (uint32 type, byte *instr); -}; - - - -class MidiDriver_MPU401 : public MidiDriver { -private: - MidiChannel_MPU401 _midi_channels [16]; - bool _started_thread; - TimerCallback *_timer_proc; - void *_timer_param; - - static int midi_driver_thread (void *param); - -public: - MidiDriver_MPU401(); - - virtual void setTimerCallback (void *timer_param, void (*timer_proc) (void *)); - virtual uint32 getBaseTempo (void) { return 0x4A0000; } - - virtual MidiChannel *allocateChannel(); - virtual MidiChannel *getPercussionChannel() { return &_midi_channels [9]; } -}; - - - -void MidiChannel_MPU401::init (MidiDriver_MPU401 *owner, byte channel) -{ - _owner = owner; - _channel = channel; - _allocated = false; -} - -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); } - - - -MidiDriver_MPU401::MidiDriver_MPU401() : MidiDriver() -{ - uint i; - for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) { - _midi_channels [i].init (this, i); - } -} - - - -MidiChannel *MidiDriver_MPU401::allocateChannel() +/* Default (empty) property method */ +uint32 MidiDriver::property(int prop, uint32 param) { - MidiChannel_MPU401 *chan; - uint i; - - for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) { - if (i == 9) continue; - chan = &_midi_channels[i]; - if (!chan->_allocated) { - chan->allocate(); - return (chan); - } - } - return NULL; + return 0; } -void MidiDriver_MPU401::setTimerCallback (void *timer_param, void (*timer_proc) (void *)) -{ - if (!_timer_proc || !timer_proc) { - _timer_proc = (TimerCallback *) timer_proc; - _timer_param = timer_param; - if (!_started_thread && timer_proc) - g_system->create_thread (midi_driver_thread, this); - _started_thread = true; - } -} - -int MidiDriver_MPU401::midi_driver_thread(void *param) +/* retrieve a string representation of an error code */ +const char *MidiDriver::get_error_name(int error_code) { - MidiDriver_MPU401 *mid = (MidiDriver_MPU401 *)param; - int old_time, cur_time; - - old_time = g_system->get_msecs(); - - for (;;) { - g_system->delay_msecs(10); - - cur_time = g_system->get_msecs(); - while (old_time < cur_time) { - old_time += 10; - // Don't use mid->_se_on_timer() - // We must come in through IMuseMonitor to protect - // against conflicts with script access to IMuse. - if (mid->_timer_proc) - (*(mid->_timer_proc)) (mid->_timer_param); - } - } + static const char *const midi_errors[] = { + "No error", + "Cannot connect", + "Streaming not available", + "Device not available", + "Driver already open" + }; - return 0; + if ((uint) error_code >= ARRAYSIZE(midi_errors)) + return "Unknown Error"; + return midi_errors[error_code]; } - - -// FIXME - the following disables reverb support in the QuickTime / CoreAudio -// midi backends. For some reasons, reverb will suck away a *lot* of CPU time. -// Until we know for sure what is causing this and if there is a better way to -// fix the problem, we just disable all reverb for these backends. -#define COREAUDIO_REVERB_HACK - #if defined(WIN32) && !defined(_WIN32_WCE) /* Windows MIDI driver */ @@ -792,397 +655,6 @@ MidiDriver *MidiDriver_SEQ_create() #endif -#if defined(__APPLE__) || defined(macintosh) -// FIXME - this is for Mac OS X and Mac OS 9. It's not really possible -// to check for these *cleanly* without a configure script, though.. - - -#if defined(MACOSX) - // On MacOSX, these are in a frameworks! -#include <QuickTime/QuickTimeComponents.h> -#include <QuickTime/QuickTimeMusic.h> -#else -#include <QuickTimeComponents.h> -#include <QuickTimeMusic.h> -#endif - - -/* QuickTime MIDI driver */ -class MidiDriver_QT:public MidiDriver_MPU401 { -public: - int open(int mode); - void close(); - void send(uint32 b); - void pause(bool p) { } - void set_stream_callback(void *param, StreamCallback *sc); - void setPitchBendRange (byte channel, uint range); - -private: - NoteAllocator qtNoteAllocator; - NoteChannel qtNoteChannel[16]; - NoteRequest simpleNoteRequest; - - StreamCallback *_stream_proc; - void *_stream_param; - int _mode; - - // Pitch bend tracking. Necessary since QTMA handles - // pitch bending so differently from MPU401. - uint16 _pitchbend [16]; - byte _pitchbend_range [16]; -}; - -void MidiDriver_QT::set_stream_callback(void *param, StreamCallback *sc) -{ - _stream_param = param; - _stream_proc = sc; -} - -int MidiDriver_QT::open(int mode) -{ - ComponentResult qtErr = noErr; - int i; - - qtNoteAllocator = NULL; - - if (mode == MO_STREAMING) - return MERR_STREAMING_NOT_AVAILABLE; - - _mode = mode; - - for (i = 0; i < 15; i++) - qtNoteChannel[i] = NULL; - - qtNoteAllocator = OpenDefaultComponent(kNoteAllocatorComponentType, 0); - if (qtNoteAllocator == NULL) - goto bail; - - simpleNoteRequest.info.flags = 0; - *(short *)(&simpleNoteRequest.info.polyphony) = EndianS16_NtoB(15); // simultaneous tones - *(Fixed *) (&simpleNoteRequest.info.typicalPolyphony) = EndianU32_NtoB(0x00010000); - - qtErr = NAStuffToneDescription(qtNoteAllocator, 1, &simpleNoteRequest.tone); - if (qtErr != noErr) - goto bail; - - for (i = 0; i < 15; i++) { - qtErr = NANewNoteChannel(qtNoteAllocator, &simpleNoteRequest, &(qtNoteChannel[i])); - if ((qtErr != noErr) || (qtNoteChannel == NULL)) - goto bail; - } - return 0; - -bail: - error("Init QT failed %x %x %d\n", (int)qtNoteAllocator, (int)qtNoteChannel, (int)qtErr); - for (i = 0; i < 15; i++) { - if (qtNoteChannel[i] != NULL) - NADisposeNoteChannel(qtNoteAllocator, qtNoteChannel[i]); - qtNoteChannel[i] = NULL; - } - - if (qtNoteAllocator != NULL) { - CloseComponent(qtNoteAllocator); - qtNoteAllocator = NULL; - } - - return MERR_DEVICE_NOT_AVAILABLE; -} - -void MidiDriver_QT::close() -{ - _mode = 0; - - for (int i = 0; i < 15; i++) { - if (qtNoteChannel[i] != NULL) - NADisposeNoteChannel(qtNoteAllocator, qtNoteChannel[i]); - qtNoteChannel[i] = NULL; - } - - if (qtNoteAllocator != NULL) { - CloseComponent(qtNoteAllocator); - qtNoteAllocator = NULL; - } -} - -void MidiDriver_QT::send(uint32 b) -{ - if (_mode != MO_SIMPLE) - error("MidiDriver_QT:send called but driver is not in simple mode"); - - MusicMIDIPacket midPacket; - unsigned char *midiCmd = midPacket.data; - midPacket.length = 3; - midiCmd[3] = (b & 0xFF000000) >> 24; - midiCmd[2] = (b & 0x00FF0000) >> 16; - midiCmd[1] = (b & 0x0000FF00) >> 8; - midiCmd[0] = (b & 0x000000FF); - - unsigned char chanID = midiCmd[0] & 0x0F; - switch (midiCmd[0] & 0xF0) { - case 0x80: // Note off - NAPlayNote(qtNoteAllocator, qtNoteChannel[chanID], midiCmd[1], 0); - break; - - case 0x90: // Note on - NAPlayNote(qtNoteAllocator, qtNoteChannel[chanID], midiCmd[1], midiCmd[2]); - break; - - case 0xB0: // Effect - switch (midiCmd[1]) { - case 0x01: // Modulation - NASetController(qtNoteAllocator, qtNoteChannel[chanID], - kControllerModulationWheel, midiCmd[2] << 8); - break; - - case 0x07: // Volume - NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerVolume, midiCmd[2] * 300); - break; - - case 0x0A: // Pan - NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerPan, - (midiCmd[2] << 1) + 0xFF); - break; - - case 0x40: // Sustain on/off - NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerSustain, midiCmd[2]); - break; - - case 0x5b: // ext effect depth -#if !defined(COREAUDIO_REVERB_HACK) - NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerReverb, midiCmd[2] << 8); -#endif - break; - - case 0x5d: // chorus depth - NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerChorus, midiCmd[2] << 8); - break; - - case 0x7b: // mode message all notes off - for (int i = 0; i < 128; i++) - NAPlayNote(qtNoteAllocator, qtNoteChannel[chanID], i, 0); - break; - case 0x64: - case 0x65: - case 0x06: - case 0x26: - // pitch bend changes - ignore those for now - break; - - case 0x12: // Occurs in Scumm games - case 0x79: // Occurs in Simon1 - // What are these ?!? Ignore it for now - break; - - default: - // Error: Unknown MIDI effect: 007f76b3 - warning("Unknown MIDI effect: %08x", (int)b); - break; - } - break; - - case 0xC0: // Program change - NASetInstrumentNumber(qtNoteAllocator, qtNoteChannel[chanID], midiCmd[1] + 1); - break; - - case 0xE0:{ // Pitch bend - // QuickTime specifies pitchbend in semitones, using 8.8 fixed point values; - // but iMuse sends us the pitch bend data as 0-16383. which has to be mapped - // to +/- 12 semitones. Based on this, we first center the input data, then - // multiply it by a factor. If all was right, the factor would be 3/8, but for - // mysterious reasons the actual factor we have to use is more like 1/32 or 3/64. - // Maybe the QT docs are right, and - _pitchbend[chanID] = ((uint16) midiCmd[1] | (uint16) (midiCmd[2] << 7)); - long theBend = ((long) _pitchbend[chanID] - 0x2000) * _pitchbend_range[chanID] / 32; - - NASetController(qtNoteAllocator, qtNoteChannel[chanID], kControllerPitchBend, theBend); - } - break; - - default: - error("Unknown Command: %08x", (int)b); - NASendMIDI(qtNoteAllocator, qtNoteChannel[chanID], &midPacket); - break; - } -} - -void MidiDriver_QT::setPitchBendRange (byte channel, uint range) -{ - if (_pitchbend_range[channel] == range) - return; - _pitchbend_range[channel] = range; - - long theBend = _pitchbend[channel]; - theBend = (theBend - 0x2000) * range / 32; - NASetController(qtNoteAllocator, qtNoteChannel[channel], kControllerPitchBend, theBend); -} - -MidiDriver *MidiDriver_QT_create() -{ - return new MidiDriver_QT(); -} - -#endif // __APPLE__ || macintosh - - -#ifdef __APPLE__ - -#include <AudioUnit/AudioUnit.h> - -/* CoreAudio MIDI driver */ -/* Based on code by Benjamin W. Zale */ -class MidiDriver_CORE:public MidiDriver_MPU401 { -public: - MidiDriver_CORE():au_MusicDevice(NULL), au_output(NULL) { - } int open(int mode); - void close(); - void send(uint32 b); - void pause(bool p); - void set_stream_callback(void *param, StreamCallback *sc); - // void setPitchBendRange (byte channel, uint range); - -private: - AudioUnit au_MusicDevice; - AudioUnit au_output; - - int _mode; -}; - - -void MidiDriver_CORE::set_stream_callback(void *param, StreamCallback *sc) -{ -} - - -int MidiDriver_CORE::open(int mode) -{ - if (au_output != NULL) - return MERR_ALREADY_OPEN; - - if (mode == MO_STREAMING) - return MERR_STREAMING_NOT_AVAILABLE; - - _mode = mode; - - int err; - AudioUnitConnection auconnect; - ComponentDescription compdesc; - Component compid; - - // Open the Music Device - compdesc.componentType = kAudioUnitComponentType; - compdesc.componentSubType = kAudioUnitSubType_MusicDevice; - compdesc.componentManufacturer = kAudioUnitID_DLSSynth; - compdesc.componentFlags = 0; - compdesc.componentFlagsMask = 0; - compid = FindNextComponent(NULL, &compdesc); - au_MusicDevice = (AudioUnit) OpenComponent(compid); - - // open the output unit - au_output = (AudioUnit) OpenDefaultComponent(kAudioUnitComponentType, kAudioUnitSubType_Output); - - // connect the units - auconnect.sourceAudioUnit = au_MusicDevice; - auconnect.sourceOutputNumber = 0; - auconnect.destInputNumber = 0; - err = - AudioUnitSetProperty(au_output, kAudioUnitProperty_MakeConnection, kAudioUnitScope_Input, 0, - (void *)&auconnect, sizeof(AudioUnitConnection)); - - // initialize the units - AudioUnitInitialize(au_MusicDevice); - AudioUnitInitialize(au_output); - - // start the output - AudioOutputUnitStart(au_output); - - return 0; -} - -void MidiDriver_CORE::close() -{ - // Stop the output - AudioOutputUnitStop(au_output); - - // Cleanup - CloseComponent(au_output); - CloseComponent(au_MusicDevice); - - _mode = 0; -} - -void MidiDriver_CORE::send(uint32 b) -{ - unsigned char first_byte, seccond_byte, status_byte; - status_byte = (b & 0x000000FF); - first_byte = (b & 0x0000FF00) >> 8; - seccond_byte = (b & 0x00FF0000) >> 16; - -#ifdef COREAUDIO_REVERB_HACK - if ((status_byte&0xF0) == 0xB0 && first_byte == 0x5b) - return; -#endif - - MusicDeviceMIDIEvent(au_MusicDevice, status_byte, first_byte, seccond_byte, 0); -} - -void MidiDriver_CORE::pause(bool) -{ -} - -MidiDriver *MidiDriver_CORE_create() -{ - return new MidiDriver_CORE(); -} - -#endif // __APPLE__ - -/* NULL driver */ -class MidiDriver_NULL:public MidiDriver_MPU401 { -public: - int open(int mode); - void close() { } - void send(uint32 b) { } - void pause(bool p) { } - void set_stream_callback(void *param, StreamCallback *sc) { } - // void setPitchBendRange (byte channel, uint range); -private: -}; - -int MidiDriver_NULL::open(int mode) -{ - warning("Music not enabled - MIDI support selected with no MIDI driver available. Try Adlib"); - return 0; -} - -MidiDriver *MidiDriver_NULL_create() -{ - return new MidiDriver_NULL(); -} - - - -/* Default (empty) property method */ -uint32 MidiDriver::property(int prop, uint32 param) -{ - return 0; -} - - -/* retrieve a string representation of an error code */ -const char *MidiDriver::get_error_name(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]; -} #if defined(UNIX) && defined(USE_ALSA) diff --git a/sound/module.mk b/sound/module.mk index 41abc334f7..1e066a5837 100644 --- a/sound/module.mk +++ b/sound/module.mk @@ -4,7 +4,8 @@ MODULE_OBJS = \ sound/fmopl.o \ sound/mididrv.o \ sound/midistreamer.o \ - sound/mixer.o + sound/mixer.o \ + sound/mpu401.o # Include common rules include common.rules diff --git a/sound/mpu401.cpp b/sound/mpu401.cpp new file mode 100644 index 0000000000..f2e2ca5e9e --- /dev/null +++ b/sound/mpu401.cpp @@ -0,0 +1,104 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2001/2002 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 "common/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; +} + +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); } + + +MidiDriver_MPU401::MidiDriver_MPU401() : MidiDriver() +{ + uint i; + for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) { + _midi_channels [i].init (this, i); + } +} + + + +MidiChannel *MidiDriver_MPU401::allocateChannel() +{ + MidiChannel_MPU401 *chan; + uint i; + + for (i = 0; i < ARRAYSIZE(_midi_channels); ++i) { + if (i == 9) continue; + chan = &_midi_channels[i]; + if (!chan->_allocated) { + chan->allocate(); + return (chan); + } + } + return NULL; +} + + +void MidiDriver_MPU401::setTimerCallback (void *timer_param, void (*timer_proc) (void *)) +{ + if (!_timer_proc || !timer_proc) { + _timer_proc = (TimerCallback *) timer_proc; + _timer_param = timer_param; + if (!_started_thread && timer_proc) + g_system->create_thread (midi_driver_thread, this); + _started_thread = true; + } +} + +int MidiDriver_MPU401::midi_driver_thread(void *param) +{ + MidiDriver_MPU401 *mid = (MidiDriver_MPU401 *)param; + int old_time, cur_time; + + old_time = g_system->get_msecs(); + + for (;;) { + g_system->delay_msecs(10); + + cur_time = g_system->get_msecs(); + while (old_time < cur_time) { + old_time += 10; + // Don't use mid->_se_on_timer() + // We must come in through IMuseMonitor to protect + // against conflicts with script access to IMuse. + if (mid->_timer_proc) + (*(mid->_timer_proc)) (mid->_timer_param); + } + } + + return 0; +} + diff --git a/sound/mpu401.h b/sound/mpu401.h new file mode 100644 index 0000000000..4de50b6418 --- /dev/null +++ b/sound/mpu401.h @@ -0,0 +1,96 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2001/2002 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$ + * + */ + +#ifndef SOUND_MPU401_H +#define SOUND_MPU401_H + +#include "mididrv.h" + +//////////////////////////////////////// +// +// Common MPU401 implementation methods +// +//////////////////////////////////////// + +typedef void TimerCallback (void *); + +class MidiDriver_MPU401; + +class MidiChannel_MPU401 : public MidiChannel { + friend class MidiDriver_MPU401; + +private: + MidiDriver_MPU401 *_owner; + bool _allocated; + byte _channel; + + void init (MidiDriver_MPU401 *owner, byte channel); + void allocate() { _allocated = true; } + +public: + void release() { _allocated = false; } + + // Regular messages + void noteOff (byte note); + void noteOn (byte note, byte velocity); + void programChange (byte program); + void pitchBend (int16 bend); + + // Control Change messages + void controlChange (byte control, byte value); + void modulationWheel (byte value) { controlChange (1, value); } + void volume (byte value) { controlChange (7, value); } + void panPosition (byte value) { controlChange (10, value); } + void pitchBendFactor (byte value); + void detune (byte value) { controlChange (17, value); } + void priority (byte value) { controlChange (18, value); } + void sustain (bool value) { controlChange (64, value ? 1 : 0); } + void effectLevel (byte value) { controlChange (91, value); } + void chorusLevel (byte value) { controlChange (93, value); } + void allNotesOff() { controlChange (123, 0); } + + // SysEx messages + void sysEx_customInstrument (uint32 type, byte *instr); +}; + + + +class MidiDriver_MPU401 : public MidiDriver { +private: + MidiChannel_MPU401 _midi_channels [16]; + bool _started_thread; + TimerCallback *_timer_proc; + void *_timer_param; + + static int midi_driver_thread (void *param); + +public: + MidiDriver_MPU401(); + + virtual void setTimerCallback (void *timer_param, void (*timer_proc) (void *)); + virtual uint32 getBaseTempo (void) { return 0x4A0000; } + + virtual MidiChannel *allocateChannel(); + virtual MidiChannel *getPercussionChannel() { return &_midi_channels [9]; } +}; + + +#endif |