arduino-audio-tools
Loading...
Searching...
No Matches
AudioSTK.h
Go to the documentation of this file.
1#pragma once
2
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
43
45
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();
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;
77};
78
83template <class StkCls=stk::Instrmnt, class T=int16_t>
84class STKInstrument : public STKGenerator<StkCls, T> {
85 public:
86 STKInstrument() = default;
87
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
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>
143
152class STKEffect : public AudioEffect {
153 public:
154 STKEffect(stk::Effect& stkEffect) { p_effect = &stkEffect; }
155
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
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
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); }
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
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); }
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); }
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); }
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); }
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
#define TRACEI()
Definition AudioLoggerIDF.h:32
Abstract Base class for Sound Effects.
Definition AudioEffect.h:23
AudioInfo info
Definition BaseStream.h:173
Source for reading generated tones. Please note.
Definition AudioStreams.h:397
void setInput(SoundGenerator< T > &generator)
Definition AudioStreams.h:406
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
STKChorus(const STKChorus &copy)=default
AudioEffect * clone() override
Definition AudioSTK.h:177
STKChorus(float baseDelay=6000)
Definition AudioSTK.h:174
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
STKChowningReverb(const STKChowningReverb &copy)=default
AudioEffect * clone() override
Definition AudioSTK.h:235
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:237
Echo Effect.
Definition AudioSTK.h:192
STKEcho(const STKEcho &copy)=default
STKEcho(unsigned long maximumDelay=(unsigned long) Stk::sampleRate())
Definition AudioSTK.h:194
AudioEffect * clone() override
Definition AudioSTK.h:198
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
stk::Effect * p_effect
Definition AudioSTK.h:163
STKEffect(stk::Effect &stkEffect)
Definition AudioSTK.h:154
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
STKFreeVerb(const STKFreeVerb &copy)=default
AudioEffect * clone() override
Definition AudioSTK.h:217
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
void setInput(StkCls &instrument)
Definition AudioSTK.h:44
StkCls * p_instrument
Definition AudioSTK.h:75
T max_value
Definition AudioSTK.h:76
AudioInfo defaultConfig()
provides the default configuration
Definition AudioSTK.h:47
STKGenerator(StkCls &instrument)
Definition AudioSTK.h:40
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
float amplitude
Definition AudioSTK.h:107
void noteOff()
Definition AudioSTK.h:97
STKInstrument(StkCls &instrument)
Definition AudioSTK.h:88
void noteOn(float freq, float vol)
Definition AudioSTK.h:95
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
STKLentPitShift(float periodRatio=1.0, int tMax=512)
Definition AudioSTK.h:288
STKLentPitShift(const STKLentPitShift &copy)=default
AudioEffect * clone() override
Definition AudioSTK.h:292
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
STKNReverb(const STKNReverb &copy)=default
STKNReverb(float t60=1.0)
Definition AudioSTK.h:252
AudioEffect * clone() override
Definition AudioSTK.h:254
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
STKPerryReverb(const STKPerryReverb &copy)=default
STKPerryReverb(float t60=1.0)
Definition AudioSTK.h:270
AudioEffect * clone() override
Definition AudioSTK.h:272
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
STKPitShift(const STKPitShift &copy)=default
AudioEffect * clone() override
Definition AudioSTK.h:312
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
void setInput(StkCls &instrument)
Definition AudioSTK.h:123
STKStream(StkCls &instrument)
Definition AudioSTK.h:119
void setInput(StkCls *instrument)
Definition AudioSTK.h:127
AudioInfo defaultConfig()
Definition AudioSTK.h:132
STKStream()
Definition AudioSTK.h:117
STKGenerator< StkCls, int16_t > generator
Definition AudioSTK.h:141
Base class to define the abstract interface for the sound generating classes.
Definition SoundGenerator.h:28
AudioInfo info
Definition SoundGenerator.h:114
virtual bool begin()
Definition SoundGenerator.h:39
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
int16_t effect_t
Definition AudioEffect.h:14
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
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
virtual void logInfo(const char *source="")
Definition AudioTypes.h:125