Skip to content

Commit c71fb14

Browse files
committed
Some STK examples
1 parent 0d7213c commit c71fb14

File tree

6 files changed

+84
-18
lines changed

6 files changed

+84
-18
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @file streams-stk-audioout.ino
3+
* @brief Plays random notes on instrument. For available instruments
4+
* see https://pschatzmann.github.io/Arduino-STK/html/classstk_1_1Instrmnt.html
5+
* I used an AudioKitStream to test the output, but you can replace it with any other output class (e.g. I2SStream)
6+
* @author Phil Schatzmann
7+
* @copyright Copyright (c) 2021
8+
*/
9+
#include "AudioTools.h"
10+
#include "AudioLibs/AudioSTK.h"
11+
#include "AudioLibs/AudioKit.h"
12+
13+
Sitar instrument(440);
14+
STKStream in(instrument);
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+
instrument.noteOn(freq, note_amplitude);
36+
timeout = millis()+800;
37+
active = false;
38+
} else {
39+
// silence for 100 ms
40+
instrument.noteOff(note_amplitude);
41+
timeout = millis()+100;
42+
active = true;
43+
}
44+
}
45+
}
46+
47+
void setup() {
48+
Serial.begin(115200);
49+
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
50+
51+
// setup input
52+
auto icfg = in.defaultConfig();
53+
in.begin(icfg);
54+
55+
// setup output
56+
auto ocfg = out.defaultConfig(TX_MODE);
57+
ocfg.copyFrom(icfg);
58+
ocfg.sd_active = false;
59+
out.begin(ocfg);
60+
61+
}
62+
63+
void loop() {
64+
play();
65+
copier.copy();
66+
}

examples/sandbox/streams-stk-analog/streams-stk-analog.ino renamed to examples/examples-stk/streams-stk_generator-audiokit/streams-stk_generator-audiokit.ino

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@
66
* @copyright GPLv3
77
*/
88

9-
// Add this in your sketch or change the setting in AudioConfig.h
10-
#define USE_STK
11-
129
#include "AudioTools.h"
13-
#include "Clarinet.h"
14-
15-
10+
#include "AudioLibs/AudioKit.h"
11+
#include "AudioLibs/AudioSTK.h"
1612

17-
stk::Clarinet clarinet(440); // the stk clarinet instrument
13+
Clarinet clarinet(440); // the stk clarinet instrument
1814
STKGenerator<int16_t> generator(clarinet); // subclass of SoundGenerator
1915
GeneratedSoundStream<int16_t> in(generator); // Stream generated from sine wave
20-
AnalogAudioStream out; // Output stream
16+
AudioKitStream out;
2117
StreamCopy copier(out, in); // copy stkStream to a2dpStream
2218
MusicalNotes notes; // notes with frequencies
2319

2420
// Arduino Setup
2521
void setup(void) {
2622
Serial.begin(115200);
27-
AudioLogger::instance().begin(Serial, AudioLogger::Info);
23+
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
2824

2925
// start Output
3026
Serial.println("starting Analog Output...");

examples/examples-stk/streams-synthstk-audiokit/README.md renamed to examples/examples-stk/streams-stk_synth-audiokit/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We can use the STK framework to implement a Synthesizer which receives Midi Messages and translates them
44
into sound. Here we use a Clarinet...
55

6+
We demo how to use the Stream output classes provided by STK.
7+
68
For [further info see my blog](https://www.pschatzmann.ch/home/2021/12/21/ai-thinker-audiokit-a-simply-synthesizer-with-stk/)
79

810
### Dependencies

examples/examples-stk/streams-synthstk-audiokit/streams-synthstk-audiokit.ino renamed to examples/examples-stk/streams-stk_synth-audiokit/streams-stk_synth-audiokit.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void setupActions(){
3939
}
4040

4141
void setup() {
42-
Serial.begin(9600);
42+
Serial.begin(115200);
4343
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
4444

4545
voicer.addInstrument(&clarinet, group);

examples/examples-stream/streams-a2dp-serial/streams-a2dp-serial.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "AudioLibs/AudioA2DP.h"
1313

1414

15-
1615
A2DPStream in = A2DPStream::instance() ; // A2DP input - A2DPStream is a singleton!
1716
CsvStream<int16_t> out(Serial, 2); // ASCII stream as csv
1817
StreamCopy copier(out, in); // copy in to out

src/AudioLibs/AudioSTK.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,25 @@ class STKGenerator : public SoundGenerator<T> {
8484

8585
/**
8686
* @brief STK Stream for Instrument or Voicer
87-
*
88-
* @tparam T
8987
*/
90-
template <class T>
91-
class STKStream : public GeneratedSoundStream<T> {
88+
class STKStream : public GeneratedSoundStream<int16_t> {
9289
public:
9390
STKStream(Instrmnt &instrument){
9491
generator.setInput(instrument);
95-
GeneratedSoundStream<T>::setInput(generator);
92+
GeneratedSoundStream<int16_t>::setInput(generator);
9693
}
9794
STKStream(Voicer &voicer){
9895
generator.setInput(voicer);
99-
GeneratedSoundStream<T>::setInput(generator);
96+
GeneratedSoundStream<int16_t>::setInput(generator);
97+
}
98+
void setInput(Instrmnt &instrument){
99+
generator.setInput(instrument);
100+
}
101+
void setInput(Voicer &voicer){
102+
generator.setInput(voicer);
100103
}
101104
protected:
102-
STKGenerator<T> generator;
105+
STKGenerator<int16_t> generator;
103106

104107
};
105108

0 commit comments

Comments
 (0)