ape  0.5.0
Audio Programming Environment
resampling.h
Go to the documentation of this file.
1 #ifndef CPPAPE_RESAMPLING_H
2 #define CPPAPE_RESAMPLING_H
3 
4 #include "misc.h"
5 #include "audiofile.h"
6 #include <vector>
7 #include <memory>
8 
9 namespace ape
10 {
16  template<typename T>
18  {
19 
20  class Impl
21  {
22  public:
23 
24  virtual void produce(DynamicSampleMatrix<T>& output, std::size_t frames, double factor) = 0;
25  virtual ~Impl() {}
26  };
27 
28  class MatrixImpl : public Impl
29  {
30  public:
31 
32  MatrixImpl(umatrix<const T> matrix)
33  : source(matrix), position{}
34  {
35 
36  }
37 
38  void produce(DynamicSampleMatrix<T>& output, std::size_t frames, double factor) override
39  {
40 
41  for (std::size_t f = 0; f < frames; ++f)
42  {
43  const auto x0 = static_cast<std::size_t>(position);
44  auto x1 = x0 + 1;
45 
46  if (x1 >= source.samples())
47  x1 -= source.samples();
48 
49 
50  const auto weight = position - x0;
51 
52  for (std::size_t c = 0; c < source.channels(); ++c)
53  {
54  const auto y0 = source[c][x0];
55  const auto y1 = source[c][x1];
56 
57  output.channels[c][f] = y0 * (1 - weight) + weight * y1;
58 
59  }
60 
61  position += factor;
62 
63  while (position >= source.samples())
64  position -= source.samples();
65  }
66  }
67 
68  double position;
69  umatrix<const float> source;
70  };
71 
72  public:
73 
78  : channels(data.channels())
79  {
80  impl = std::make_unique<MatrixImpl>(data);
81  }
82 
92  umatrix<const T> produce(std::size_t frames, double factor = 1)
93  {
94  buffer.resize(channels, frames);
95  impl->produce(buffer, frames, factor);
96  return buffer;
97  }
98 
103  template<typename Func>
104  auto produce(std::size_t frames, Func && f, double factor = 1)
105  {
106  const auto matrix = produce(frames, factor);
107 
108  for (std::size_t frame = 0; frame < frames; ++frame)
109  {
110  for (std::size_t c = 0; c < channelBuffer.size(); ++c)
111  channelBuffer[c] = matrix[c][frame];
112 
113  f(frame, channelBuffer);
114  }
115  }
116 
117  private:
118  std::size_t channels;
119  std::vector<T> channelBuffer;
120  DynamicSampleMatrix<T> buffer;
121  std::unique_ptr<Impl> impl;
122  };
123 }
124 
125 #endif
ape::umatrix
A container representing a 2d rectangular array (and can be used syntactically like one)....
Definition: misc.h:212
ape::RealSourceResampler::produce
umatrix< const T > produce(std::size_t frames, double factor=1)
Produce the next buffer of samples.
Definition: resampling.h:92
audiofile.h
misc.h
ape
Definition: audiofile.h:7
ape::RealSourceResampler::RealSourceResampler
RealSourceResampler(umatrix< const T > data)
Construct the resampler from a umatrix
Definition: resampling.h:77
ape::DynamicSampleMatrix::channels
std::vector< T * > channels
Definition: misc.h:401
ape::DynamicSampleMatrix
An owned 2d rectangular matrix that supports a T** representation and being aliased as a umatrix.
Definition: misc.h:361
ape::RealSourceResampler::produce
auto produce(std::size_t frames, Func &&f, double factor=1)
Produce a buffer with a inlined callback for each frame with the following signature: void(size_t fra...
Definition: resampling.h:104
ape::RealSourceResampler
A real-time variably resampled view on a buffer, that wraps around. Capable of producing a batched,...
Definition: resampling.h:17