Merge pull request #20154 from marcelofg55/midi_driver

Added a new MIDIDriver class
This commit is contained in:
Rémi Verschelde 2018-07-25 01:17:57 +02:00 committed by GitHub
commit a501678ba1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 1029 additions and 4 deletions

5
drivers/SCsub vendored
View file

@ -21,6 +21,11 @@ if (env["platform"] == "windows"):
if env['xaudio2']:
SConscript("xaudio2/SCsub")
# Midi drivers
SConscript('alsamidi/SCsub')
SConscript('coremidi/SCsub')
SConscript('winmidi/SCsub')
# Graphics drivers
if (env["platform"] != "server"):
SConscript('gles3/SCsub')

8
drivers/alsamidi/SCsub Normal file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env python
Import('env')
# Driver source files
env.add_source_files(env.drivers_sources, "*.cpp")
Export('env')

View file

@ -0,0 +1,201 @@
/*************************************************************************/
/* alsa_midi.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifdef ALSAMIDI_ENABLED
#include <errno.h>
#include "alsa_midi.h"
#include "os/os.h"
#include "print_string.h"
static int get_message_size(uint8_t message) {
switch (message & 0xF0) {
case 0x80: // note off
case 0x90: // note on
case 0xA0: // aftertouch
case 0xB0: // continuous controller
return 3;
case 0xC0: // patch change
case 0xD0: // channel pressure
case 0xE0: // pitch bend
return 2;
}
return 256;
}
void MIDIDriverALSAMidi::thread_func(void *p_udata) {
MIDIDriverALSAMidi *md = (MIDIDriverALSAMidi *)p_udata;
uint64_t timestamp = 0;
uint8_t buffer[256];
int expected_size = 255;
int bytes = 0;
while (!md->exit_thread) {
int ret;
md->lock();
for (int i = 0; i < md->connected_inputs.size(); i++) {
snd_rawmidi_t *midi_in = md->connected_inputs[i];
do {
uint8_t byte = 0;
ret = snd_rawmidi_read(midi_in, &byte, 1);
if (ret < 0) {
if (ret != -EAGAIN) {
ERR_PRINTS("snd_rawmidi_read error: " + String(snd_strerror(ret)));
}
} else {
if (byte & 0x80) {
// Flush previous packet if there is any
if (bytes) {
md->receive_input_packet(timestamp, buffer, bytes);
bytes = 0;
}
expected_size = get_message_size(byte);
}
if (bytes < 256) {
buffer[bytes++] = byte;
// If we know the size of the current packet receive it if it reached the expected size
if (bytes >= expected_size) {
md->receive_input_packet(timestamp, buffer, bytes);
bytes = 0;
}
}
}
} while (ret > 0);
}
md->unlock();
OS::get_singleton()->delay_usec(1000);
}
}
Error MIDIDriverALSAMidi::open() {
void **hints;
if (snd_device_name_hint(-1, "rawmidi", &hints) < 0)
return ERR_CANT_OPEN;
int i = 0;
for (void **n = hints; *n != NULL; n++) {
char *name = snd_device_name_get_hint(*n, "NAME");
if (name != NULL) {
snd_rawmidi_t *midi_in;
int ret = snd_rawmidi_open(&midi_in, NULL, name, SND_RAWMIDI_NONBLOCK);
if (ret >= 0) {
connected_inputs.insert(i++, midi_in);
}
}
if (name != NULL)
free(name);
}
snd_device_name_free_hint(hints);
mutex = Mutex::create();
thread = Thread::create(MIDIDriverALSAMidi::thread_func, this);
return OK;
}
void MIDIDriverALSAMidi::close() {
if (thread) {
exit_thread = true;
Thread::wait_to_finish(thread);
memdelete(thread);
thread = NULL;
}
if (mutex) {
memdelete(mutex);
mutex = NULL;
}
for (int i = 0; i < connected_inputs.size(); i++) {
snd_rawmidi_t *midi_in = connected_inputs[i];
snd_rawmidi_close(midi_in);
}
connected_inputs.clear();
}
void MIDIDriverALSAMidi::lock() const {
if (mutex)
mutex->lock();
}
void MIDIDriverALSAMidi::unlock() const {
if (mutex)
mutex->unlock();
}
PoolStringArray MIDIDriverALSAMidi::get_connected_inputs() {
PoolStringArray list;
lock();
for (int i = 0; i < connected_inputs.size(); i++) {
snd_rawmidi_t *midi_in = connected_inputs[i];
snd_rawmidi_info_t *info;
snd_rawmidi_info_malloc(&info);
snd_rawmidi_info(midi_in, info);
list.push_back(snd_rawmidi_info_get_name(info));
snd_rawmidi_info_free(info);
}
unlock();
return list;
}
MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
mutex = NULL;
thread = NULL;
exit_thread = false;
}
MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
close();
}
#endif

View file

@ -0,0 +1,69 @@
/*************************************************************************/
/* alsa_midi.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifdef ALSAMIDI_ENABLED
#ifndef ALSA_MIDI_H
#define ALSA_MIDI_H
#include <alsa/asoundlib.h>
#include <stdio.h>
#include "core/os/mutex.h"
#include "core/os/thread.h"
#include "core/vector.h"
#include "os/midi_driver.h"
class MIDIDriverALSAMidi : public MIDIDriver {
Thread *thread;
Mutex *mutex;
Vector<snd_rawmidi_t *> connected_inputs;
bool exit_thread;
static void thread_func(void *p_udata);
void lock() const;
void unlock() const;
public:
virtual Error open();
virtual void close();
virtual PoolStringArray get_connected_inputs();
MIDIDriverALSAMidi();
virtual ~MIDIDriverALSAMidi();
};
#endif
#endif

8
drivers/coremidi/SCsub Normal file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env python
Import('env')
# Driver source files
env.add_source_files(env.drivers_sources, "*.cpp")
Export('env')

View file

@ -0,0 +1,105 @@
/*************************************************************************/
/* core_midi.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifdef COREMIDI_ENABLED
#include "core_midi.h"
#include "print_string.h"
#include <CoreAudio/HostTime.h>
#include <CoreServices/CoreServices.h>
void MIDIDriverCoreMidi::read(const MIDIPacketList *packet_list, void *read_proc_ref_con, void *src_conn_ref_con) {
MIDIPacket *packet = const_cast<MIDIPacket *>(packet_list->packet);
for (int i = 0; i < packet_list->numPackets; i++) {
receive_input_packet(packet->timeStamp, packet->data, packet->length);
packet = MIDIPacketNext(packet);
}
}
Error MIDIDriverCoreMidi::open() {
CFStringRef name = CFStringCreateWithCString(NULL, "Godot", kCFStringEncodingASCII);
OSStatus result = MIDIClientCreate(name, NULL, NULL, &client);
CFRelease(name);
if (result != noErr) {
ERR_PRINTS("MIDIClientCreate failed: " + String(GetMacOSStatusErrorString(result)));
return ERR_CANT_OPEN;
}
result = MIDIInputPortCreate(client, CFSTR("Godot Input"), MIDIDriverCoreMidi::read, (void *)this, &port_in);
if (result != noErr) {
ERR_PRINTS("MIDIInputPortCreate failed: " + String(GetMacOSStatusErrorString(result)));
return ERR_CANT_OPEN;
}
int sources = MIDIGetNumberOfSources();
for (int i = 0; i < sources; i++) {
MIDIEndpointRef source = MIDIGetSource(i);
if (source != NULL) {
MIDIPortConnectSource(port_in, source, (void *)this);
connected_sources.insert(i, source);
}
}
return OK;
}
void MIDIDriverCoreMidi::close() {
for (int i = 0; i < connected_sources.size(); i++) {
MIDIEndpointRef source = connected_sources[i];
MIDIPortDisconnectSource(port_in, source);
}
connected_sources.clear();
if (port_in != 0) {
MIDIPortDispose(port_in);
port_in = 0;
}
if (client != 0) {
MIDIClientDispose(client);
client = 0;
}
}
MIDIDriverCoreMidi::MIDIDriverCoreMidi() {
client = 0;
}
MIDIDriverCoreMidi::~MIDIDriverCoreMidi() {
close();
}
#endif

View file

@ -0,0 +1,61 @@
/*************************************************************************/
/* core_midi.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifdef COREMIDI_ENABLED
#ifndef CORE_MIDI_H
#define CORE_MIDI_H
#include <stdio.h>
#include <CoreMIDI/CoreMIDI.h>
#include "core/vector.h"
#include "os/midi_driver.h"
class MIDIDriverCoreMidi : public MIDIDriver {
MIDIClientRef client;
MIDIPortRef port_in;
Vector<MIDIEndpointRef> connected_sources;
static void read(const MIDIPacketList *packet_list, void *read_proc_ref_con, void *src_conn_ref_con);
public:
virtual Error open();
virtual void close();
MIDIDriverCoreMidi();
virtual ~MIDIDriverCoreMidi();
};
#endif
#endif

8
drivers/winmidi/SCsub Normal file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env python
Import('env')
# Driver source files
env.add_source_files(env.drivers_sources, "*.cpp")
Export('env')

View file

@ -0,0 +1,100 @@
/*************************************************************************/
/* win_midi.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifdef WINMIDI_ENABLED
#include "win_midi.h"
#include "print_string.h"
void MIDIDriverWinMidi::read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
if (wMsg == MIM_DATA) {
receive_input_packet((uint64_t)dwParam2, (uint8_t *)&dwParam1, 3);
}
}
Error MIDIDriverWinMidi::open() {
for (UINT i = 0; i < midiInGetNumDevs(); i++) {
HMIDIIN midi_in;
MMRESULT res = midiInOpen(&midi_in, i, (DWORD_PTR)read, (DWORD_PTR)this, CALLBACK_FUNCTION);
if (res == MMSYSERR_NOERROR) {
midiInStart(midi_in);
connected_sources.insert(i, midi_in);
} else {
char err[256];
midiInGetErrorText(res, err, 256);
ERR_PRINTS("midiInOpen error: " + String(err));
}
}
return OK;
}
PoolStringArray MIDIDriverWinMidi::get_connected_inputs() {
PoolStringArray list;
for (int i = 0; i < connected_sources.size(); i++) {
HMIDIIN midi_in = connected_sources[i];
UINT id = 0;
MMRESULT res = midiInGetID(midi_in, &id);
if (res == MMSYSERR_NOERROR) {
MIDIINCAPS caps;
res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS));
if (res == MMSYSERR_NOERROR) {
list.push_back(caps.szPname);
}
}
}
return list;
}
void MIDIDriverWinMidi::close() {
for (int i = 0; i < connected_sources.size(); i++) {
HMIDIIN midi_in = connected_sources[i];
midiInStop(midi_in);
midiInClose(midi_in);
}
connected_sources.clear();
}
MIDIDriverWinMidi::MIDIDriverWinMidi() {
}
MIDIDriverWinMidi::~MIDIDriverWinMidi() {
close();
}
#endif

View file

@ -0,0 +1,61 @@
/*************************************************************************/
/* win_midi.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifdef WINMIDI_ENABLED
#ifndef WIN_MIDI_H
#define WIN_MIDI_H
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include "core/vector.h"
#include "os/midi_driver.h"
class MIDIDriverWinMidi : public MIDIDriver {
Vector<HMIDIIN> connected_sources;
static void CALLBACK read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
public:
virtual Error open();
virtual void close();
virtual PoolStringArray get_connected_inputs();
MIDIDriverWinMidi();
virtual ~MIDIDriverWinMidi();
};
#endif
#endif