Commit 7efc0e4f authored by Jacob Vosmaer's avatar Jacob Vosmaer

Move vibrato code to separate file

parent 620af2b3
PRG = crowbx PRG = crowbx
OBJ = crowbx.o midi.o dac.o voice.o uart.o gate.o poly.o mono.o list.o OBJ = crowbx.o midi.o dac.o voice.o uart.o gate.o poly.o mono.o list.o vibrato.o
MCU_TARGET = atmega328p MCU_TARGET = atmega328p
#MCU_TARGET = at90s2313 #MCU_TARGET = at90s2313
#MCU_TARGET = at90s2333 #MCU_TARGET = at90s2333
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "gate.h" #include "gate.h"
#include "voice.h" #include "voice.h"
#include "list.h" #include "list.h"
#include "vibrato.h"
#include <util/delay.h> #include <util/delay.h>
...@@ -25,20 +26,12 @@ const float detune_factor[NUM_VOICES] = { ...@@ -25,20 +26,12 @@ const float detune_factor[NUM_VOICES] = {
}; };
static uint8_t detune_amount; static uint8_t detune_amount;
static uint8_t mod_wheel;
static uint32_t phase;
static uint8_t phase_inc;
static int16_t vibrato;
void void
mono_init(void) mono_init(void)
{ {
l_flush(&notes); l_flush(&notes);
detune_amount = 0; detune_amount = 0;
current_bend = BEND_CENTER; current_bend = BEND_CENTER;
mod_wheel = 0;
vibrato = 0;
phase_inc = 0;
} }
void void
...@@ -66,7 +59,7 @@ mono_set_pitch(void) ...@@ -66,7 +59,7 @@ mono_set_pitch(void)
uint8_t n = l_first(&notes); uint8_t n = l_first(&notes);
for (uint8_t v = 0; v < NUM_VOICES; v++) { for (uint8_t v = 0; v < NUM_VOICES; v++) {
int16_t detune = detune_factor[v] * detune_amount; int16_t detune = detune_factor[v] * detune_amount;
detune += vibrato; detune += vibrato_detune();
dac_set_note2(v, n, detune, current_bend); dac_set_note2(v, n, detune, current_bend);
} }
} }
...@@ -105,10 +98,10 @@ mono_control_change(uint8_t ctl, uint8_t val) ...@@ -105,10 +98,10 @@ mono_control_change(uint8_t ctl, uint8_t val)
mono_set_pitch(); mono_set_pitch();
break; break;
case CC_MOD_WHEEL: case CC_MOD_WHEEL:
mod_wheel = val; vibrato_set_depth(val);
break; break;
case CC_VIB_RATE: case CC_VIB_RATE:
phase_inc = val; vibrato_set_rate(val);
break; break;
} }
} }
...@@ -120,39 +113,9 @@ mono_pitch_bend(uint16_t bend) ...@@ -120,39 +113,9 @@ mono_pitch_bend(uint16_t bend)
mono_set_pitch(); mono_set_pitch();
} }
#define VIB_MID (INT16_MAX >> 1)
void
mono_update_vibrato(void)
{
int16_t old_vibrato = vibrato;
uint16_t phase16 = phase >> 16;
if (phase16 < INT16_MAX) {
vibrato = phase16 - VIB_MID;
} else {
vibrato = phase16 - INT16_MAX;
vibrato = INT16_MAX - vibrato;
vibrato -= VIB_MID;
}
float pitch_factor = dac_pitch_delta(7) / ((float) INT16_MAX);
vibrato *= pitch_factor;
float mod_factor = mod_wheel / 127.0;
vibrato *= mod_factor;
if (vibrato == old_vibrato) {
return;
}
mono_set_pitch();
}
void void
mono_update_clock(uint8_t delta) mono_update_clock(uint8_t delta)
{ {
phase += delta * 4096.0 * (1 + phase_inc); vibrato_update(delta);
mono_update_vibrato(); mono_set_pitch();
} }
#include "vibrato.h"
#include "dac.h"
static uint32_t phase;
static uint8_t phase_inc;
static int16_t vibrato;
static uint8_t depth;
void
vibrato_set_rate(uint8_t val)
{
phase_inc = val;
}
void
vibrato_set_depth(uint8_t val)
{
depth = val;
}
int16_t
vibrato_detune(void)
{
return vibrato;
}
#define VIB_MID (INT16_MAX >> 1)
void
vibrato_update(uint8_t delta)
{
phase += delta * 4096.0 * (1 + phase_inc);
uint16_t phase16 = phase >> 16;
if (phase16 < INT16_MAX) {
vibrato = phase16 - VIB_MID;
} else {
vibrato = phase16 - INT16_MAX;
vibrato = INT16_MAX - vibrato;
vibrato -= VIB_MID;
}
float pitch_factor = dac_pitch_delta(7) / ((float) INT16_MAX);
vibrato *= pitch_factor;
float depth_factor = depth / 127.0;
vibrato *= depth_factor;
}
#ifndef _vibrato_h
#define _vibrato_h
#include <stdint.h>
int16_t vibrato_detune(void);
void vibrato_update(uint8_t delta);
void vibrato_set_rate(uint8_t val);
void vibrato_set_depth(uint8_t val);
#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