Skip to content

Commit c0fad84

Browse files
committed
STKGenerator: support setFrequency()
1 parent e61a788 commit c0fad84

File tree

1 file changed

+114
-123
lines changed

1 file changed

+114
-123
lines changed
Lines changed: 114 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,88 @@
11
#pragma once
22

3-
#include "AudioToolsConfig.h"
43
#include "AudioTools/CoreAudio/AudioEffects/AudioEffect.h"
54
#include "AudioTools/CoreAudio/AudioStreams.h"
5+
#include "AudioToolsConfig.h"
66

77
#ifdef ESP32
8-
# include "freertos/FreeRTOS.h"
8+
#include "freertos/FreeRTOS.h"
99
#endif
1010
#include "StkAll.h"
1111

1212
namespace audio_tools {
1313

1414
/**
15-
* @brief The Synthesis ToolKit in C++ (STK) is a set of open source audio signal processing and algorithmic synthesis classes
16-
* written in the C++ programming language. You need to install https://github.com/pschatzmann/Arduino-STK
17-
*
15+
* @brief The Synthesis ToolKit in C++ (STK) is a set of open source audio
16+
* signal processing and algorithmic synthesis classes written in the C++
17+
* programming language. You need to install
18+
* https://github.com/pschatzmann/Arduino-STK
19+
*
1820
* You can find further informarmation in the original Readme of the STK Project
1921
*
20-
* Like many other sound libraries it originates from an University (Princeton) and can look back at a very long history:
21-
* it was created in 1995. In the 90s the computers had limited processor power and memory available.
22-
* In todays world we can get some cheap Microcontrollers, which provide almost the same capabilities.
22+
* Like many other sound libraries it originates from an University (Princeton)
23+
* and can look back at a very long history: it was created in 1995. In the 90s
24+
* the computers had limited processor power and memory available. In todays
25+
* world we can get some cheap Microcontrollers, which provide almost the same
26+
* capabilities.
2327
*
2428
* @ingroup generator
25-
* @tparam T
29+
* @tparam T
2630
*/
2731

2832
template <class StkCls, class T>
2933
class STKGenerator : public SoundGenerator<T> {
30-
public:
31-
STKGenerator() = default;
32-
33-
// Creates an STKGenerator for an instrument
34-
STKGenerator(StkCls &instrument) : SoundGenerator<T>() {
35-
this->p_instrument = &instrument;
36-
}
37-
38-
void setInput(StkCls &instrument){
39-
this->p_instrument = &instrument;
40-
}
41-
42-
/// provides the default configuration
43-
AudioInfo defaultConfig() {
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-
51-
/// Starts the processing
52-
bool begin(AudioInfo cfg){
53-
TRACEI();
54-
cfg.logInfo();
55-
SoundGenerator<T>::begin(cfg);
56-
max_value = NumberConverter::maxValue(sizeof(T)*8);
57-
stk::Stk::setSampleRate(SoundGenerator<T>::info.sample_rate);
58-
return true;
59-
}
60-
61-
/// Provides a single sample
62-
T readSample() {
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;
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+
44+
/// provides the default configuration
45+
AudioInfo defaultConfig() {
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+
}
7352

53+
/// Starts the processing
54+
bool begin(AudioInfo cfg) {
55+
TRACEI();
56+
cfg.logInfo();
57+
SoundGenerator<T>::begin(cfg);
58+
max_value = NumberConverter::maxValue(sizeof(T) * 8);
59+
stk::Stk::setSampleRate(SoundGenerator<T>::info.sample_rate);
60+
return true;
61+
}
62+
63+
/// Provides a single sample
64+
T readSample() {
65+
T result = 0;
66+
if (p_instrument != nullptr) {
67+
result = p_instrument->tick() * max_value;
68+
}
69+
return result;
70+
}
71+
/// sets the frequency
72+
void setFrequency(float frequency) override {
73+
p_instrument->noteOn(frequency, amplitude);
74+
}
75+
76+
void setAmplitude(float amplitude) {
77+
this->amplitude = amplitude;
78+
if (this->amplitude > 1.0) this->amplitude = 1.0;
79+
if (this->amplitude < 0.0) this->amplitude = 0.0;
80+
}
81+
82+
protected:
83+
StkCls* p_instrument = nullptr;
84+
T max_value;
85+
float amplitude = 0.9;
7486
};
7587

7688
/**
@@ -79,57 +91,54 @@ class STKGenerator : public SoundGenerator<T> {
7991
*/
8092
template <class StkCls>
8193
class STKStream : public GeneratedSoundStream<int16_t> {
82-
public:
83-
STKStream() {
84-
GeneratedSoundStream<int16_t>::setInput(generator);
85-
};
86-
87-
STKStream(StkCls &instrument){
88-
generator.setInput(instrument);
89-
GeneratedSoundStream<int16_t>::setInput(generator);
90-
}
91-
void setInput(StkCls &instrument){
92-
generator.setInput(instrument);
93-
GeneratedSoundStream<int16_t>::setInput(generator);
94-
}
95-
void setInput(StkCls *instrument){
96-
generator.setInput(*instrument);
97-
GeneratedSoundStream<int16_t>::setInput(generator);
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:
109-
STKGenerator<StkCls,int16_t> generator;
94+
public:
95+
STKStream() { GeneratedSoundStream<int16_t>::setInput(generator); };
11096

97+
STKStream(StkCls& instrument) {
98+
generator.setInput(instrument);
99+
GeneratedSoundStream<int16_t>::setInput(generator);
100+
}
101+
void setInput(StkCls& instrument) {
102+
generator.setInput(instrument);
103+
GeneratedSoundStream<int16_t>::setInput(generator);
104+
}
105+
void setInput(StkCls* instrument) {
106+
generator.setInput(*instrument);
107+
GeneratedSoundStream<int16_t>::setInput(generator);
108+
}
109+
110+
AudioInfo defaultConfig() {
111+
AudioInfo info;
112+
info.channels = 1;
113+
info.bits_per_sample = 16;
114+
info.sample_rate = stk::Stk::sampleRate();
115+
return info;
116+
}
117+
118+
protected:
119+
STKGenerator<StkCls, int16_t> generator;
111120
};
112121

113122
/**
114-
* @brief Use any effect from the STK framework: e.g. Chorus, Echo, FreeVerb, JCRev,
115-
* PitShift... https://github.com/pschatzmann/Arduino-STK
123+
* @brief Use any effect from the STK framework: e.g. Chorus, Echo, FreeVerb,
124+
* JCRev, PitShift... https://github.com/pschatzmann/Arduino-STK
116125
*
117126
* @ingroup effects
118127
* @author Phil Schatzmann
119128
* @copyright GPLv3
120129
*/
121130
class STKEffect : public AudioEffect {
122-
public:
123-
STKEffect(stk::Effect &stkEffect) { p_effect = &stkEffect; }
131+
public:
132+
STKEffect(stk::Effect& stkEffect) { p_effect = &stkEffect; }
124133

125134
virtual effect_t process(effect_t in) {
126135
// just convert between int16 and float
127136
float value = static_cast<float>(in) / 32767.0;
128137
return p_effect->tick(value) * 32767.0;
129138
}
130139

131-
protected:
132-
stk::Effect *p_effect = nullptr;
140+
protected:
141+
stk::Effect* p_effect = nullptr;
133142
};
134143

135144
/**
@@ -139,13 +148,11 @@ class STKEffect : public AudioEffect {
139148
* @copyright GPLv3
140149
*/
141150
class STKChorus : public AudioEffect, public stk::Chorus {
142-
public:
151+
public:
143152
STKChorus(float baseDelay = 6000) : stk::Chorus(baseDelay) {}
144153
STKChorus(const STKChorus& copy) = default;
145154

146-
AudioEffect* clone() override{
147-
return new STKChorus(*this);
148-
}
155+
AudioEffect* clone() override { return new STKChorus(*this); }
149156

150157
virtual effect_t process(effect_t in) {
151158
// just convert between int16 and float
@@ -161,14 +168,12 @@ class STKChorus : public AudioEffect, public stk::Chorus {
161168
* @copyright GPLv3
162169
*/
163170
class STKEcho : public AudioEffect, public stk::Echo {
164-
public:
171+
public:
165172
STKEcho(unsigned long maximumDelay = (unsigned long)Stk::sampleRate())
166173
: stk::Echo(maximumDelay) {}
167174
STKEcho(const STKEcho& copy) = default;
168175

169-
AudioEffect* clone() override{
170-
return new STKEcho(*this);
171-
}
176+
AudioEffect* clone() override { return new STKEcho(*this); }
172177

173178
virtual effect_t process(effect_t in) {
174179
// just convert between int16 and float
@@ -184,12 +189,10 @@ class STKEcho : public AudioEffect, public stk::Echo {
184189
* @copyright GPLv3
185190
*/
186191
class STKFreeVerb : public AudioEffect, public stk::FreeVerb {
187-
public:
192+
public:
188193
STKFreeVerb() = default;
189194
STKFreeVerb(const STKFreeVerb& copy) = default;
190-
AudioEffect* clone() override{
191-
return new STKFreeVerb(*this);
192-
}
195+
AudioEffect* clone() override { return new STKFreeVerb(*this); }
193196
virtual effect_t process(effect_t in) {
194197
// just convert between int16 and float
195198
float value = static_cast<float>(in) / 32767.0;
@@ -204,12 +207,10 @@ class STKFreeVerb : public AudioEffect, public stk::FreeVerb {
204207
* @copyright GPLv3
205208
*/
206209
class STKChowningReverb : public AudioEffect, public stk::JCRev {
207-
public:
210+
public:
208211
STKChowningReverb() = default;
209212
STKChowningReverb(const STKChowningReverb& copy) = default;
210-
AudioEffect* clone() override{
211-
return new STKChowningReverb(*this);
212-
}
213+
AudioEffect* clone() override { return new STKChowningReverb(*this); }
213214

214215
virtual effect_t process(effect_t in) {
215216
// just convert between int16 and float
@@ -225,12 +226,10 @@ class STKChowningReverb : public AudioEffect, public stk::JCRev {
225226
* @copyright GPLv3
226227
*/
227228
class STKNReverb : public AudioEffect, public stk::NRev {
228-
public:
229+
public:
229230
STKNReverb(float t60 = 1.0) : NRev(t60) {}
230231
STKNReverb(const STKNReverb& copy) = default;
231-
AudioEffect* clone() override{
232-
return new STKNReverb(*this);
233-
}
232+
AudioEffect* clone() override { return new STKNReverb(*this); }
234233
virtual effect_t process(effect_t in) {
235234
// just convert between int16 and float
236235
float value = static_cast<float>(in) / 32767.0;
@@ -245,12 +244,10 @@ class STKNReverb : public AudioEffect, public stk::NRev {
245244
* @copyright GPLv3
246245
*/
247246
class STKPerryReverb : public AudioEffect, public stk::PRCRev {
248-
public:
247+
public:
249248
STKPerryReverb(float t60 = 1.0) : PRCRev(t60) {}
250249
STKPerryReverb(const STKPerryReverb& copy) = default;
251-
AudioEffect* clone() override{
252-
return new STKPerryReverb(*this);
253-
}
250+
AudioEffect* clone() override { return new STKPerryReverb(*this); }
254251
virtual effect_t process(effect_t in) {
255252
// just convert between int16 and float
256253
float value = static_cast<float>(in) / 32767.0;
@@ -265,14 +262,12 @@ class STKPerryReverb : public AudioEffect, public stk::PRCRev {
265262
* @copyright GPLv3
266263
*/
267264
class STKLentPitShift : public AudioEffect, public stk::LentPitShift {
268-
public:
265+
public:
269266
STKLentPitShift(float periodRatio = 1.0, int tMax = 512)
270267
: stk::LentPitShift(periodRatio, tMax) {}
271268
STKLentPitShift(const STKLentPitShift& copy) = default;
272269

273-
AudioEffect* clone() override{
274-
return new STKLentPitShift(*this);
275-
}
270+
AudioEffect* clone() override { return new STKLentPitShift(*this); }
276271
virtual effect_t process(effect_t in) {
277272
// just convert between int16 and float
278273
float value = static_cast<float>(in) / 32767.0;
@@ -281,27 +276,23 @@ class STKLentPitShift : public AudioEffect, public stk::LentPitShift {
281276
};
282277

283278
/**
284-
* @brief Simple Pitch shifter effect class: This class implements a simple pitch
285-
* shifter using a delay line.
279+
* @brief Simple Pitch shifter effect class: This class implements a simple
280+
* pitch shifter using a delay line.
286281
* @ingroup effects
287282
* @author Phil Schatzmann
288283
* @copyright GPLv3
289284
*/
290285
class STKPitShift : public AudioEffect, public stk::PitShift {
291-
public:
286+
public:
292287
STKPitShift() = default;
293288
STKPitShift(const STKPitShift& copy) = default;
294289

295-
AudioEffect* clone() override{
296-
return new STKPitShift(*this);
297-
}
290+
AudioEffect* clone() override { return new STKPitShift(*this); }
298291
virtual effect_t process(effect_t in) {
299292
// just convert between int16 and float
300293
float value = static_cast<float>(in) / 32767.0;
301294
return stk::PitShift::tick(value) * 32767.0;
302295
}
303296
};
304297

305-
306-
}
307-
298+
} // namespace audio_tools

0 commit comments

Comments
 (0)