8bitalized synth output stage 1

This commit is contained in:
john stone 2012-08-14 19:58:45 +02:00
parent 0ac038179c
commit 96d7a0ed9d

View File

@ -179,16 +179,16 @@ static int8_t seq;
/* PROTOTYPES */ /* PROTOTYPES */
uint16_t synth_mix(void); uint8_t synth_mix(void);
static uint16_t timeslots[SYNTH_BUFSIZE]; static uint16_t timeslots[SYNTH_BUFSIZE];
static uint8_t timeslots_write; // current write head static uint8_t timeslots_write; // current write head
static uint8_t timeslots_read; // current read head static uint8_t timeslots_read; // current read head
uint8_t timeslots_fill;
void enqueue_timeslot(uint16_t synthval);
uint16_t dequeue_timeslot(void);
uint8_t timeslots_fill(void);
static void enqueue_timeslot(uint8_t synthval);
static uint8_t dequeue_timeslot(void);
void synth_init(void) void synth_init(void)
{ {
@ -196,12 +196,13 @@ void synth_init(void)
tick = 0; tick = 0;
row = 0; row = 0;
seq = 0; seq = 0;
timeslots_fill = 0;
//prefill timeslot buffer //prefill timeslot buffer
enqueue_timeslot(synth_mix()); enqueue_timeslot(synth_mix());
enqueue_timeslot(synth_mix()); enqueue_timeslot(synth_mix());
} }
uint16_t synth_mix(void) uint8_t synth_mix(void)
{ {
if(sample == 0) { // new tick if(sample == 0) { // new tick
for(int i = 0; i < channel_count; i++) { for(int i = 0; i < channel_count; i++) {
@ -253,7 +254,7 @@ uint16_t synth_mix(void)
} }
uint16_t output = 0; uint8_t output = 0;
for(int i = 0; i < channel_count; i++) { for(int i = 0; i < channel_count; i++) {
synth_channel_t* chan = &channels[i]; synth_channel_t* chan = &channels[i];
const synth_instrument_t* inst = &instruments[chan->inst_nr]; const synth_instrument_t* inst = &instruments[chan->inst_nr];
@ -288,36 +289,37 @@ uint16_t synth_mix(void)
/* fill all the timeslots */ /* fill all the timeslots */
void synth_poll(void) { inline void synth_poll(void) {
/* refill timeslots queue */ /* refill timeslots queue */
while (timeslots_fill() < (SYNTH_BUFSIZE-1)) while (timeslots_fill < (SYNTH_BUFSIZE-1))
enqueue_timeslot(synth_mix()); enqueue_timeslot(synth_mix());
} }
/* timeslot queue handling */ /* timeslot queue handling */
void enqueue_timeslot(uint16_t synthval) { static inline void enqueue_timeslot(uint8_t synthval) {
timeslots[timeslots_write] = synthval; timeslots[timeslots_write & SYNTH_BUFMASK] = synthval;
timeslots_fill++;
timeslots_write++; timeslots_write++;
timeslots_write &= SYNTH_BUFMASK;
} }
uint16_t dequeue_timeslot() { static inline uint8_t dequeue_timeslot() {
uint16_t t = timeslots[timeslots_read]; uint8_t t = timeslots[timeslots_read & SYNTH_BUFMASK];
PORTC = (timeslots_read != timeslots_write) ? 0b00000001 : 0b00000000; if(timeslots_fill){
timeslots_read++; /* buffer not underrun... move forward in readbuffer */
if (timeslots_read >= SYNTH_BUFSIZE) timeslots_read =0;
#if 1 /*debug */
PORTC = 0;
#endif
timeslots_fill --;
timeslots_read ++;
} else { /*buffer underrun. not moving forward*/
#if 1
PORTC = 0b00000001 ;
#endif
}
return t; 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) ISR(TIMER0_COMPA_vect)
{ {