Skip to content

Commit

Permalink
Update ARA document sync
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Sep 18, 2024
1 parent 89e4379 commit 42c42e1
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 28 deletions.
9 changes: 9 additions & 0 deletions src/audioCore/ara/ARAChangeListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "ARAChangeListener.h"
#include "ARAVirtualDocument.h"

ARAChangeListener::ARAChangeListener(ARAVirtualDocument* document)
: document(document) {}

void ARAChangeListener::changeListenerCallback(juce::ChangeBroadcaster* source) {
this->document->update();
}
17 changes: 17 additions & 0 deletions src/audioCore/ara/ARAChangeListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <JuceHeader.h>

class ARAVirtualDocument;

class ARAChangeListener : public juce::ChangeListener {
public:
ARAChangeListener(ARAVirtualDocument* document);

void changeListenerCallback(juce::ChangeBroadcaster* source) override;

private:
ARAVirtualDocument* const document;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ARAChangeListener)
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ ARAVirtualDocument::ARAVirtualDocument(
juce::ARAHostModel::PlaybackRendererInterface& araPlaybackRenderer,
const PluginOnOffFunc& pluginOnOff)
: seq(seq), controller(controller), pluginOnOff(pluginOnOff),
araEditorRenderer(araEditorRenderer), araPlaybackRenderer(araPlaybackRenderer) {}
araEditorRenderer(araEditorRenderer), araPlaybackRenderer(araPlaybackRenderer) {
this->listener = std::make_unique<ARAChangeListener>(this);
}

ARAVirtualDocument::~ARAVirtualDocument() {
this->clear();
}

