Skip to content

Commit

Permalink
Add ARA document context change listener
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Oct 1, 2024
1 parent 7586c4d commit 54d8039
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/audioCore/ara/ARAChangeListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ ARARegionChangeListener::ARARegionChangeListener(ARAVirtualDocument* document)
void ARARegionChangeListener::changeListenerCallback(juce::ChangeBroadcaster* source) {
this->document->updateRegions();
}

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

void ARAContextChangeListener::changeListenerCallback(juce::ChangeBroadcaster* source) {
this->document->updateAudioAndContext();
}
12 changes: 12 additions & 0 deletions src/audioCore/ara/ARAChangeListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ class ARARegionChangeListener : public juce::ChangeListener {

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ARARegionChangeListener)
};

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

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

private:
ARAVirtualDocument* const document;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ARAContextChangeListener)
};
36 changes: 36 additions & 0 deletions src/audioCore/ara/ARAVirtualDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ARAVirtualDocument::ARAVirtualDocument(
araEditorRenderer(araEditorRenderer), araPlaybackRenderer(araPlaybackRenderer) {
this->listener = std::make_unique<ARAChangeListener>(this);
this->regionListener = std::make_unique<ARARegionChangeListener>(this);
this->contextListener = std::make_unique<ARAContextChangeListener>(this);
}

ARAVirtualDocument::~ARAVirtualDocument() {
Expand Down Expand Up @@ -44,6 +45,37 @@ void ARAVirtualDocument::updateRegions() {
this->lockPlugin(false);
}

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

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

/** Lock Document */
juce::ARAEditGuard locker(this->controller);

/** Remove Audio Modification */
this->audioModification = nullptr;

/** Update Audio Source */
if (this->audioSource) {
this->audioSource->update();
}

/** Create New Audio Modification */
this->audioModification = std::make_unique<ARAVirtualAudioModification>(
this->controller, *(this->audioSource));

/** Update Musical Context */
if (this->musicalContext) {
this->musicalContext->update();
}

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

void ARAVirtualDocument::update() {
/** Invoke This On Message Thread */
JUCE_ASSERT_MESSAGE_THREAD
Expand Down Expand Up @@ -109,6 +141,10 @@ juce::ChangeListener* ARAVirtualDocument::getRegionListener() const {
return this->regionListener.get();
}

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

void ARAVirtualDocument::clearUnsafe() {
/** Remove Regions */
this->clearRegionsUnsafe();
Expand Down
3 changes: 3 additions & 0 deletions src/audioCore/ara/ARAVirtualDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class ARAVirtualDocument {
~ARAVirtualDocument();

void updateRegions();
void updateAudioAndContext();
void update();
void clear();

juce::ChangeListener* getListener() const;
juce::ChangeListener* getRegionListener() const;
juce::ChangeListener* getContextListener() const;

private:
SeqSourceProcessor* const seq = nullptr;
Expand All @@ -42,6 +44,7 @@ class ARAVirtualDocument {

std::unique_ptr<ARAChangeListener> listener = nullptr;
std::unique_ptr<ARARegionChangeListener> regionListener = nullptr;
std::unique_ptr<ARAContextChangeListener> contextListener = nullptr;

void clearUnsafe();
void clearRegionsUnsafe();
Expand Down
7 changes: 7 additions & 0 deletions src/audioCore/graph/PluginDecorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PluginDecorator::PluginDecorator(SeqSourceProcessor* seq,
/** ARA Change Broadcaster */
this->araChangeBroadcaster = std::make_unique<juce::ChangeBroadcaster>();
this->araRegionChangeBroadcaster = std::make_unique<juce::ChangeBroadcaster>();
this->araContextChangeBroadcaster = std::make_unique<juce::ChangeBroadcaster>();
}

PluginDecorator::PluginDecorator(std::unique_ptr<juce::AudioPluginInstance> plugin,
Expand Down Expand Up @@ -169,6 +170,8 @@ void PluginDecorator::setARA(
this->araVirtualDocument->getListener());
this->araRegionChangeBroadcaster->addChangeListener(
this->araVirtualDocument->getRegionListener());
this->araContextChangeBroadcaster->addChangeListener(
this->araVirtualDocument->getContextListener());
}

/** Callback */
Expand Down Expand Up @@ -411,6 +414,10 @@ void PluginDecorator::invokeARADocumentRegionChange() {
this->araRegionChangeBroadcaster->sendChangeMessage();
}

void PluginDecorator::invokeARADocumentContextChange() {
this->araContextChangeBroadcaster->sendChangeMessage();
}

const juce::String PluginDecorator::getName() const {
if (!this->plugin) { return ""; }
return this->plugin->getName() + ((bool)this->araDocumentController ? "(ARA)" : "");
Expand Down
2 changes: 2 additions & 0 deletions src/audioCore/graph/PluginDecorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class PluginDecorator final : public juce::AudioProcessor,

void invokeARADocumentChange();
void invokeARADocumentRegionChange();
void invokeARADocumentContextChange();

class SafePointer {
private:
Expand Down Expand Up @@ -168,6 +169,7 @@ class PluginDecorator final : public juce::AudioProcessor,
std::unique_ptr<ARAVirtualDocument> araVirtualDocument = nullptr;
std::unique_ptr<juce::ChangeBroadcaster> araChangeBroadcaster = nullptr;
std::unique_ptr<juce::ChangeBroadcaster> araRegionChangeBroadcaster = nullptr;
std::unique_ptr<juce::ChangeBroadcaster> araContextChangeBroadcaster = nullptr;

int pluginOnOffCount = 0;
juce::SpinLock pluginOnOffMutex;
Expand Down
11 changes: 11 additions & 0 deletions src/audioCore/graph/SeqSourceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ void SeqSourceProcessor::setRecording(bool recording) {
}
this->recordingFlag = recording;

/** Sync ARA */
if (!recording) {
this->syncARAContext();
}

/** Callback */
UICallbackAPI<int>::invoke(UICallbackType::SeqRecChanged, this->index);
}
Expand Down Expand Up @@ -549,6 +554,12 @@ const juce::Array<float> SeqSourceProcessor::getOutputLevels() const {
return this->outputLevels;
}

void SeqSourceProcessor::syncARAContext() {
if (auto plugin = this->getInstrProcessor()) {
plugin->invokeARADocumentContextChange();
}
}

void SeqSourceProcessor::prepareToPlay(
double sampleRate, int maximumExpectedSamplesPerBlock) {
this->juce::AudioProcessorGraph::prepareToPlay(
Expand Down
2 changes: 2 additions & 0 deletions src/audioCore/graph/SeqSourceProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class SeqSourceProcessor final : public juce::AudioProcessorGraph,

const juce::Array<float> getOutputLevels() const;

void syncARAContext();

public:
void prepareToPlay(double sampleRate, int maximumExpectedSamplesPerBlock) override;
void releaseResources() override {};
Expand Down

0 comments on commit 54d8039

Please sign in to comment.