From f143d7652308b722a973cdf8ea2cda6ce44be7eb Mon Sep 17 00:00:00 2001 From: Tim Mayberry Date: Thu, 27 Aug 2015 18:07:25 +1000 Subject: [PATCH] Extract MIDI input/output processing in PortAudioBackend into new methods --- libs/backends/portaudio/portaudio_backend.cc | 120 ++++++++++--------- libs/backends/portaudio/portaudio_backend.h | 2 + 2 files changed, 68 insertions(+), 54 deletions(-) diff --git a/libs/backends/portaudio/portaudio_backend.cc b/libs/backends/portaudio/portaudio_backend.cc index 142614bbff..3555d09928 100644 --- a/libs/backends/portaudio/portaudio_backend.cc +++ b/libs/backends/portaudio/portaudio_backend.cc @@ -1493,34 +1493,7 @@ PortAudioBackend::blocking_process_main () i, (float*)((*it)->get_buffer(_samples_per_period)), _samples_per_period); } - /* de-queue incoming midi*/ - i = 0; - for (std::vector::const_iterator it = _system_midi_in.begin(); - it != _system_midi_in.end(); - ++it, ++i) { - PortMidiBuffer* mbuf = static_cast((*it)->get_buffer(0)); - mbuf->clear(); - uint64_t timestamp; - pframes_t sample_offset; - uint8_t data[256]; - size_t size = sizeof(data); - while (_midiio->dequeue_input_event(i, - m_cycle_timer.get_start(), - m_cycle_timer.get_next_start(), - timestamp, - data, - size)) { - sample_offset = m_cycle_timer.samples_since_cycle_start(timestamp); - midi_event_put(mbuf, sample_offset, data, size); - DEBUG_MIDI(string_compose("Dequeuing incoming MIDI data for device: %1 " - "sample_offset: %2 timestamp: %3, size: %4\n", - _midiio->get_inputs()[i]->name(), - sample_offset, - timestamp, - size)); - size = sizeof(data); - } - } + process_incoming_midi (); /* clear output buffers */ for (std::vector::const_iterator it = _system_outputs.begin(); @@ -1562,33 +1535,8 @@ PortAudioBackend::blocking_process_main () _active = false; return false; } - /* mixdown midi */ - for (std::vector::iterator it = _system_midi_out.begin(); - it != _system_midi_out.end(); - ++it) { - static_cast(*it)->next_period(); - } - /* queue outgoing midi */ - i = 0; - for (std::vector::const_iterator it = _system_midi_out.begin(); - it != _system_midi_out.end(); - ++it, ++i) { - const PortMidiBuffer* src = - static_cast(*it)->const_buffer(); - for (PortMidiBuffer::const_iterator mit = src->begin(); mit != src->end(); - ++mit) { - uint64_t timestamp = - m_cycle_timer.timestamp_from_sample_offset((*mit)->timestamp()); - DEBUG_MIDI(string_compose("Queuing outgoing MIDI data for device: " - "%1 sample_offset: %2 timestamp: %3, size: %4\n", - _midiio->get_outputs()[i]->name(), - (*mit)->timestamp(), - timestamp, - (*mit)->size())); - _midiio->enqueue_output_event(i, timestamp, (*mit)->data(), (*mit)->size()); - } - } + process_outgoing_midi (); /* write back audio */ i = 0; @@ -1653,6 +1601,70 @@ PortAudioBackend::blocking_process_freewheel() return true; } +void +PortAudioBackend::process_incoming_midi () +{ + uint32_t i = 0; + for (std::vector::const_iterator it = _system_midi_in.begin(); + it != _system_midi_in.end(); + ++it, ++i) { + PortMidiBuffer* mbuf = static_cast((*it)->get_buffer(0)); + mbuf->clear(); + uint64_t timestamp; + pframes_t sample_offset; + uint8_t data[256]; + size_t size = sizeof(data); + while (_midiio->dequeue_input_event(i, + m_cycle_timer.get_start(), + m_cycle_timer.get_next_start(), + timestamp, + data, + size)) { + sample_offset = m_cycle_timer.samples_since_cycle_start(timestamp); + midi_event_put(mbuf, sample_offset, data, size); + DEBUG_MIDI(string_compose("Dequeuing incoming MIDI data for device: %1 " + "sample_offset: %2 timestamp: %3, size: %4\n", + _midiio->get_inputs()[i]->name(), + sample_offset, + timestamp, + size)); + size = sizeof(data); + } + } +} + +void +PortAudioBackend::process_outgoing_midi () +{ + /* mixdown midi */ + for (std::vector::iterator it = _system_midi_out.begin(); + it != _system_midi_out.end(); + ++it) { + static_cast(*it)->next_period(); + } + /* queue outgoing midi */ + uint32_t i = 0; + for (std::vector::const_iterator it = _system_midi_out.begin(); + it != _system_midi_out.end(); + ++it, ++i) { + const PortMidiBuffer* src = + static_cast(*it)->const_buffer(); + + for (PortMidiBuffer::const_iterator mit = src->begin(); mit != src->end(); + ++mit) { + uint64_t timestamp = + m_cycle_timer.timestamp_from_sample_offset((*mit)->timestamp()); + DEBUG_MIDI(string_compose("Queuing outgoing MIDI data for device: " + "%1 sample_offset: %2 timestamp: %3, size: %4\n", + _midiio->get_outputs()[i]->name(), + (*mit)->timestamp(), + timestamp, + (*mit)->size())); + _midiio->enqueue_output_event(i, timestamp, (*mit)->data(), (*mit)->size()); + } + } +} + void PortAudioBackend::process_port_connection_changes () { diff --git a/libs/backends/portaudio/portaudio_backend.h b/libs/backends/portaudio/portaudio_backend.h index 15f32befc2..08de76ccfb 100644 --- a/libs/backends/portaudio/portaudio_backend.h +++ b/libs/backends/portaudio/portaudio_backend.h @@ -327,6 +327,8 @@ class PortAudioBackend : public AudioBackend { bool blocking_process_main (); void process_port_connection_changes (); + void process_incoming_midi (); + void process_outgoing_midi (); private: std::string _instance_name;