Skip to content

Commit

Permalink
Report current time in samples to plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
psobot committed May 7, 2024
1 parent a00c02d commit e688aaf
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion pedalboard/ExternalPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class AbstractExternalPlugin : public Plugin {
};

template <typename ExternalPluginType>
class ExternalPlugin : public AbstractExternalPlugin {
class ExternalPlugin : public AbstractExternalPlugin, juce::AudioPlayHead {
public:
ExternalPlugin(
std::string &_pathToPluginFile,
Expand Down Expand Up @@ -630,6 +630,10 @@ class ExternalPlugin : public AbstractExternalPlugin {
~ExternalPlugin() {
{
std::lock_guard<std::mutex> lock(EXTERNAL_PLUGIN_MUTEX);
if (pluginInstance) {
pluginInstance->setPlayHead(nullptr);
}

pluginInstance.reset();
NUM_ACTIVE_EXTERNAL_PLUGINS--;

Expand Down Expand Up @@ -695,6 +699,9 @@ class ExternalPlugin : public AbstractExternalPlugin {
{
std::lock_guard<std::mutex> lock(EXTERNAL_PLUGIN_MUTEX);
// Delete the plugin instance itself:
if (pluginInstance) {
pluginInstance->setPlayHead(nullptr);
}
pluginInstance.reset();
NUM_ACTIVE_EXTERNAL_PLUGINS--;
}
Expand All @@ -714,6 +721,7 @@ class ExternalPlugin : public AbstractExternalPlugin {
loadError.toStdString());
}

pluginInstance->setPlayHead(this);
pluginInstance->enableAllBuses();

auto mainInputBus = pluginInstance->getBus(true, 0);
Expand All @@ -723,6 +731,9 @@ class ExternalPlugin : public AbstractExternalPlugin {
auto exception = std::invalid_argument(
"Plugin '" + pluginInstance->getName().toStdString() +
"' does not produce audio output.");
if (pluginInstance) {
pluginInstance->setPlayHead(nullptr);
}
pluginInstance.reset();
throw exception;
}
Expand All @@ -741,6 +752,7 @@ class ExternalPlugin : public AbstractExternalPlugin {
pathToPluginFile.toStdString() + ": " +
loadError.toStdString());
}
pluginInstance->setPlayHead(this);
}
}

Expand Down Expand Up @@ -912,7 +924,10 @@ class ExternalPlugin : public AbstractExternalPlugin {
juce::AudioBuffer<float> audioBuffer(numOutputChannels, bufferSize);
audioBuffer.clear();

currentPositionInfo.isPlaying = true;
pluginInstance->processBlock(audioBuffer, emptyNoteBuffer);
currentPositionInfo.isPlaying = false;
currentPositionInfo.timeInSamples += bufferSize;
auto noiseFloor = audioBuffer.getMagnitude(0, bufferSize);

audioBuffer.clear();
Expand All @@ -922,7 +937,10 @@ class ExternalPlugin : public AbstractExternalPlugin {
// the messages in a MidiBuffer get erased every time we call processBlock!
{
juce::MidiBuffer noteOnBuffer(noteOn);
currentPositionInfo.isPlaying = true;
pluginInstance->processBlock(audioBuffer, noteOnBuffer);
currentPositionInfo.isPlaying = false;
currentPositionInfo.timeInSamples += bufferSize;
}

// Then keep pumping the message thread until we get some louder output:
Expand All @@ -945,8 +963,11 @@ class ExternalPlugin : public AbstractExternalPlugin {

audioBuffer.clear();
{
currentPositionInfo.isPlaying = true;
juce::MidiBuffer noteOnBuffer(noteOn);
pluginInstance->processBlock(audioBuffer, noteOnBuffer);
currentPositionInfo.isPlaying = false;
currentPositionInfo.timeInSamples += bufferSize;
}

if (juce::Time::currentTimeMillis() >= endTime)
Expand All @@ -958,8 +979,12 @@ class ExternalPlugin : public AbstractExternalPlugin {
audioBuffer.clear();
{
juce::MidiBuffer allNotesOffBuffer(allNotesOff);
currentPositionInfo.isPlaying = true;
pluginInstance->processBlock(audioBuffer, allNotesOffBuffer);
currentPositionInfo.isPlaying = false;
currentPositionInfo.timeInSamples += bufferSize;
}
currentPositionInfo.timeInSamples = 0;
pluginInstance->reset();
pluginInstance->releaseResources();

Expand Down Expand Up @@ -1077,6 +1102,7 @@ class ExternalPlugin : public AbstractExternalPlugin {
// Force prepare() to be called again later by invalidating lastSpec:
lastSpec.maximumBlockSize = 0;
samplesProvided = 0;
currentPositionInfo.timeInSamples = 0;
}
}

Expand All @@ -1102,6 +1128,8 @@ class ExternalPlugin : public AbstractExternalPlugin {

pluginInstance->setNonRealtime(true);
pluginInstance->prepareToPlay(spec.sampleRate, spec.maximumBlockSize);
currentPositionInfo.timeInSamples = 0;
currentPositionInfo.isPlaying = false;

lastSpec = spec;
}
Expand Down Expand Up @@ -1173,8 +1201,11 @@ class ExternalPlugin : public AbstractExternalPlugin {
channelPointers.size(),
outputBlock.getNumSamples());

currentPositionInfo.isPlaying = true;
pluginInstance->processBlock(audioBuffer, emptyMidiBuffer);
currentPositionInfo.isPlaying = false;
samplesProvided += outputBlock.getNumSamples();
currentPositionInfo.timeInSamples += outputBlock.getNumSamples();

// To compensate for any latency added by the plugin,
// only tell Pedalboard to use the last _n_ samples.
Expand Down Expand Up @@ -1269,7 +1300,10 @@ class ExternalPlugin : public AbstractExternalPlugin {
juce::MidiBuffer midiChunk;
midiChunk.addEvents(midiInputBuffer, i, chunkSampleCount, -i);

currentPositionInfo.isPlaying = true;
pluginInstance->processBlock(audioChunk, midiChunk);
currentPositionInfo.isPlaying = false;
currentPositionInfo.timeInSamples += chunkSampleCount;
}
}

Expand Down Expand Up @@ -1337,6 +1371,11 @@ class ExternalPlugin : public AbstractExternalPlugin {
ExternalPluginReloadType reloadType = ExternalPluginReloadType::Unknown;
juce::PluginDescription foundPluginDescription;

bool getCurrentPosition(CurrentPositionInfo &result) override {
result = currentPositionInfo;
return true;
}

private:
std::unique_ptr<juce::AudioPluginInstance>
createPluginInstance(const juce::PluginDescription &foundPluginDescription,
Expand Down Expand Up @@ -1371,6 +1410,7 @@ class ExternalPlugin : public AbstractExternalPlugin {
juce::String pathToPluginFile;
juce::AudioPluginFormatManager pluginFormatManager;
std::unique_ptr<juce::AudioPluginInstance> pluginInstance;
juce::AudioPlayHead::CurrentPositionInfo currentPositionInfo;

long samplesProvided = 0;
float initializationTimeout = DEFAULT_INITIALIZATION_TIMEOUT_SECONDS;
Expand Down

0 comments on commit e688aaf

Please sign in to comment.