Compare commits

..

No commits in common. "6efd54202750093db4303424e59b29e737a6ebde" and "64a04067e0ac7d31e7a2cff88327667696b28805" have entirely different histories.

4 changed files with 22 additions and 281 deletions

View File

@ -1,116 +0,0 @@
//waveform generator
// hacked from https://www.instructables.com/Arduino-Waveform-Generator-1/
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
#define nsamp 32
#define dacmax 256
const byte nclk = 200; // a guess
long int freq; //frequency in Hz
long unsigned int phase;
long unsigned int phase_inc;
int note = 0;
int gate;
int decay = 2;
float pattern[4];
long unsigned int pattern_inc[4];
void setup() {
TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 3905;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
Serial.begin(115200);
if (!mcp.begin(0x64)) {
while (1) {
delay(100);
}
}
// mcp.setSpeed(400000L);
// mcp.setSpeed(800000L);
mcp.setSpeed(800000L);
freq=440;
phase=0;
pattern[0] = 110.0;
pattern[1] = 82.40688922821751;
pattern[2] = 97.99885899543737;
pattern[3] = 82.40688922821751;
for( int i = 0; i < 4; i++ ) {
pattern_inc[i] = pattern[i] * 975592.231884058;
}
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;
float phi=2*pi*phip;
int val=0;
//saw
val = dacmax * isamp / nsamp;
//val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1;
//sine
//val=(sin(phi)+1.0)*dacmax/2;
//val=((sin(phi)+0.333*sin(3*phi))/0.943+1)*dacmax/2;
val=max(val,0);
val=min(val,dacmax-1);
waveform[isamp]=val;
}
}
ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz
note += 1;
if( note > 3 ) {
note = 0;
}
phase_inc = pattern_inc[note];
gate = 4095;
}
void loop() {
phase += phase_inc;
int redphase = phase >> 27;
mcp.fastWrite(waveform[redphase] << 4, gate, 0, 0);
if( gate > 0 ) {
gate -= decay;
}
}

View File

@ -8,44 +8,14 @@
Adafruit_MCP4728 mcp; Adafruit_MCP4728 mcp;
#define nsamp 256 #define nsamp 128
#define dacmax 256 #define dacmax 256
unsigned int phase; unsigned int phase;
unsigned int length;
unsigned int repeats; unsigned int repeats;
int note = 0;
//int freqs[] = { 256, 228, 205, 192, 171, 154, 137, 128, 114, 102, 96, 85, 77, 68, 64 };
//int freqs[] = { 128, 114, 102, 96, 85, 77, 68, 64, 56, 51, 48, 42, 38, 34, 32 };
int freqs[] = { 64, 56, 51, 48, 42, 38, 34, 32, 28, 25, 24, 21, 19, 17, 16 };
int tune[] = { 0, 4, 3, 5, -1, -1, -1, -1 };
byte waveform[nsamp];
int pd = 30;
void setup() { void setup() {
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
OCR1A = 10000;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
Serial.begin(115200); Serial.begin(115200);
if (!mcp.begin(0x64)) { if (!mcp.begin(0x64)) {
@ -58,40 +28,32 @@ void setup() {
mcp.setSpeed(800000L); mcp.setSpeed(800000L);
phase=0; phase=0;
note=0;
} }
ISR(TIMER1_COMPA_vect){//timer1 byte waveform[nsamp];
if( tune[note] > -1 ) {
length=freqs[tune[note]];
for (int i=0; i<nsamp; ++i){ void pluck(){
waveform[i]=random(256); for (int i=0; i<nsamp; ++i){
} waveform[i]=random(256);
} }
note++;
if( note > 7 ) {
note = 0;
}
pd = analogRead(A0) >> 4;
Serial.println(pd);
} }
void loop() { void loop() {
byte s1, s2; byte s1, s2;
int p0 = phase; int p0 = phase;
int p1 = phase - pd; s1 = waveform[phase];
if( p1 < 0 ) {
p1 += length;
}
phase += 1; phase += 1;
if( phase >= length ) { if( phase >= nsamp ) {
phase = 0; phase = 0;
repeats += 1;
} }
s1 = waveform[p1]; s2 = waveform[phase];
s2 = waveform[p0]; waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
mcp.fastWrite(s1 << 4, 0, 0, 0); mcp.fastWrite(s1 << 4, 0, 0, 0);
if( repeats > 100 ) {
pluck();
repeats = 0;
}
} }

View File

@ -1,81 +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() {
cli();
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
OCR1A = 1952;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();
Serial.begin(115200);
if (!mcp.begin(0x64)) {
while (1) {
delay(100);
}
}
// mcp.setSpeed(400000L);
// mcp.setSpeed(800000L);
mcp.setSpeed(800000L);
randomSeed(analogRead(A0));
phase=0;
}
byte waveform[nsamp];
void 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;
}
s2 = waveform[phase];
waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
mcp.fastWrite(s1 << 4, 0, 0, 0);
}
ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz
pluck();
}

View File

@ -16,25 +16,7 @@ long unsigned int phase;
long unsigned int phase_inc; long unsigned int phase_inc;
void setup() { void setup() {
TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt // TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt
// cli();
// //set timer1 interrupt at 1Hz
// TCCR1A = 0;// set entire TCCR1A register to 0
// TCCR1B = 0;// same for TCCR1B
// TCNT1 = 0;//initialize counter value to 0
// // set compare match register for 1hz increments
// OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// // turn on CTC mode
// TCCR1B |= (1 << WGM12);
// // Set CS10 and CS12 bits for 1024 prescaler
// TCCR1B |= (1 << CS12) | (1 << CS10);
// // enable timer compare interrupt
// TIMSK1 |= (1 << OCIE1A);
// sei();
Serial.begin(115200); Serial.begin(115200);
@ -65,13 +47,14 @@ void setwave(){
int val=0; int val=0;
//saw //saw
//val = dacmax * isamp / nsamp; //val = dacmax * isamp / nsamp;
//val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1; //val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1;
//sine //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=((sin(phi)+0.333*sin(3*phi))/0.943+1)*dacmax/2;
// val=max(val,0);
// val=min(val,dacmax-1); val=max(val,0);
val=min(val,dacmax-1);
waveform[isamp]=val; waveform[isamp]=val;
} }
} }
@ -82,21 +65,14 @@ void calc_phase_inc(){
// by measurement, 2^27 is about 138Hz (C below middle C) // by measurement, 2^27 is about 138Hz (C below middle C)
// 2^27 / 138 = 972592.231884058 // 2^27 / 138 = 972592.231884058
phase_inc = freq * 975592.231884058; phase_inc = freq * 975592.231884058;
Serial.println(phase_inc);
} }
// ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz
// freq = 55 + 1760.0 * analogRead(A0) / 1024.0;
// Serial.println(freq);
// calc_phase_inc();
// }
void loop() { void loop() {
phase += phase_inc; phase += phase_inc;
int redphase = phase >> 27; int redphase = phase >> 27;
freq = 55 + 1760.0 * analogRead(A0) / 1024.0;
calc_phase_inc();
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0); mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
} }