Skip to content

Commit

Permalink
Use std::span instead of std::vector #39
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Nov 17, 2023
1 parent 2f09eca commit bb4884f
Show file tree
Hide file tree
Showing 37 changed files with 96 additions and 228 deletions.
Empty file modified cpp/StftPitchShift/BrewStftPitchShift.cmake
100644 → 100755
Empty file.
Empty file modified cpp/StftPitchShift/CLI.h
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions cpp/StftPitchShift/Cepster.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cmath>
#include <complex>
#include <memory>
#include <span>
#include <vector>

#include <StftPitchShift/FFT.h>
Expand Down Expand Up @@ -36,7 +37,7 @@ namespace stftpitchshift
cutoff = static_cast<size_t>(quefrency * samplerate);
}

void lifter(std::vector<T>& envelope)
void lifter(const std::span<T> envelope)
{
assert(envelope.size() == spectrum.size());

Expand Down Expand Up @@ -71,7 +72,7 @@ namespace stftpitchshift
std::vector<std::complex<T>> spectrum;
std::vector<T> cepstrum;

static void lowpass(std::vector<T>& cepstrum, const size_t cutoff)
static void lowpass(const std::span<T> cepstrum, const size_t cutoff)
{
for (size_t i = 1; i < std::min(cutoff, cepstrum.size()); ++i)
{
Expand Down
Empty file modified cpp/StftPitchShift/ConfigStftPitchShift.cmake
100644 → 100755
Empty file.
Empty file modified cpp/StftPitchShift/ConfigStftPitchShift.cmake.template
100644 → 100755
Empty file.
Empty file modified cpp/StftPitchShift/DebStftPitchShift.cmake
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions cpp/StftPitchShift/Dump.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <iomanip>
#include <iostream>
#include <fstream>
#include <span>
#include <sstream>
#include <vector>

namespace stftpitchshift
{
Expand All @@ -22,7 +22,7 @@ namespace stftpitchshift
}

template<class T>
void operator()(const std::vector<T>& data)
void operator()(const std::span<T> data)
{
if (fileindex < minindex)
{
Expand Down
1 change: 1 addition & 0 deletions cpp/StftPitchShift/ETC.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <regex>
#include <set>
#include <span>
#include <sstream>
#include <string>
#include <tuple>
Expand Down
10 changes: 5 additions & 5 deletions cpp/StftPitchShift/FFT.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <complex>
#include <vector>
#include <span>

namespace stftpitchshift
{
Expand All @@ -12,11 +12,11 @@ namespace stftpitchshift

virtual ~FFT() {}

virtual void fft(const std::vector<float>& frame, std::vector<std::complex<float>>& dft) = 0;
virtual void fft(const std::vector<double>& frame, std::vector<std::complex<double>>& dft) = 0;
virtual void fft(const std::span<float> frame, const std::span<std::complex<float>> dft) = 0;
virtual void fft(const std::span<double> frame, const std::span<std::complex<double>> dft) = 0;

virtual void ifft(const std::vector<std::complex<float>>& dft, std::vector<float>& frame) = 0;
virtual void ifft(const std::vector<std::complex<double>>& dft, std::vector<double>& frame) = 0;
virtual void ifft(const std::span<std::complex<float>> dft, const std::span<float> frame) = 0;
virtual void ifft(const std::span<std::complex<double>> dft, const std::span<double> frame) = 0;

};
}
Empty file modified cpp/StftPitchShift/IO.cpp
100644 → 100755
Empty file.
Empty file modified cpp/StftPitchShift/IO.h
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion cpp/StftPitchShift/LibStftPitchShift.cmake
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ target_include_directories(${PROJECT_NAME}
)

target_compile_features(${PROJECT_NAME}
PRIVATE cxx_std_11
PRIVATE cxx_std_20
)

install(
Expand Down
8 changes: 4 additions & 4 deletions cpp/StftPitchShift/Normalizer.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cmath>
#include <complex>
#include <numeric>
#include <vector>
#include <span>

template<class T>
class Normalizer
Expand All @@ -15,12 +15,12 @@ class Normalizer
{
}

void calibrate(const std::vector<std::complex<T>>& data)
void calibrate(const std::span<std::complex<T>> data)
{
target = rms(data);
}

void normalize(std::vector<std::complex<T>>& data) const
void normalize(const std::span<std::complex<T>> data) const
{
const T a = target;
const T b = rms(data);
Expand All @@ -42,7 +42,7 @@ class Normalizer

T target;

static T rms(const std::vector<std::complex<T>>& data)
static T rms(const std::span<std::complex<T>> data)
{
// without 1/N and sqrt

Expand Down
6 changes: 4 additions & 2 deletions cpp/StftPitchShift/Pitcher.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <algorithm>
#include <cmath>
#include <complex>
#include <limits>
#include <span>
#include <vector>

#include <StftPitchShift/Resampler.h>
Expand Down Expand Up @@ -41,7 +43,7 @@ namespace stftpitchshift
}
}

void shiftpitch(std::vector<std::complex<T>>& dft)
void shiftpitch(const std::span<std::complex<T>> dft)
{
if (values.empty())
{
Expand Down Expand Up @@ -69,7 +71,7 @@ namespace stftpitchshift
}
}

dft = buffer[0];
std::copy(buffer[0].begin(), buffer[0].end(), dft.begin());

return;
}
Expand Down
15 changes: 9 additions & 6 deletions cpp/StftPitchShift/RFFT.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#include <StftPitchShift/FFT.h>

#include <cassert>
#include <complex>
#include <map>
#include <memory>
#include <span>
#include <stdexcept>
#include <string>
#include <vector>

namespace stftpitchshift
{
Expand Down Expand Up @@ -116,7 +119,7 @@ namespace stftpitchshift

public:

void fft(const std::vector<T>& frame, std::vector<std::complex<T>>& dft)
void fft(const std::span<T> frame, const std::span<std::complex<T>> dft)
{
assert(dft.size() == frame.size() / 2 + 1);

Expand Down Expand Up @@ -144,7 +147,7 @@ namespace stftpitchshift
}
}

void ifft(const std::vector<std::complex<T>>& dft, std::vector<T>& frame)
void ifft(const std::span<std::complex<T>> dft, const std::span<T> frame)
{
assert(dft.size() == frame.size() / 2 + 1);

Expand Down Expand Up @@ -234,22 +237,22 @@ namespace stftpitchshift

public:

void fft(const std::vector<float>& frame, std::vector<std::complex<float>>& dft) override
void fft(const std::span<float> frame, const std::span<std::complex<float>> dft) override
{
precision.f.fft(frame, dft);
}

void fft(const std::vector<double>& frame, std::vector<std::complex<double>>& dft) override
void fft(const std::span<double> frame, const std::span<std::complex<double>> dft) override
{
precision.d.fft(frame, dft);
}

void ifft(const std::vector<std::complex<float>>& dft, std::vector<float>& frame) override
void ifft(const std::span<std::complex<float>> dft, const std::span<float> frame) override
{
precision.f.ifft(dft, frame);
}

void ifft(const std::vector<std::complex<double>>& dft, std::vector<double>& frame) override
void ifft(const std::span<std::complex<double>> dft, const std::span<double> frame) override
{
precision.d.ifft(dft, frame);
}
Expand Down
32 changes: 17 additions & 15 deletions cpp/StftPitchShift/Resampler.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <span>
#include <vector>

namespace stftpitchshift
Expand All @@ -28,31 +30,31 @@ namespace stftpitchshift
value = factor;
}

void linear(std::vector<T>& x) const
void linear(const std::span<T> x) const
{
linear<T>(x, x);
}

void linear(const std::vector<T>& x,
std::vector<T>& y) const
void linear(const std::span<T> x,
const std::span<T> y) const
{
linear<T>(x, y);
}

void linear(std::vector<std::complex<T>>& x) const
void linear(const std::span<std::complex<T>> x) const
{
linear<std::complex<T>>(x, x);
}

void linear(const std::vector<std::complex<T>>& x,
std::vector<std::complex<T>>& y) const
void linear(const std::span<std::complex<T>> x,
const std::span<std::complex<T>> y) const
{
linear<std::complex<T>>(x, y);
}

void bilinear(const std::vector<T>& x0,
const std::vector<T>& x1,
std::vector<T>& y) const
void bilinear(const std::span<T> x0,
const std::span<T> x1,
const std::span<T> y) const
{
assert(x0.size() == y.size());
assert(x1.size() == y.size());
Expand All @@ -69,9 +71,9 @@ namespace stftpitchshift
}
}

void bilinear(const std::vector<std::complex<T>>& x0,
const std::vector<std::complex<T>>& x1,
std::vector<std::complex<T>>& y) const
void bilinear(const std::span<std::complex<T>> x0,
const std::span<std::complex<T>> x1,
const std::span<std::complex<T>> y) const
{
assert(x0.size() == y.size());
assert(x1.size() == y.size());
Expand All @@ -93,8 +95,8 @@ namespace stftpitchshift
double value;

template<class V>
void linear(const std::vector<V>& x,
std::vector<V>& y) const
void linear(const std::span<V> x,
const std::span<V> y) const
{
assert(x.size() == y.size());

Expand Down Expand Up @@ -149,7 +151,7 @@ namespace stftpitchshift
}
else if (x.data() != y.data())
{
y = x;
std::copy(x.begin(), x.end(), y.begin());
}
}

Expand Down
Loading

0 comments on commit bb4884f

Please sign in to comment.