Skip to content

Commit

Permalink
fix crash when changing buffer size while Pitch detector is active
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Jun 26, 2024
1 parent 44c1a2a commit 0b962b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 29 additions & 17 deletions Source/Module/modules/audio/AudioModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "Module/ModuleIncludes.h"
#include "AudioModule.h"

AudioModule::AudioModule(const String& name) :
Module(name),
Expand Down Expand Up @@ -169,11 +170,11 @@ void AudioModule::updateAudioSetup()
int numSelectedInputChannelsInSetup = setup.inputChannels.countNumberOfSetBits();
int numSelectedOutputChannelsInSetup = setup.outputChannels.countNumberOfSetBits();

graph.suspendProcessing(true);

graph.setPlayConfigDetails(numSelectedInputChannelsInSetup, numSelectedOutputChannelsInSetup, currentSampleRate, currentBufferSize);
graph.prepareToPlay(currentSampleRate, currentBufferSize);

graph.suspendProcessing(true);

var mData = monitorParams.getJSONData();
for (auto& c : monitorOutChannels) monitorParams.removeControllable(c);
monitorOutChannels.clear();
Expand Down Expand Up @@ -235,12 +236,15 @@ void AudioModule::updateAudioSetup()
audioModuleListeners.call(&AudioModuleListener::audioSetupChanged);
audioModuleListeners.call(&AudioModuleListener::monitorSetupChanged);

setupPitchDetector();

if (setup.outputDeviceName.isEmpty()) setWarningMessage("Module is not connected to an audio output");
else clearWarning();

am.addAudioCallback(&player);
am.addAudioCallback(this);


graph.suspendProcessing(false);
}

Expand All @@ -258,6 +262,23 @@ void AudioModule::updateSelectedMonitorChannels()
numActiveMonitorOutputs = selectedMonitorOutChannels.size();
}

void AudioModule::setupPitchDetector()
{
if (pitchDetectionMethod != nullptr) pitchDetector.reset();

PitchDetectionMethod pdm = pitchDetectionMethod->getValueDataAsEnum<PitchDetectionMethod>();

AudioDeviceManager::AudioDeviceSetup s;
am.getAudioDeviceSetup(s);

switch (pdm)
{
case NONE: pitchDetector.reset(nullptr); break;
case MPM: pitchDetector.reset(new PitchMPM((int)s.sampleRate, s.bufferSize)); break;
case YIN: pitchDetector.reset(new PitchYIN((int)s.sampleRate, s.bufferSize)); break;
}
}

void AudioModule::onControllableFeedbackUpdateInternal(ControllableContainer* cc, Controllable* c)
{
Module::onControllableFeedbackUpdateInternal(cc, c);
Expand All @@ -268,18 +289,9 @@ void AudioModule::onControllableFeedbackUpdateInternal(ControllableContainer* cc
}
else if (c == pitchDetectionMethod)
{
PitchDetectionMethod pdm = pitchDetectionMethod->getValueDataAsEnum<PitchDetectionMethod>();

AudioDeviceManager::AudioDeviceSetup s;
am.getAudioDeviceSetup(s);


switch (pdm)
{
case NONE: pitchDetector.reset(nullptr); break;
case MPM: pitchDetector.reset(new PitchMPM((int)s.sampleRate, s.bufferSize)); break;
case YIN: pitchDetector.reset(new PitchYIN((int)s.sampleRate, s.bufferSize)); break;
}
graph.suspendProcessing(true);
setupPitchDetector();
graph.suspendProcessing(false);

}
else if (c == ltcFPS)
Expand Down Expand Up @@ -374,9 +386,9 @@ void AudioModule::audioDeviceIOCallbackWithContext(const float* const* inputChan

if (pitchDetector != nullptr)
{
if ((int)pitchDetector->getBufferSize() != numSamples) pitchDetector->setBufferSize(numSamples);

if (inputChannelData[0][0] >= 0)
jassert((int)pitchDetector->getBufferSize() == numSamples);
if (inputChannelData[0][0] >= 0) //do not process on the same frame if buffer changed
{
float freq = pitchDetector->getPitch(inputChannelData[0]);
frequency->setValue(freq);
Expand Down
2 changes: 2 additions & 0 deletions Source/Module/modules/audio/AudioModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class AudioModule :
virtual void updateAudioSetup();
void updateSelectedMonitorChannels();

void setupPitchDetector();

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

Expand Down

0 comments on commit 0b962b1

Please sign in to comment.