arduino-audio-tools
AudioSTK.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "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 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 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  StkFloat tick (StkFloat input, unsigned int channel=0) override
192  {
193  return FreeVerb::tick(input, input, channel);
194  }
195  virtual effect_t process(effect_t in) {
196  // just convert between int16 and float
197  float value = static_cast<float>(in) / 32767.0;
198  return tick(value) * 32767.0;
199  }
200 };
201 
208 class STKChowningReverb : public AudioEffect, public stk::JCRev {
209 public:
210  STKChowningReverb() = default;
211  STKChowningReverb(const STKChowningReverb& copy) = default;
212  AudioEffect* clone() override{
213  return new STKChowningReverb(*this);
214  }
215 
216  virtual effect_t process(effect_t in) {
217  // just convert between int16 and float
218  float value = static_cast<float>(in) / 32767.0;
219  return tick(value) * 32767.0;
220  }
221 };
222 
229 class STKNReverb : public AudioEffect, public stk::NRev {
230 public:
231  STKNReverb(float t60 = 1.0) : NRev(t60) {}
232  STKNReverb(const STKNReverb& copy) = default;
233  AudioEffect* clone() override{
234  return new STKNReverb(*this);
235  }
236  virtual effect_t process(effect_t in) {
237  // just convert between int16 and float
238  float value = static_cast<float>(in) / 32767.0;
239  return tick(value) * 32767.0;
240  }
241 };
242 
249 class STKPerryReverb : public AudioEffect, public stk::PRCRev {
250 public:
251  STKPerryReverb(float t60 = 1.0) : PRCRev(t60) {}
252  STKPerryReverb(const STKPerryReverb& copy) = default;
253  AudioEffect* clone() override{
254  return new STKPerryReverb(*this);
255  }
256  virtual effect_t process(effect_t in) {
257  // just convert between int16 and float
258  float value = static_cast<float>(in) / 32767.0;
259  return tick(value) * 32767.0;
260  }
261 };
262 
269 class STKLentPitShift : public AudioEffect, public stk::LentPitShift {
270 public:
271  STKLentPitShift(float periodRatio = 1.0, int tMax = 512)
272  : stk::LentPitShift(periodRatio, tMax) {}
273  STKLentPitShift(const STKLentPitShift& copy) = default;
274 
275  AudioEffect* clone() override{
276  return new STKLentPitShift(*this);
277  }
278  virtual effect_t process(effect_t in) {
279  // just convert between int16 and float
280  float value = static_cast<float>(in) / 32767.0;
281  return tick(value) * 32767.0;
282  }
283 };
284 
292 class STKPitShift : public AudioEffect, public stk::PitShift {
293 public:
294  STKPitShift() = default;
295  STKPitShift(const STKPitShift& copy) = default;
296 
297  AudioEffect* clone() override{
298  return new STKPitShift(*this);
299  }
300  virtual effect_t process(effect_t in) {
301  // just convert between int16 and float
302  float value = static_cast<float>(in) / 32767.0;
303  return tick(value) * 32767.0;
304  }
305 };
306 
307 
308 }
309 
Abstract Base class for Sound Effects.
Definition: AudioEffect.h:20
Source for reading generated tones. Please note.
Definition: AudioStreams.h:446
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition: AudioTypes.h:330
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:208
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:216
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:195
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:269
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:278
CCRMA's NRev reverberator class.
Definition: AudioSTK.h:229
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:236
Perry's simple reverberator class.
Definition: AudioSTK.h:249
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:256
Simple Pitch shifter effect class: This class implements a simple pitch shifter using a delay line.
Definition: AudioSTK.h:292
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition: AudioSTK.h:300
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: AnalogAudio.h:10
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:50
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:57