1
+ /* *
2
+ * @file streams-stk-desktop.ino
3
+ * @brief Build and run on desktop using PortAudio
4
+ * @author Phil Schatzmann
5
+ * @copyright Copyright (c) 2021
6
+ */
7
+
8
+ #include " Arduino.h"
9
+ #include " AudioTools.h"
10
+ #include " AudioLibs/AudioSTK.h"
11
+ #include " AudioLibs/PortAudioStream.h"
12
+
13
+ STKStream<Instrmnt> in;
14
+ PortAudioStream out;
15
+ StreamCopy copier;
16
+ MusicalNotes notes;
17
+ Instrmnt* p_instrument=nullptr ; // instrument depends on file system
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 (static_cast <long >(sizeof (notes_array)/sizeof (uint16_t )))];
35
+
36
+ Serial.print (" playing " );
37
+ Serial.println (freq);
38
+
39
+ p_instrument->noteOn (freq, note_amplitude);
40
+ timeout = millis ()+800 ;
41
+ active = false ;
42
+ } else {
43
+ // silence for 100 ms
44
+ p_instrument->noteOff (note_amplitude);
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
+ // We can allocate the incstument only after SD_M
56
+ p_instrument = new BeeThree ();
57
+ in.setInput (p_instrument);;
58
+
59
+ // setup input
60
+ auto icfg = in.defaultConfig ();
61
+ in.begin (icfg);
62
+
63
+ // setup output
64
+ auto ocfg = out.defaultConfig (TX_MODE);
65
+ ocfg.copyFrom (icfg);
66
+ out.begin (ocfg);
67
+
68
+ }
69
+
70
+ void loop () {
71
+ play ();
72
+ copier.copy ();
73
+ }
0 commit comments