/* This file is part of Ingen. * Copyright (C) 2007 Dave Robillard * * Ingen 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. * * Ingen 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 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 St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "AlsaMidiDriver.h" #include #include #include #include "types.h" #include #include "ThreadManager.h" #include "AudioDriver.h" #include "MidiBuffer.h" #include "DuplexPort.h" #ifdef HAVE_LASH #include "LashDriver.h" #endif using std::cout; using std::cerr; using std::endl; namespace Ingen { //// AlsaMidiPort //// AlsaMidiPort::AlsaMidiPort(AlsaMidiDriver* driver, DuplexPort* patch_port) : DriverPort(patch_port->is_input()), Raul::ListNode(this), _driver(driver), _patch_port(patch_port), _port_id(0), _midi_pool(new unsigned char*[patch_port->buffer_size()]), _events(1024) { assert(patch_port->poly() == 1); if (patch_port->is_input()) { if ((_port_id = snd_seq_create_simple_port(driver->seq_handle(), patch_port->path().c_str(), SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { cerr << "[AlsaMidiPort] Error creating sequencer port." << endl; exit(EXIT_FAILURE); } } else { if ((_port_id = snd_seq_create_simple_port(driver->seq_handle(), patch_port->path().c_str(), SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { cerr << "[AlsaMidiPort] Error creating sequencer port." << endl; exit(EXIT_FAILURE); } } /* Allocate event pool. This pool is used when preparing a block from the queue * of Alsa events. The buffer member of the MidiMessage's in the patch port's * buffer will be set directly to an element in this pool, then next cycle they * will be overwritten (eliminating the need for any allocation/freeing). */ for (size_t i=0; i < patch_port->buffer_size(); ++i) _midi_pool[i] = new unsigned char[MAX_MIDI_EVENT_SIZE]; patch_port->buffer(0)->clear(); patch_port->fixed_buffers(true); } AlsaMidiPort::~AlsaMidiPort() { snd_seq_delete_simple_port(_driver->seq_handle(), _port_id); // Free event pool for (size_t i=0; i < _patch_port->buffer_size(); ++i) delete[] _midi_pool[i]; delete[] _midi_pool; } void AlsaMidiPort::set_name(const string& name) { snd_seq_port_info_t* info = NULL; snd_seq_port_info_malloc(&info); snd_seq_get_port_info(_driver->seq_handle(), _port_id, info); snd_seq_port_info_set_name(info, name.c_str()); snd_seq_set_port_info(_driver->seq_handle(), _port_id, info); snd_seq_port_info_free(info); } void AlsaMidiPort::event(snd_seq_event_t* const ev) { // Abuse the tick field to hold the timestamp ev->time.tick = _driver->audio_driver()->frame_time(); // Fix noteons with velocity 0 (required for DSSI spec) if (ev->type == SND_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0) ev->type = SND_SEQ_EVENT_NOTEOFF; _events.push(*ev); } /** Generates a flat array of MIDI events for patching. * * Prepares all events that occurred during the time interval passed * (which ideally are the events from the previous cycle with an exact * 1 cycle delay) and creates a flat port buffer for this cycle. */ void AlsaMidiPort::prepare_block(const SampleCount block_start, const SampleCount block_end) { assert(block_end >= block_start); const SampleCount nframes = block_end - block_start; snd_seq_event_t* ev = NULL; size_t num_events = 0; size_t event_size = 0; // decoded length of Alsa event in bytes int timestamp = 0; MidiBuffer* patch_buf = dynamic_cast(_patch_port->buffer(0)); assert(patch_buf); patch_buf->prepare_write(nframes); while (!_events.empty() && _events.front().time.tick < block_end) { assert(num_events < _patch_port->buffer_size()); ev = &_events.front(); timestamp = ev->time.tick - block_start; if (timestamp < 0) { // FIXME: remove this (obviously not realtime safe) cerr << "[AlsaMidiPort] Missed event by " << -timestamp << " samples!" << endl; timestamp = 0; } assert(timestamp < (int)(block_end - block_start)); // Reset decoder so we don't get running status snd_midi_event_reset_decode(_driver->event_coder()); // FIXME: is this realtime safe? if ((event_size = snd_midi_event_decode(_driver->event_coder(), _midi_pool[num_events], MAX_MIDI_EVENT_SIZE, ev)) > 0) { patch_buf->put_event(timestamp, event_size, _midi_pool[num_events]); ++num_events; } else { cerr << "[AlsaMidiPort] Unable to decode MIDI event" << endl; } _events.pop(); } } //// AlsaMidiDriver //// bool AlsaMidiDriver::_midi_thread_exit_flag = true; AlsaMidiDriver::AlsaMidiDriver(AudioDriver* audio_driver) : _audio_driver(audio_driver), _seq_handle(NULL), _event_coder(NULL), _is_activated(false) { if (snd_seq_open(&_seq_handle, "hw", SND_SEQ_OPEN_INPUT, 0) < 0) { cerr << "[AlsaMidiDriver] Error opening ALSA sequencer." << endl; exit(EXIT_FAILURE); } else { cout << "[AlsaMidiDriver] Successfully opened ALSA sequencer." << endl; } if (snd_midi_event_new(3, &_event_coder)) { cerr << "[AlsaMidiDriver] Failed to initialize ALSA MIDI event coder!"; exit(EXIT_FAILURE); } else { snd_midi_event_reset_encode(_event_coder); snd_midi_event_reset_decode(_event_coder); } snd_seq_set_client_name(_seq_handle, "Ingen"); } AlsaMidiDriver::~AlsaMidiDriver() { deactivate(); snd_midi_event_free(_event_coder); snd_seq_close(_seq_handle); } /** Launch and start the MIDI thread. */ void AlsaMidiDriver::activate() { // Just exit if already running if (_midi_thread_exit_flag == false) return; bool success = false; _midi_thread_exit_flag = false; //if (Engine::instance().audio_driver()->is_realtime()) { pthread_attr_t attr; pthread_attr_init(&attr); if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) { cerr << "[AlsaMidiDriver] Unable to set realtime scheduling for MIDI thread." << endl; } sched_param param; param.sched_priority = 10; pthread_attr_setstacksize(&attr, 1500000); if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) || pthread_attr_setschedparam(&attr, ¶m)) cout << "[AlsaMidiDriver] Unable to set SCHED_FIFO priority " << param.sched_priority << endl; if (!pthread_create(&_process_thread, &attr, process_midi_in, this)) { cout << "[AlsaMidiDriver] Started realtime MIDI thread (SCHED_FIFO, priority " << param.sched_priority << ")" << endl; success = true; } else { cerr << "[AlsaMidiDriver] Unable to start realtime MIDI thread." << endl; } pthread_attr_destroy(&attr); //} if (!success) { // FIXME: check for success pthread_create(&_process_thread, NULL, process_midi_in, this); cout << "[AlsaMidiDriver] Started non-realtime MIDI thread." << endl; } #ifdef HAVE_LASH cerr << "FIXME: LASH + ALSA" << endl; //Engine::instance().lash_driver()->set_alsa_client_id(snd_seq_client_id(_seq_handle)); #endif _is_activated = true; } /** Terminate the MIDI thread. */ void AlsaMidiDriver::deactivate() { if (_is_activated) { _midi_thread_exit_flag = true; pthread_cancel(_process_thread); pthread_join(_process_thread, NULL); _is_activated = false; } } /** Build flat arrays of events to be used as input for the given cycle. */ void AlsaMidiDriver::prepare_block(const SampleCount block_start, const SampleCount block_end) { for (Raul::List::iterator i = _in_ports.begin(); i != _in_ports.end(); ++i) (*i)->prepare_block(block_start, block_end); } /** Add an Alsa MIDI port. * * Realtime safe, this is to be called at the beginning of a process cycle to * insert (and actually begin using) a new port. * * See create_port() and remove_port(). */ void AlsaMidiDriver::add_port(DriverPort* port) { assert(ThreadManager::current_thread_id() == THREAD_PROCESS); assert(dynamic_cast(port)); if (port->is_input()) _in_ports.push_back((AlsaMidiPort*)port); else _out_ports.push_back((AlsaMidiPort*)port); } /** Remove an Alsa MIDI port. * * Realtime safe. This is to be called at the beginning of a process cycle to * remove the port from the lists read by the audio thread, so the port * will no longer be used and can be removed afterwards. * * It is the callers responsibility to delete the returned port. */ DriverPort* AlsaMidiDriver::remove_port(const Path& path) { assert(ThreadManager::current_thread_id() == THREAD_PROCESS); // FIXME: duplex? for (Raul::List::iterator i = _in_ports.begin(); i != _in_ports.end(); ++i) if ((*i)->patch_port()->path() == path) return _in_ports.erase(i)->elem(); // FIXME: leak for (Raul::List::iterator i = _out_ports.begin(); i != _out_ports.end(); ++i) if ((*i)->patch_port()->path() == path) return _out_ports.erase(i)->elem(); // FIXME: leak cerr << "[AlsaMidiDriver::remove_port] WARNING: Failed to find Jack port to remove!" << endl; return NULL; } /** MIDI thread. */ void* AlsaMidiDriver::process_midi_in(void* alsa_driver) { AlsaMidiDriver* ad = (AlsaMidiDriver*)alsa_driver; snd_seq_event_t* ev; int npfd = snd_seq_poll_descriptors_count(ad->_seq_handle, POLLIN); struct pollfd pfd; snd_seq_poll_descriptors(ad->_seq_handle, &pfd, npfd, POLLIN); while ( ! _midi_thread_exit_flag) if (poll(&pfd, npfd, 100000) > 0) while (snd_seq_event_input(ad->_seq_handle, &ev) > 0) for (Raul::List::iterator i = ad->_in_ports.begin(); i != ad->_in_ports.end(); ++i) if ((*i)->port_id() == ev->dest.port) (*i)->event(ev); cout << "[AlsaMidiDriver] Exiting MIDI thread." << endl; return NULL; } } // namespace Ingen