Commit f7cb65f2 authored by Jacob Vosmaer's avatar Jacob Vosmaer

First working version of pitch envelope

parent d2385e6c
......@@ -9,6 +9,7 @@
#include "poly2.h"
#include "mono.h"
#include "vibrato.h"
#include "pitch.h"
enum _programs {
PGM_POLY,
......@@ -98,11 +99,28 @@ do_clock_update(void)
vibrato_update(delta);
gate_update(delta);
pitch_update_clock(delta);
(*update_clock[current_program])(delta);
prev_clock = clock;
}
void
handle_cc(uint8_t cc, uint8_t val)
{
if ((cc > 127) || (val > 127)) {
return;
}
switch (cc) {
case CC_PITCH_DECAY:
pitch_envelope_set_decay(val);
break;
}
(*control_change[current_program])(cc, val);
}
int
main(void)
{
......@@ -136,7 +154,7 @@ main(void)
program_change(data1);
break;
case MIDI_CONTROL_CHANGE:
(*control_change[current_program])(data1, data2);
handle_cc(data1, data2);
break;
case MIDI_PITCH_BEND:
(*pitch_bend[current_program])((data2 << 7) | data1);
......
#include <avr/io.h>
#include "voice.h"
#include "gate.h"
#include "pitch.h"
static uint8_t _gate_retrig[NUM_VOICES];
......@@ -49,6 +50,7 @@ gate_on(uint8_t v)
gate_off(v);
_gate_retrig[v] = RETRIG_DELAY;
pitch_envelope_trigger(v);
}
void
......
......@@ -15,6 +15,7 @@ enum _statuses {
enum _cc {
CC_MOD_WHEEL = 1,
CC_DETUNE = 14,
CC_PITCH_DECAY = 15,
CC_VIB_RATE = 76,
};
......
......@@ -3,6 +3,11 @@
#include "dac.h"
#include "safe_add.h"
static uint16_t _decay_counter[NUM_VOICES];
static uint8_t _decay;
enum { DECAY_SCALE = 10000 };
void
pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend)
{
......@@ -12,6 +17,7 @@ pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend)
uint16_t data = dac_note_pitch(voice, note);
data = safe_add(data, detune);
data = safe_add(data, (((float) _decay_counter[voice]) / DECAY_SCALE) * dac_pitch_delta(1));
float bend_factor = ((int16_t) (bend & BEND_MAX) - BEND_CENTER) / ((float) BEND_MAX);
int16_t bend_data = dac_pitch_delta(4) * bend_factor;
......@@ -19,3 +25,36 @@ pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend)
dac_send(dac_command_set(voice), data);
}
void
pitch_update_clock(uint8_t delta)
{
for_each_voice(v) {
if (_decay_counter[v] <= delta) {
_decay_counter[v] = 0;
continue;
}
_decay_counter[v] = _decay_counter[v] - delta;
}
}
void
pitch_envelope_set_decay(uint8_t decay)
{
if (decay > 127) {
decay = 127;
}
_decay = decay;
}
void
pitch_envelope_trigger(uint8_t voice)
{
if (voice >= NUM_VOICES) {
return;
}
_decay_counter[voice] = (_decay/127.0) * DECAY_SCALE;
}
......@@ -5,6 +5,9 @@
void pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend);
uint16_t pitch_delta(uint8_t semitones);
void pitch_update_clock(uint8_t delta);
void pitch_envelope_set_decay(uint8_t decay);
void pitch_envelope_trigger(uint8_t voice);
enum {
BEND_CENTER = 8192,
......
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