This commit is contained in:
2026-02-12 21:00:02 -08:00
parent 77f8236347
commit 8bdbf227ca
1141 changed files with 1010880 additions and 2 deletions

View File

@@ -0,0 +1,34 @@
#pragma once
#include "StkAll.h"
/**
* @brief Demo how you can compose your own instrument
* @author Phil Schatzmann
*/
class MyFirstInstrument : public Instrmnt {
public:
MyFirstInstrument() {
adsr.setAllTimes( 0.005, 0.01, 0.8, 0.010 );
echo.setDelay(1024);
}
//! Start a note with the given frequency and amplitude.
void noteOn( StkFloat frequency, StkFloat amplitude ) {
wave.setFrequency(frequency);
adsr.keyOn();
}
//! Stop a note with the given amplitude (speed of decay).
void noteOff( StkFloat amplitude ){
adsr.keyOff();
}
float tick( unsigned int channel = 0) override {
return echo.tick(wave.tick()) * adsr.tick();
};
protected:
stk::SineWave wave;
stk::ADSR adsr;
stk::Echo echo {1024};
};

View File

@@ -0,0 +1,65 @@
/**
* @file streams-stk-audioout.ino
* @brief Plays random notes on self designed instrument: see https://github.com/pschatzmann/Arduino-STK/wiki/Building-your-own-Instrument
* @author Phil Schatzmann
* @copyright Copyright (c) 2021
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioSTK.h" // install https://github.com/pschatzmann/Arduino-STK
#include "AudioTools/AudioLibs/AudioBoardStream.h" // install https://github.com/pschatzmann/arduino-audio-driver
#include "MyFirstInstrument.h"
MyFirstInstrument instrument;
STKStream<Instrmnt> in(instrument);
AudioBoardStream out(AudioKitEs8388V1);
StreamCopy copier(out, in);
MusicalNotes notes;
float note_amplitude = 0.5;
static float notes_array[] = { // frequencies aleatoric C-scale
N_C3, N_D3, N_E3, N_F3, N_G3, N_A3, N_B3,
N_C4, N_D4, N_E4, N_F4, N_G4, N_A4, N_B4,
N_C5, N_D5, N_E5, N_F5, N_G5, N_A5, N_B5
};
void play() {
static bool active=true;
static float freq;
static uint64_t timeout;
if (millis()>timeout){
if (active){
// play note for 800 ms
freq = notes_array[random(sizeof(notes_array)/sizeof(float))];
instrument.noteOn(freq, note_amplitude);
timeout = millis()+800;
active = false;
} else {
// silence for 100 ms
instrument.noteOff(note_amplitude);
timeout = millis()+100;
active = true;
}
}
}
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial,AudioLogger::Warning);
// setup input
auto icfg = in.defaultConfig();
in.begin(icfg);
// setup output
auto ocfg = out.defaultConfig(TX_MODE);
ocfg.copyFrom(icfg);
ocfg.sd_active = false;
out.begin(ocfg);
}
void loop() {
play();
copier.copy();
}