Skip to content

Commit

Permalink
Merge pull request #218 from EmerickH/master
Browse files Browse the repository at this point in the history
Fix MIDI lag and disconnect on module disable
  • Loading branch information
benkuper authored Feb 16, 2024
2 parents 5f546ed + 641c495 commit d2ed464
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Source/Common/MIDI/MIDIDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void MIDIOutputDevice::close()
if (usageCount == 0)
{
device = nullptr;
LOG("MIDI In " << name << " closed");
LOG("MIDI Out " << name << " closed");
}
}

Expand Down
6 changes: 0 additions & 6 deletions Source/Common/MIDI/MIDIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ MIDIManager::MIDIManager()
midiRouterDefaultType = dynamic_cast<ChataigneEngine*>(Engine::mainEngine)->defaultBehaviors.addEnumParameter("MIDI Router Ouput Type", "Choose the default type when choosing a MIDI Module as Router output");
midiRouterDefaultType->addOption("Control Change", MIDIManager::CONTROL_CHANGE)->addOption("Note On", MIDIManager::NOTE_ON)->addOption("Note Off", MIDIManager::NOTE_OFF);

startTimer(500); //check devices each half seconds
checkDevices();
}

Expand Down Expand Up @@ -180,8 +179,3 @@ MIDIOutputDevice* MIDIManager::getOutputDeviceWithName(const String& name)
for (auto& d : outputs) if (d->name == name) return d;
return nullptr;
}

void MIDIManager::timerCallback()
{
checkDevices();
}
12 changes: 6 additions & 6 deletions Source/Common/MIDI/MIDIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

#pragma once

class MIDIManager :
public Timer
class MIDIManager
{
public:
juce_DeclareSingleton(MIDIManager, true)
Expand Down Expand Up @@ -55,14 +54,15 @@ class MIDIManager :
void removeMIDIManagerListener(Listener* listener) { listeners.remove(listener); }



// Inherited via Timer
virtual void timerCallback() override;

static String getNoteName(const int& pitch, bool includeOctave = true, int octaveShift = 0)
{
return MidiMessage::getMidiNoteName(pitch, true, includeOctave, 3 - octaveShift);
}

MidiDeviceListConnection connection = MidiDeviceListConnection::make ([this]
{
checkDevices();
});

JUCE_DECLARE_NON_COPYABLE(MIDIManager)
};
22 changes: 15 additions & 7 deletions Source/Module/modules/midi/MIDIModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,23 @@ void MIDIModule::onControllableFeedbackUpdateInternal(ControllableContainer* cc,
}
}

void MIDIModule::onContainerParameterChangedInternal(Parameter* p)
{
Module::onContainerParameterChangedInternal(p);
if (p == enabled){
updateMIDIDevices();
}
}

void MIDIModule::updateMIDIDevices()
{
MIDIInputDevice* newInput = midiParam->inputDevice;
//if (inputDevice != newInput)
//{
MIDIInputDevice* newInput = nullptr;
MIDIOutputDevice* newOutput = nullptr;
if (enabled->boolValue()){
newInput = midiParam->inputDevice;
newOutput = midiParam->outputDevice;
}

if (inputDevice != nullptr)
{
inputDevice->removeMIDIInputListener(this);
Expand All @@ -317,11 +329,7 @@ void MIDIModule::updateMIDIDevices()
mtcReceiver->addMTCListener(this);
noteOns.clear();
}
//}

MIDIOutputDevice* newOutput = midiParam->outputDevice;
//if (outputDevice != newOutput)
//{
if (outputDevice != nullptr)
{
if (sendClock->boolValue()) outClock.setOutDevice(nullptr);
Expand Down
1 change: 1 addition & 0 deletions Source/Module/modules/midi/MIDIModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class MIDIModule :
void sendMidiMachineControlGoto(int hours, int minutes, int seconds, int frames);

void onControllableFeedbackUpdateInternal(ControllableContainer* cc, Controllable* c) override;
virtual void onContainerParameterChangedInternal(Parameter* p) override;
void updateMIDIDevices();

virtual void noteOnReceived(const int& channel, const int& pitch, const int& velocity) override;
Expand Down

0 comments on commit d2ed464

Please sign in to comment.