Skip to content

Commit 4d0216b

Browse files
committed
tbd
1 parent 070dc1c commit 4d0216b

File tree

13 files changed

+226
-16
lines changed

13 files changed

+226
-16
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
# set the project name
4+
project(arduino_sketch)
5+
set (SKETCH "streams-stk_allinstruments-audiokit.ino")
6+
set (CMAKE_CXX_STANDARD 11)
7+
set (DCMAKE_CXX_FLAGS "-Werror")
8+
9+
include(FetchContent)
10+
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
11+
12+
FetchContent_Declare(arduino-stk GIT_REPOSITORY "https://github.com/pschatzmann/arduino-stk.git" )
13+
FetchContent_GetProperties(arduino-stk)
14+
if(NOT arduino-stk_POPULATED)
15+
FetchContent_Populate(arduino-stk)
16+
add_subdirectory(${arduino-stk_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/arduino-stk)
17+
endif()
18+
19+
# Build with arduino-audio-tools
20+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
21+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../.. ${CMAKE_CURRENT_BINARY_DIR}/arduino-audio-tools )
22+
endif()
23+
24+
# build sketch as executable
25+
set_source_files_properties(${SKETCH} PROPERTIES LANGUAGE CXX)
26+
add_executable (arduino_sketch ${SKETCH})
27+
28+
# set preprocessor defines
29+
target_compile_definitions(arduino_emulator PUBLIC -DDEFINE_MAIN)
30+
target_compile_definitions(arduino_sketch PUBLIC -DARDUINO -DIS_DESKTOP -DEXIT_ON_STOP)
31+
target_compile_definitions(arduino-stk PUBLIC -DIS_DESKTOP)
32+
33+
# OS/X might need this setting for core audio
34+
target_compile_options(portaudio PRIVATE -Wno-deprecated)
35+
36+
# specify libraries
37+
target_link_libraries(arduino_sketch arduino-stk portaudio arduino_emulator arduino-audio-tools )
38+
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* @file streams-stk_allinstruments-audioout.ino
3+
* @brief Test sketch which pays all notes of all instruments
4+
* @author Phil Schatzmann
5+
* @copyright Copyright (c) 2021
6+
*/
7+
#include "AudioTools.h"
8+
#include "AudioLibs/AudioSTK.h"
9+
10+
#ifdef IS_DESKTOP
11+
#include "AudioLibs/PortAudioStream.h"
12+
typedef PortAudioStream MyStdOut;
13+
#else
14+
#include "AudioLibs/AudioKit.h"
15+
typedef AudioKitStream MyStdOut;
16+
17+
#endif
18+
Instrmnt *p_Instrmnt=nullptr; // will be allocated dynamically
19+
STKStream<Instrmnt> in;
20+
MyStdOut out; // or replace with I2SStream or any other output class
21+
StreamCopy copier(out, in);
22+
MusicalNotes notes;
23+
24+
/// We do not have enough memory to allocate all instruments, so we provide an array with allocator methods
25+
struct InstrumentInfo {
26+
const char* name;
27+
std::function<Instrmnt * ()> instrument;
28+
};
29+
30+
InstrumentInfo instrumentArray[] = {
31+
{"BeeThree", []() { return new BeeThree(); }},
32+
{"BlowHole", []() { return new BlowHole(20); }},
33+
{"Bowed", []() { return new Bowed(); }},
34+
{"Clarinet", []() { return new Clarinet(); }},
35+
// {"Drummer", []() { return new Drummer(); }}, // comment out for STM32 or ESP8266
36+
{"Flute", []() { return new Flute(20); }},
37+
{"Rhodey", []() { return new Rhodey(); }},
38+
{"TubeBell", []() { return new TubeBell(); }},
39+
{"Mandolin", []() { return new Mandolin(20); }},
40+
{"ModalBar", []() { return new ModalBar(); }},
41+
{"Moog", []() { return new Moog(); }},
42+
{"Plucked", []() { return new Plucked(); }},
43+
{"Saxofony", []() { return new Saxofony(20); }},
44+
{"Shakers", []() { return new Shakers(); }},
45+
{"Sitar", []() { return new Sitar(); }},
46+
{"StifKarp", []() { return new StifKarp(); }},
47+
{"TubeBell", []() { return new TubeBell(); }},
48+
{"Wurley", []() { return new Wurley(); }},
49+
{"BlowBotl", []() { return new BlowBotl(); }},
50+
{"Brass", []() { return new Brass(); }},
51+
{"FMVoices", []() { return new FMVoices(); }},
52+
{"PercFlut", []() { return new PercFlut(); }},
53+
{"HevyMetl", []() { return new HevyMetl(); }},
54+
{"Recorder", []() { return new Recorder(); }},
55+
{"Resonate", []() { return new Resonate(); }},
56+
{"Simple", []() { return new Simple(); }},
57+
{"Whistle", []() { return new Whistle(); }},
58+
{nullptr, nullptr}
59+
};
60+
bool active = true;
61+
uint64_t timeout = 0;
62+
int instrumentIdx = 0;
63+
int noteMin = 0;
64+
int noteMax = 127;
65+
int noteStep = 12; // 1 octave
66+
int noteIndex = noteMin;
67+
int onLengthMs = 400;
68+
int offLengthMs = 50;
69+
float amplitude = 1.0;
70+
71+
void selectInstrument() {
72+
if (p_Instrmnt==nullptr){
73+
Serial.println(instrumentArray[instrumentIdx].name);
74+
p_Instrmnt = instrumentArray[instrumentIdx].instrument();
75+
in.setInput(p_Instrmnt);
76+
in.begin();
77+
}
78+
}
79+
80+
void noteOn() {
81+
// play note for 800 ms
82+
if (p_Instrmnt!=nullptr){
83+
float freq = notes.stkNoteToFrequency(noteIndex);
84+
p_Instrmnt->noteOn(freq, amplitude);
85+
}
86+
timeout = millis()+onLengthMs;
87+
active = false;
88+
}
89+
90+
void noteOff() {
91+
// note off - silence for 100 ms
92+
if (p_Instrmnt!=nullptr){
93+
p_Instrmnt->noteOff(amplitude);
94+
}
95+
timeout = millis()+offLengthMs;
96+
97+
// select next note
98+
noteIndex += noteStep;
99+
// switch instrument after we played all notes
100+
if (noteIndex>=noteMax){
101+
noteIndex = noteMin;
102+
instrumentIdx++;
103+
if (instrumentArray[instrumentIdx].name==nullptr){
104+
instrumentIdx = 0;
105+
}
106+
// delete old instrument
107+
if (p_Instrmnt!=nullptr){
108+
delete p_Instrmnt;
109+
p_Instrmnt = nullptr;
110+
selectInstrument();
111+
}
112+
}
113+
active = true;
114+
}
115+
116+
void play() {
117+
118+
if (millis()>timeout){
119+
if (active){
120+
Serial.print("- playing ");
121+
Serial.println(noteIndex);
122+
noteOn();
123+
} else {
124+
noteOff();
125+
}
126+
}
127+
}
128+
129+
void setup() {
130+
Serial.begin(115200);
131+
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
132+
StkLogLevel = StkWarning;
133+
134+
// setup input
135+
auto icfg = in.defaultConfig();
136+
in.begin(icfg);
137+
138+
// setup output
139+
auto ocfg = out.defaultConfig(TX_MODE);
140+
ocfg.copyFrom(icfg);
141+
// ocfg.sd_active = false;
142+
out.begin(ocfg);
143+
144+
// select first instument
145+
selectInstrument();
146+
147+
}
148+
149+
void loop() {
150+
play();
151+
copier.copy();
152+
}

