- A new "MIDI source" node, which can feed MIDI events from any ALSA device into the graph. - All done rt safe in C++. The older Python implementation in noisicaa.devices has been removed. - Still quite hacky: - It's built into the portaudio backend. There should probably be a separate "ALSA sequencer" backend for this, with the option to have an alternative "Jack MIDI" backend. But for that I have to figure out how to have separate backends for audio and MIDI and how that interacts for all possible combinations. - It just blindly connects to all readable MIDI ports, collecting all events. Filtering out the events for a specific port (e.g. a MIDI keyboard) happens in the "MIDI source" node. The engine should track, which ports are being used and only connect to those. Squashed commit of the following: commit c811be510347d1fd23abea081ba0a4d93e8cb6bf Author: Ben Niemann <pink@odahoda.de> Date: Mon Jan 14 03:36:18 2019 +0100 Move ALSADeviceManager to a separate file. commit 6e5d9a2c691fdf639f0173b9dd2ebfde7f58f4f4 Author: Ben Niemann <pink@odahoda.de> Date: Mon Jan 14 03:25:29 2019 +0100 Fix/improve tests. commit 94b4fa253f8a4f8a84d13dd718dbaeac99fee5fe Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 13:57:07 2019 +0100 Reanimate playback from the instrument library. commit 17a288980fc361f190876763dbe4a6a6bbd0c2b3 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 12:57:54 2019 +0100 Remove the now obsolete noisicaa.devices package. commit aa2f9bbc1ae61295157f66948b276861dee00379 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 12:45:50 2019 +0100 Strip the PianoWidget down to just the keys. commit 1c87b29f7abb51defa28b33f902f8de85ae7eb55 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 12:24:57 2019 +0100 Add piano to MIDI source node. - Make BasePipelineGraphNode.pipeline_node_id globally available. - Allow sending processor messages from the UI. - Pass the MIDI events to the rt thread via a FIFO queue. commit f19114e966ab2d9261fd3a86b93d2ca88e9f3fba Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 11:29:43 2019 +0100 Remove the System Out node again. And the related Backend::input(). Not needed after all. commit a839f259e3b8e338072be9c8b9fa58d8dc0d36a4 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 10:03:33 2019 +0100 Wire up MIDI source to events from the backend. - Make the event buffer accessible via the block context. - Backend creates sequence of (uri, midi) tuples. - ProcessorMidiSource filters that list and emit list of midi events. commit 347dc0168b00315eed233fdec40c8a9d6b5ffe41 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 09:21:36 2019 +0100 Make the main ALSA sequences listen to all output ports again. Now also tracking new ports as they appear. commit 86b6b7a59974c18c6078761fe1010456e5f26e43 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 09:20:52 2019 +0100 Bug fix. commit 776dbd4a946ecfa8e178cd7e3e108a4c3519f3cb Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 08:43:47 2019 +0100 Editor tracks devices in a QAbstractItemModel. And MIDI Source node uses that for the port selector combobox. commit a9c578e377948d187a0ee8ede90c29cc32b337a1 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 08:42:30 2019 +0100 Also handle port changes somewhat gracefully. commit f4cd8c7535b36e7c6b9323ff2861d72e376bac08 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 05:34:01 2019 +0100 Also handle CC events. commit 1329e51ff9747764a2bb5c6578f3490047cee135 Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 04:30:45 2019 +0100 Device manager that tracks ALSA sequencer clients. - Allow backends to post engine notifications. - PortAudioBackend runs a separate (non-rt) thread, with a sequencer client just for listening for client notifications. - Create a DeviceDescription proto for ALSA sequencer clients and post them as engine notifications. commit 10c5b827de47479e6a8046c44cd32494693c762b Author: Ben Niemann <pink@odahoda.de> Date: Sun Jan 13 01:34:47 2019 +0100 A MIDI source node, which doesn't really do anything yet. commit e09a5c70e3b950f3c6e30b81c2e8b67d65a947b3 Author: Ben Niemann <pink@odahoda.de> Date: Sat Jan 12 11:51:36 2019 +0100 Use C string for Spec::get_buffer_idx() to avoid malloc in the audio thread. commit 24cfffdf60a4ad888e65fe839165666ebef0f9f0 Author: Ben Niemann <pink@odahoda.de> Date: Sat Jan 12 08:13:26 2019 +0100 Add a "System In" node to the graph and wire it up to the MIDI events from the backend. Also rename "Audio Out" to "System Out", because that makes more sense, now that there is more than audio being passed around. commit 77be27b0e487b0830d913bdcc54cf56ea35114cf Author: Ben Niemann <pink@odahoda.de> Date: Sat Jan 12 08:10:55 2019 +0100 Add Backend::input() method to read incoming MIDI events. Also switch to an enum for the channel arg. commit 5c4acefc476ace640d8a0ac40d6816ca48399207 Author: Ben Niemann <pink@odahoda.de> Date: Sat Jan 12 08:08:20 2019 +0100 PortAudioBackend also reads MIDI events into a buffer. Very prototypish implementation. It just scans for all available devices and connects to their outputs. Still need to think about how to deal with different devices.looper
parent
e71cf92ba9
commit
f4ce7f53d2
@ -0,0 +1,249 @@
|
||||
/*
|
||||
* @begin:license
|
||||
*
|
||||
* Copyright (c) 2015-2019, Benjamin Niemann <pink@odahoda.de>
|
||||
*
|
||||
* 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.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* @end:license
|
||||
*/
|
||||
|
||||
#include <google/protobuf/util/message_differencer.h>
|
||||
|
||||
#include "noisicaa/core/logging.h"
|
||||
#include "noisicaa/core/scope_guard.h"
|
||||
#include "noisicaa/core/slots.inl.h"
|
||||
#include "noisicaa/host_system/host_system.h"
|
||||
#include "noisicaa/audioproc/public/devices.pb.h"
|
||||
#include "noisicaa/audioproc/public/engine_notification.pb.h"
|
||||
#include "noisicaa/audioproc/engine/misc.h"
|
||||
#include "noisicaa/audioproc/engine/alsa_device_manager.h"
|
||||
|
||||
namespace noisicaa {
|
||||
|
||||
ALSADeviceManager::ALSADeviceManager(
|
||||
int client_id,
|
||||
Slot<pb::EngineNotification>& notifications)
|
||||
: _logger(LoggerRegistry::get_logger("noisicaa.audioproc.engine.backend.alsa_device_manager")),
|
||||
_client_id(client_id),
|
||||
_notifications(notifications) {}
|
||||
|
||||
ALSADeviceManager::~ALSADeviceManager() {
|
||||
for (const auto& it : _devices) {
|
||||
remove_device(it.second);
|
||||
}
|
||||
_devices.clear();
|
||||
|
||||
if (_seq != nullptr) {
|
||||
snd_seq_close(_seq);
|
||||
_seq = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Status ALSADeviceManager::setup() {
|
||||
RETURN_IF_ALSA_ERROR(snd_seq_open(&_seq, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK));
|
||||
RETURN_IF_ALSA_ERROR(snd_seq_set_client_name(_seq, "noisicaa device monitor"));
|
||||
|
||||
snd_seq_port_info_t *pinfo;
|
||||
snd_seq_port_info_alloca(&pinfo);
|
||||
|
||||
snd_seq_port_info_set_capability(pinfo, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_NO_EXPORT);
|
||||
snd_seq_port_info_set_type(pinfo, SND_SEQ_PORT_TYPE_APPLICATION);
|
||||
snd_seq_port_info_set_name(pinfo, "Input");
|
||||
RETURN_IF_ALSA_ERROR(snd_seq_create_port(_seq, pinfo));
|
||||
int input_port_id = snd_seq_port_info_get_port(pinfo);
|
||||
|
||||
// Connect to System Announce port
|
||||
RETURN_IF_ALSA_ERROR(
|
||||
snd_seq_connect_from(
|
||||
_seq, input_port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE));
|
||||
|
||||
snd_seq_client_info_t *cinfo;
|
||||
snd_seq_client_info_alloca(&cinfo);
|
||||
snd_seq_client_info_set_client(cinfo, -1);
|
||||
while (snd_seq_query_next_client(_seq, cinfo) == 0) {
|
||||
int client_id = snd_seq_client_info_get_client(cinfo);
|
||||
if (client_id == snd_seq_client_id(_seq)
|
||||
|| client_id == _client_id
|
||||
|| client_id == SND_SEQ_CLIENT_SYSTEM) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StatusOr<pb::DeviceDescription> stor_device = get_device_description(client_id);
|
||||
RETURN_IF_ERROR(stor_device);
|
||||
pb::DeviceDescription device = stor_device.result();
|
||||
add_device(device);
|
||||
_devices[device.uri()] = device;
|
||||
}
|
||||
|
||||
return Status::Ok();
|
||||
}
|
||||
|
||||
StatusOr<pb::DeviceDescription> ALSADeviceManager::get_device_description(int client_id) {
|
||||
snd_seq_client_info_t *cinfo;
|
||||
snd_seq_client_info_alloca(&cinfo);
|
||||
|
||||
snd_seq_port_info_t *pinfo;
|
||||
snd_seq_port_info_alloca(&pinfo);
|
||||
|
||||
RETURN_IF_ALSA_ERROR(snd_seq_get_any_client_info(_seq, client_id, cinfo));
|
||||
|
||||
pb::DeviceDescription device;
|
||||
device.set_uri(sprintf("alsa://%d", client_id));
|
||||
device.set_type(pb::DeviceDescription::MIDI_CONTROLLER);
|
||||
device.set_display_name(snd_seq_client_info_get_name(cinfo));
|
||||
|
||||
snd_seq_port_info_set_client(pinfo, client_id);
|
||||
snd_seq_port_info_set_port(pinfo, -1);
|
||||
while (snd_seq_query_next_port(_seq, pinfo) == 0) {
|
||||
unsigned int cap = snd_seq_port_info_get_capability(pinfo);
|
||||
if (cap & SND_SEQ_PORT_CAP_NO_EXPORT) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pb::DevicePortDescription* port = device.add_ports();
|
||||
int port_id = snd_seq_port_info_get_port(pinfo);
|
||||
port->set_uri(sprintf("alsa://%d/%d", client_id, port_id));
|
||||
port->set_display_name(snd_seq_port_info_get_name(pinfo));
|
||||
|
||||
if (cap & (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_DUPLEX)) {
|
||||
port->set_readable(true);
|
||||
}
|
||||
if (cap & (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_DUPLEX)) {
|
||||
port->set_writable(true);
|
||||
}
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
void ALSADeviceManager::add_device(const pb::DeviceDescription& device) {
|
||||
_logger->info("Added device:\n%s", device.DebugString().c_str());
|
||||
|
||||
pb::EngineNotification notification;
|
||||
pb::DeviceManagerMessage* m = notification.add_device_manager_messages();
|
||||
m->mutable_added()->CopyFrom(device);
|
||||
_notifications.emit(notification);
|
||||
}
|
||||
|
||||
void ALSADeviceManager::update_device(const pb::DeviceDescription& device) {
|
||||
_logger->info("Updated device:\n%s", device.DebugString().c_str());
|
||||
|
||||
pb::EngineNotification notification;
|
||||
pb::DeviceManagerMessage* m = notification.add_device_manager_messages();
|
||||
m->mutable_removed()->CopyFrom(device);
|
||||
m = notification.add_device_manager_messages();
|
||||
m->mutable_added()->CopyFrom(device);
|
||||
_notifications.emit(notification);
|
||||
}
|
||||
|
||||
void ALSADeviceManager::remove_device(const pb::DeviceDescription& device) {
|
||||
_logger->info("Removed device:\n%s", device.DebugString().c_str());
|
||||
|
||||
pb::EngineNotification notification;
|
||||
pb::DeviceManagerMessage* m = notification.add_device_manager_messages();
|
||||
m->mutable_removed()->CopyFrom(device);
|
||||
_notifications.emit(notification);
|
||||
}
|
||||
|
||||
void ALSADeviceManager::process_events() {
|
||||
while (true) {
|
||||
snd_seq_event_t* event;
|
||||
int rc = snd_seq_event_input(_seq, &event);
|
||||
if (rc == -ENOSPC) {
|
||||
_logger->warning("ALSA midi queue overrun.");
|
||||
return;
|
||||
}
|
||||
if (rc == -EAGAIN) {
|
||||
return;
|
||||
}
|
||||
if (rc < 0) {
|
||||
_logger->error("ALSA error %d: %s", rc, snd_strerror(rc));
|
||||
return;
|
||||
}
|
||||
|
||||
switch (event->type) {
|
||||
case SND_SEQ_EVENT_PORT_START:
|
||||
case SND_SEQ_EVENT_PORT_CHANGE:
|
||||
case SND_SEQ_EVENT_PORT_EXIT:
|
||||
case SND_SEQ_EVENT_CLIENT_START:
|
||||
case SND_SEQ_EVENT_CLIENT_CHANGE: {
|
||||
if (event->data.addr.client == snd_seq_client_id(_seq)
|
||||
|| event->data.addr.client == _client_id
|
||||
|| event->data.addr.client == SND_SEQ_CLIENT_SYSTEM) {
|
||||
break;
|
||||
}
|
||||
|
||||
StatusOr<pb::DeviceDescription> stor_device = get_device_description(event->data.addr.client);
|
||||
if (stor_device.is_error()) {
|
||||
_logger->error(
|
||||
"Failed to get device description for ALSA sequencer client %d",
|
||||
event->data.addr.client);
|
||||
} else {
|
||||
pb::DeviceDescription device = stor_device.result();
|
||||
|
||||
auto it = _devices.find(device.uri());
|
||||
if (it == _devices.end()) {
|
||||
add_device(device);
|
||||
_devices[device.uri()] = device;
|
||||
} else if (!google::protobuf::util::MessageDifferencer::Equals(device, it->second)) {
|
||||
update_device(device);
|
||||
_devices[device.uri()] = device;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SND_SEQ_EVENT_CLIENT_EXIT: {
|
||||
if (event->data.addr.client == snd_seq_client_id(_seq)
|
||||
|| event->data.addr.client == _client_id
|
||||
|| event->data.addr.client == SND_SEQ_CLIENT_SYSTEM) {
|
||||
break;
|
||||
}
|
||||
|
||||
string uri = sprintf("alsa://%d", event->data.addr.client);
|
||||
auto it = _devices.find(uri);
|
||||
if (it == _devices.end()) {
|
||||
_logger->warning("Got CLIENT_EXIT event for unknown client.");
|
||||
} else {
|
||||
remove_device(it->second);
|
||||
_devices.erase(it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SND_SEQ_EVENT_PORT_SUBSCRIBED:
|
||||
case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
|
||||
// Ignore these events.
|
||||
break;
|
||||
|
||||
default:
|
||||
_logger->error(
|
||||
"Unknown MIDI event: type=%d flags=%x tag=%x queue=%x time=%d source=%d.%d dest=%d.%d",
|
||||
event->type,
|
||||
event->flags,
|
||||
event->tag,
|
||||
event->queue,
|
||||
event->time.tick,
|
||||
event->source.client,
|
||||
event->source.port,
|
||||
event->dest.client,
|
||||
event->dest.port);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace noisicaa
|
@ -0,0 +1,68 @@
|
||||
// -*- mode: c++ -*-
|
||||
|
||||
/*
|
||||
* @begin:license
|
||||
*
|
||||
* Copyright (c) 2015-2019, Benjamin Niemann <pink@odahoda.de>
|
||||
*
|
||||
* 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.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* @end:license
|
||||
*/
|
||||
|
||||
#ifndef _NOISICAA_AUDIOPROC_ENGINE_ALSA_DEVICE_MANAGER_H
|
||||
#define _NOISICAA_AUDIOPROC_ENGINE_ALSA_DEVICE_MANAGER_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "alsa/asoundlib.h"
|
||||
#include "noisicaa/core/slots.h"
|
||||
#include "noisicaa/core/status.h"
|
||||
|
||||
namespace noisicaa {
|
||||
|
||||
class Logger;
|
||||
namespace pb {
|
||||
class EngineNotification;
|
||||
class DeviceDescription;
|
||||
}
|
||||
|
||||
class ALSADeviceManager {
|
||||
public:
|
||||
ALSADeviceManager(
|
||||
int client_id,
|
||||
Slot<pb::EngineNotification>& notifications);
|
||||
~ALSADeviceManager();
|
||||
|
||||
Status setup();
|
||||
void process_events();
|
||||
|
||||
private:
|
||||
StatusOr<pb::DeviceDescription> get_device_description(int client_id);
|
||||
void add_device(const pb::DeviceDescription& device);
|
||||
void update_device(const pb::DeviceDescription& device);
|
||||
void remove_device(const pb::DeviceDescription& device);
|
||||
|
||||
private:
|
||||
Logger* _logger;
|
||||
int _client_id;
|
||||
Slot<pb::EngineNotification>& _notifications;
|
||||
snd_seq_t* _seq = nullptr;
|
||||
map<string, pb::DeviceDescription> _devices;
|
||||
};
|
||||
|
||||
} // namespace noisicaa
|
||||
|
||||
#endif
|