poly.c 1.27 KB
Newer Older
Jacob Vosmaer's avatar
Jacob Vosmaer committed
1 2 3 4
#include "dac.h"
#include "poly.h"
#include "gate.h"
#include "voice.h"
5
#include "poly_voice.h"
Jacob Vosmaer's avatar
Jacob Vosmaer committed
6 7
#include "vibrato.h"
#include "midi.h"
8
#include "notes.h"
Jacob Vosmaer's avatar
Jacob Vosmaer committed
9

10 11 12
static uint8_t notes[NUM_VOICES];
static uint16_t current_bend;

Jacob Vosmaer's avatar
Jacob Vosmaer committed
13 14 15
void
poly_init(void)
{
16
	gate_init();
Jacob Vosmaer's avatar
Jacob Vosmaer committed
17
	voice_init();
18
	current_bend = BEND_CENTER;
19 20 21
	for_each_voice(v) {
		notes[v] = NUM_NOTES;
	}
Jacob Vosmaer's avatar
Jacob Vosmaer committed
22 23
}

24 25 26
void
poly_set_pitch(uint8_t v, uint8_t note)
{
Jacob Vosmaer's avatar
Jacob Vosmaer committed
27
	dac_set_note2(v, note, vibrato_detune(), current_bend);
28
}
29

Jacob Vosmaer's avatar
Jacob Vosmaer committed
30 31 32 33 34 35 36 37 38
void
poly_note_on(uint8_t note)
{
	uint8_t v = voice_acquire(note);
	if (v == NUM_VOICES) {
		// This should never happen if the voice assigner is working correctly.
		return;
	}

39
	poly_set_pitch(v, note);
40
	gate_on(v);
41
	notes[v] = note;
Jacob Vosmaer's avatar
Jacob Vosmaer committed
42 43 44 45 46
}

void
poly_note_off(uint8_t note)
{
Jacob Vosmaer's avatar
Jacob Vosmaer committed
47
	for_each_voice(v) {
48
		if (notes[v] == note) {
49
			notes[v] = NUM_NOTES;
50 51 52
			voice_release(v);
			gate_off(v);
		}
Jacob Vosmaer's avatar
Jacob Vosmaer committed
53 54
	}
}
55 56 57 58

void
poly_control_change(uint8_t ctl, uint8_t val)
{
Jacob Vosmaer's avatar
Jacob Vosmaer committed
59 60 61 62 63 64 65 66
	switch (ctl) {
	case CC_MOD_WHEEL:
		vibrato_set_depth(val);
		break;
	case CC_VIB_RATE:
		vibrato_set_rate(val);
		break;
	}
67
}
68 69 70 71

void
poly_pitch_bend(uint16_t bend)
{
72
	current_bend = bend;
Jacob Vosmaer's avatar
Jacob Vosmaer committed
73
	for_each_voice(v) {
74 75
		poly_set_pitch(v, notes[v]);
	}
76
}
77 78 79 80

void
poly_update_clock(uint8_t delta)
{
Jacob Vosmaer's avatar
Jacob Vosmaer committed
81
	vibrato_update(delta);
Jacob Vosmaer's avatar
Jacob Vosmaer committed
82
	for_each_voice(v) {
Jacob Vosmaer's avatar
Jacob Vosmaer committed
83 84
		poly_set_pitch(v, notes[v]);
	}
85
	gate_update(delta);
86
}