2024-01-05 04:44:26 +00:00
|
|
|
// Very basic Karplus-Strong
|
|
|
|
|
|
|
|
// based on this explanation http://sites.music.columbia.edu/cmc/MusicAndComputers/chapter4/04_09.php
|
2024-01-05 04:25:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 03:53:43 +00:00
|
|
|
randomSeed(analogRead(A0));
|
2024-01-05 04:25:01 +00:00
|
|
|
mcp.setSpeed(800000L);
|
|
|
|
|
|
|
|
phase=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte waveform[nsamp];
|
2024-01-07 03:53:43 +00:00
|
|
|
|
|
|
|
|
2024-01-05 04:25:01 +00:00
|
|
|
void pluck(){
|
2024-01-07 03:53:43 +00:00
|
|
|
for (int i=0; i<nsamp; ++i){
|
2024-01-05 04:25:01 +00:00
|
|
|
waveform[i]=random(256);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
byte s1, s2;
|
|
|
|
int p0 = phase;
|
|
|
|
s1 = waveform[phase];
|
|
|
|
phase += 1;
|
2024-01-07 03:53:43 +00:00
|
|
|
if( phase >= nsamp ) {
|
2024-01-05 04:25:01 +00:00
|
|
|
phase = 0;
|
|
|
|
repeats += 1;
|
|
|
|
}
|
|
|
|
s2 = waveform[phase];
|
|
|
|
waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
|
|
|
|
mcp.fastWrite(s1 << 4, 0, 0, 0);
|
|
|
|
if( repeats > 100 ) {
|
2024-01-07 03:53:43 +00:00
|
|
|
pluck();
|
|
|
|
repeats = 0;
|
2024-01-05 04:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|