2018-07-14 12:11:28 +00:00
|
|
|
/*************************************************************************/
|
2019-02-12 13:23:35 +00:00
|
|
|
/* midi_driver_alsamidi.cpp */
|
2018-07-14 12:11:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-07-14 12:11:28 +00:00
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
|
2019-02-12 13:23:35 +00:00
|
|
|
#include "midi_driver_alsamidi.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include "core/os/os.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/print_string.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2018-07-14 12:11:28 +00:00
|
|
|
|
|
|
|
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
|
2020-01-21 20:46:25 +00:00
|
|
|
case 0xE0: // pitch bend
|
|
|
|
case 0xF2: // song position pointer
|
2018-07-14 12:11:28 +00:00
|
|
|
return 3;
|
|
|
|
|
|
|
|
case 0xC0: // patch change
|
|
|
|
case 0xD0: // channel pressure
|
2020-01-21 20:46:25 +00:00
|
|
|
case 0xF1: // time code quarter frame
|
|
|
|
case 0xF3: // song select
|
2018-07-14 12:11:28 +00:00
|
|
|
return 2;
|
2020-01-21 20:46:25 +00:00
|
|
|
|
|
|
|
case 0xF0: // SysEx start
|
|
|
|
case 0xF4: // reserved
|
|
|
|
case 0xF5: // reserved
|
|
|
|
case 0xF6: // tune request
|
|
|
|
case 0xF7: // SysEx end
|
|
|
|
case 0xF8: // timing clock
|
|
|
|
case 0xF9: // reserved
|
|
|
|
case 0xFA: // start
|
|
|
|
case 0xFB: // continue
|
|
|
|
case 0xFC: // stop
|
|
|
|
case 0xFD: // reserved
|
|
|
|
case 0xFE: // active sensing
|
|
|
|
case 0xFF: // reset
|
|
|
|
return 1;
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::thread_func(void *p_udata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata);
|
2018-07-14 12:11:28 +00:00
|
|
|
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) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(ret)));
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
} 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);
|
2020-01-21 20:46:25 +00:00
|
|
|
// After a SysEx start, all bytes are data until a SysEx end, so
|
|
|
|
// we're going to end the command at the SES, and let the common
|
|
|
|
// driver ignore the following data bytes.
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
|
2018-07-14 12:11:28 +00:00
|
|
|
return ERR_CANT_OPEN;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-07-14 12:11:28 +00:00
|
|
|
|
|
|
|
int i = 0;
|
2020-04-01 23:20:12 +00:00
|
|
|
for (void **n = hints; *n != nullptr; n++) {
|
2018-07-14 12:11:28 +00:00
|
|
|
char *name = snd_device_name_get_hint(*n, "NAME");
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
if (name != nullptr) {
|
2018-07-14 12:11:28 +00:00
|
|
|
snd_rawmidi_t *midi_in;
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
|
2018-07-14 12:11:28 +00:00
|
|
|
if (ret >= 0) {
|
|
|
|
connected_inputs.insert(i++, midi_in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (name != nullptr) {
|
2018-07-14 12:11:28 +00:00
|
|
|
free(name);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
snd_device_name_free_hint(hints);
|
|
|
|
|
2020-01-18 14:59:59 +00:00
|
|
|
exit_thread = false;
|
2021-01-19 12:29:41 +00:00
|
|
|
thread.start(MIDIDriverALSAMidi::thread_func, this);
|
2018-07-14 12:11:28 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::close() {
|
2021-01-19 12:29:41 +00:00
|
|
|
exit_thread = true;
|
|
|
|
thread.wait_to_finish();
|
2018-07-14 12:11:28 +00:00
|
|
|
|
|
|
|
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 {
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.lock();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::unlock() const {
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.unlock();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
PackedStringArray MIDIDriverALSAMidi::get_connected_inputs() {
|
|
|
|
PackedStringArray list;
|
2018-07-14 12:11:28 +00:00
|
|
|
|
|
|
|
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() {
|
|
|
|
exit_thread = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-02-12 13:23:35 +00:00
|
|
|
#endif // ALSAMIDI_ENABLED
|