Skip to content

Commit f81da69

Browse files
committed
CopyAll
1 parent 90f96d3 commit f81da69

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Implementing a Synthesizer using the STK Framwork
2+
3+
We can use the STK framework to implement a Synthesizer which receives Midi Messages and translates them
4+
into sound. Here we use a Clarinet...
5+
6+
For [further info see my blog](https://www.pschatzmann.ch/home/2021/12/21/ai-thinker-audiokit-a-simply-synthesizer-with-stk/)
7+
8+
### Dependencies
9+
10+
- https://github.com/pschatzmann/arduino-audio-tools
11+
- https://github.com/pschatzmann/arduino-audiokit
12+
- https://github.com/pschatzmann/arduino-midi
13+
- https://github.com/pschatzmann/Arduino-STK
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @file streams-synthstk-audiokit.ino
3+
* @brief see https://www.pschatzmann.ch/home/2021/12/17/ai-thinker-audiokit-a-simple-synthesizer-with-stk/
4+
* @author Phil Schatzmann
5+
* @copyright Copyright (c) 2021
6+
*/
7+
#include "AudioTools.h"
8+
#include "AudioLibs/AudioKit.h"
9+
#include "StkAll.h"
10+
11+
AudioKitStream kit;
12+
Clarinet clarinet(440);
13+
Voicer voicer;
14+
ArdStreamOut output(&kit);
15+
float noteAmplitude = 128;
16+
int group = 0;
17+
18+
void actionKeyOn(bool active, int pin, void* ptr){
19+
int note = *((int*)ptr);
20+
voicer.noteOn(note, noteAmplitude, group);
21+
}
22+
23+
void actionKeyOff(bool active, int pin, void* ptr){
24+
int note = *((int*)ptr);
25+
voicer.noteOff(note, noteAmplitude, group);
26+
}
27+
28+
// We want to play some notes on the AudioKit keys
29+
void setupActions(){
30+
// assign buttons to notes
31+
auto act_low = AudioActions::ActiveLow;
32+
static int note[] = {48,50,52,53,55,57}; // midi keys
33+
kit.audioActions().add(PIN_KEY1, actionKeyOn, actionKeyOff, act_low, &(note[0])); // C3
34+
kit.audioActions().add(PIN_KEY2, actionKeyOn, actionKeyOff, act_low, &(note[1])); // D3
35+
kit.audioActions().add(PIN_KEY3, actionKeyOn, actionKeyOff, act_low, &(note[2])); // E3
36+
kit.audioActions().add(PIN_KEY4, actionKeyOn, actionKeyOff, act_low, &(note[3])); // F3
37+
kit.audioActions().add(PIN_KEY5, actionKeyOn, actionKeyOff, act_low, &(note[4])); // G3
38+
kit.audioActions().add(PIN_KEY6, actionKeyOn, actionKeyOff, act_low, &(note[5])); // A3
39+
}
40+
41+
void setup() {
42+
Serial.begin(9600);
43+
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
44+
45+
voicer.addInstrument(&clarinet, group);
46+
47+
// define data format
48+
auto cfg = kit.defaultConfig(TX_MODE);
49+
cfg.channels = 1;
50+
cfg.bits_per_sample = 16;
51+
cfg.sample_rate = Stk::sampleRate();
52+
cfg.sd_active = false;
53+
kit.begin(cfg);
54+
55+
// play notes with keys
56+
setupActions();
57+
58+
}
59+
60+
void loop() {
61+
for (int j=0;j<1024;j++) {
62+
output.tick( voicer.tick() );
63+
}
64+
kit.processActions();
65+
}

src/AudioTools/AudioCopy.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,28 @@ class StreamCopyT {
181181
}
182182

183183
/// copies all data - returns true if we copied anything
184-
bool copyAll(int delayWithDataMs=5, int delayNoDataMs=1000){
184+
bool copyAll(int retryCount=2, int retryWaitMs=2000){
185185
LOGD(LOG_METHOD);
186186
bool result = false;
187187
if (from==nullptr || to == nullptr)
188188
return result;
189189

190+
int retry = 0;
190191
// copy whily source has data available
191-
size_t available = 1024;
192-
while(available){
193-
if (copy()) {
194-
result = true;
195-
delay(delayWithDataMs);
192+
while (true){
193+
int count = copy();
194+
if (count==0){
195+
// wait for more data
196+
retry++;
197+
delay(retryWaitMs);
196198
} else {
197-
delay(delayNoDataMs);
199+
result = true; // we have some data
200+
retry = 0;
201+
}
202+
// stop the processing
203+
if (retry>retryCount){
204+
break;
198205
}
199-
available = from->available();
200206
}
201207
return result;
202208
}

0 commit comments

Comments
 (0)