Skip to content

Commit

Permalink
remove std::function usage in process
Browse files Browse the repository at this point in the history
re #18228
  • Loading branch information
x37v committed May 30, 2023
1 parent 75fddac commit dd23360
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
53 changes: 25 additions & 28 deletions RNBO_JuceAudioProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,32 +461,30 @@ bool JuceAudioProcessor::isBusesLayoutSupported (const BusesLayout& /*layouts*/)
void JuceAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
auto samples = static_cast<Index>(buffer.getNumSamples());
wrapProcess(samples, midiMessages, [this, samples, &buffer]() {
_rnboObject.process(
buffer.getArrayOfReadPointers(), static_cast<Index>(buffer.getNumChannels()),
buffer.getArrayOfWritePointers(), static_cast<Index>(buffer.getNumChannels()),
samples,
&_midiInput, &_midiOutput
);
}
auto tc = preProcess(midiMessages);
_rnboObject.process(
buffer.getArrayOfReadPointers(), static_cast<Index>(buffer.getNumChannels()),
buffer.getArrayOfWritePointers(), static_cast<Index>(buffer.getNumChannels()),
samples,
&_midiInput, &_midiOutput
);
postProcess(tc, midiMessages);
}

void JuceAudioProcessor::processBlock (juce::AudioBuffer<double>& buffer, juce::MidiBuffer& midiMessages)
{
auto samples = static_cast<Index>(buffer.getNumSamples());
wrapProcess(samples, midiMessages, [this, samples, &buffer]() {
_rnboObject.process(
buffer.getArrayOfReadPointers(), static_cast<Index>(buffer.getNumChannels()),
buffer.getArrayOfWritePointers(), static_cast<Index>(buffer.getNumChannels()),
samples,
&_midiInput, &_midiOutput
);
}
auto tc = preProcess(midiMessages);
_rnboObject.process(
buffer.getArrayOfReadPointers(), static_cast<Index>(buffer.getNumChannels()),
buffer.getArrayOfWritePointers(), static_cast<Index>(buffer.getNumChannels()),
samples,
&_midiInput, &_midiOutput
);
postProcess(tc, midiMessages);
}

void JuceAudioProcessor::wrapProcess(Index numSamples, juce::MidiBuffer& midiMessages, std::function<void(void)> process) {
TimeConverter JuceAudioProcessor::preProcess(juce::MidiBuffer& midiMessages) {
RNBO::MillisecondTime time = _rnboObject.getCurrentTime();

//transport
Expand Down Expand Up @@ -527,28 +525,27 @@ void JuceAudioProcessor::wrapProcess(Index numSamples, juce::MidiBuffer& midiMes
}
}

// fill midi input
TimeConverter timeConverter(_rnboObject.getSampleRate(), time);

// fill midi input
_midiInput.clear(); // make sure midi input starts clear
for (auto meta: midiMessages)
{
MillisecondTime t = timeConverter.convertSampleOffsetToMilliseconds(meta.samplePosition);
_midiInput.addEvent(MidiEvent(t, 0, meta.data, (Index)meta.numBytes));
}
midiMessages.clear(); // clear the input that we consumed above so juce doesn't get confused
return timeConverter;
}

process();

void JuceAudioProcessor::postProcess(TimeConverter& timeConverter, juce::MidiBuffer& midiMessages) {
// consume midi output
midiMessages.clear(); // clear the input that we consumed above so juce doesn't get confused
if (!_midiOutput.empty()) {
std::for_each(_midiOutput.begin(),
_midiOutput.end(),
[&timeConverter, &midiMessages](const MidiEvent& ev) {
int sampleNumber = static_cast<int>(timeConverter.convertMillisecondsToSampleOffset(ev.getTime()));
auto midiMessage = MidiMessage(ev.getData(), (int)ev.getLength());
midiMessages.addEvent(midiMessage, sampleNumber);
});
for (const auto& ev: _midiOutput) {
int sampleNumber = static_cast<int>(timeConverter.convertMillisecondsToSampleOffset(ev.getTime()));
auto midiMessage = MidiMessage(ev.getData(), (int)ev.getLength());
midiMessages.addEvent(midiMessage, sampleNumber);
}
_midiOutput.clear();
}
}
Expand Down
4 changes: 3 additions & 1 deletion RNBO_JuceAudioProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "RNBO.h"
#include "RNBO_BinaryData.h"
#include "RNBO_TimeConverter.h"
#include <unordered_map>
#include <vector>
#include <mutex>
Expand Down Expand Up @@ -142,7 +143,8 @@ namespace RNBO {
private:
void loadDataRef(const juce::String refName, const juce::String fileName, std::unique_ptr<juce::AudioFormatReader> reader);

void wrapProcess(Index numSamples, juce::MidiBuffer& midiMessages, std::function<void(void)> process);
TimeConverter preProcess(juce::MidiBuffer& midiMessages);
void postProcess(TimeConverter& timeConverter, juce::MidiBuffer& midiMessages);

class SyncEventHandler : public RNBO::EventHandler
{
Expand Down

0 comments on commit dd23360

Please sign in to comment.