2012-09-02 23:17:59 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include "synth.h"
|
|
|
|
#include "freq_table.h"
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
|
|
// sample rate is 8M / (3 * 64)
|
|
|
|
|
|
|
|
enum {
|
|
|
|
channel_count = 3,
|
|
|
|
//tick_length = 400,
|
|
|
|
// tick_length = 256,
|
|
|
|
row_length = 4,
|
|
|
|
pattern_length = 16
|
|
|
|
};
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
static const synth_instrument_t instruments[] = { { 1 << 15, 100, 12, 0 }, { 0,
|
|
|
|
100, 12, 0 }, { 0, 200, 10, 0 }, { 1 << 13, 0, 0, 2 }, { 1 << 13, 0, 5,
|
|
|
|
2 }, };
|
2012-09-02 23:17:59 +02:00
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
static const uint8_t wave_table[][2] = { { 0, WAVE_PULSE }, { 3, WAVE_PULSE }, {
|
|
|
|
7, WAVE_PULSE }, { 12, WAVE_PULSE }, { 256 - 4, 0xff },
|
2012-09-02 23:17:59 +02:00
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
{ 0, WAVE_PULSE }, { 2, WAVE_PULSE }, { 7, WAVE_PULSE }, { 10, WAVE_PULSE }, {
|
|
|
|
256 - 4, 0xff },
|
2012-09-02 23:17:59 +02:00
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
{ 0, WAVE_NOISE }, { 0, WAVE_PULSE }, { 0xff, 0xff },
|
2012-09-02 23:17:59 +02:00
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
{ 0, WAVE_PULSE }, { 0xff, 0xff },
|
2012-09-02 23:17:59 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
static const uint8_t patterns[][pattern_length][2] PROGMEM = { { }, { { 33 - 12,
|
|
|
|
0 }, { 0, 0 }, { 0xff, 1 }, { 0, 0 }, { 33, 1 }, { 0xff, 1 }, { 33, 1 },
|
|
|
|
{ 0xff, 1 }, { 33, 1 }, { 0xff, 1 }, { 33 - 12, 1 }, { 0xff, 1 }, { 33
|
|
|
|
- 12, 1 }, { 0xff, 1 }, { 33, 1 }, { 0xff, 1 }, }, { { 28 - 12,
|
|
|
|
0 }, { 0, 0 }, { 0xff, 1 }, { 0, 0 }, { 28, 1 }, { 0xff, 1 }, { 28, 1 },
|
|
|
|
{ 0xff, 1 }, { 28, 1 }, { 0xff, 1 }, { 28 - 12, 1 }, { 0xff, 1 }, { 28
|
|
|
|
- 12, 1 }, { 0xff, 1 }, { 28, 1 }, { 0xff, 1 }, }, { { 0, 0 }, {
|
|
|
|
0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, {
|
|
|
|
57, 3 }, }, { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
|
|
|
|
{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 57, 4 }, },
|
|
|
|
|
|
|
|
{ { 60, 2 }, }, { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 57, 2 }, { 0, 0 }, {
|
|
|
|
0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 55, 2 }, {
|
|
|
|
0, 0 }, { 57, 2 }, { 0, 0 }, }, { { 55, 2 }, }, { { 0, 0 }, { 0, 0 }, {
|
|
|
|
0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0,
|
|
|
|
0 }, { 0, 0 }, { 0, 0 }, { 57, 2 }, }, { { 55 - 3, 2 }, },
|
2012-09-02 23:17:59 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
static const uint8_t pattern_table[][channel_count] = { { 1, 0, 5 },
|
|
|
|
{ 1, 3, 0 }, { 1, 0, 7 }, { 1, 3, 6 }, { 2, 0, 7 }, { 2, 4, 8 }, { 2, 0,
|
|
|
|
9 }, { 2, 4, 0 }, };
|
2012-09-02 23:17:59 +02:00
|
|
|
enum {
|
|
|
|
pattern_table_length = sizeof(pattern_table) / sizeof(pattern_table[0])
|
|
|
|
};
|
|
|
|
|
|
|
|
static synth_channel_t channels[channel_count];
|
|
|
|
static int8_t sample;
|
|
|
|
static int8_t tick;
|
|
|
|
static int8_t row;
|
|
|
|
static int8_t seq;
|
|
|
|
|
|
|
|
/* PROTOTYPES */
|
|
|
|
uint8_t synth_mix(void);
|
|
|
|
|
|
|
|
static uint8_t timeslots[SYNTH_BUFSIZE];
|
|
|
|
static uint8_t timeslots_write; // current write head
|
|
|
|
static uint8_t timeslots_read; // current read head
|
|
|
|
|
|
|
|
/*register for atomic ++ and -- */
|
|
|
|
register uint8_t timeslots_fill asm("r2");
|
|
|
|
|
|
|
|
static void enqueue_timeslot(uint8_t synthval);
|
|
|
|
static uint8_t dequeue_timeslot(void);
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
void synth_init(void) {
|
2012-09-02 23:17:59 +02:00
|
|
|
sample = 0;
|
|
|
|
tick = 0;
|
|
|
|
row = 0;
|
|
|
|
seq = 0;
|
|
|
|
timeslots_fill = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
inline uint8_t synth_mix(void) {
|
|
|
|
if (sample == 0) { // new tick
|
|
|
|
for (int i = 1; i < channel_count; i++) {
|
2012-09-02 23:17:59 +02:00
|
|
|
synth_channel_t* chan = &channels[i];
|
|
|
|
|
|
|
|
const synth_instrument_t* inst = &instruments[chan->inst_nr];
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
if (chan->level > inst->decay)
|
|
|
|
chan->level -= inst->decay;
|
|
|
|
else
|
|
|
|
chan->level = 0;
|
2012-09-02 23:17:59 +02:00
|
|
|
|
|
|
|
chan->pulse_width += inst->pulse_sweep;
|
|
|
|
|
|
|
|
chan->pos++;
|
2012-10-07 02:04:16 +02:00
|
|
|
if (wave_table[chan->pos][1] == 0xff)
|
|
|
|
chan->pos += wave_table[chan->pos][0];
|
2012-09-02 23:17:59 +02:00
|
|
|
|
|
|
|
// enter new rol
|
|
|
|
// w
|
2012-10-07 02:04:16 +02:00
|
|
|
if (tick == 0) {
|
2012-09-02 23:17:59 +02:00
|
|
|
uint8_t pattern_nr = pattern_table[seq][i];
|
|
|
|
uint8_t note = pgm_read_byte(&patterns[pattern_nr][row][0]);
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
if (note) { // new note, maybe?
|
|
|
|
if (note == 0xff) {
|
2012-09-02 23:17:59 +02:00
|
|
|
chan->level = 0;
|
|
|
|
} else {
|
|
|
|
chan->level = 80; // TODO: less?
|
|
|
|
chan->note = note;
|
2012-10-07 02:04:16 +02:00
|
|
|
chan->inst_nr =
|
|
|
|
pgm_read_byte(&patterns[pattern_nr][row][1]);
|
2012-09-02 23:17:59 +02:00
|
|
|
inst = &instruments[chan->inst_nr];
|
|
|
|
chan->pos = inst->wave_table_pos;
|
2012-10-07 02:04:16 +02:00
|
|
|
if (inst->pulse_width)
|
|
|
|
chan->pulse_width = inst->pulse_width;
|
2012-09-02 23:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-07 02:04:16 +02:00
|
|
|
if (++sample == 0) {
|
|
|
|
if (++tick == row_length) {
|
2012-09-02 23:17:59 +02:00
|
|
|
tick = 0;
|
2012-10-07 02:04:16 +02:00
|
|
|
if (++row == pattern_length) {
|
2012-09-02 23:17:59 +02:00
|
|
|
row = 0;
|
2012-10-07 02:04:16 +02:00
|
|
|
if (++seq == pattern_table_length) {
|
2012-09-02 23:17:59 +02:00
|
|
|
seq = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t output = 0;
|
2012-10-07 02:04:16 +02:00
|
|
|
for (int i = 0; i < channel_count; i++) {
|
2012-09-02 23:17:59 +02:00
|
|
|
synth_channel_t* chan = &channels[i];
|
2012-10-07 02:04:16 +02:00
|
|
|
// const synth_instrument_t* inst = &instruments[chan->inst_nr];
|
2012-09-02 23:17:59 +02:00
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
chan->phase +=
|
|
|
|
pgm_read_word(&freq_table[(uint8_t)(chan->note + wave_table[chan->pos][0])]);
|
2012-09-02 23:17:59 +02:00
|
|
|
|
|
|
|
uint8_t amp;
|
2012-10-07 02:04:16 +02:00
|
|
|
switch (wave_table[chan->pos][1]) {
|
2012-09-02 23:17:59 +02:00
|
|
|
case WAVE_PULSE:
|
|
|
|
amp = -(chan->phase < chan->pulse_width);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_SAW:
|
|
|
|
amp = (chan->phase >> 8);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_NOISE: // shitty noise
|
|
|
|
chan->phase = (chan->phase >> 1) ^ (-(chan->phase & 1) & 0xb400);
|
|
|
|
amp = (chan->phase >> 8);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
amp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
output += (((amp & 0xff) * chan->level) >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fill all the timeslots */
|
|
|
|
inline void synth_poll(void) {
|
|
|
|
/* refill timeslots queue */
|
|
|
|
// while (timeslots_fill < (SYNTH_BUFSIZE-1))
|
2012-10-07 02:04:16 +02:00
|
|
|
if (timeslots_fill < (SYNTH_BUFSIZE - 1))
|
2012-09-02 23:17:59 +02:00
|
|
|
enqueue_timeslot(synth_mix());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timeslot queue handling */
|
|
|
|
static inline void enqueue_timeslot(uint8_t synthval) {
|
|
|
|
timeslots[timeslots_write & SYNTH_BUFMASK] = synthval;
|
|
|
|
timeslots_fill++;
|
|
|
|
timeslots_write++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint8_t dequeue_timeslot() {
|
|
|
|
uint8_t t = timeslots[timeslots_read & SYNTH_BUFMASK];
|
2012-10-07 02:04:16 +02:00
|
|
|
if (timeslots_fill) {
|
2012-09-02 23:17:59 +02:00
|
|
|
/* buffer not underrun... move forward in readbuffer */
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
timeslots_fill--;
|
|
|
|
timeslots_read++;
|
|
|
|
}
|
2012-09-02 23:17:59 +02:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-10-07 02:04:16 +02:00
|
|
|
ISR(TIMER0_COMPA_vect) {
|
2012-09-02 23:17:59 +02:00
|
|
|
/* calculate next analog sample value in synth mixer:*/
|
|
|
|
OCR1B = dequeue_timeslot();
|
|
|
|
}
|
|
|
|
|