Wonderfully broken Karplus-Strong algorithm

main
Mike Lynch 2024-01-05 15:25:01 +11:00
parent 66b95576a9
commit 51c2f3bf8d
1 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,61 @@
//waveform generator
// hacked from https://www.instructables.com/Arduino-Waveform-Generator-1/
#include <Adafruit_MCP4728.h>
#include <Wire.h>
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; ++i){
waveform[i]=random(256);
}
}
void loop() {
byte s1, s2;
int p0 = phase;
s1 = waveform[phase];
phase += 1;
if( phase > 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;
}
}