Skip to content

Commit

Permalink
Merge pull request #16 from SLM-Audio/syl/remove-alloca
Browse files Browse the repository at this point in the history
Move runBlockDispatch to Plugin
  • Loading branch information
MeijisIrlnd authored Oct 25, 2024
2 parents d9db756 + 6e988b4 commit a90b6c3
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 78 deletions.
3 changes: 1 addition & 2 deletions examples/delay/source/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "Plugin.h"
#include "PluginEditor.h"
#include <mostly_harmless/audio/mostlyharmless_AudioHelpers.h>

namespace examples::delay {
std::vector<mostly_harmless::Parameter<float>> createParams() {
Expand Down Expand Up @@ -34,7 +33,7 @@ namespace examples::delay {
m_delay.process(buffer, m_parameters);
};

mostly_harmless::runBlockDispatch<float>(buffer, context, std::move(onEvent), std::move(onAudio));
mostly_harmless::Plugin<float>::runBlockDispatch(buffer, context, std::move(onEvent), std::move(onAudio));
}

void Plugin::flushParams(mostly_harmless::events::InputEventContext context) noexcept {
Expand Down
2 changes: 1 addition & 1 deletion examples/delay/source/PluginEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "PluginEditor.h"
#include "Parameters.h"
#include <magic_enum.hpp>
#include <magic_enum/magic_enum.hpp>
namespace examples::delay {
PluginEditor::PluginEditor(std::uint32_t width, std::uint32_t height) : mostly_harmless::gui::WebviewEditor(width, height, mostly_harmless::gui::Colour{ 0xFF89CC04 }) {
std::stringstream initialDataStream;
Expand Down
2 changes: 1 addition & 1 deletion examples/gain/source/GainEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <fmt/core.h>
#include <iostream>
#include <sstream>
#include <magic_enum.hpp>
#include <magic_enum/magic_enum.hpp>
#include "Gain.h"
#include <mostlyharmless_WebResources.h>

Expand Down
1 change: 0 additions & 1 deletion include/mostly_harmless/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ set(MOSTLYHARMLESS_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/gui/mostlyharmless_Colour.h
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_TaskThread.h
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Timer.h
${CMAKE_CURRENT_SOURCE_DIR}/audio/mostlyharmless_AudioHelpers.h
${PLATFORM_HEADERS}
PARENT_SCOPE)
73 changes: 0 additions & 73 deletions include/mostly_harmless/audio/mostlyharmless_AudioHelpers.h

This file was deleted.

2 changes: 2 additions & 0 deletions include/mostly_harmless/mostlyharmless_Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ namespace mostly_harmless {
void paramsFlush(const clap_input_events* in, const clap_output_events* out) noexcept override;

protected:
void runBlockDispatch(marvin::containers::BufferView<SampleType> bufferView, events::InputEventContext eventContext, std::function<void(const clap_event_header* event)>&& eventCallback, std::function<void(marvin::containers::BufferView<SampleType>)>&& blockCallback) noexcept;
/**
* Retrieves a non owning flat view into the internal params struct - useful for iterating over params, etc etc
* \return A non owning view into the internal params.
Expand Down Expand Up @@ -224,6 +225,7 @@ namespace mostly_harmless {


private:
std::vector<SampleType*> m_channelScratchVec;
std::vector<Parameter<SampleType>> m_indexedParams;
std::unordered_map<clap_id, Parameter<SampleType>*> m_idParams;
std::optional<TransportState> m_lastTransportState{};
Expand Down
32 changes: 32 additions & 0 deletions source/mostlyharmless_Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace mostly_harmless {
runOnMainThread(std::move(messageThreadCallback));
};
m_guiDispatchThread.action = std::move(guiDispatchCallback);
m_channelScratchVec.reserve(64);
}

template <marvin::FloatType SampleType>
Expand Down Expand Up @@ -218,6 +219,37 @@ namespace mostly_harmless {
default: break;
}
}
template <marvin::FloatType SampleType>
void Plugin<SampleType>::runBlockDispatch(marvin::containers::BufferView<SampleType> bufferView, events::InputEventContext eventContext, std::function<void(const clap_event_header*)>&& eventCallback, std::function<void(marvin::containers::BufferView<SampleType>)>&& blockCallback) noexcept {
const auto numChannels = bufferView.getNumChannels();
m_channelScratchVec.resize(numChannels);
size_t lastEventIndex{ 0 };
auto* const* rawBuff = bufferView.getArrayOfWritePointers();
for (size_t i = 0; i < bufferView.getNumSamples(); ++i) {
if (eventContext.next() && eventContext.next()->time == static_cast<std::uint32_t>(i)) {
while (eventContext.next() && eventContext.next()->time == static_cast<std::uint32_t>(i)) {
eventCallback(eventContext.next());
++eventContext;
}
for (size_t channel = 0; channel < numChannels; ++channel) {
auto* offsetChannelPtr = rawBuff[channel] + static_cast<std::ptrdiff_t>(lastEventIndex);
m_channelScratchVec[channel] = offsetChannelPtr;
}
const auto numSamples = i - lastEventIndex;
marvin::containers::BufferView<SampleType> slice{ m_channelScratchVec.data(), numChannels, numSamples };
blockCallback(slice);
lastEventIndex = i;
}
}
const auto remaining = static_cast<std::int64_t>(bufferView.getNumSamples()) - static_cast<std::int64_t>(lastEventIndex);
if (remaining <= 0) return;
for (size_t channel = 0; channel < numChannels; ++channel) {
auto* offsetChannelPtr = rawBuff[channel] + static_cast<std::ptrdiff_t>(lastEventIndex);
m_channelScratchVec[channel] = offsetChannelPtr;
}
marvin::containers::BufferView<SampleType> slice{ m_channelScratchVec.data(), bufferView.getNumChannels(), static_cast<size_t>(remaining) };
blockCallback(slice);
}

template <marvin::FloatType SampleType>
std::span<Parameter<SampleType>> Plugin<SampleType>::getParamView() noexcept {
Expand Down

0 comments on commit a90b6c3

Please sign in to comment.