Compare commits

...

4 Commits

Author SHA1 Message Date
Mike Lynch 29d7156d16 Updated comments 2024-01-05 15:44:26 +11:00
Mike Lynch 51c2f3bf8d Wonderfully broken Karplus-Strong algorithm 2024-01-05 15:25:01 +11:00
Mike Lynch 66b95576a9 Phased accumulator with a 32-bit waveform is kind of working 2024-01-04 11:19:09 +11:00
Mike Lynch 5c8e20e17d I am frustrated! 2024-01-03 11:50:22 +11:00
2 changed files with 84 additions and 27 deletions

View File

@ -0,0 +1,62 @@
// Very basic Karplus-Strong
// based on this explanation http://sites.music.columbia.edu/cmc/MusicAndComputers/chapter4/04_09.php
#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;
}
}

View File

@ -7,44 +7,39 @@
Adafruit_MCP4728 mcp;
#define nsamp 256
#define nsamp 32
#define dacmax 256
const byte nclk=42; //number of clock cycles of the loop
long int freq; //frequency in mHz
const byte nclk = 200; // a guess
long int freq; //frequency in Hz
long unsigned int phase;
long unsigned int phase_inc;
void setup() {
//TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt
//Wire.setClock(1400000L); // wooooo
// TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
// Try to initialize!
if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip");
while (1) {
delay(10);
delay(100);
}
} else {
Serial.println("Adafruit MCP4728 initialised");
}
// mcp.setSpeed(400000L);
// mcp.setSpeed(800000L);
mcp.setSpeed(800000L);
freq=660;
freq=440;
phase=0;
calc_phase_inc();
//phase_inc = 1;
setwave();
}
const float pi=3.14159265;
byte waveform[nsamp];
byte phaseb = 0;
void setwave(){
for (int isamp=0; isamp<nsamp; ++isamp){
float phip=(isamp+0.5)/nsamp;
@ -52,10 +47,10 @@ void setwave(){
int val=0;
//saw
val = dacmax * isamp / nsamp;
//val = dacmax * isamp / nsamp;
//val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1;
//sine
//val=(sin(phi)+1.0)*dacmax/2;
val=(sin(phi)+1.0)*dacmax/2;
//val=((sin(phi)+0.333*sin(3*phi))/0.943+1)*dacmax/2;
val=max(val,0);
@ -65,19 +60,19 @@ void setwave(){
}
//calculate the phase increment. 2^32/16E6=268.435456
//calculate the phase increment.
void calc_phase_inc(){
phase_inc=0.268435456*nclk*freq;
Serial.println("phase inc");
// by measurement, 2^27 is about 138Hz (C below middle C)
// 2^27 / 138 = 972592.231884058
phase_inc = freq * 975592.231884058;
Serial.println(phase_inc);
}
//regular running: generate waves
void loop() {
//phase+=900;
//int redphase = phase >> 24;
phase += analogRead(A0);
int redphase = phase % nsamp;
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
phase += phase_inc;
int redphase = phase >> 27;
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
}