Compare commits
No commits in common. "29d7156d1649067e6038f091890a9c8ca9ea4256" and "cdeb974109d7eefcb483288acdd5d018f49effe3" have entirely different histories.
29d7156d16
...
cdeb974109
|
@ -1,62 +0,0 @@
|
|||
// 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;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -7,39 +7,44 @@
|
|||
|
||||
Adafruit_MCP4728 mcp;
|
||||
|
||||
#define nsamp 32
|
||||
#define nsamp 256
|
||||
#define dacmax 256
|
||||
|
||||
const byte nclk = 200; // a guess
|
||||
long int freq; //frequency in Hz
|
||||
const byte nclk=42; //number of clock cycles of the loop
|
||||
long int freq; //frequency in mHz
|
||||
long unsigned int phase;
|
||||
long unsigned int phase_inc;
|
||||
|
||||
void setup() {
|
||||
//TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt
|
||||
|
||||
//Wire.setClock(1400000L); // wooooo
|
||||
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(100);
|
||||
delay(10);
|
||||
}
|
||||
} else {
|
||||
Serial.println("Adafruit MCP4728 initialised");
|
||||
}
|
||||
|
||||
// mcp.setSpeed(400000L);
|
||||
// mcp.setSpeed(800000L);
|
||||
mcp.setSpeed(800000L);
|
||||
|
||||
freq=440;
|
||||
freq=660;
|
||||
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;
|
||||
|
@ -47,10 +52,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);
|
||||
|
@ -60,19 +65,19 @@ void setwave(){
|
|||
}
|
||||
|
||||
|
||||
//calculate the phase increment.
|
||||
//calculate the phase increment. 2^32/16E6=268.435456
|
||||
void calc_phase_inc(){
|
||||
// by measurement, 2^27 is about 138Hz (C below middle C)
|
||||
// 2^27 / 138 = 972592.231884058
|
||||
phase_inc = freq * 975592.231884058;
|
||||
phase_inc=0.268435456*nclk*freq;
|
||||
Serial.println("phase inc");
|
||||
Serial.println(phase_inc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//regular running: generate waves
|
||||
void loop() {
|
||||
phase += phase_inc;
|
||||
int redphase = phase >> 27;
|
||||
//phase+=900;
|
||||
//int redphase = phase >> 24;
|
||||
phase += analogRead(A0);
|
||||
int redphase = phase % nsamp;
|
||||
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue