Commit d63b3368 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add voice assigning

parent ed03c60b
PRG = crowbx
OBJ = crowbx.o midi.o dac.o
OBJ = crowbx.o midi.o dac.o voice.o
MCU_TARGET = atmega328p
#MCU_TARGET = at90s2313
#MCU_TARGET = at90s2333
......
......@@ -2,7 +2,7 @@
#include <util/delay.h>
#include "midi.h"
#include "voices.h"
#include "voice.h"
#include "dac.h"
void
......
#include <avr/io.h>
#include <util/delay.h>
#include "dac.h"
#include "voices.h"
#include "voice.h"
const uint8_t dac_chan[NUM_VOICES] = {2, 3, 4, 5};
......
#include "voice.h"
uint8_t avail[NUM_VOICES+1];
uint8_t avail_head = 0;
uint8_t avail_tail = 0;
uint8_t
avail_advance(uint8_t i)
{
return (i+1) % (NUM_VOICES+1);
}
uint8_t
avail_is_full(void)
{
return avail_advance(avail_tail) == avail_head;
}
void
avail_enqueue(uint8_t v)
{
if (avail_is_full()) {
return;
}
avail[avail_tail] = v;
avail_tail = avail_advance(avail_tail);
}
uint8_t
avail_is_empty(void)
{
return avail_tail == avail_head;
}
uint8_t
avail_dequeue(void)
{
if (avail_is_empty()) {
return NUM_VOICES;
}
uint8_t v = avail[avail_head];
avail_head = avail_advance(avail_head);
return v;
}
uint8_t playing[NUM_VOICES];
uint8_t playing_head = NUM_VOICES;
void
playing_play(uint8_t v)
{
playing[v] = playing_head;
playing_head = v;
}
void
playing_release(uint8_t v)
{
if (playing_head == v) {
playing_head = playing[v];
return;
}
for (uint8_t i = playing_head; playing[i] != NUM_VOICES; i = playing[i]) {
if (playing[i] == v) {
playing[i] = playing[v];
return;
}
}
}
uint8_t
playing_oldest(void)
{
uint8_t i = playing_head;
for (; playing[i] != NUM_VOICES; i = playing[i]) {
// walk the list
}
return i;
}
void
voice_init(void)
{
for (uint8_t i = 0; i < NUM_VOICES; i++) {
avail_enqueue(i);
}
}
uint8_t
voice_acquire(void)
{
if (!avail_is_empty()) {
uint8_t v = avail_dequeue();
playing_play(v);
return v;
}
uint8_t v = playing_oldest();
playing_release(v);
playing_play(v);
return v;
}
void
voice_release(uint8_t v)
{
playing_release(v);
avail_enqueue(v);
}
#ifndef voices_h
#define voices_h
#ifndef voice_h
#define voice_h
#include <inttypes.h>
enum _voices {
VOICE1,
......@@ -9,4 +11,7 @@ enum _voices {
NUM_VOICES,
};
uint8_t voice_acquire(void);
void voice_release(uint8_t);
#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