examples/examples-vs1053/streams-generator-vs1053/streams-generator-vs1053.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ void setup(){
4040
//cfg.dcs_pin = VS1053_DCS;
4141
//cfg.dreq_pin = VS1053_DREQ;
4242
//cfg.reset_pin = VS1053_RESET;
43-
out.begin(cfg);
43+
if (!out.begin(cfg)){
44+
Serial.println("SPI Error");
45+
stop();
46+
}
4447

4548
}
4649

examples/examples-vs1053/streams-midi_memory-vs1053/streams-midi_memory-vs1053.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ void setup(){
2424

2525
auto config = out.defaultConfig(TX_MODE);
2626
config.is_encoded_data = true; // vs1053 is accepting encoded midi data
27+
//config.cs_pin = VS1053_CS;
28+
//config.dcs_pin = VS1053_DCS;
29+
//config.dreq_pin = VS1053_DREQ;
30+
//config.reset_pin = VS1053_RESET;
2731
out.begin(config);
2832
}
2933

src/AudioHttp/AbstractURLStream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class AbstractURLStream : public AudioStream {
1919
virtual bool setMetadataCallback(void (*fn)(MetaDataType info, const char* str, int len)) {
2020
return false;
2121
}
22+
/// Writes are not supported
23+
int availableForWrite() {
24+
return 0;
25+
}
2226

2327
};
2428

