arduino-audio-tools
AudioSTK.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioEffects/AudioEffect.h"
5 #ifdef ESP32
6 # include "freertos/FreeRTOS.h"
7 #endif
8 #include "StkAll.h"
9 
10 namespace audio_tools {
11 
26 template <class StkCls, class T>
27 class STKGenerator : public SoundGenerator<T> {
28  public:
29  STKGenerator() = default;
30 
31  // Creates an STKGenerator for an instrument
32  STKGenerator(StkCls &instrument) : SoundGenerator<T>() {
33  this->p_instrument = &instrument;
34  }
35 
36  void setInput(StkCls &instrument){
37  this->p_instrument = &instrument;
38  }
39 
42  AudioInfo info;
43  info.channels = 2;
44  info.bits_per_sample = sizeof(T) * 8;
45  info.sample_rate = stk::Stk::sampleRate();
46  return info;
47  }
48 
50  bool begin(AudioInfo cfg){
51  TRACEI();
52  cfg.logInfo();
54  max_value = NumberConverter::maxValue(sizeof(T)*8);
55  stk::Stk::setSampleRate(SoundGenerator<T>::info.sample_rate);
56  return true;
57  }
58 
60  T readSample() {
61  T result = 0;
62  if (p_instrument!=nullptr) {
63  result = p_instrument->tick()*max_value;
64  }
65  return result;
66  }
67 
68  protected:
69  StkCls *p_instrument=nullptr;
70  T max_value;
71 
72 };
73 
78 template <class StkCls>
79 class STKStream : public GeneratedSoundStream<int16_t> {
80  public:
81  STKStream() {
83  };
84 
85  STKStream(StkCls &instrument){
86  generator.setInput(instrument);
88  }
89  void setInput(StkCls &instrument){
90  generator.setInput(instrument);
92  }
93  void setInput(StkCls *instrument){
94  generator.setInput(*instrument);
96  }
97 
98  AudioInfo defaultConfig() {
99  AudioInfo info;
100  info.channels = 1;
101  info.bits_per_sample = 16;
102  info.sample_rate = stk::Stk::sampleRate();
103  return info;
104  }
105 
106  protected:
108 
109 };
110 
119 class STKEffect : public AudioEffect {
120 public:
121  STKEffect(stk::Effect &stkEffect) { p_effect = &stkEffect; }
122 
123  virtual effect_t process(effect_t in) {
124  // just convert between int16 and float
125  float value = static_cast<float>(in) / 32767.0;
126  return p_effect->tick(value) * 32767.0;
127  }
128 
129 protected:
130  stk::Effect *p_effect = nullptr;
131 };
132 
139 class STKChorus : public AudioEffect, public stk::Chorus {
140 public:
141  STKChorus(float baseDelay = 6000) : stk::Chorus(baseDelay) {}
142  STKChorus(const STKChorus& copy) = default;
143 
144  AudioEffect* clone() override{
145  return new STKChorus(*this);
146  }
147 
148  virtual effect_t process(effect_t in) {
149  // just convert between int16 and float
150  float value = static_cast<float>(in) / 32767.0;
151  return stk::Chorus::tick(value) * 32767.0;
152  }
153 };
154 
161 class STKEcho : public AudioEffect, public stk::Echo {
162 public:
163  STKEcho(unsigned long maximumDelay = (unsigned long)Stk::sampleRate())
164  : stk::Echo(maximumDelay) {}
165  STKEcho(const STKEcho& copy) = default;
166 
167  AudioEffect* clone() override{
168  return new STKEcho(*this);
169  }
170 
171  virtual effect_t process(effect_t in) {
172  // just convert between int16 and float
173  float value = static_cast<float>(in) / 32767.0;
174  return stk::Echo::tick(value) * 32767.0;
175  }
176 };
177 
184 class STKFreeVerb : public AudioEffect, public stk::FreeVerb {
185 public:
186  STKFreeVerb() = default;
187  STKFreeVerb(const STKFreeVerb& copy) = default;
188  AudioEffect* clone() override{
189  return new STKFreeVerb(*this);
190  }
191  virtual effect_t process(effect_t in) {
192  // just convert between int16 and float
193  float value = static_cast<float>(in) / 32767.0;
194  return stk::FreeVerb::tick(value) * 32767.0;
195  }
196 };
197 
204 class STKChowningReverb : public AudioEffect, public stk::JCRev {
205 public:
206  STKChowningReverb() = default;
207  STKChowningReverb(const STKChowningReverb& copy) = default;
208  AudioEffect* clone() override{
209  return new STKChowningReverb(*this);
210  }
211 
212  virtual effect_t process(effect_t in) {
213  // just convert between int16 and float
214  float value = static_cast<float>(in) / 32767.0;
215  return stk::JCRev::tick(value) * 32767.0;
216  }
217 };
218 
225 class STKNReverb : public AudioEffect, public stk::NRev {
226 public:
227  STKNReverb(float t60 = 1.0) : NRev(t60) {}
228  STKNReverb(const STKNReverb& copy) = default;
229  AudioEffect* clone() override{
230  return new STKNReverb(*this);
231  }
232  virtual effect_t process(effect_t in) {
233  // just convert between int16 and float
234  float value = static_cast<float>(in) / 32767.0;
235  return stk::NRev::tick(value) * 32767.0;
236  }
237 };
238 
245 class STKPerryReverb : public AudioEffect, public stk::PRCRev {
246 public:
247  STKPerryReverb(float t60 = 1.0) : PRCRev(t60) {}
248  STKPerryReverb(const STKPerryReverb& copy) = default;
249  AudioEffect* clone() override{
250  return new STKPerryReverb(*this);
251  }
252  virtual effect_t process(effect_t in) {
253  // just convert between int16 and float
254  float value = static_cast<float>(in) / 32767.0;
255  return stk::PRCRev::tick(value) * 32767.0;
256  }
257 };
258 
265 class STKLentPitShift : public AudioEffect, public stk::LentPitShift {
266 public:
267  STKLentPitShift(float periodRatio = 1.0, int tMax = 512)
268  : stk::LentPitShift(periodRatio, tMax) {}
269  STKLentPitShift(const STKLentPitShift& copy) = default;
270 
271  AudioEffect* clone() override{
272  return new STKLentPitShift(*this);
273  }
274  virtual effect_t process(effect_t in) {
275  // just convert between int16 and float
276  float value = static_cast<float>(in) / 32767.0;
277  return stk::LentPitShift::tick(value) * 32767.0;
278  }
279 };
280 
288 class STKPitShift : public AudioEffect, public stk::PitShift {
289 public:
290  STKPitShift() = default;
291  STKPitShift(const STKPitShift& copy) = default;
292 
293  AudioEffect* clone() override{
294  return new STKPitShift(*this);
295  }
296  virtual effect_t process(effect_t in) {
297  // just convert between int16 and float
298  float value = static_cast<float>(in) / 32767.0;
299  return stk::PitShift::tick(value) * 32767.0;
300  }
301 };
302 
303 
304 }
305 
Abstract Base class for Sound Effects.
Definition: AudioEffect.h:21
Source for reading generated tones. Please note.
Definition: AudioStreams.h:390
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition: AudioTypes.h:317
Chorus Effect.
Definition: AudioSTK.h:139
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:148
John Chowning's reverberator class.
Definition: AudioSTK.h:204
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:212
Echo Effect.
Definition: AudioSTK.h:161
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:171
Use any effect from the STK framework: e.g. Chorus, Echo, FreeVerb, JCRev, PitShift....
Definition: AudioSTK.h:119
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:123
Jezar at Dreampoint's FreeVerb, implemented in STK.
Definition: AudioSTK.h:184
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:191
The Synthesis ToolKit in C++ (STK) is a set of open source audio signal processing and algorithmic sy...
Definition: AudioSTK.h:27
AudioInfo defaultConfig()
provides the default configuration
Definition: AudioSTK.h:41
T readSample()
Provides a single sample.
Definition: AudioSTK.h:60
bool begin(AudioInfo cfg)
Starts the processing.
Definition: AudioSTK.h:50
Pitch shifter effect class based on the Lent algorithm.
Definition: AudioSTK.h:265
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:274
CCRMA's NRev reverberator class.
Definition: AudioSTK.h:225
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:232
Perry's simple reverberator class.
Definition: AudioSTK.h:245
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:252
Simple Pitch shifter effect class: This class implements a simple pitch shifter using a delay line.
Definition: AudioSTK.h:288
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:296
STK Stream for Instrument or Voicer.
Definition: AudioSTK.h:79
Base class to define the abstract interface for the sound generating classes.
Definition: SoundGenerator.h:25
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:59