Skip to content

Commit c91cf6a

Browse files
committed
stk examples
1 parent 8e04c04 commit c91cf6a

File tree

3 files changed

+74
-33
lines changed

3 files changed

+74
-33
lines changed

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,10 @@
1212
#include "AudioLibs/AudioKit.h"
1313
#include "AudioLibs/AudioSTK.h"
1414

15-
MemoryLoop mloop("sinewave.raw", sinewave_raw, sinewave_raw_len);
15+
MemoryLoop mloop("crashcym.raw", crashcym_raw, crashcym_raw_len);
1616
STKStream<MemoryLoop> in(mloop);
1717
AudioKitStream out;
1818
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-
}
4719

4820
void setup() {
4921
Serial.begin(115200);
@@ -62,6 +34,5 @@ void setup() {
6234
}
6335

6436
void loop() {
65-
play();
6637
copier.copy();
67-
}
38+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @file streams-sine-audiokit.ino
3+
* @brief We just use the sine generator from STK
4+
* @author Phil Schatzmann
5+
* @copyright Copyright (c) 2021
6+
*/
7+
8+
#include "SD_MMC.h"
9+
#include "AudioTools.h"
10+
#include "AudioLibs/AudioSTK.h"
11+
#include "AudioLibs/AudioKit.h"
12+
13+
SineWave sine;
14+
STKStream<SineWave> in(sine);
15+
AudioKitStream out;
16+
StreamCopy copier(out, in);
17+
MusicalNotes notes;
18+
19+
float note_amplitude = 0.5;
20+
static uint16_t notes_array[] = { // frequencies aleatoric C-scale
21+
N_C3, N_D3, N_E3, N_F3, N_G3, N_A3, N_B3,
22+
N_C4, N_D4, N_E4, N_F4, N_G4, N_A4, N_B4,
23+
N_C5, N_D5, N_E5, N_F5, N_G5, N_A5, N_B5
24+
};
25+
26+
void play() {
27+
static bool active=true;
28+
static float freq;
29+
static uint64_t timeout;
30+
31+
if (millis()>timeout){
32+
if (active){
33+
// play note for 800 ms
34+
freq = notes_array[random(sizeof(notes_array)/sizeof(uint16_t))];
35+
36+
Serial.print("playing ");
37+
Serial.println(freq);
38+
39+
sine.setFrequency(freq);
40+
timeout = millis()+800;
41+
active = false;
42+
} else {
43+
// silence for 100 ms
44+
sine.setFrequency(0);
45+
timeout = millis()+100;
46+
active = true;
47+
}
48+
}
49+
}
50+
51+
void setup() {
52+
Serial.begin(115200);
53+
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
54+
55+
// setup input
56+
auto icfg = in.defaultConfig();
57+
in.begin(icfg);
58+
59+
// setup output
60+
auto ocfg = out.defaultConfig(TX_MODE);
61+
ocfg.copyFrom(icfg);
62+
ocfg.sd_active = false;
63+
out.begin(ocfg);
64+
65+
}
66+
67+
void loop() {
68+
play();
69+
copier.copy();
70+
}

src/AudioLibs/AudioSTK.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class STKGenerator : public SoundGenerator<T> {
4949

5050
/// Starts the processing
5151
bool begin(AudioBaseInfo cfg){
52-
LOGI(LOG_METHOD);
52+
LOGI(LOG_METHOD);
5353
cfg.logInfo();
5454
SoundGenerator<T>::begin(cfg);
5555
max_value = NumberConverter::maxValue(sizeof(T)*8);
@@ -95,7 +95,7 @@ class STKStream : public GeneratedSoundStream<int16_t> {
9595

9696
AudioBaseInfo defaultConfig() {
9797
AudioBaseInfo info;
98-
info.channels = 2;
98+
info.channels = 1;
9999
info.bits_per_sample = 16;
100100
info.sample_rate = stk::Stk::sampleRate();
101101
return info;

0 commit comments

Comments
 (0)