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
32template <class StkCls, class T>
33class STKGenerator : public SoundGenerator<T> {
34 public:
35 STKGenerator() = default;
36
37 // Creates an STKGenerator for an instrument
38 STKGenerator(StkCls& instrument) : SoundGenerator<T>() {
39 this->p_instrument = &instrument;
40 }
41
42 void setInput(StkCls& instrument) { this->p_instrument = &instrument; }
43
46 AudioInfo info;
47 info.channels = 2;
48 info.bits_per_sample = sizeof(T) * 8;
49 info.sample_rate = stk::Stk::sampleRate();
50 return info;
51 }
52
54 bool begin(AudioInfo cfg) {
55 TRACEI();
56 cfg.logInfo();
58 max_value = NumberConverter::maxValue(sizeof(T) * 8);
59 stk::Stk::setSampleRate(SoundGenerator<T>::info.sample_rate);
60 return true;
61 }
62
65 T result = 0;
66 if (p_instrument != nullptr) {
67 result = p_instrument->tick() * max_value;
68 }
69 return result;
70 }
72 void setFrequency(float frequency) override {
73 p_instrument->noteOn(frequency, amplitude);
74 }
75
77 void setAmplitude(float amplitude) {
78 this->amplitude = amplitude;
79 if (this->amplitude > 1.0) this->amplitude = 1.0;
80 if (this->amplitude < 0.0) this->amplitude = 0.0;
81 }
82
83 protected:
84 StkCls* p_instrument = nullptr;
85 T max_value;
86 float amplitude = 0.9;
87};
88
93template <class StkCls>
94class STKStream : public GeneratedSoundStream<int16_t> {
95 public:
97
98 STKStream(StkCls& instrument) {
99 generator.setInput(instrument);
101 }
102 void setInput(StkCls& instrument) {
103 generator.setInput(instrument);
105 }
106 void setInput(StkCls* instrument) {
107 generator.setInput(*instrument);
109 }
110
111 AudioInfo defaultConfig() {
112 AudioInfo info;
113 info.channels = 1;
114 info.bits_per_sample = 16;
115 info.sample_rate = stk::Stk::sampleRate();
116 return info;
117 }
118
119 protected:
121};
122
131class STKEffect : public AudioEffect {
132 public:
133 STKEffect(stk::Effect& stkEffect) { p_effect = &stkEffect; }
134
135 virtual effect_t process(effect_t in) {
136 // just convert between int16 and float
137 float value = static_cast<float>(in) / 32767.0;
138 return p_effect->tick(value) * 32767.0;
139 }
140
141 protected:
142 stk::Effect* p_effect = nullptr;
143};
144
151class STKChorus : public AudioEffect, public stk::Chorus {
152 public:
153 STKChorus(float baseDelay = 6000) : stk::Chorus(baseDelay) {}
154 STKChorus(const STKChorus& copy) = default;
155
156 AudioEffect* clone() override { return new STKChorus(*this); }
157
158 virtual effect_t process(effect_t in) {
159 // just convert between int16 and float
160 float value = static_cast<float>(in) / 32767.0;
161 return stk::Chorus::tick(value) * 32767.0;
162 }
163};
164
171class STKEcho : public AudioEffect, public stk::Echo {
172 public:
173 STKEcho(unsigned long maximumDelay = (unsigned long)Stk::sampleRate())
174 : stk::Echo(maximumDelay) {}
175 STKEcho(const STKEcho& copy) = default;
176
177 AudioEffect* clone() override { return new STKEcho(*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::Echo::tick(value) * 32767.0;
183 }
184};
185
192class STKFreeVerb : public AudioEffect, public stk::FreeVerb {
193 public:
194 STKFreeVerb() = default;
195 STKFreeVerb(const STKFreeVerb& copy) = default;
196 AudioEffect* clone() override { return new STKFreeVerb(*this); }
197 virtual effect_t process(effect_t in) {
198 // just convert between int16 and float
199 float value = static_cast<float>(in) / 32767.0;
200 return stk::FreeVerb::tick(value) * 32767.0;
201 }
202};
203
210class STKChowningReverb : public AudioEffect, public stk::JCRev {
211 public:
212 STKChowningReverb() = default;
213 STKChowningReverb(const STKChowningReverb& copy) = default;
214 AudioEffect* clone() override { return new STKChowningReverb(*this); }
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 stk::JCRev::tick(value) * 32767.0;
220 }
221};
222
229class 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 { return new STKNReverb(*this); }
234 virtual effect_t process(effect_t in) {
235 // just convert between int16 and float
236 float value = static_cast<float>(in) / 32767.0;
237 return stk::NRev::tick(value) * 32767.0;
238 }
239};
240
247class STKPerryReverb : public AudioEffect, public stk::PRCRev {
248 public:
249 STKPerryReverb(float t60 = 1.0) : PRCRev(t60) {}
250 STKPerryReverb(const STKPerryReverb& copy) = default;
251 AudioEffect* clone() override { return new STKPerryReverb(*this); }
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
265class 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 { return new STKLentPitShift(*this); }
272 virtual effect_t process(effect_t in) {
273 // just convert between int16 and float
274 float value = static_cast<float>(in) / 32767.0;
275 return stk::LentPitShift::tick(value) * 32767.0;
276 }
277};
278
286class STKPitShift : public AudioEffect, public stk::PitShift {
287 public:
288 STKPitShift() = default;
289 STKPitShift(const STKPitShift& copy) = default;
290
291 AudioEffect* clone() override { return new STKPitShift(*this); }
292 virtual effect_t process(effect_t in) {
293 // just convert between int16 and float
294 float value = static_cast<float>(in) / 32767.0;
295 return stk::PitShift::tick(value) * 32767.0;
296 }
297};
298
299} // 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:151
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:158
John Chowning's reverberator class.
Definition AudioSTK.h:210
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:216
Echo Effect.
Definition AudioSTK.h:171
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:179
Use any effect from the STK framework: e.g. Chorus, Echo, FreeVerb, JCRev, PitShift....
Definition AudioSTK.h:131
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:135
Jezar at Dreampoint's FreeVerb, implemented in STK.
Definition AudioSTK.h:192
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:197
The Synthesis ToolKit in C++ (STK) is a set of open source audio signal processing and algorithmic sy...
Definition AudioSTK.h:33
void setFrequency(float frequency) override
sets the frequency
Definition AudioSTK.h:72
AudioInfo defaultConfig()
provides the default configuration
Definition AudioSTK.h:45
void setAmplitude(float amplitude)
Defines the amplitude (0.0 ... 1.0)
Definition AudioSTK.h:77
T readSample()
Provides a single sample.
Definition AudioSTK.h:64
bool begin(AudioInfo cfg)
Starts the processing.
Definition AudioSTK.h:54
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:272
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:234
Perry's simple reverberator class.
Definition AudioSTK.h:247
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:286
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:292
STK Stream for Instrument or Voicer.
Definition AudioSTK.h:94
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