src/AudioI2S/I2SConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class I2SConfig : public AudioBaseInfo {
9292
#endif
9393

9494
void logInfo() {
95-
LOGI("rx/tx mode: %s", rx_tx_mode == TX_MODE ? "TX":"RX");
95+
LOGI("rx/tx mode: %s", RxTxModeNames[rx_tx_mode]);
9696
LOGI("port_no: %d", port_no);
9797
LOGI("is_master: %s", is_master ? "Master":"Slave");
9898
LOGI("sample rate: %d", sample_rate);

src/AudioI2S/I2SESP32.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ class I2SBase {
286286
return (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_I2S;
287287
case I2S_LEFT_JUSTIFIED_FORMAT:
288288
case I2S_MSB_FORMAT:
289-
return (i2s_comm_format_t) I2S_COMM_FORMAT_I2S_MSB;
289+
return (i2s_comm_format_t) (I2S_COMM_FORMAT_I2S|I2S_COMM_FORMAT_I2S_MSB);
290290
case I2S_RIGHT_JUSTIFIED_FORMAT:
291291
case I2S_LSB_FORMAT:
292-
return (i2s_comm_format_t) I2S_COMM_FORMAT_I2S_LSB;
292+
return (i2s_comm_format_t) (I2S_COMM_FORMAT_I2S|I2S_COMM_FORMAT_I2S_LSB);
293293
// this is strange but the docu specifies that
294294
// case I2S_PCM_LONG:
295295
// return (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_PCM_LONG;

src/AudioLibs/AudioSTK.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ class STKGenerator : public SoundGenerator<T> {
7878
template <class StkCls>
7979
class STKStream : public GeneratedSoundStream<int16_t> {
8080
public:
81-
STKStream() = default;
81+
STKStream() {
82+
GeneratedSoundStream<int16_t>::setInput(generator);
83+
};
8284

8385
STKStream(StkCls &instrument){
8486
generator.setInput(instrument);

src/AudioLibs/VS1053Stream.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ class VS1053Stream : public AudioStreamX {
327327
bool beginTx() {
328328
LOGI(LOG_METHOD);
329329
p_out->begin(cfg);
330-
p_vs1053->begin();
330+
bool result = p_vs1053->begin();
331331
p_vs1053->startSong();
332332
p_vs1053->switchToMp3Mode(); // optional, some boards require this
333333
if (p_vs1053->getChipVersion() == 4) { // Only perform an update if we really are using a VS1053, not. eg. VS1003
334334
p_vs1053->loadDefaultVs1053Patches();
335335
}
336336
delay(500);
337337
setVolume(VS1053_DEFAULT_VOLUME);
338-
return true;
338+
return result;
339339
}
340340

341341
#if VS1053_EXT
@@ -346,17 +346,16 @@ class VS1053Stream : public AudioStreamX {
346346
rec.setSampleRate(cfg.sample_rate);
347347
rec.setChannels(cfg.channels);
348348
rec.setInput(cfg.input_device);
349-
p_vs1053->beginInput(rec);
350-
return true;
349+
return p_vs1053->beginInput(rec);
351350
}
352351

353352
bool beginMidi(){
354353
LOGI(LOG_METHOD);
355354
p_out->begin(cfg);
356-
p_vs1053->beginMidi();
355+
bool result = p_vs1053->beginMidi();
357356
delay(500);
358357
setVolume(VS1053_DEFAULT_VOLUME);
359-
return true;
358+
return result;
360359
}
361360

362361
#endif

src/AudioTools/AudioCopy.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ class StreamCopyT {
9494

9595
// // If we try to write to a server we might not have any output destination yet
9696
int to_write = to->availableForWrite();
97-
// if (to_write<=0){
98-
// delay(500);
99-
// return 0;
100-
// }
97+
if (to_write==0){
98+
delay(500);
99+
return 0;
100+
}
101101

102102

103103
size_t result = 0;

0 commit comments

Comments
 (0)