void ARAVirtualDocument::update() {
/** Invoke This On Message Thread */
JUCE_ASSERT_MESSAGE_THREAD

/** Turn Off Plugin */
this->pluginOnOff(false);
this->lockPlugin(true);

/** Lock Document */
juce::ARAEditGuard locker(this->controller);
Expand Down Expand Up @@ -47,12 +52,12 @@ void ARAVirtualDocument::update() {
this->addRegionToRenderer();

/** Turn On Plugin */
this->pluginOnOff(true);
this->lockPlugin(false);
}

void ARAVirtualDocument::clear() {
/** Turn Off Plugin */
this->pluginOnOff(false);
this->lockPlugin(true);

/** Lock Document */
juce::ARAEditGuard locker(this->controller);
Expand All @@ -61,7 +66,11 @@ void ARAVirtualDocument::clear() {
this->clearUnsafe();

/** Turn On Plugin */
this->pluginOnOff(true);
this->lockPlugin(false);
}

juce::ChangeListener* ARAVirtualDocument::getListener() const {
return this->listener.get();
}

void ARAVirtualDocument::clearUnsafe() {
Expand Down Expand Up @@ -97,3 +106,20 @@ void ARAVirtualDocument::addRegionToRenderer() {
}
}
}

void ARAVirtualDocument::lockPlugin(bool locked) {
juce::GenericScopedLock locker(this->pluginLockMutex);

if (locked) {
if (this->pluginLockCount == 0) {
this->pluginOnOff(false);
}
this->pluginLockCount++;
}
else {
this->pluginLockCount--;
if (this->pluginLockCount == 0) {
this->pluginOnOff(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <JuceHeader.h>
#include "ARAObjects.h"
#include "ARAChangeListener.h"

class SeqSourceProcessor;

Expand All @@ -20,22 +21,30 @@ class ARAVirtualDocument {
void update();
void clear();

juce::ChangeListener* getListener() const;

private:
SeqSourceProcessor* const seq = nullptr;
ARA::Host::DocumentController& controller;
juce::ARAHostModel::EditorRendererInterface& araEditorRenderer;
juce::ARAHostModel::PlaybackRendererInterface& araPlaybackRenderer;
const PluginOnOffFunc pluginOnOff;
int pluginLockCount = 0;
juce::SpinLock pluginLockMutex;

std::unique_ptr<ARAVirtualAudioSource> audioSource = nullptr;
std::unique_ptr<ARAVirtualAudioModification> audioModification = nullptr;
std::unique_ptr<ARAVirtualMusicalContext> musicalContext = nullptr;
std::unique_ptr<ARAVirtualRegionSequence> regionSequence = nullptr;
std::unique_ptr<ARAVirtualPlaybackRegion> playbackRegion = nullptr;

std::unique_ptr<ARAChangeListener> listener = nullptr;

void clearUnsafe();
void removeRegionToRenderer();
void addRegionToRenderer();

void lockPlugin(bool locked);

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ARAVirtualDocument)
};
111 changes: 90 additions & 21 deletions src/audioCore/graph/PluginDecorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../uiCallback/UICallback.h"
#include "../misc/AudioLock.h"
#include "../misc/VMath.h"
#include "../misc/ARAController.h"
#include "../ara/ARAController.h"
#include "../AudioCore.h"
#include "../Utils.h"
#include <VSP4.h>
Expand All @@ -25,6 +25,9 @@ PluginDecorator::PluginDecorator(SeqSourceProcessor* seq,
for (int i = 0; i < this->paramCCList.size(); i++) {
this->paramCCList[i] = -1;
}

/** ARA Change Broadcaster */
this->araChangeBroadcaster = std::make_unique<juce::ChangeBroadcaster>();
}

PluginDecorator::PluginDecorator(std::unique_ptr<juce::AudioPluginInstance> plugin,
Expand Down Expand Up @@ -95,8 +98,8 @@ void PluginDecorator::setPlugin(
else {
this->plugin->setPlayHead(this->getPlayHead());
this->plugin->setNonRealtime(this->isNonRealtime());
this->plugin->prepareToPlay(this->getSampleRate(), this->getBlockSize());
this->pluginPrepared = true;
this->pluginOnOffInternal(true,
this->getSampleRate(), this->getBlockSize());

//this->updatePluginBuses();

Expand Down Expand Up @@ -137,15 +140,8 @@ void PluginDecorator::setARA(
this->araPlaybackRenderer = pluginInstance.getPlaybackRendererInterface();

/** Plugin On Off */
auto pluginOnOffFunc = [plugin = this->plugin.get()](bool on) {
if (plugin) {
if (on) {
plugin->prepareToPlay(plugin->getSampleRate(), plugin->getBlockSize());
}
else {
plugin->releaseResources();
}
}
auto pluginOnOffFunc = [this](bool on) {
this->pluginOnOffInternal(on, this->getSampleRate(), this->getBlockSize());
};

/** Create ARA Virtual Document */
Expand All @@ -158,14 +154,18 @@ void PluginDecorator::setARA(
/** Prepare Plugin */
this->plugin->setPlayHead(this->getPlayHead());
this->plugin->setNonRealtime(this->isNonRealtime());
this->plugin->prepareToPlay(this->getSampleRate(), this->getBlockSize());
this->pluginPrepared = true;
this->pluginOnOffInternal(true,
this->getSampleRate(), this->getBlockSize());

//this->updatePluginBuses();

/** Prepare ARA Document */
if (this->araDocumentController) {
if (this->araVirtualDocument) {
this->araVirtualDocument->update();

/** Add Change Listener */
this->araChangeBroadcaster->addChangeListener(
this->araVirtualDocument->getListener());
}

/** Callback */
Expand All @@ -183,8 +183,8 @@ void PluginDecorator::handleARALoadError(const juce::String& pluginIdentifier) {
/** Prepare Plugin */
this->plugin->setPlayHead(this->getPlayHead());
this->plugin->setNonRealtime(this->isNonRealtime());
this->plugin->prepareToPlay(this->getSampleRate(), this->getBlockSize());
this->pluginPrepared = true;
this->pluginOnOffInternal(true,
this->getSampleRate(), this->getBlockSize());

//this->updatePluginBuses();

Expand Down Expand Up @@ -395,6 +395,41 @@ void PluginDecorator::clearMIDICCListener() {
this->ccListener = MIDICCListener{};
}

void PluginDecorator::invokeARADocumentChange() {
/**
* TODO Make the Celemony Melodyne happy.
* Celemony Melodyne GUI make the host crashed when ARA host document changed.
* Solve this later.
*/

this->pluginOnOffInternal(false,
this->getSampleRate(), this->getBlockSize());

this->araChangeBroadcaster->sendSynchronousChangeMessage();
//if (this->araDocumentController) {
// /** Plugin On Off */
// auto pluginOnOffFunc = [this](bool on) {
// this->pluginOnOffInternal(on, this->getSampleRate(), this->getBlockSize());
// };

// /** Create ARA Virtual Document */
// this->araVirtualDocument = std::make_unique<ARAVirtualDocument>(
// this->seq, this->araDocumentController->getDocumentController(),
// this->araEditorRenderer, this->araPlaybackRenderer, pluginOnOffFunc);

// if (this->araVirtualDocument) {
// this->araVirtualDocument->update();

// /** Add Change Listener */
// this->araChangeBroadcaster->addChangeListener(
// this->araVirtualDocument->getListener());
// }
//}

this->pluginOnOffInternal(true,
this->getSampleRate(), this->getBlockSize());
}

const juce::String PluginDecorator::getName() const {
if (!this->plugin) { return ""; }
return this->plugin->getName() + ((bool)this->araDocumentController ? "(ARA)" : "");
Expand All @@ -418,14 +453,15 @@ void PluginDecorator::prepareToPlay(
this->doubleBuffer = std::make_unique<juce::AudioBuffer<double>>(channels, this->getBlockSize());
}

this->plugin->prepareToPlay(sampleRate, maximumExpectedSamplesPerBlock);
this->pluginPrepared = true;
this->pluginOnOffInternal(true,
this->getSampleRate(), this->getBlockSize());
}

void PluginDecorator::releaseResources() {
if (!this->plugin) { return; }
this->pluginPrepared = false;
this->plugin->releaseResources();

this->pluginOnOffInternal(false,
this->getSampleRate(), this->getBlockSize());
}

void PluginDecorator::memoryWarningReceived() {
Expand Down Expand Up @@ -828,3 +864,36 @@ void PluginDecorator::updateBuffer() {
}
}
}

void PluginDecorator::pluginOnOffInternal(bool shouldOn, double sampleRate, int blockSize) {
/** Try to fix Celemony Melodyne crash but this not works */
/*juce::GenericScopedLock locker(this->pluginOnOffMutex);
if (shouldOn) {
if (this->pluginOnOffCount == 0 && plugin) {
plugin->prepareToPlay(sampleRate, blockSize);
this->pluginPrepared = true;
}
this->pluginOnOffCount++;
}
else {
this->pluginOnOffCount--;
if (this->pluginOnOffCount == 0 && plugin) {
this->pluginPrepared = false;
plugin->releaseResources();
}
}*/

if (shouldOn) {
if (plugin) {
plugin->prepareToPlay(sampleRate, blockSize);
this->pluginPrepared = true;
}
}
else {
if (plugin) {
this->pluginPrepared = false;
plugin->releaseResources();
}
}
}
10 changes: 9 additions & 1 deletion src/audioCore/graph/PluginDecorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <JuceHeader.h>
#include "../project/Serializable.h"
#include "../misc/ARAVirtualDocument.h"
#include "../ara/ARAVirtualDocument.h"

class SeqSourceProcessor;

Expand Down Expand Up @@ -57,6 +57,8 @@ class PluginDecorator final : public juce::AudioProcessor,
void setMIDICCListener(const MIDICCListener& listener);
void clearMIDICCListener();

void invokeARADocumentChange();

class SafePointer {
private:
juce::WeakReference<PluginDecorator> weakRef;
Expand Down Expand Up @@ -163,6 +165,10 @@ class PluginDecorator final : public juce::AudioProcessor,
juce::ARAHostModel::EditorRendererInterface araEditorRenderer;
juce::ARAHostModel::PlaybackRendererInterface araPlaybackRenderer;
std::unique_ptr<ARAVirtualDocument> araVirtualDocument = nullptr;
std::unique_ptr<juce::ChangeBroadcaster> araChangeBroadcaster = nullptr;

int pluginOnOffCount = 0;
juce::SpinLock pluginOnOffMutex;

void numChannelsChanged() override;
void numBusesChanged() override;
Expand All @@ -175,6 +181,8 @@ class PluginDecorator final : public juce::AudioProcessor,

void updateBuffer();

void pluginOnOffInternal(bool shouldOn, double sampleRate, int blockSize);

JUCE_DECLARE_WEAK_REFERENCEABLE(PluginDecorator)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginDecorator)
};
Loading

0 comments on commit 42c42e1

Please sign in to comment.