Skip to content

Commit a9c6305

Browse files
committed
stk file errors
1 parent d4ad1c5 commit a9c6305

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @file streams-stk_files-audiokit.ino
3+
* @brief Example which Loads raw samples from the SDMMC file system
4+
* 1. Make sure that files are stored on SD in the /rawwaves directory
5+
* 2. In ArdConfig.h make sure that we used #define STK_USE_FILES
6+
* 3. On AudiKit all switches must be switched on
7+
* @author Phil Schatzmann
8+
* @copyright Copyright (c) 2021
9+
*/
10+
11+
#include "SD_MMC.h"
12+
#include "AudioTools.h"
13+
#include "AudioLibs/AudioSTK.h"
14+
#include "AudioLibs/AudioKit.h"
15+
16+
STKStream<Instrmnt> in;
17+
AudioKitStream out;
18+
StreamCopy copier;
19+
MusicalNotes notes;
20+
Instrmnt* p_instrument=nullptr; // instrument depends on file system
21+
22+
float note_amplitude = 0.5;
23+
static uint16_t notes_array[] = { // frequencies aleatoric C-scale
24+
N_C3, N_D3, N_E3, N_F3, N_G3, N_A3, N_B3,
25+
N_C4, N_D4, N_E4, N_F4, N_G4, N_A4, N_B4,
26+
N_C5, N_D5, N_E5, N_F5, N_G5, N_A5, N_B5
27+
};
28+
29+
void play() {
30+
static bool active=true;
31+
static float freq;
32+
static uint64_t timeout;
33+
34+
if (millis()>timeout){
35+
if (active){
36+
// play note for 800 ms
37+
freq = notes_array[random(sizeof(notes_array)/sizeof(uint16_t))];
38+
p_instrument->noteOn(freq, note_amplitude);
39+
timeout = millis()+800;
40+
active = false;
41+
} else {
42+
// silence for 100 ms
43+
p_instrument->noteOff(note_amplitude);
44+
timeout = millis()+100;
45+
active = true;
46+
}
47+
}
48+
}
49+
50+
void setup() {
51+
Serial.begin(115200);
52+
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
53+
54+
if(!SD_MMC.begin("/sdcard", true)){ // 1-bit mode
55+
LOGE("Could not open SD_MMC");
56+
return;
57+
}
58+
59+
// We can allocate the incstument only after SD_M
60+
p_instrument = new BeeThree();
61+
in.setInput(p_instrument);;
62+
63+
// setup input
64+
auto icfg = in.defaultConfig();
65+
in.begin(icfg);
66+
67+
// setup output
68+
auto ocfg = out.defaultConfig(TX_MODE);
69+
ocfg.copyFrom(icfg);
70+
ocfg.sd_active = false;
71+
out.begin(ocfg);
72+
73+
}
74+
75+
void loop() {
76+
play();
77+
copier.copy();
78+
}

src/AudioLibs/AudioSTK.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class STKGenerator : public SoundGenerator<T> {
3939
/// provides the default configuration
4040
AudioBaseInfo defaultConfig() {
4141
AudioBaseInfo info;
42-
info.channels = 1;
42+
info.channels = 2;
4343
info.bits_per_sample = sizeof(T) * 8;
4444
info.sample_rate = stk::Stk::sampleRate();
4545
return info;
@@ -76,13 +76,29 @@ class STKGenerator : public SoundGenerator<T> {
7676
template <class StkCls>
7777
class STKStream : public GeneratedSoundStream<int16_t> {
7878
public:
79+
STKStream() {
80+
GeneratedSoundStream<int16_t>::setInput(generator);
81+
};
82+
7983
STKStream(StkCls &instrument){
8084
generator.setInput(instrument);
8185
GeneratedSoundStream<int16_t>::setInput(generator);
8286
}
8387
void setInput(StkCls &instrument){
8488
generator.setInput(instrument);
8589
}
90+
void setInput(StkCls *instrument){
91+
generator.setInput(*instrument);
92+
}
93+
94+
AudioBaseInfo defaultConfig() {
95+
AudioBaseInfo info;
96+
info.channels = 2;
97+
info.bits_per_sample = 16;
98+
info.sample_rate = stk::Stk::sampleRate();
99+
return info;
100+
}
101+
86102
protected:
87103
STKGenerator<StkCls,int16_t> generator;
88104

src/AudioTools/AudioStreams.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ class GeneratedSoundStream : public AudioStreamX {
542542
/// start the processing
543543
bool begin(AudioBaseInfo cfg) {
544544
LOGD(LOG_METHOD);
545+
if (generator_ptr==nullptr){
546+
LOGE("Source not defined");
547+
return false;
548+
}
545549
generator_ptr->begin(cfg);
546550
if (audioBaseInfoDependent != nullptr)
547551
audioBaseInfoDependent->setAudioInfo(generator_ptr->audioInfo());

0 commit comments

Comments
 (0)