Commit 4f491184 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use voice assigner in main loop

parent d63b3368
......@@ -6,31 +6,23 @@
#include "dac.h"
void
gate_on(uint8_t voice)
gate_on(uint8_t v)
{
if (voice > NUM_VOICES) {
if (v > NUM_VOICES) {
return;
}
PORTC |= _BV(voice);
PORTC |= _BV(v);
}
void
gate_off(uint8_t voice)
gate_off(uint8_t v)
{
if (voice > NUM_VOICES) {
if (v > NUM_VOICES) {
return;
}
PORTC &= ~_BV(voice);
}
void
gate_off_all(void)
{
for (uint8_t i = 0; i < NUM_VOICES; i++) {
gate_off(i);
}
PORTC &= ~_BV(v);
}
void
......@@ -46,12 +38,36 @@ io_init(void)
DDRD = _BV(PORTD1) | _BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7);
}
void
note_on(uint8_t key)
{
uint8_t v = voice_acquire(key);
if (v == NUM_VOICES) {
return;
}
dac_set_note(v, key);
gate_on(v);
}
void
note_off(uint8_t key)
{
uint8_t v = voice_release(key);
if (v == NUM_VOICES) {
return;
}
gate_off(v);
}
int
main(void)
{
io_init();
dac_init();
midi_init();
voice_init();
for (;;) {
if (!midi_read()) {
......@@ -63,15 +79,13 @@ main(void)
uint8_t velocity = midi_data() & 0xff;
if (velocity) {
for (uint8_t i = 0; i < NUM_VOICES; i++) {
dac_set_note(i, key);
gate_on(i);
}
note_on(key);
} else {
gate_off_all();
note_off(key);
}
} else if (midi_status() == MIDI_NOTE_OFF) {
gate_off_all();
uint8_t key = midi_data() >> 8;
note_off(key);
}
}
......
......@@ -81,33 +81,43 @@ playing_oldest(void)
return i;
}
uint8_t keys[NUM_VOICES];
void
voice_init(void)
{
for (uint8_t i = 0; i < NUM_VOICES; i++) {
avail_enqueue(i);
keys[i] = 0xff; // sentinel bogus value
}
}
uint8_t
voice_acquire(void)
voice_acquire(uint8_t key)
{
uint8_t v;
if (!avail_is_empty()) {
uint8_t v = avail_dequeue();
playing_play(v);
return v;
v = avail_dequeue();
} else {
v = playing_oldest();
playing_release(v);
}
uint8_t v = playing_oldest();
playing_release(v);
playing_play(v);
keys[v] = key;
return v;
}
void
voice_release(uint8_t v)
{
playing_release(v);
avail_enqueue(v);
uint8_t
voice_release(uint8_t key)
{
for (uint8_t v = 0; v < NUM_VOICES; v++) {
if (keys[v] == key) {
playing_release(v);
avail_enqueue(v);
return v;
}
}
return NUM_VOICES;
}
......@@ -11,7 +11,7 @@ enum _voices {
NUM_VOICES,
};
uint8_t voice_acquire(void);
void voice_release(uint8_t);
uint8_t voice_acquire(uint8_t key);
uint8_t voice_release(uint8_t key);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment