diff --git a/firmware/main.c b/firmware/main.c index 8c4db07..6208a71 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -111,6 +111,7 @@ int main(void) for(;;) /* ever */ { //do something + synth_poll(); if (10 == 1){ uint16_t out = darkval; USART0_put_uint16(out); @@ -125,11 +126,6 @@ int main(void) } -ISR(TIMER0_COMPA_vect) -{ - /* calculate next analog sample value in synth mixer:*/ - OCR1B = synth_mix(); -} ISR(TIMER2_COMPA_vect) { diff --git a/firmware/synth.c b/firmware/synth.c index 1af8987..97182a2 100644 --- a/firmware/synth.c +++ b/firmware/synth.c @@ -2,6 +2,7 @@ #include "synth.h" #include "freq_table.h" #include +#include // sample rate is 8M / (3 * 64) @@ -177,12 +178,27 @@ static int8_t row; static int8_t seq; +/* PROTOTYPES */ +uint16_t synth_mix(void); + +static uint16_t timeslots[SYNTH_BUFSIZE]; +static uint8_t timeslots_write; // current write head +static uint8_t timeslots_read; // current read head + +void enqueue_timeslot(uint16_t synthval); +uint16_t dequeue_timeslot(void); +uint8_t timeslots_fill(void); + + void synth_init(void) { sample = 0; tick = 0; row = 0; seq = 0; + //prefill timeslot buffer + enqueue_timeslot(synth_mix()); + enqueue_timeslot(synth_mix()); } uint16_t synth_mix(void) @@ -270,3 +286,41 @@ uint16_t synth_mix(void) return output; } + +/* fill all the timeslots */ +void synth_poll(void) { + /* refill timeslots queue */ + while (timeslots_fill() < (SYNTH_BUFSIZE-1)) + enqueue_timeslot(synth_mix()); +} + +/* timeslot queue handling */ +void enqueue_timeslot(uint16_t synthval) { + timeslots[timeslots_write] = synthval; + timeslots_write++; + if (timeslots_write >= SYNTH_BUFSIZE) + timeslots_write = 0; +} + +uint16_t dequeue_timeslot() { + uint16_t t = timeslots[timeslots_read]; + timeslots_read++; + if (timeslots_read >= SYNTH_BUFSIZE) timeslots_read =0; + return t; +} + +uint8_t timeslots_fill() { + if (timeslots_write >= timeslots_read) + return timeslots_write - timeslots_read; + else + return SYNTH_BUFSIZE - (timeslots_read - timeslots_write); +} + + +ISR(TIMER0_COMPA_vect) +{ + /* calculate next analog sample value in synth mixer:*/ + OCR1B = dequeue_timeslot(); +} + + diff --git a/firmware/synth.h b/firmware/synth.h index 24f0088..efd4e27 100644 --- a/firmware/synth.h +++ b/firmware/synth.h @@ -1,3 +1,8 @@ +#ifndef SYNTH_H +#define SYNTH_H + +#define SYNTH_BUFSIZE 2 + enum { WAVE_OFF, WAVE_PULSE, WAVE_SAW, WAVE_NOISE }; typedef struct { @@ -23,5 +28,6 @@ typedef struct { void synth_init(void); -uint16_t synth_mix(void); +void synth_poll(void); +#endif