VCTR
Loading...
Searching...
No Matches
vctr_juce_helpers.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2023 by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23#pragma once
24
25#include "../vctr/vctr.h"
26#include <juce_dsp/juce_dsp.h>
27
33{
34
62template <is::anyVctr Src>
63auto toSIMDRegister (const Src& src, size_t i = 0)
64{
65 using SIMDRegister = juce::dsp::SIMDRegister<typename Src::value_type>;
66
67 VCTR_ASSERT (src.size() == SIMDRegister::SIMDNumElements);
68
69 if constexpr (Config::archARM)
70 {
71 return SIMDRegister { src.getNeon (i).value };
72 }
73 else
74 {
75 constexpr size_t registerSize = SIMDRegister::SIMDRegisterSize;
76
77 if constexpr (registerSize == 32)
78 {
79 return SIMDRegister { src.getAVX (i).value };
80 }
81
82 return SIMDRegister { src.getSSE (i).value };
83 }
84}
85
102template <class SampleType>
103auto blockChannelAsSpan (const juce::dsp::AudioBlock<SampleType>& block, size_t channelIdx)
104{
105 auto numSamples = block.getNumSamples();
106 return Span { block.getChannelPointer (channelIdx), numSamples };
107}
108
116template <class SampleType>
117auto blockChannelAsSpanSIMDAligned (const juce::dsp::AudioBlock<SampleType>& block, size_t channelIdx)
118{
119 auto numSamples = block.getNumSamples();
120 return Span { block.getChannelPointer (channelIdx), numSamples, simdAlignedSpanStorageInfo<SampleType>() };
121}
122
131template <class SampleType>
132auto audioBufferChannelAsSpan (juce::AudioBuffer<SampleType>& buffer, size_t channelIdx)
133{
134 auto numSamples = size_t (buffer.getNumSamples());
135 return Span { buffer.getWritePointer (int (channelIdx)), numSamples };
136}
137
146template <class SampleType>
147auto audioBufferChannelAsSpan (const juce::AudioBuffer<SampleType>& buffer, size_t channelIdx)
148{
149 auto numSamples = size_t (buffer.getNumSamples());
150 return Span { buffer.getReadPointer (int (channelIdx)), numSamples };
151}
152
153}
154
155namespace vctr::expressions
156{
157
158template <std::floating_point SampleType, is::reductionExpressionChainBuilderForSource<Span<const SampleType>> ChannelReduction>
160{
161public:
163
164 template <class Src>
165 constexpr AudioBlockChannelReduction (const Src& s, ChannelReduction& re)
166 : src (s),
167 channelReduction (re)
168 {}
169
170 constexpr const auto& getStorageInfo() const
171 {
172 return storageInfo;
173 }
174
175 constexpr size_t size() const
176 {
177 return src.getNumChannels();
178 }
179
180 constexpr bool isNotAliased (const void*) const
181 {
182 return false;
183 }
184
185 template <size_t i, class RuntimeArgs>
186 constexpr void iterateOverRuntimeArgChain (const RuntimeArgs& rtArgs)
187 {
188 tryApplyingRuntimeArgsToSrc<i + 1> (rtArgs, src);
189 }
190
191 VCTR_FORCEDINLINE constexpr auto operator[] (size_t i) const
192 {
193 return channelReduction << juce_helpers::blockChannelAsSpan (src, i);
194 }
195
196private:
197 juce::dsp::AudioBlock<const SampleType> src;
198 ChannelReduction& channelReduction;
199
200 static constexpr StaticStorageInfo<false, false, alignof (SampleType)> storageInfo;
201};
202}
203
204namespace vctr::juce_helpers
205{
206
231template <std::floating_point SampleType, is::reductionExpressionChainBuilderForSource<Span<const SampleType>> ChannelReduction>
232auto transformChannelsByExpression (const juce::dsp::AudioBlock<SampleType>& block, ChannelReduction& channelReduction)
233{
235}
236}
The view type.
Definition: Span.h:66
Definition: vctr_juce_helpers.h:160
Helper functions to integrate vctr into dsp code written with JUCE (https://github....
Definition: vctr_juce_helpers.h:33
auto blockChannelAsSpanSIMDAligned(const juce::dsp::AudioBlock< SampleType > &block, size_t channelIdx)
Returns a Span that views a single channel of a juce::dsp::AudioBlock, expecting SIMD aligned memory.
Definition: vctr_juce_helpers.h:117
auto blockChannelAsSpan(const juce::dsp::AudioBlock< SampleType > &block, size_t channelIdx)
Returns a Span that views a single channel of a juce::dsp::AudioBlock.
Definition: vctr_juce_helpers.h:103
auto transformChannelsByExpression(const juce::dsp::AudioBlock< SampleType > &block, ChannelReduction &channelReduction)
Returns an expression that will apply channelReduction on each channel of the source AudioBlock.
Definition: vctr_juce_helpers.h:232
auto toSIMDRegister(const Src &src, size_t i=0)
Loads elements of src into a juce::SIMDRegister.
Definition: vctr_juce_helpers.h:63
auto audioBufferChannelAsSpan(juce::AudioBuffer< SampleType > &buffer, size_t channelIdx)
Returns a Span that views a single channel of a juce::AudioBuffer.
Definition: vctr_juce_helpers.h:132
typename detail::ValueType< std::remove_cvref_t< T > >::Type ValueType
If T is an expression template, it equals its return type, if it's a type that defines value_type as ...
Definition: Traits.h:201
The base class to every expression template.
Definition: ExpressionTemplate.h:37
A storage info type especially used to pass compile time constant traits when viewing externally owne...
Definition: SIMDHelpers.h:183