arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSTK.h
1#pragma once
2
3#include "AudioConfig.h"
4#include "AudioTools/CoreAudio/AudioEffects/AudioEffect.h"
5#include "AudioTools/CoreAudio/AudioStreams.h"
6
7#ifdef ESP32
8# include "freertos/FreeRTOS.h"
9#endif
10#include "StkAll.h"
11
12namespace audio_tools {
13
28template <class StkCls, class T>
29class STKGenerator : public SoundGenerator<T> {
30 public:
31 STKGenerator() = default;
32
33 // Creates an STKGenerator for an instrument
35 this->p_instrument = &instrument;
36 }
37
38 void setInput(StkCls &instrument){
39 this->p_instrument = &instrument;
40 }
41
44 AudioInfo info;
45 info.channels = 2;
46 info.bits_per_sample = sizeof(T) * 8;
47 info.sample_rate = stk::Stk::sampleRate();
48 return info;
49 }
50
52 bool begin(AudioInfo cfg){
53 TRACEI();
54 cfg.logInfo();
56 max_value = NumberConverter::maxValue(sizeof(T)*8);
57 stk::Stk::setSampleRate(SoundGenerator<T>::info.sample_rate);
58 return true;
59 }
60
63 T result = 0;
64 if (p_instrument!=nullptr) {
65 result = p_instrument->tick()*max_value;
66 }
67 return result;
68 }
69
70 protected:
71 StkCls *p_instrument=nullptr;
72 T max_value;
73
74};
75
80template <class StkCls>
81class STKStream : public GeneratedSoundStream<int16_t> {
82 public:
83 STKStream() {
85 };
86
88 generator.setInput(instrument);
90 }
91 void setInput(StkCls &instrument){
92 generator.setInput(instrument);
94 }
95 void setInput(StkCls *instrument){
96 generator.setInput(*instrument);
98 }
99
100 AudioInfo defaultConfig() {
101 AudioInfo info;
102 info.channels = 1;
103 info.bits_per_sample = 16;
104 info.sample_rate = stk::Stk::sampleRate();
105 return info;
106 }
107
108 protected:
110
111};
112
121class STKEffect : public AudioEffect {
122public:
123 STKEffect(stk::Effect &stkEffect) { p_effect = &stkEffect; }
124
125 virtual effect_t process(effect_t in) {
126 // just convert between int16 and float
127 float value = static_cast<float>(in) / 32767.0;
128 return p_effect->tick(value) * 32767.0;
129 }
130
131protected:
132 stk::Effect *p_effect = nullptr;
133};
134
141class STKChorus : public AudioEffect, public stk::Chorus {
142public:
143 STKChorus(float baseDelay = 6000) : stk::Chorus(baseDelay) {}
144 STKChorus(const STKChorus& copy) = default;
145
146 AudioEffect* clone() override{
147 return new STKChorus(*this);
148 }
149
150 virtual effect_t process(effect_t in) {
151 // just convert between int16 and float
152 float value = static_cast<float>(in) / 32767.0;
153 return stk::Chorus::tick(value) * 32767.0;
154 }
155};
156
163class STKEcho : public AudioEffect, public stk::Echo {
164public:
165 STKEcho(unsigned long maximumDelay = (unsigned long)Stk::sampleRate())
166 : stk::Echo(maximumDelay) {}
167 STKEcho(const STKEcho& copy) = default;
168
169 AudioEffect* clone() override{
170 return new STKEcho(*this);
171 }
172
173 virtual effect_t process(effect_t in) {
174 // just convert between int16 and float
175 float value = static_cast<float>(in) / 32767.0;
176 return stk::Echo::tick(value) * 32767.0;
177 }
178};
179
186class STKFreeVerb : public AudioEffect, public stk::FreeVerb {
187public:
188 STKFreeVerb() = default;
189 STKFreeVerb(const STKFreeVerb& copy) = default;
190 AudioEffect* clone() override{
191 return new STKFreeVerb(*this);
192 }
193 virtual effect_t process(effect_t in) {
194 // just convert between int16 and float
195 float value = static_cast<float>(in) / 32767.0;
196 return stk::FreeVerb::tick(value) * 32767.0;
197 }
198};
199
206class STKChowningReverb : public AudioEffect, public stk::JCRev {
207public:
208 STKChowningReverb() = default;
209 STKChowningReverb(const STKChowningReverb& copy) = default;
210 AudioEffect* clone() override{
211 return new STKChowningReverb(*this);
212 }
213
214 virtual effect_t process(effect_t in) {
215 // just convert between int16 and float
216 float value = static_cast<float>(in) / 32767.0;
217 return stk::JCRev::tick(value) * 32767.0;
218 }
219};
220
227class STKNReverb : public AudioEffect, public stk::NRev {
228public:
229 STKNReverb(float t60 = 1.0) : NRev(t60) {}
230 STKNReverb(const STKNReverb& copy) = default;
231 AudioEffect* clone() override{
232 return new STKNReverb(*this);
233 }
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 {
248public:
249 STKPerryReverb(float t60 = 1.0) : PRCRev(t60) {}
250 STKPerryReverb(const STKPerryReverb& copy) = default;
251 AudioEffect* clone() override{
252 return new STKPerryReverb(*this);
253 }
254 virtual effect_t process(effect_t in) {
255 // just convert between int16 and float
256 float value = static_cast<float>(in) / 32767.0;
257 return stk::PRCRev::tick(value) * 32767.0;
258 }
259};
260
267class STKLentPitShift : public AudioEffect, public stk::LentPitShift {
268public:
269 STKLentPitShift(float periodRatio = 1.0, int tMax = 512)
270 : stk::LentPitShift(periodRatio, tMax) {}
271 STKLentPitShift(const STKLentPitShift& copy) = default;
272
273 AudioEffect* clone() override{
274 return new STKLentPitShift(*this);
275 }
276 virtual effect_t process(effect_t in) {
277 // just convert between int16 and float
278 float value = static_cast<float>(in) / 32767.0;
279 return stk::LentPitShift::tick(value) * 32767.0;
280 }
281};
282
290class STKPitShift : public AudioEffect, public stk::PitShift {
291public:
292 STKPitShift() = default;
293 STKPitShift(const STKPitShift& copy) = default;
294
295 AudioEffect* clone() override{
296 return new STKPitShift(*this);
297 }
298 virtual effect_t process(effect_t in) {
299 // just convert between int16 and float
300 float value = static_cast<float>(in) / 32767.0;
301 return stk::PitShift::tick(value) * 32767.0;
302 }
303};
304
305
306}
307
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:311
Chorus Effect.
Definition AudioSTK.h:141
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:150
John Chowning's reverberator class.
Definition AudioSTK.h:206
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:214
Echo Effect.
Definition AudioSTK.h:163
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:173
Use any effect from the STK framework: e.g. Chorus, Echo, FreeVerb, JCRev, PitShift....
Definition AudioSTK.h:121
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:125
Jezar at Dreampoint's FreeVerb, implemented in STK.
Definition AudioSTK.h:186
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:193
The Synthesis ToolKit in C++ (STK) is a set of open source audio signal processing and algorithmic sy...
Definition AudioSTK.h:29
AudioInfo defaultConfig()
provides the default configuration
Definition AudioSTK.h:43
T readSample()
Provides a single sample.
Definition AudioSTK.h:62
bool begin(AudioInfo cfg)
Starts the processing.
Definition AudioSTK.h:52
Pitch shifter effect class based on the Lent algorithm.
Definition AudioSTK.h:267
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:276
CCRMA's NRev reverberator class.
Definition AudioSTK.h:227
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:254
Simple Pitch shifter effect class: This class implements a simple pitch shifter using a delay line.
Definition AudioSTK.h:290
virtual effect_t process(effect_t in)
calculates the effect output from the input
Definition AudioSTK.h:298
STK Stream for Instrument or Voicer.
Definition AudioSTK.h:81
Base class to define the abstract interface for the sound generating classes.
Definition SoundGenerator.h:26
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioConfig.h:885
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