From 76007233aee981d8982eca6b1c4444e03a106fc2 Mon Sep 17 00:00:00 2001 From: hogliux Date: Mon, 11 Jul 2016 15:27:35 +0100 Subject: [PATCH] Correctly account for midi ports in Alsa when listing/opening devices and other ALSA midi cleanups --- .../native/juce_linux_Midi.cpp | 49 +++++++++---------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_linux_Midi.cpp b/modules/juce_audio_devices/native/juce_linux_Midi.cpp index 9ec37a64ed..04b9e89e49 100644 --- a/modules/juce_audio_devices/native/juce_linux_Midi.cpp +++ b/modules/juce_audio_devices/native/juce_linux_Midi.cpp @@ -50,6 +50,9 @@ public: { snd_seq_open (&handle, "default", forInput ? SND_SEQ_OPEN_INPUT : SND_SEQ_OPEN_OUTPUT, 0); + + snd_seq_set_client_name (handle, forInput ? JUCE_ALSA_MIDI_INPUT_NAME + : JUCE_ALSA_MIDI_OUTPUT_NAME); } ~AlsaClient() @@ -71,11 +74,6 @@ public: bool isInput() const noexcept { return input; } - void setName (const String& name) - { - snd_seq_set_client_name (handle, name.toUTF8()); - } - void registerCallback (AlsaPortAndCallback* cb) { if (cb != nullptr) @@ -336,19 +334,23 @@ static AlsaPort iterateMidiClient (const AlsaClient::Ptr& seq, && (snd_seq_port_info_get_capability (portInfo) & (forInput ? SND_SEQ_PORT_CAP_READ : SND_SEQ_PORT_CAP_WRITE)) != 0) { - deviceNamesFound.add (snd_seq_client_info_get_name (clientInfo)); + const String clientName = snd_seq_client_info_get_name (clientInfo); + const String portName = snd_seq_port_info_get_name(portInfo); + + if (clientName == portName) + deviceNamesFound.add (clientName); + else + deviceNamesFound.add (clientName + ": " + portName); if (deviceNamesFound.size() == deviceIndexToOpen + 1) { const int sourcePort = snd_seq_port_info_get_port (portInfo); - const int sourceClient = snd_seq_client_info_get_client (clientInfo); if (sourcePort != -1) { - const String name (forInput ? JUCE_ALSA_MIDI_INPUT_NAME - : JUCE_ALSA_MIDI_OUTPUT_NAME); - seq->setName (name); - port.createPort (seq, name, forInput); + const int sourceClient = snd_seq_client_info_get_client (clientInfo); + + port.createPort (seq, portName, forInput); port.connectWith (sourceClient, sourcePort); } } @@ -398,19 +400,6 @@ static AlsaPort iterateMidiDevices (const bool forInput, return port; } -AlsaPort createMidiDevice (const bool forInput, const String& deviceNameToOpen) -{ - AlsaPort port; - AlsaClient::Ptr client (new AlsaClient (forInput)); - - if (client->get()) - { - client->setName (deviceNameToOpen + (forInput ? " Input" : " Output")); - port.createPort (client, forInput ? "in" : "out", forInput); - } - - return port; -} //============================================================================== class MidiOutputDevice @@ -461,7 +450,7 @@ public: numBytes -= numSent; data += numSent; - snd_seq_ev_set_source (&event, 0); + snd_seq_ev_set_source (&event, port.portId); snd_seq_ev_set_subs (&event); snd_seq_ev_set_direct (&event); @@ -518,8 +507,11 @@ MidiOutput* MidiOutput::openDevice (int deviceIndex) MidiOutput* MidiOutput::createNewDevice (const String& deviceName) { MidiOutput* newDevice = nullptr; + AlsaPort port; - AlsaPort port (createMidiDevice (false, deviceName)); + const AlsaClient::Ptr client (globalAlsaSequencer (false)); + + port.createPort (client, deviceName, false); if (port.isValid()) { @@ -595,8 +587,11 @@ MidiInput* MidiInput::openDevice (int deviceIndex, MidiInputCallback* callback) MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback) { MidiInput* newDevice = nullptr; + AlsaPort port; - AlsaPort port (createMidiDevice (true, deviceName)); + const AlsaClient::Ptr client (globalAlsaSequencer (true)); + + port.createPort (client, deviceName, true); if (port.isValid()) {