Skip to content

Commit

Permalink
Notify loading
Browse files Browse the repository at this point in the history
  • Loading branch information
leonp-s committed Jun 28, 2024
1 parent 4eeffaf commit 373c86c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
38 changes: 36 additions & 2 deletions zones_convolver/ConvolutionEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include "ConvolutionEngine.h"

#include <juce_events/juce_events.h>

namespace zones
{
LoadIRJob::LoadIRJob (juce::dsp::AudioBlock<const float> ir_block,
const juce::dsp::ProcessSpec & spec,
const Convolver::ConvolverSpec & convolver_spec,
ConvolutionCommandQueue::VisitorQueue & command_queue)
ConvolutionCommandQueue::VisitorQueue & command_queue,
std::mutex & load_mutex,
std::function<void ()> on_loading_complete)
: ThreadPoolJob ("load_ir_job")
, spec_ (spec)
, convolver_spec_ (convolver_spec)
, command_queue_ (command_queue)
, load_mutex_ (load_mutex)
, on_loading_complete_ (on_loading_complete)
{
ir_buffer_.setSize (static_cast<int> (ir_block.getNumChannels ()),
static_cast<int> (ir_block.getNumSamples ()));
Expand All @@ -22,8 +28,13 @@ juce::ThreadPoolJob::JobStatus LoadIRJob::runJob ()
ConvolutionCommandQueue::Commands command =
ConvolutionCommandQueue::EngineReadyCommand {.convolver = std::move (convolver)};

std::lock_guard guard {load_mutex_};

if (! shouldExit ())
{
command_queue_.PushCommand (command);
on_loading_complete_ ();
}

return jobHasFinished;
}
Expand Down Expand Up @@ -109,11 +120,34 @@ void ConvolutionEngine::reset ()
void ConvolutionEngine::LoadIR (juce::dsp::AudioBlock<const float> ir_block,
const Convolver::ConvolverSpec & convolver_spec)
{
std::lock_guard guard (
load_mutex_); // Obtained to guarantee load order - probably not the best way to do this...

thread_pool_.removeAllJobs (true, 0);

if (spec_ != std::nullopt)
thread_pool_.addJob (new LoadIRJob (ir_block, *spec_, convolver_spec, command_queue_),
{
is_loading_ = true;
if (OnLoadingUpdated)
OnLoadingUpdated ();

thread_pool_.addJob (new LoadIRJob (ir_block,
*spec_,
convolver_spec,
command_queue_,
load_mutex_,
[&]
{
is_loading_ = false;
juce::MessageManager::callAsync (
[&]
{
if (OnLoadingUpdated)
OnLoadingUpdated ();
});
}),
true);
}
}

void ConvolutionEngine::Clear ()
Expand Down
12 changes: 11 additions & 1 deletion zones_convolver/ConvolutionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class LoadIRJob : public juce::ThreadPoolJob
explicit LoadIRJob (juce::dsp::AudioBlock<const float> ir_block,
const juce::dsp::ProcessSpec & spec,
const Convolver::ConvolverSpec & convolver_spec,
ConvolutionCommandQueue::VisitorQueue & command_queue);
ConvolutionCommandQueue::VisitorQueue & command_queue,
std::mutex & load_mutex,
std::function<void ()> on_loading_complete);
~LoadIRJob () override = default;
JobStatus runJob () override;

Expand All @@ -104,6 +106,8 @@ class LoadIRJob : public juce::ThreadPoolJob
juce::dsp::ProcessSpec spec_;
Convolver::ConvolverSpec convolver_spec_;
ConvolutionCommandQueue::VisitorQueue & command_queue_;
std::mutex & load_mutex_;
std::function<void ()> on_loading_complete_;
};

class ConvolutionEngine
Expand All @@ -130,6 +134,9 @@ class ConvolutionEngine
void process (const juce::dsp::ProcessContextReplacing<float> & replacing) override;
void reset () override;

[[nodiscard]] bool IsLoading () const;
std::function<void ()> OnLoadingUpdated;

private:
std::unique_ptr<Convolver> convolver_;
std::unique_ptr<Convolver> pending_convolver_;
Expand All @@ -140,5 +147,8 @@ class ConvolutionEngine
juce::ThreadPool & thread_pool_;
ConvolutionCommandQueue::VisitorQueue command_queue_;
ConvolutionNotificationQueue::VisitorQueue notification_queue_;

std::mutex load_mutex_;
bool is_loading_ = false;
};
}

0 comments on commit 373c86c

Please sign in to comment.