godot/drivers/alsamidi/midi_driver_alsamidi.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4.7 KiB
C++
Raw Normal View History

2018-07-14 20:11:28 +08:00
/**************************************************************************/
/* midi_driver_alsamidi.cpp */
2018-07-14 20:11:28 +08:00
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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 "midi_driver_alsamidi.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include <errno.h>
2018-07-14 20:11:28 +08:00
MIDIDriverALSAMidi::InputConnection::InputConnection(int p_device_index,
snd_rawmidi_t *p_rawmidi) :
parser(p_device_index), rawmidi_ptr(p_rawmidi) {}
2018-07-14 20:11:28 +08:00
void MIDIDriverALSAMidi::InputConnection::read() {
int read_count;
do {
uint8_t buffer[32];
read_count = snd_rawmidi_read(rawmidi_ptr, buffer, sizeof(buffer));
if (read_count < 0) {
if (read_count != -EAGAIN) {
ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(read_count)));
}
} else {
for (int i = 0; i < read_count; i++) {
parser.parse_fragment(buffer[i]);
}
}
} while (read_count > 0);
2018-07-14 20:11:28 +08:00
}
void MIDIDriverALSAMidi::thread_func(void *p_udata) {
2022-04-05 18:40:26 +08:00
MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata);
2018-07-14 20:11:28 +08:00
2022-08-19 16:38:13 +08:00
while (!md->exit_thread.is_set()) {
2018-07-14 20:11:28 +08:00
md->lock();
for (InputConnection &conn : md->connected_inputs) {
conn.read();
2018-07-14 20:11:28 +08:00
}
md->unlock();
OS::get_singleton()->delay_usec(1000);
}
}
Error MIDIDriverALSAMidi::open() {
void **hints;
if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
2018-07-14 20:11:28 +08:00
return ERR_CANT_OPEN;
}
2018-07-14 20:11:28 +08:00
lock();
int device_index = 0;
for (void **h = hints; *h != nullptr; h++) {
char *name = snd_device_name_get_hint(*h, "NAME");
2018-07-14 20:11:28 +08:00
2020-04-02 07:20:12 +08:00
if (name != nullptr) {
2018-07-14 20:11:28 +08:00
snd_rawmidi_t *midi_in;
2020-04-02 07:20:12 +08:00
int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
2018-07-14 20:11:28 +08:00
if (ret >= 0) {
// Get display name.
snd_rawmidi_info_t *info;
snd_rawmidi_info_malloc(&info);
snd_rawmidi_info(midi_in, info);
connected_input_names.push_back(snd_rawmidi_info_get_name(info));
snd_rawmidi_info_free(info);
connected_inputs.push_back(InputConnection(device_index, midi_in));
// Only increment device_index for successfully connected devices.
device_index++;
2018-07-14 20:11:28 +08:00
}
}
if (name != nullptr) {
2018-07-14 20:11:28 +08:00
free(name);
}
2018-07-14 20:11:28 +08:00
}
snd_device_name_free_hint(hints);
unlock();
2018-07-14 20:11:28 +08:00
2022-08-19 16:38:13 +08:00
exit_thread.clear();
thread.start(MIDIDriverALSAMidi::thread_func, this);
2018-07-14 20:11:28 +08:00
return OK;
}
void MIDIDriverALSAMidi::close() {
2022-08-19 16:38:13 +08:00
exit_thread.set();
2023-04-28 00:34:30 +08:00
if (thread.is_started()) {
thread.wait_to_finish();
}
2018-07-14 20:11:28 +08:00
for (const InputConnection &conn : connected_inputs) {
snd_rawmidi_close(conn.rawmidi_ptr);
2018-07-14 20:11:28 +08:00
}
2018-07-14 20:11:28 +08:00
connected_inputs.clear();
connected_input_names.clear();
2018-07-14 20:11:28 +08:00
}
void MIDIDriverALSAMidi::lock() const {
mutex.lock();
2018-07-14 20:11:28 +08:00
}
void MIDIDriverALSAMidi::unlock() const {
mutex.unlock();
2018-07-14 20:11:28 +08:00
}
MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
2022-08-19 16:38:13 +08:00
exit_thread.clear();
2018-07-14 20:11:28 +08:00
}
MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
close();
}
#endif // ALSAMIDI_ENABLED