arduino-audio-tools
Loading...
Searching...
No Matches
AudioSTK.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioEffects/AudioEffect.h"
4#include "AudioTools/CoreAudio/AudioStreams.h"
5#include "AudioToolsConfig.h"
6
7#ifdef ESP32
8#include "freertos/FreeRTOS.h"
9#endif
10#include "StkAll.h"
11
12namespace audio_tools {
13
34template <class StkCls=stk::Generator, class T=int16_t>
35class STKGenerator : public SoundGenerator<T> {
36 public:
37 STKGenerator() = default;
38
39 // Creates an STKGenerator for an instrument
40 STKGenerator(StkCls& instrument) : SoundGenerator<T>() {
41 this->p_instrument = &instrument;
42 }
43
44 void setInput(StkCls& instrument) { this->p_instrument = &instrument; }
45
48 AudioInfo info;
49 info.channels = 2;
50 info.bits_per_sample = sizeof(T) * 8;
51 info.sample_rate = stk::Stk::sampleRate();
52 return info;
53 }
54
56 bool begin(AudioInfo cfg) {
57 TRACEI();
58 cfg.logInfo();
60 max_value = NumberConverter::maxValue(sizeof(T) * 8);
61 stk::Stk::setSampleRate(SoundGenerator<T>::info.sample_rate);
62 return true;
63 }
64
67 T result = 0;
68 if (p_instrument != nullptr) {
69 result = p_instrument->tick() * max_value;
70 }
71 return result;
72 }
73
74 protected:
75 StkCls* p_instrument = nullptr;
76 T max_value;
77};
78
83template <class StkCls=stk::Instrmnt, class T=int16_t>
84class STKInstrument : public STKGenerator<StkCls, T> {
85 public:
86 STKInstrument() = default;
87
88 STKInstrument(StkCls& instrument) : STKGenerator<StkCls, T>(instrument) {}
89
91 void setFrequency(float frequency) override {
92 this->p_instrument->noteOn(frequency, amplitude);
93 }
94
95 void noteOn(float freq, float vol) { this->p_instrument->noteOn(freq, vol); }
96
97 void noteOff() { this->p_instrument->noteOff(); }
98
100 void setAmplitude(float amplitude) {
101 this->amplitude = amplitude;
102 if (this->amplitude > 1.0) this->amplitude = 1.0;
103 if (this->amplitude < 0.0) this->amplitude = 0.0;
104 }
105
106 protected:
107 float amplitude = 1.0;
108};
109
114template <class StkCls>
115class STKStream : public GeneratedSoundStream<int16_t> {
116 public:
118
119 STKStream(StkCls& instrument) {
120 generator.setInput(instrument);
122 }
123 void setInput(StkCls& instrument) {
124 generator.setInput(instrument);
126 }
127 void setInput(StkCls* instrument) {
128 generator.setInput(*instrument);
130 }
131
132 AudioInfo defaultConfig() {
133 AudioInfo info;
134 info.channels = 1;
135 info.bits_per_sample = 16;
136 info.sample_rate = stk::Stk::sampleRate();
137 return info;
138 }
139
140 protected:
142};
143
152class STKEffect : public AudioEffect {
153 public:
154 STKEffect(stk::Effect& stkEffect) { p_effect = &stkEffect; }
155
156 virtual effect_t process(effect_t in) {
157 // just convert between int16 and float
158 float value = static_cast<float>(in) / 32767.0;
159 return p_effect->tick(value) * 32767.0;
160 }
161
162 protected:
163 stk::Effect* p_effect = nullptr;
164};
165
172class STKChorus : public AudioEffect, public stk::Chorus {
173 public:
174 STKChorus(float baseDelay = 6000) : stk::Chorus(baseDelay) {}
175 STKChorus(const STKChorus& copy) = default;
176
177 AudioEffect* clone() override { return new STKChorus(*this); }
178
179 virtual effect_t process(effect_t in) {
180 // just convert between int16 and float
181 float value = static_cast<float>(in) / 32767.0;
182 return stk::Chorus::tick(value) * 32767.0;
183 }
184};
185
192class STKEcho : public AudioEffect, public stk::Echo {
193 public:
194 STKEcho(unsigned long maximumDelay = (unsigned long)Stk::sampleRate())
195 : stk::Echo(maximumDelay) {}
196 STKEcho(const STKEcho& copy) = default;
197
198 AudioEffect* clone() override { return new STKEcho(*this); }
199
200 virtual effect_t process(effect_t in) {
201 // just convert between int16 and float
202 float value = static_cast<float>(in) / 32767.0;
203 return stk::Echo::tick(value) * 32767.0;
204 }
205};
206
213class STKFreeVerb : public AudioEffect, public stk::FreeVerb {
214 public:
215 STKFreeVerb() = default;
216 STKFreeVerb(const STKFreeVerb& copy) = default;
217 AudioEffect* clone() override { return new STKFreeVerb(*this); }
218 virtual effect_t process(effect_t in) {
219 // just convert between int16 and float
220 float value = static_cast<float>(in) / 32767.0;
221 return stk::FreeVerb::tick(value) * 32767.0;
222 }
223};
224
231class STKChowningReverb : public AudioEffect, public stk::JCRev {
232 public:
233 STKChowningReverb() = default;
234 STKChowningReverb(const STKChowningReverb& copy) = default;
235 AudioEffect* clone() override { return new STKChowningReverb(*this); }
236
237 virtual effect_t process(effect_t in) {
238 // just convert between int16 and float
239 float value = static_cast<float>(in) / 32767.0;
240 return stk::JCRev::tick(value) * 32767.0;
241 }
242};
243
250class STKNReverb : public AudioEffect, public stk::NRev {
251 public:
252 STKNReverb(float t60 = 1.0) : NRev(t60) {}
253 STKNReverb(const STKNReverb& copy) = default;
254 AudioEffect* clone() override { return new STKNReverb(*this); }
255 virtual effect_t process(effect_t in) {
256 // just convert between int16 and float
257 float value = static_cast<float>(in) / 32767.0;
258 return stk::NRev::tick(value) * 32767.0;
259 }
260};
261
268class STKPerryReverb : public AudioEffect, public stk::PRCRev {
269 public:
270 STKPerryReverb(float t60 = 1.0) : PRCRev(t60) {}
271 STKPerryReverb(const STKPerryReverb& copy) = default;
272 AudioEffect* clone() override { return new STKPerryReverb(*this); }
273 virtual effect_t process(effect_t in) {
274 // just convert between int16 and float
275 float value = static_cast<float>(in) / 32767.0;
276 return stk::PRCRev::tick(value) * 32767.0;
277 }
278};
279
286class STKLentPitShift : public AudioEffect, public stk::LentPitShift {
287 public:
288 STKLentPitShift(float periodRatio = 1.0, int tMax = 512)
289 : stk::LentPitShift(periodRatio, tMax) {}
290 STKLentPitShift(const STKLentPitShift& copy) = default;
291
292 AudioEffect* clone() override { return new STKLentPitShift(*this); }
293 virtual effect_t process(effect_t in) {
294 // just convert between int16 and float
295 float value = static_cast<float>(in) / 32767.0;
296 return stk::LentPitShift::tick(value) * 32767.0;
297 }
298};
299
307class STKPitShift : public AudioEffect, public stk::PitShift {
308 public:
309 STKPitShift() = default;
310 STKPitShift(const STKPitShift& copy) = default;
311
312 AudioEffect* clone() override { return new STKPitShift(*this); }
313 virtual effect_t process(effect_t in) {
314 // just convert between int16 and float
315 float value = static_cast<float>(in) / 32767.0;
316 return stk::PitShift::tick(value) * 32767.0;
317 }
318};
319
320} // namespace audio_tools
Abstract Base class for Sound Effects.
Definition AudioEffect.h:23
Source for reading generated tones. Please note.
Definition AudioStreams.h:394
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition AudioTypes.h:301
Chorus Effect.
Definition AudioSTK.h:172
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:179
John Chowning's reverberator class.
Definition AudioSTK.h:231
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:237
Echo Effect.
Definition AudioSTK.h:192
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:200
Use any effect from the STK framework: e.g. Chorus, Echo, FreeVerb, JCRev, PitShift....
Definition AudioSTK.h:152
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:156
Jezar at Dreampoint's FreeVerb, implemented in STK.
Definition AudioSTK.h:213
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:218
The Synthesis ToolKit in C++ (STK) is a set of open source audio signal processing and algorithmic sy...
Definition AudioSTK.h:35
AudioInfo defaultConfig()
provides the default configuration
Definition AudioSTK.h:47
T readSample()
Provides a single sample.
Definition AudioSTK.h:66
bool begin(AudioInfo cfg)
Starts the processing.
Definition AudioSTK.h:56
STK Stream for Instrument.
Definition AudioSTK.h:84
void setFrequency(float frequency) override
sets the frequency
Definition AudioSTK.h:91
void setAmplitude(float amplitude)
Defines the amplitude (0.0 ... 1.0)
Definition AudioSTK.h:100
Pitch shifter effect class based on the Lent algorithm.
Definition AudioSTK.h:286
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:293
CCRMA's NRev reverberator class.
Definition AudioSTK.h:250
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:255
Perry's simple reverberator class.
Definition AudioSTK.h:268
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:273
Simple Pitch shifter effect class: This class implements a simple pitch shifter using a delay line.
Definition AudioSTK.h:307
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:313
STK Stream for Instrument or Voicer.
Definition AudioSTK.h:115
Base class to define the abstract interface for the sound generating classes.
Definition SoundGenerator.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61