Commit 2a1d8332 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add some comments (and remove notes initialization)

parent 0c7a45ba
#include "voice.h"
// avail is a fixed-size queue implemented with an array and two
// pointers. It is used to track which voices are available (i.e. not
// playing). It is a queue in order to get 'rotating' voice assignment.
#define AVAIL_SIZE (NUM_VOICES + 1)
uint8_t avail[AVAIL_SIZE];
uint8_t avail_head = 0;
......@@ -46,6 +49,11 @@ avail_dequeue(void)
return v;
}
// play is an ordered set implemented as a sort of bounded singly linked
// list. It is used to keep track of which notes are playing. The linked
// list aspect is used to be able to determine the oldest playing note,
// and to be able to remove voices from anywhere in the list. A voice may
// only occur once in the list.
uint8_t playing[NUM_VOICES];
uint8_t playing_head = NUM_VOICES;
......@@ -82,6 +90,8 @@ playing_oldest(void)
return i;
}
// notes maps voices to the MIDI note they are currently playing, if they
// are playing.
uint8_t notes[NUM_VOICES];
void
......@@ -89,7 +99,6 @@ voice_init(void)
{
for (uint8_t i = 0; i < NUM_VOICES; i++) {
avail_enqueue(i);
notes[i] = 0xff; // sentinel bogus value
}
}
......@@ -115,6 +124,7 @@ voice_acquire(uint8_t note)
uint8_t v = avail_dequeue();
if (v == NUM_VOICES) {
// This is impossible unless there is a mistake in our program.
return NUM_VOICES;
}
......@@ -126,7 +136,7 @@ voice_acquire(uint8_t note)
uint8_t
voice_release(uint8_t note)
{
// Find oldest voice playing note
// Find oldest voice playing this note
uint8_t v = NUM_VOICES;
for (uint8_t i = playing_head; i != NUM_VOICES; i = playing[i]) {
if (notes[i] == note) {
......@@ -135,6 +145,7 @@ voice_release(uint8_t note)
}
if (v == NUM_VOICES) {
// This is possible if we received a bogus MIDI 'note off'.
return NUM_VOICES;
}
......
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