MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
mostlyharmless_AudioHelpers.h
Go to the documentation of this file.
1//
2// Created by Syl on 19/08/2024.
3//
4
5#ifndef MOSTLYHARMLESS_MOSTLYHARMLESS_AUDIOHELPERS_H
6#define MOSTLYHARMLESS_MOSTLYHARMLESS_AUDIOHELPERS_H
9#include <functional>
10namespace mostly_harmless {
36 template <marvin::FloatType SampleType>
39 std::function<void(const clap_event_header* event)>&& eventCallback,
40 std::function<void(marvin::containers::BufferView<SampleType>)>&& blockCallback) {
41 const auto numChannels = buffer.getNumChannels();
42 auto** channelArray = static_cast<SampleType**>(alloca(sizeof(SampleType*) * numChannels));
43 size_t lastEventIndex{ 0 };
44 auto* const* rawBuff = buffer.getArrayOfWritePointers();
45 for (size_t i = 0; i < buffer.getNumSamples(); ++i) {
46 if (eventContext.next() && eventContext.next()->time == static_cast<std::uint32_t>(i)) {
47 while (eventContext.next() && eventContext.next()->time == static_cast<std::uint32_t>(i)) {
48 eventCallback(eventContext.next());
49 ++eventContext;
50 }
51 for (size_t channel = 0; channel < numChannels; ++channel) {
52 auto* offsetChannelPtr = rawBuff[channel] + static_cast<std::ptrdiff_t>(lastEventIndex);
53 channelArray[channel] = offsetChannelPtr;
54 }
55 const auto numSamples = i - lastEventIndex;
56 marvin::containers::BufferView<SampleType> slice{ channelArray, buffer.getNumChannels(), numSamples };
57 blockCallback(slice);
58 lastEventIndex = i;
59 }
60 }
61 const auto remaining = static_cast<std::int64_t>(buffer.getNumSamples()) - static_cast<std::int64_t>(lastEventIndex);
62 if (remaining <= 0) return;
63 for (size_t channel = 0; channel < numChannels; ++channel) {
64 auto* offsetChannelPtr = rawBuff[channel] + static_cast<std::ptrdiff_t>(lastEventIndex);
65 channelArray[channel] = offsetChannelPtr;
66 }
67 marvin::containers::BufferView<SampleType> slice{ channelArray, buffer.getNumChannels(), static_cast<size_t>(remaining) };
68 blockCallback(slice);
69 }
70} // namespace mostly_harmless
71
72#endif // MOSTLYHARMLESS_MOSTLYHARMLESS_AUDIOHELPERS_H
The top-level namespace, contains all things plugin-related.
Definition mostlyharmless_BusConfig.h:3
void runBlockDispatch(marvin::containers::BufferView< SampleType > buffer, mostly_harmless::events::InputEventContext eventContext, std::function< void(const clap_event_header *event)> &&eventCallback, std::function< void(marvin::containers::BufferView< SampleType >)> &&blockCallback)
Splits an input buffer into chunks and dispatches them, allowing for block based processing.
Definition mostlyharmless_AudioHelpers.h:37
Trivially copyable view into a preallocated SampleType**.
Definition marvin_BufferView.h:22
SampleType *const * getArrayOfWritePointers() noexcept
size_t getNumChannels() const noexcept
size_t getNumSamples() const noexcept
Trivially copyable wrapper around the clap-api provided clap_input_events queue, for easier iterating...
Definition mostlyharmless_InputEventContext.h:9
const clap_event_header_t * next() noexcept