From 51c2f3bf8d188b717aa442c6e8691aa48202f2b2 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Fri, 5 Jan 2024 15:25:01 +1100 Subject: [PATCH] Wonderfully broken Karplus-Strong algorithm --- Karplus_Strong/Karplus_Strong.ino | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Karplus_Strong/Karplus_Strong.ino diff --git a/Karplus_Strong/Karplus_Strong.ino b/Karplus_Strong/Karplus_Strong.ino new file mode 100644 index 0000000..6de9abc --- /dev/null +++ b/Karplus_Strong/Karplus_Strong.ino @@ -0,0 +1,61 @@ +//waveform generator + +// hacked from https://www.instructables.com/Arduino-Waveform-Generator-1/ + +#include +#include + +Adafruit_MCP4728 mcp; + +#define nsamp 128 +#define dacmax 256 + +unsigned int phase; +unsigned int repeats; + + +void setup() { + + Serial.begin(115200); + + if (!mcp.begin(0x64)) { + while (1) { + delay(100); + } + } + +// mcp.setSpeed(400000L); +// mcp.setSpeed(800000L); + mcp.setSpeed(800000L); + + phase=0; + pluck(); +} + +byte waveform[nsamp]; +void pluck(){ + Serial.println("pluck"); + for (int i=0; i nsamp ) { + phase = 0; + repeats += 1; + } + s2 = waveform[phase]; + waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1); + mcp.fastWrite(s1 << 4, 0, 0, 0); + if( repeats > 100 ) { + pluck(); + repeats = 0; + + } +} +