1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

ARAPluginDemo: Fix incorrect access of playhead from the GUI

This commit is contained in:
attila 2022-07-18 18:49:37 +02:00
parent 29447dd7f3
commit 2ea0a1b1f6

View file

@ -615,6 +615,27 @@ protected:
} }
}; };
struct PlayHeadState
{
void update (AudioPlayHead* aph)
{
const auto info = aph->getPosition();
if (info.hasValue() && info->getIsPlaying())
{
isPlaying.store (true);
timeInSeconds.store (info->getTimeInSeconds().orFallback (0));
}
else
{
isPlaying.store (false);
}
}
std::atomic<bool> isPlaying { false };
std::atomic<double> timeInSeconds { 0.0 };
};
//============================================================================== //==============================================================================
class ARADemoPluginAudioProcessorImpl : public AudioProcessor, class ARADemoPluginAudioProcessorImpl : public AudioProcessor,
public AudioProcessorARAExtension public AudioProcessorARAExtension
@ -630,11 +651,13 @@ public:
//============================================================================== //==============================================================================
void prepareToPlay (double sampleRate, int samplesPerBlock) override void prepareToPlay (double sampleRate, int samplesPerBlock) override
{ {
prepareToPlayForARA (sampleRate, samplesPerBlock, getMainBusNumOutputChannels(), getProcessingPrecision()); playHeadState.isPlaying.store (false);
prepareToPlayForARA (sampleRate, samplesPerBlock, getMainBusNumOutputChannels(), getProcessingPrecision());
} }
void releaseResources() override void releaseResources() override
{ {
playHeadState.isPlaying.store (false);
releaseResourcesForARA(); releaseResourcesForARA();
} }
@ -653,7 +676,10 @@ public:
ScopedNoDenormals noDenormals; ScopedNoDenormals noDenormals;
if (! processBlockForARA (buffer, isRealtime(), getPlayHead())) auto* playHead = getPlayHead();
playHeadState.update (playHead);
if (! processBlockForARA (buffer, isRealtime(), playHead))
processBlockBypassed (buffer, midiMessages); processBlockBypassed (buffer, midiMessages);
} }
@ -674,6 +700,8 @@ public:
void getStateInformation (MemoryBlock&) override {} void getStateInformation (MemoryBlock&) override {}
void setStateInformation (const void*, int) override {} void setStateInformation (const void*, int) override {}
PlayHeadState playHeadState;
private: private:
//============================================================================== //==============================================================================
static BusesProperties getBusesProperties() static BusesProperties getBusesProperties()
@ -1043,8 +1071,8 @@ public:
void paint (Graphics& g) override { g.fillAll (juce::Colours::yellow.darker (0.2f)); } void paint (Graphics& g) override { g.fillAll (juce::Colours::yellow.darker (0.2f)); }
}; };
OverlayComponent(std::function<AudioPlayHead*()> getAudioPlayheadIn) OverlayComponent (PlayHeadState& playHeadStateIn)
: getAudioPlayhead (std::move (getAudioPlayheadIn)) : playHeadState (&playHeadStateIn)
{ {
addChildComponent (playheadMarker); addChildComponent (playheadMarker);
setInterceptsMouseClicks (false, false); setInterceptsMouseClicks (false, false);
@ -1074,12 +1102,9 @@ public:
private: private:
void doResize() void doResize()
{ {
auto* aph = getAudioPlayhead(); if (playHeadState->isPlaying.load())
const auto info = aph->getPosition();
if (info.hasValue() && info->getIsPlaying())
{ {
const auto markerX = info->getTimeInSeconds().orFallback (0) * pixelPerSecond; const auto markerX = playHeadState->timeInSeconds.load() * pixelPerSecond;
const auto playheadLine = getLocalBounds().withTrimmedLeft ((int) (markerX - markerWidth / 2.0) - horizontalOffset) const auto playheadLine = getLocalBounds().withTrimmedLeft ((int) (markerX - markerWidth / 2.0) - horizontalOffset)
.removeFromLeft ((int) markerWidth); .removeFromLeft ((int) markerWidth);
playheadMarker.setVisible (true); playheadMarker.setVisible (true);
@ -1098,7 +1123,7 @@ private:
static constexpr double markerWidth = 2.0; static constexpr double markerWidth = 2.0;
std::function<AudioPlayHead*()> getAudioPlayhead; PlayHeadState* playHeadState;
double pixelPerSecond = 1.0; double pixelPerSecond = 1.0;
int horizontalOffset = 0; int horizontalOffset = 0;
PlayheadMarkerComponent playheadMarker; PlayheadMarkerComponent playheadMarker;
@ -1110,9 +1135,9 @@ class DocumentView : public Component,
private ARAEditorView::Listener private ARAEditorView::Listener
{ {
public: public:
explicit DocumentView (ARADocument& document, std::function<AudioPlayHead*()> getAudioPlayhead) explicit DocumentView (ARADocument& document, PlayHeadState& playHeadState)
: araDocument (document), : araDocument (document),
overlay (std::move (getAudioPlayhead)) overlay (playHeadState)
{ {
addAndMakeVisible (tracksBackground); addAndMakeVisible (tracksBackground);
@ -1371,8 +1396,7 @@ public:
if (auto* editorView = getARAEditorView()) if (auto* editorView = getARAEditorView())
{ {
auto* document = ARADocumentControllerSpecialisation::getSpecialisedDocumentController(editorView->getDocumentController())->getDocument(); auto* document = ARADocumentControllerSpecialisation::getSpecialisedDocumentController(editorView->getDocumentController())->getDocument();
documentView = std::make_unique<DocumentView> (*document, documentView = std::make_unique<DocumentView> (*document, p.playHeadState );
[this]() { return getAudioProcessor()->getPlayHead(); });
} }
addAndMakeVisible (documentView.get()); addAndMakeVisible (documentView.get());