implemented timeslot queue to fix #2 but sound is still a bit strange

This commit is contained in:
bigalex 2012-08-13 22:33:28 +02:00
parent 6d2d1eae1e
commit 8d8e8ed9a0
3 changed files with 62 additions and 6 deletions

View File

@ -111,6 +111,7 @@ int main(void)
for(;;) /* ever */ { for(;;) /* ever */ {
//do something //do something
synth_poll();
if (10 == 1){ if (10 == 1){
uint16_t out = darkval; uint16_t out = darkval;
USART0_put_uint16(out); 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) ISR(TIMER2_COMPA_vect)
{ {

View File

@ -2,6 +2,7 @@
#include "synth.h" #include "synth.h"
#include "freq_table.h" #include "freq_table.h"
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/interrupt.h>
// sample rate is 8M / (3 * 64) // sample rate is 8M / (3 * 64)
@ -177,12 +178,27 @@ static int8_t row;
static int8_t seq; 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) void synth_init(void)
{ {
sample = 0; sample = 0;
tick = 0; tick = 0;
row = 0; row = 0;
seq = 0; seq = 0;
//prefill timeslot buffer
enqueue_timeslot(synth_mix());
enqueue_timeslot(synth_mix());
} }
uint16_t synth_mix(void) uint16_t synth_mix(void)
@ -270,3 +286,41 @@ uint16_t synth_mix(void)
return output; 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();
}

View File

@ -1,3 +1,8 @@
#ifndef SYNTH_H
#define SYNTH_H
#define SYNTH_BUFSIZE 2
enum { WAVE_OFF, WAVE_PULSE, WAVE_SAW, WAVE_NOISE }; enum { WAVE_OFF, WAVE_PULSE, WAVE_SAW, WAVE_NOISE };
typedef struct { typedef struct {
@ -23,5 +28,6 @@ typedef struct {
void synth_init(void); void synth_init(void);
uint16_t synth_mix(void); void synth_poll(void);
#endif