Skip to content

Commit 87f8007

Browse files
authored
Merge pull request #11 from benkuper/copilot/fix-cpu-usage-on-macos
Fix macOS CPU regression: lazy-init Ableton Link + correct RT-safe API usage
2 parents 4a13754 + 26530e5 commit 87f8007

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

‎Source/Node/nodes/vst/VSTNode.cpp‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,22 @@ void VSTNode::processBlockInternal(AudioBuffer<float>& buffer, MidiBuffer& midiM
364364

365365
if (prevWetDry == weight)
366366
{
367-
if (weight == 1) processVSTBlock(buffer, midiMessages, false);
368-
else if (weight == 0) processBlockBypassed(buffer, midiMessages);
367+
if (weight == 1)
368+
{
369+
processVSTBlock(buffer, midiMessages, false);
370+
}
371+
else if (weight == 0)
372+
{
373+
processBlockBypassed(buffer, midiMessages);
374+
}
375+
else
376+
{
377+
AudioBuffer<float> vstBuffer(buffer.getNumChannels(), buffer.getNumSamples());
378+
vstBuffer.clear();
379+
processVSTBlock(vstBuffer, midiMessages, false);
380+
buffer.applyGain(1 - weight);
381+
for (int c = 0; c < buffer.getNumChannels(); c++) buffer.addFrom(c, 0, vstBuffer, c, 0, buffer.getNumSamples(), weight);
382+
}
369383
}
370384
else
371385
{

‎Source/Transport/Transport.cpp‎

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,6 @@ Transport::Transport() :
7777
useAbletonLink = addBoolParameter("Enable Ableton Link", "Use Ableton Link. Enabling this will sync to other ableton link clients", false);
7878
numLinkClients = addIntParameter("Num Link Clients", "Number of Ableton Link clients", 0, 0);
7979

80-
#if USE_ABLETONLINK
81-
link.reset(new ableton::Link{ bpm->floatValue() });
82-
83-
link->setTempoCallback([this](const double p) {
84-
bpm->setValue(p);
85-
});
86-
87-
link->setNumPeersCallback([this](const int p) {
88-
numLinkClients->setValue(p);
89-
});
90-
91-
link->setStartStopCallback([this](const int p) {
92-
if (p == 0) stop();
93-
else if (p == 2) play();
94-
});
95-
96-
#endif
97-
9880
AudioManager::getInstance()->am.addAudioCallback(this);
9981

10082
}
@@ -264,9 +246,12 @@ void Transport::onContainerParameterChanged(Parameter* p)
264246
transportListeners.call(&TransportListener::playStateChanged, isCurrentlyPlaying->boolValue(), false);
265247

266248
#if USE_ABLETONLINK
267-
auto session = link->captureAppSessionState();
268-
session.setIsPlaying(isCurrentlyPlaying->boolValue(), link->clock().micros());
269-
link->commitAudioSessionState(session);
249+
if (link != nullptr && link->isEnabled())
250+
{
251+
auto session = link->captureAppSessionState();
252+
session.setIsPlaying(isCurrentlyPlaying->boolValue(), link->clock().micros());
253+
link->commitAppSessionState(session);
254+
}
270255
#endif
271256
}
272257
else if (p == recQuantization)
@@ -290,11 +275,11 @@ void Transport::onContainerParameterChanged(Parameter* p)
290275
}
291276

292277
#if USE_ABLETONLINK
293-
if (link != nullptr)
278+
if (link != nullptr && link->isEnabled())
294279
{
295280
auto session = link->captureAppSessionState();
296281
session.setTempo(bpm->floatValue(), link->clock().micros());
297-
link->commitAudioSessionState(session);
282+
link->commitAppSessionState(session);
298283
}
299284
#endif
300285

@@ -479,6 +464,23 @@ void Transport::setupAbletonLink()
479464
#if USE_ABLETONLINK
480465
if (useAbletonLink->boolValue())
481466
{
467+
if (link == nullptr)
468+
{
469+
link.reset(new ableton::Link{ bpm->floatValue() });
470+
471+
link->setTempoCallback([this](const double p) {
472+
bpm->setValue(p);
473+
});
474+
475+
link->setNumPeersCallback([this](const int p) {
476+
numLinkClients->setValue(p);
477+
});
478+
479+
link->setStartStopCallback([this](const int p) {
480+
if (p == 0) stop();
481+
else if (p == 2) play();
482+
});
483+
}
482484

483485
link->enableStartStopSync(true);
484486
link->enable(true);
@@ -488,11 +490,11 @@ void Transport::setupAbletonLink()
488490
}
489491
else
490492
{
491-
link->enable(false);
493+
if (link != nullptr) link->enable(false);
492494
}
493-
#endif
494495

495-
LOG("Ableton Link is now " << (link->isEnabled() ? "enabled" : "disabled"));
496+
LOG("Ableton Link is now " << (link != nullptr && link->isEnabled() ? "enabled" : "disabled"));
497+
#endif
496498
}
497499

498500
void Transport::audioDeviceIOCallbackWithContext(const float* const* inputChannelData,
@@ -503,12 +505,13 @@ void Transport::audioDeviceIOCallbackWithContext(const float* const* inputChanne
503505
const AudioIODeviceCallbackContext& context)
504506
{
505507

506-
if (link->isEnabled() && link->numPeers() > 0)
508+
#if USE_ABLETONLINK
509+
if (link != nullptr && link->isEnabled() && link->numPeers() > 0)
507510
{
508511
const int bPerBar = beatsPerBar->intValue();
509512

510513
const auto time = link->clock().micros();
511-
const auto session = link->captureAppSessionState();
514+
const auto session = link->captureAudioSessionState();
512515
const auto beat = session.beatAtTime(time, bPerBar);
513516
const auto phase = session.phaseAtTime(time, bPerBar);
514517

@@ -521,7 +524,9 @@ void Transport::audioDeviceIOCallbackWithContext(const float* const* inputChanne
521524

522525
setCurrentTime(curSample);
523526
}
524-
else if (isCurrentlyPlaying->boolValue())
527+
else
528+
#endif
529+
if (isCurrentlyPlaying->boolValue())
525530
{
526531
setCurrentTime(timeInSamples + numSamples);
527532
}

0 commit comments

Comments
 (0)