Commit 22f28a29 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Always access list via pointer

parent bbaf5e5d
......@@ -3,112 +3,108 @@
#include "list.h"
#define TEST_ARRAY_SIZE 10
uint8_t test_array[TEST_ARRAY_SIZE];
struct list l = {
.array = test_array,
.sup = TEST_ARRAY_SIZE,
};
INIT_LIST(l, TEST_ARRAY_SIZE);
void
test_flush(void)
{
desc("test l_flush()");
l_flush(&l);
assert(l.head == l.sup);
assert(l.sup == TEST_ARRAY_SIZE);
l_flush(l);
assert(l->head == l->sup);
assert(l->sup == TEST_ARRAY_SIZE);
}
void
test_empty(void)
{
desc("test l_empty()");
l_flush(&l);
l_flush(l);
assert(l_empty(&l));
assert(l_empty(l));
l_push(&l, 5);
assert(!l_empty(&l));
l_push(l, 5);
assert(!l_empty(l));
l_delete(&l, 5);
assert(l_empty(&l));
l_delete(l, 5);
assert(l_empty(l));
}
void
test_push(void)
{
desc("test l_push(), l_first(), l_last()");
l_flush(&l);
l_flush(l);
l_push(&l, 2);
l_push(&l, 5);
l_push(&l, 3);
l_push(l, 2);
l_push(l, 5);
l_push(l, 3);
assert(l_first(&l) == 3);
assert(l_last(&l) == 2);
assert(l_first(l) == 3);
assert(l_last(l) == 2);
l_delete(&l, 5);
assert(l_first(&l) == 3);
assert(l_last(&l) == 2);
l_delete(l, 5);
assert(l_first(l) == 3);
assert(l_last(l) == 2);
l_delete(&l, 2);
assert(l_first(&l) == 3);
assert(l_last(&l) == 3);
l_delete(l, 2);
assert(l_first(l) == 3);
assert(l_last(l) == 3);
l_push(&l, 1);
l_push(&l, 9);
l_push(&l, 9);
assert(l_first(&l) == 9);
l_push(l, 1);
l_push(l, 9);
l_push(l, 9);
assert(l_first(l) == 9);
l_delete(&l, 9);
assert(l_first(&l) == 1);
assert(l_last(&l) == 3);
l_delete(l, 9);
assert(l_first(l) == 1);
assert(l_last(l) == 3);
l_delete(&l, 3);
assert(l_first(&l) == 1);
assert(l_last(&l) == 1);
l_delete(l, 3);
assert(l_first(l) == 1);
assert(l_last(l) == 1);
l_delete(&l, 1);
assert(l_empty(&l));
l_delete(l, 1);
assert(l_empty(l));
}
void
test_contains(void)
{
desc("test l_contains()");
l_flush(&l);
l_push(&l, 3);
l_push(&l, 4);
l_push(&l, 10);
l_push(&l, 1);
assert(!l_contains(&l, 0));
assert(l_contains(&l, 1));
assert(!l_contains(&l, 2));
assert(l_contains(&l, 3));
assert(l_contains(&l, 4));
assert(!l_contains(&l, 5));
assert(!l_contains(&l, 6));
assert(!l_contains(&l, 7));
assert(!l_contains(&l, 8));
assert(!l_contains(&l, 9));
l_flush(l);
l_push(l, 3);
l_push(l, 4);
l_push(l, 10);
l_push(l, 1);
assert(!l_contains(l, 0));
assert(l_contains(l, 1));
assert(!l_contains(l, 2));
assert(l_contains(l, 3));
assert(l_contains(l, 4));
assert(!l_contains(l, 5));
assert(!l_contains(l, 6));
assert(!l_contains(l, 7));
assert(!l_contains(l, 8));
assert(!l_contains(l, 9));
for (uint8_t i = 10; i != 0; i++) {
assert(!l_contains(&l, i));
assert(!l_contains(l, i));
}
l_flush(&l);
l_flush(l);
// paranoid test, what if there are 3s everywhere
for (uint8_t i = 0; i < TEST_ARRAY_SIZE; i++) {
test_array[i] = 3;
l->array[i] = 3;
}
assert(!l_contains(&l, 3));
assert(l_empty(&l));
assert(!l_contains(l, 3));
assert(l_empty(l));
l_push(&l, 4);
assert(!l_contains(&l, 3));
assert(l_contains(&l, 4));
l_push(l, 4);
assert(!l_contains(l, 3));
assert(l_contains(l, 4));
}
void
......@@ -116,32 +112,32 @@ test_append(void)
{
desc("test l_append()");
l_flush(&l);
l_push(&l, 3);
l_push(&l, 4);
l_push(&l, 9);
l_append(&l, 2);
l_flush(l);
l_push(l, 3);
l_push(l, 4);
l_push(l, 9);
l_append(l, 2);
assert(l.head == 9);
assert(l.array[9] == 4);
assert(l.array[4] == 3);
assert(l.array[3] == 2);
assert(l.array[2] == l.sup);
assert(l->head == 9);
assert(l->array[9] == 4);
assert(l->array[4] == 3);
assert(l->array[3] == 2);
assert(l->array[2] == l->sup);
l_flush(&l);
l_append(&l, 5);
l_flush(l);
l_append(l, 5);
assert(l_last(&l) == 5);
assert(l_first(&l) == 5);
assert(l_last(l) == 5);
assert(l_first(l) == 5);
l_flush(&l);
l_push(&l, 2);
l_push(&l, 3);
l_push(&l, 4);
l_append(&l, 4);
l_flush(l);
l_push(l, 2);
l_push(l, 3);
l_push(l, 4);
l_append(l, 4);
assert(l_last(&l) == 4);
assert(l_first(&l) == 3);
assert(l_last(l) == 4);
assert(l_first(l) == 3);
}
void
......@@ -149,20 +145,20 @@ test_index(void)
{
desc("test l_index()");
l_flush(&l);
l_push(&l, 3);
l_push(&l, 4);
l_push(&l, 9);
l_append(&l, 2);
l_flush(l);
l_push(l, 3);
l_push(l, 4);
l_push(l, 9);
l_append(l, 2);
assert(l_index(&l, 3) == 2);
assert(l_index(&l, 4) == 1);
assert(l_index(&l, 9) == 0);
assert(l_index(&l, 2) == 3);
assert(l_index(l, 3) == 2);
assert(l_index(l, 4) == 1);
assert(l_index(l, 9) == 0);
assert(l_index(l, 2) == 3);
assert(l_index(&l, 5) == l.sup);
assert(l_index(&l, l.sup) == l.sup);
assert(l_index(&l, l.sup + 1) == l.sup);
assert(l_index(l, 5) == l->sup);
assert(l_index(l, l->sup) == l->sup);
assert(l_index(l, l->sup + 1) == l->sup);
}
int
......
......@@ -9,6 +9,15 @@ struct list {
uint8_t sup;
};
#define INIT_LIST(name, size) \
static uint8_t _ ## name ## _array[size]; \
static struct list _ ## name = {\
.array = _ ## name ## _array,\
.sup = size,\
};\
static struct list *name = &_ ## name;
void l_push(struct list *l, uint8_t x);
void l_append(struct list *l, uint8_t x);
void l_delete(struct list *l, uint8_t x);
......
......@@ -9,11 +9,7 @@
#include <util/delay.h>
static uint8_t _notes_array[NUM_NOTES];
static struct list notes = {
.array = _notes_array,
.sup = NUM_NOTES,
};
INIT_LIST(notes, NUM_NOTES);
static uint16_t current_bend;
......@@ -30,7 +26,7 @@ static uint8_t detune_amount;
void
mono_init(void)
{
l_flush(&notes);
l_flush(notes);
detune_amount = 0;
current_bend = BEND_CENTER;
}
......@@ -42,7 +38,7 @@ mono_note_on(uint8_t n)
return;
}
l_push(&notes, n);
l_push(notes, n);
mono_set_pitch();
for (uint8_t v = 0; v < NUM_VOICES; v++) {
......@@ -53,11 +49,11 @@ mono_note_on(uint8_t n)
void
mono_set_pitch(void)
{
if (l_empty(&notes)) {
if (l_empty(notes)) {
return;
}
uint8_t n = l_first(&notes);
uint8_t n = l_first(notes);
for (uint8_t v = 0; v < NUM_VOICES; v++) {
int16_t detune = detune_factor[v] * detune_amount;
detune += vibrato_detune();
......@@ -72,9 +68,9 @@ mono_note_off(uint8_t n)
return;
}
l_delete(&notes, n);
l_delete(notes, n);
if (l_empty(&notes)) {
if (l_empty(notes)) {
for (uint8_t v = 0; v < NUM_VOICES; v++) {
gate_off(v);
}
......
......@@ -10,28 +10,19 @@
static uint8_t playing[NUM_VOICES];
static uint16_t current_bend;
static uint8_t want_play_array[NUM_NOTES];
static struct list want_play = {
.array = want_play_array,
.sup = NUM_NOTES,
};
static uint8_t avail_array[NUM_VOICES];
static struct list avail = {
.array = avail_array,
.sup = NUM_VOICES,
};
INIT_LIST(avail, NUM_VOICES);
INIT_LIST(want_play, NUM_NOTES);
void poly2_update_playing(void);
void
poly2_init(void)
{
l_flush(&want_play);
l_flush(&avail);
l_flush(want_play);
l_flush(avail);
for (uint8_t i = 0; i < NUM_VOICES; i++) {
playing[i] = NUM_NOTES;
l_push(&avail, i);
l_push(avail, i);
}
current_bend = BEND_CENTER;
......@@ -50,7 +41,7 @@ poly2_note_on(uint8_t note)
return; // should never happen
}
l_append(&want_play, note);
l_append(want_play, note);
poly2_update_playing();
}
......@@ -70,21 +61,21 @@ void
poly2_update_playing(void)
{
for (uint8_t v = 0; v < NUM_VOICES; v++) {
if ((playing[v] < NUM_NOTES) && !(l_index(&want_play, playing[v]) < NUM_VOICES)) {
if ((playing[v] < NUM_NOTES) && !(l_index(want_play, playing[v]) < NUM_VOICES)) {
gate_off(v);
playing[v] = NUM_NOTES;
l_push(&avail, v);
l_push(avail, v);
}
}
uint8_t want_play_i = 0;
for (uint8_t j = want_play.head; (want_play_i < NUM_VOICES) && (j != want_play.sup); want_play_i++, (j = l_succ(&want_play, j))) {
for (uint8_t j = want_play->head; (want_play_i < NUM_VOICES) && (j != want_play->sup); want_play_i++, (j = l_succ(want_play, j))) {
if (poly2_is_playing(j)) {
continue;
}
uint8_t v = l_last(&avail);
if (v == NUM_VOICES) {
uint8_t v = l_last(avail);
if (v == avail->sup) {
// no voices available
continue;
}
......@@ -92,7 +83,7 @@ poly2_update_playing(void)
poly2_set_pitch(v, j);
gate_on(v);
playing[v] = j;
l_delete(&avail, v);
l_delete(avail, v);
}
}
......@@ -103,7 +94,7 @@ poly2_note_off(uint8_t note)
return; // should never happen
}
l_delete(&want_play, note);
l_delete(want_play, note);
poly2_update_playing();
}
......
......@@ -40,65 +40,57 @@
// 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.
static uint8_t avail_array[NUM_VOICES];
static struct list avail = {
.array = avail_array,
.sup = NUM_VOICES,
};
INIT_LIST(avail, NUM_VOICES);
void
avail_enqueue(uint8_t v)
{
l_push(&avail, v);
l_push(avail, v);
}
uint8_t
avail_is_empty(void)
{
return l_empty(&avail);
return l_empty(avail);
}
uint8_t
avail_dequeue(void)
{
uint8_t v = l_last(&avail);
l_delete(&avail, v);
uint8_t v = l_last(avail);
l_delete(avail, v);
return v;
}
static uint8_t playing_array[NUM_VOICES];
static struct list playing = {
.array = playing_array,
.sup = NUM_VOICES,
};
INIT_LIST(playing, NUM_VOICES);
void
playing_play(uint8_t v)
{
l_push(&playing, v);
l_push(playing, v);
}
void
playing_release(uint8_t v)
{
l_delete(&playing, v);
l_delete(playing, v);
}
uint8_t
playing_oldest(void)
{
return l_last(&playing);
return l_last(playing);
}
void
voice_init(void)
{
l_flush(&avail);
l_flush(avail);
for (uint8_t i = 0; i < NUM_VOICES; i++) {
avail_enqueue(i);
}
l_flush(&playing);
l_flush(playing);
}
// Release the oldest playing voice, regardless of what note it is playing.
......@@ -134,7 +126,7 @@ voice_acquire()
void
voice_release(uint8_t v)
{
if (l_contains(&playing, v)) {
if (l_contains(playing, v)) {
playing_release(v);
avail_enqueue(v);
}
......
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