Beacon sequencer plus tweaks to the KP algorithm

main
Mike Lynch 2024-01-21 16:36:50 +11:00
parent 64a04067e0
commit f90ae78b13
2 changed files with 84 additions and 22 deletions

View File

@ -8,14 +8,44 @@
Adafruit_MCP4728 mcp;
#define nsamp 128
#define nsamp 256
#define dacmax 256
unsigned int phase;
unsigned int length;
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() {
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);
if (!mcp.begin(0x64)) {
@ -28,32 +58,40 @@ void setup() {
mcp.setSpeed(800000L);
phase=0;
note=0;
}
byte waveform[nsamp];
void pluck(){
for (int i=0; i<nsamp; ++i){
waveform[i]=random(256);
ISR(TIMER1_COMPA_vect){//timer1
if( tune[note] > -1 ) {
length=freqs[tune[note]];
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() {
byte s1, s2;
int p0 = phase;
s1 = waveform[phase];
int p1 = phase - pd;
if( p1 < 0 ) {
p1 += length;
}
phase += 1;
if( phase >= nsamp ) {
if( phase >= length ) {
phase = 0;
repeats += 1;
}
s2 = waveform[phase];
waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
s1 = waveform[p1];
s2 = waveform[p0];
waveform[p0] = ( s1 & s2 ) + ((s1 ^ s2) >> 1);
mcp.fastWrite(s1 << 4, 0, 0, 0);
if( repeats > 100 ) {
pluck();
repeats = 0;
}
}

View File

@ -16,7 +16,25 @@ long unsigned int phase;
long unsigned int phase_inc;
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);
@ -47,14 +65,13 @@ 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)+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;
}
}
@ -65,14 +82,21 @@ 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;
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() {
phase += phase_inc;
int redphase = phase >> 27;
freq = 55 + 1760.0 * analogRead(A0) / 1024.0;
calc_phase_inc();
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
}