aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-12-14 16:23:10 -0800
committerChris Robinson <[email protected]>2022-12-14 16:23:10 -0800
commit42090e93719748675384cc6ec9106fb39f869920 (patch)
tree70715c1514d2db153ca66fbc26ed30302eb54fd6
parent7c3f201f2625e37807c608e718b190cc48107b9c (diff)
Avoid manually specifying FFT template parameters
-rw-r--r--alc/effects/convolution.cpp6
-rw-r--r--alc/effects/pshifter.cpp4
-rw-r--r--common/alcomplex.h12
-rw-r--r--common/alspan.h43
-rw-r--r--common/phase_shifter.h4
-rw-r--r--utils/makemhr/makemhr.h4
6 files changed, 47 insertions, 26 deletions
diff --git a/alc/effects/convolution.cpp b/alc/effects/convolution.cpp
index 26ef6fd9..20510af2 100644
--- a/alc/effects/convolution.cpp
+++ b/alc/effects/convolution.cpp
@@ -332,7 +332,7 @@ void ConvolutionState::deviceUpdate(const DeviceBase *device, const Buffer &buff
done += todo;
std::fill(iter, fftbuffer.end(), std::complex<double>{});
- forward_fft<double>(fftbuffer);
+ forward_fft(al::as_span(fftbuffer));
filteriter = std::copy_n(fftbuffer.cbegin(), m, filteriter);
}
}
@@ -539,7 +539,7 @@ void ConvolutionState::process(const size_t samplesToDo,
*/
auto fftiter = std::copy_n(mInput.cbegin(), ConvolveUpdateSamples, mFftBuffer.begin());
std::fill(fftiter, mFftBuffer.end(), complex_f{});
- forward_fft<float>(mFftBuffer);
+ forward_fft(al::as_span(mFftBuffer));
std::copy_n(mFftBuffer.cbegin(), m, &mComplexData[curseg*m]);
@@ -575,7 +575,7 @@ void ConvolutionState::process(const size_t samplesToDo,
* second-half samples (and this output's second half is
* subsequently saved for next time).
*/
- inverse_fft<float>(mFftBuffer);
+ inverse_fft(al::as_span(mFftBuffer));
/* The iFFT'd response is scaled up by the number of bins, so apply
* the inverse to normalize the output.
diff --git a/alc/effects/pshifter.cpp b/alc/effects/pshifter.cpp
index f8409292..0156d777 100644
--- a/alc/effects/pshifter.cpp
+++ b/alc/effects/pshifter.cpp
@@ -184,7 +184,7 @@ void PshifterState::process(const size_t samplesToDo, const al::span<const Float
mFftBuffer[k] = mFIFO[src] * HannWindow[k];
for(size_t src{0u}, k{STFT_SIZE-mPos};src < mPos;++src,++k)
mFftBuffer[k] = mFIFO[src] * HannWindow[k];
- forward_fft<double>(mFftBuffer);
+ forward_fft(al::as_span(mFftBuffer));
/* Analyze the obtained data. Since the real FFT is symmetric, only
* STFT_HALF_SIZE+1 samples are needed.
@@ -243,7 +243,7 @@ void PshifterState::process(const size_t samplesToDo, const al::span<const Float
/* Apply an inverse FFT to get the time-domain siganl, and accumulate
* for the output with windowing.
*/
- inverse_fft<double>(mFftBuffer);
+ inverse_fft(al::as_span(mFftBuffer));
for(size_t dst{mPos}, k{0u};dst < STFT_SIZE;++dst,++k)
mOutputAccum[dst] += HannWindow[k]*mFftBuffer[k].real() * (4.0/OVERSAMP/STFT_SIZE);
for(size_t dst{0u}, k{STFT_SIZE-mPos};dst < mPos;++dst,++k)
diff --git a/common/alcomplex.h b/common/alcomplex.h
index 46dbb5bc..a5126f39 100644
--- a/common/alcomplex.h
+++ b/common/alcomplex.h
@@ -19,19 +19,19 @@ complex_fft(const al::span<std::complex<Real>> buffer, const Real sign);
* Calculate the frequency-domain response of the time-domain signal in the
* provided buffer, which MUST BE power of two.
*/
-template<typename Real>
+template<typename Real, size_t N>
std::enable_if_t<std::is_floating_point<Real>::value>
-forward_fft(const al::span<std::complex<Real>> buffer)
-{ complex_fft(buffer, Real{-1}); }
+forward_fft(const al::span<std::complex<Real>,N> buffer)
+{ complex_fft(buffer.subspan(0), Real{-1}); }
/**
* Calculate the time-domain signal of the frequency-domain response in the
* provided buffer, which MUST BE power of two.
*/
-template<typename Real>
+template<typename Real, size_t N>
std::enable_if_t<std::is_floating_point<Real>::value>
-inverse_fft(const al::span<std::complex<Real>> buffer)
-{ complex_fft(buffer, Real{1}); }
+inverse_fft(const al::span<std::complex<Real>,N> buffer)
+{ complex_fft(buffer.subspan(0), Real{1}); }
/**
* Calculate the complex helical sequence (discrete-time analytical signal) of
diff --git a/common/alspan.h b/common/alspan.h
index 4a0e0430..dcc5750b 100644
--- a/common/alspan.h
+++ b/common/alspan.h
@@ -51,27 +51,27 @@ namespace detail_ {
template<typename T, size_t E>
struct is_span_<span<T,E>> : std::true_type { };
template<typename T>
- using is_span = is_span_<std::remove_cv_t<T>>;
+ constexpr bool is_span_v = is_span_<std::remove_cv_t<T>>::value;
template<typename T>
struct is_std_array_ : std::false_type { };
template<typename T, size_t N>
struct is_std_array_<std::array<T,N>> : std::true_type { };
template<typename T>
- using is_std_array = is_std_array_<std::remove_cv_t<T>>;
+ constexpr bool is_std_array_v = is_std_array_<std::remove_cv_t<T>>::value;
template<typename T, typename = void>
- struct has_size_and_data : std::false_type { };
+ constexpr bool has_size_and_data = false;
template<typename T>
- struct has_size_and_data<T,
+ constexpr bool has_size_and_data<T,
void_t<decltype(al::size(std::declval<T>())), decltype(al::data(std::declval<T>()))>>
- : std::true_type { };
+ = true;
} // namespace detail_
-#define REQUIRES(...) bool rt_=true, std::enable_if_t<rt_ && (__VA_ARGS__),bool> = true
+#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true
#define IS_VALID_CONTAINER(C) \
- !detail_::is_span<C>::value && !detail_::is_std_array<C>::value && \
- !std::is_array<C>::value && detail_::has_size_and_data<C>::value && \
+ !detail_::is_span_v<C> && !detail_::is_std_array_v<C> && \
+ !std::is_array<C>::value && detail_::has_size_and_data<C> && \
std::is_convertible<std::remove_pointer_t<decltype(al::data(std::declval<C&>()))>(*)[],element_type(*)[]>::value
template<typename T, size_t E>
@@ -94,13 +94,13 @@ public:
static constexpr size_t extent{E};
- template<REQUIRES(extent==0)>
+ template<bool is0=(extent == 0), REQUIRES(is0)>
constexpr span() noexcept { }
constexpr span(pointer ptr, index_type /*count*/) : mData{ptr} { }
constexpr span(pointer first, pointer /*last*/) : mData{first} { }
constexpr span(element_type (&arr)[E]) noexcept : span{al::data(arr), al::size(arr)} { }
constexpr span(std::array<value_type,E> &arr) noexcept : span{al::data(arr), al::size(arr)} { }
- template<REQUIRES(std::is_const<element_type>::value)>
+ template<typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,E> &arr) noexcept
: span{al::data(arr), al::size(arr)}
{ }
@@ -205,7 +205,7 @@ public:
constexpr span(element_type (&arr)[N]) noexcept : span{al::data(arr), al::size(arr)} { }
template<size_t N>
constexpr span(std::array<value_type,N> &arr) noexcept : span{al::data(arr), al::size(arr)} { }
- template<size_t N, REQUIRES(std::is_const<element_type>::value)>
+ template<size_t N, typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,N> &arr) noexcept
: span{al::data(arr), al::size(arr)}
{ }
@@ -299,6 +299,27 @@ constexpr inline auto span<T,E>::subspan(size_t offset, size_t count) const
span<element_type>{mData+offset, mData+offset+count};
}
+/* Helpers to deal with the lack of user-defined deduction guides (C++17). */
+template<typename T, typename U>
+constexpr auto as_span(T *ptr, U count_or_end) { return span<T>{ptr, count_or_end}; }
+template<typename T, size_t N>
+constexpr auto as_span(T (&arr)[N]) noexcept { return span<T,N>{al::data(arr), al::size(arr)}; }
+template<typename T, size_t N>
+constexpr auto as_span(std::array<T,N> &arr) noexcept
+{ return span<T,N>{al::data(arr), al::size(arr)}; }
+template<typename T, size_t N>
+constexpr auto as_span(const std::array<T,N> &arr) noexcept
+{ return span<std::add_const_t<T>,N>{al::data(arr), al::size(arr)}; }
+template<typename U, REQUIRES(!detail_::is_span_v<U> && !detail_::is_std_array_v<U>
+ && !std::is_array<U>::value && detail_::has_size_and_data<U>)>
+constexpr auto as_span(U &cont)
+{
+ using value_type = std::remove_pointer_t<decltype(al::data(std::declval<U&>()))>;
+ return span<value_type>{al::data(cont), al::size(cont)};
+}
+template<typename T, size_t N>
+constexpr auto as_span(span<T,N> span_) noexcept { return span_; }
+
#undef IS_VALID_CONTAINER
#undef REQUIRES
diff --git a/common/phase_shifter.h b/common/phase_shifter.h
index 6cfdf053..0d4166bc 100644
--- a/common/phase_shifter.h
+++ b/common/phase_shifter.h
@@ -53,12 +53,12 @@ struct PhaseShifterT {
std::fill_n(fftBuffer.get(), fft_size, complex_d{});
fftBuffer[half_size] = 1.0;
- forward_fft<double>({fftBuffer.get(), fft_size});
+ forward_fft(al::as_span(fftBuffer.get(), fft_size));
for(size_t i{0};i < half_size+1;++i)
fftBuffer[i] = complex_d{-fftBuffer[i].imag(), fftBuffer[i].real()};
for(size_t i{half_size+1};i < fft_size;++i)
fftBuffer[i] = std::conj(fftBuffer[fft_size - i]);
- inverse_fft<double>({fftBuffer.get(), fft_size});
+ inverse_fft(al::as_span(fftBuffer.get(), fft_size));
auto fftiter = fftBuffer.get() + half_size + (FilterSize/2 - 1);
for(float &coeff : mCoeffs)
diff --git a/utils/makemhr/makemhr.h b/utils/makemhr/makemhr.h
index 82604528..ce21af1d 100644
--- a/utils/makemhr/makemhr.h
+++ b/utils/makemhr/makemhr.h
@@ -113,12 +113,12 @@ void MagnitudeResponse(const uint n, const complex_d *in, double *out);
// Performs a forward FFT.
inline void FftForward(const uint n, complex_d *inout)
-{ forward_fft<double>({inout, n}); }
+{ forward_fft(al::as_span(inout, n)); }
// Performs an inverse FFT.
inline void FftInverse(const uint n, complex_d *inout)
{
- inverse_fft<double>({inout, n});
+ inverse_fft(al::as_span(inout, n));
double f{1.0 / n};
for(uint i{0};i < n;i++)
inout[i] *= f;