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

Rename variable, more bounds checks

parent af8e5f55
......@@ -39,21 +39,21 @@ io_init(void)
}
void
note_on(uint8_t key)
note_on(uint8_t note)
{
uint8_t v = voice_acquire(key);
uint8_t v = voice_acquire(note);
if (v == NUM_VOICES) {
return;
}
dac_set_note(v, key);
dac_set_note(v, note);
gate_on(v);
}
void
note_off(uint8_t key)
note_off(uint8_t note)
{
uint8_t v = voice_release(key);
uint8_t v = voice_release(note);
if (v == NUM_VOICES) {
return;
}
......@@ -75,17 +75,17 @@ main(void)
}
if (midi_status() == MIDI_NOTE_ON) {
uint8_t key = midi_data() >> 8;
uint8_t note = midi_data() >> 8;
uint8_t velocity = midi_data() & 0xff;
if (velocity) {
note_on(key);
note_on(note);
} else {
note_off(key);
note_off(note);
}
} else if (midi_status() == MIDI_NOTE_OFF) {
uint8_t key = midi_data() >> 8;
note_off(key);
uint8_t note = midi_data() >> 8;
note_off(note);
}
}
......
#include "voice.h"
uint8_t avail[NUM_VOICES+1];
#define AVAIL_SIZE (NUM_VOICES + 1)
uint8_t avail[AVAIL_SIZE];
uint8_t avail_head = 0;
uint8_t avail_tail = 0;
uint8_t
avail_advance(uint8_t i)
{
return (i+1) % (NUM_VOICES+1);
return (i + 1) % AVAIL_SIZE;
}
uint8_t
......@@ -81,40 +82,54 @@ playing_oldest(void)
return i;
}
uint8_t keys[NUM_VOICES];
uint8_t notes[NUM_VOICES];
void
voice_init(void)
{
for (uint8_t i = 0; i < NUM_VOICES; i++) {
avail_enqueue(i);
keys[i] = 0xff; // sentinel bogus value
notes[i] = 0xff; // sentinel bogus value
}
}
// Release the oldest playing voice, regardless of what note it is playing.
void
release_oldest(void)
{
uint8_t v = playing_oldest();
if (v == NUM_VOICES) {
return;
}
playing_release(v);
avail_enqueue(v);
}
uint8_t
voice_acquire(uint8_t key)
voice_acquire(uint8_t note)
{
uint8_t v;
if (!avail_is_empty()) {
v = avail_dequeue();
} else {
v = playing_oldest();
playing_release(v);
if (avail_is_empty()) {
release_oldest();
}
uint8_t v = avail_dequeue();
if (v == NUM_VOICES) {
return NUM_VOICES;
}
playing_play(v);
keys[v] = key;
notes[v] = note;
return v;
}
uint8_t
voice_release(uint8_t key)
voice_release(uint8_t note)
{
// Find oldest voice playing key
// Find oldest voice playing note
uint8_t v = NUM_VOICES;
for (uint8_t i = playing_head; i != NUM_VOICES; i = playing[i]) {
if (keys[i] == key) {
if (notes[i] == note) {
v = i;
}
}
......
......@@ -11,7 +11,8 @@ enum _voices {
NUM_VOICES,
};
uint8_t voice_acquire(uint8_t key);
uint8_t voice_release(uint8_t key);
void voice_init(void);
uint8_t voice_acquire(uint8_t note);
uint8_t voice_release(uint8_t note);
#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