Skip to content

Commit 0ae8e67

Browse files
committed
STK add template parameter
1 parent c71fb14 commit 0ae8e67

File tree

5 files changed

+77
-91
lines changed

5 files changed

+77
-91
lines changed

examples/examples-stk/streams-stk-audiokit/streams-stk-audiokit.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "AudioLibs/AudioKit.h"
1212

1313
Sitar instrument(440);
14-
STKStream in(instrument);
14+
STKStream<Instrmnt> in(instrument);
1515
AudioKitStream out;
1616
StreamCopy copier(out, in);
1717
MusicalNotes notes;

examples/examples-stk/streams-stk_generator-audiokit/streams-stk_generator-audiokit.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "AudioLibs/AudioSTK.h"
1212

1313
Clarinet clarinet(440); // the stk clarinet instrument
14-
STKGenerator<int16_t> generator(clarinet); // subclass of SoundGenerator
14+
STKGenerator<Instrmnt, int16_t> generator(clarinet); // subclass of SoundGenerator
1515
GeneratedSoundStream<int16_t> in(generator); // Stream generated from sine wave
1616
AudioKitStream out;
1717
StreamCopy copier(out, in); // copy stkStream to a2dpStream
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @file streams-stk_loop-audiokit.ino
3+
* @author Phil Schatzmann
4+
* @brief We can use a MemoryLoop as input
5+
* @version 0.1
6+
* @date 2022-09-09
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
*/
11+
#include "AudioTools.h"
12+
#include "AudioLibs/AudioKit.h"
13+
#include "AudioLibs/AudioSTK.h"
14+
15+
MemoryLoop mloop("sinewave.raw", sinewave_raw, sinewave_raw_len);
16+
STKStream<MemoryLoop> in(mloop);
17+
AudioKitStream out;
18+
StreamCopy copier(out, in);
19+
MusicalNotes notes;
20+
21+
static uint16_t notes_array[] = { // frequencies aleatoric C-scale
22+
N_C3, N_D3, N_E3, N_F3, N_G3, N_A3, N_B3,
23+
N_C4, N_D4, N_E4, N_F4, N_G4, N_A4, N_B4,
24+
N_C5, N_D5, N_E5, N_F5, N_G5, N_A5, N_B5
25+
};
26+
27+
void play() {
28+
static bool active=true;
29+
static float freq;
30+
static uint64_t timeout;
31+
32+
if (millis()>timeout){
33+
if (active){
34+
// play note for 800 ms
35+
freq = notes_array[random(sizeof(notes_array)/sizeof(uint16_t))];
36+
mloop.setFrequency(freq);
37+
timeout = millis()+800;
38+
active = false;
39+
} else {
40+
// silence for 100 ms
41+
mloop.setFrequency(0);
42+
timeout = millis()+100;
43+
active = true;
44+
}
45+
}
46+
}
47+
48+
void setup() {
49+
Serial.begin(115200);
50+
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
51+
52+
// setup input
53+
auto icfg = in.defaultConfig();
54+
in.begin(icfg);
55+
56+
// setup output
57+
auto ocfg = out.defaultConfig(TX_MODE);
58+
ocfg.copyFrom(icfg);
59+
ocfg.sd_active = false;
60+
out.begin(ocfg);
61+
62+
}
63+
64+
void loop() {
65+
play();
66+
copier.copy();
67+
}

examples/sandbox/streams-stk-a2dp/streams-STK-a2dp.ino

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/AudioLibs/AudioSTK.h

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,20 @@ namespace audio_tools {
2222
* @tparam T
2323
*/
2424

25-
template <class T>
25+
template <class StkCls, class T>
2626
class STKGenerator : public SoundGenerator<T> {
2727
public:
2828
STKGenerator() = default;
2929

3030
// Creates an STKGenerator for an instrument
31-
STKGenerator(stk::Instrmnt &instrument) : SoundGenerator<T>() {
31+
STKGenerator(StkCls &instrument) : SoundGenerator<T>() {
3232
this->p_instrument = &instrument;
3333
}
3434

35-
// Creates an STKGenerator for a voicer (combination of multiple instruments)
36-
STKGenerator(stk::Voicer &voicer) : SoundGenerator<T>() {
37-
this->p_voicer = &voicer;
38-
}
39-
40-
void setInput(stk::Instrmnt &instrument){
35+
void setInput(StkCls &instrument){
4136
this->p_instrument = &instrument;
4237
}
4338

44-
void setInput(stk::Voicer &voicer){
45-
this->p_voicer = &voicer;
46-
}
47-
4839
/// provides the default configuration
4940
AudioBaseInfo defaultConfig() {
5041
AudioBaseInfo info;
@@ -69,40 +60,31 @@ class STKGenerator : public SoundGenerator<T> {
6960
T result = 0;
7061
if (p_instrument!=nullptr) {
7162
result = p_instrument->tick()*max_value;
72-
} else if (p_voicer!=nullptr){
73-
result = p_voicer->tick()*max_value;
7463
}
7564
return result;
7665
}
7766

7867
protected:
79-
stk::Instrmnt *p_instrument=nullptr;
80-
stk::Voicer *p_voicer=nullptr;
68+
StkCls *p_instrument=nullptr;
8169
T max_value;
8270

8371
};
8472

8573
/**
8674
* @brief STK Stream for Instrument or Voicer
8775
*/
76+
template <class StkCls>
8877
class STKStream : public GeneratedSoundStream<int16_t> {
8978
public:
90-
STKStream(Instrmnt &instrument){
79+
STKStream(StkCls &instrument){
9180
generator.setInput(instrument);
9281
GeneratedSoundStream<int16_t>::setInput(generator);
9382
}
94-
STKStream(Voicer &voicer){
95-
generator.setInput(voicer);
96-
GeneratedSoundStream<int16_t>::setInput(generator);
97-
}
98-
void setInput(Instrmnt &instrument){
83+
void setInput(StkCls &instrument){
9984
generator.setInput(instrument);
10085
}
101-
void setInput(Voicer &voicer){
102-
generator.setInput(voicer);
103-
}
10486
protected:
105-
STKGenerator<int16_t> generator;
87+
STKGenerator<StkCls,int16_t> generator;
10688

10789
};
10890

0 commit comments

Comments
 (0)