Commit 78d9d3da authored by Jacob Vosmaer's avatar Jacob Vosmaer

WIP logarithmic pitch env

parent 5c4d9285
PRG = crowbx
OBJ = crowbx.o midi.o dac.o uart.o gate.o poly.o mono.o list.o vibrato.o poly2.o poly_voice.o safe_add.o pitch.o
MCU_TARGET = atmega328p
#MCU_TARGET = at90s2313
#MCU_TARGET = at90s2333
#MCU_TARGET = at90s4414
#MCU_TARGET = at90s4433
#MCU_TARGET = at90s4434
#MCU_TARGET = at90s8515
#MCU_TARGET = at90s8535
#MCU_TARGET = atmega128
#MCU_TARGET = atmega1280
#MCU_TARGET = atmega1281
#MCU_TARGET = atmega1284p
#MCU_TARGET = atmega16
#MCU_TARGET = atmega163
#MCU_TARGET = atmega164p
#MCU_TARGET = atmega165
#MCU_TARGET = atmega165p
#MCU_TARGET = atmega168
#MCU_TARGET = atmega169
#MCU_TARGET = atmega169p
#MCU_TARGET = atmega2560
#MCU_TARGET = atmega2561
#MCU_TARGET = atmega32
#MCU_TARGET = atmega324p
#MCU_TARGET = atmega325
#MCU_TARGET = atmega3250
#MCU_TARGET = atmega329
#MCU_TARGET = atmega3290
#MCU_TARGET = atmega32u4
#MCU_TARGET = atmega48
#MCU_TARGET = atmega64
#MCU_TARGET = atmega640
#MCU_TARGET = atmega644
#MCU_TARGET = atmega644p
#MCU_TARGET = atmega645
#MCU_TARGET = atmega6450
#MCU_TARGET = atmega649
#MCU_TARGET = atmega6490
#MCU_TARGET = atmega8
#MCU_TARGET = atmega8515
#MCU_TARGET = atmega8535
#MCU_TARGET = atmega88
#MCU_TARGET = attiny2313
#MCU_TARGET = attiny24
#MCU_TARGET = attiny25
#MCU_TARGET = attiny26
#MCU_TARGET = attiny261
#MCU_TARGET = attiny44
#MCU_TARGET = attiny45
#MCU_TARGET = attiny461
#MCU_TARGET = attiny84
#MCU_TARGET = attiny85
#MCU_TARGET = attiny861
OPTIMIZE = -O3
DEFS = -DF_CPU=16000000L
......
......@@ -114,8 +114,11 @@ handle_cc(uint8_t cc, uint8_t val)
}
switch (cc) {
case CC_PITCH_DECAY:
pitch_envelope_set_decay(val);
case CC_PITCH_ENV_DECAY:
pitch_env_set_decay(val);
break;
case CC_PITCH_ENV_AMOUNT:
pitch_env_set_amount(val);
break;
}
......
......@@ -50,7 +50,7 @@ gate_on(uint8_t v)
gate_off(v);
_gate_retrig[v] = RETRIG_DELAY;
pitch_envelope_trigger(v);
pitch_env_trigger(v);
}
void
......
......@@ -15,7 +15,8 @@ enum _statuses {
enum _cc {
CC_MOD_WHEEL = 1,
CC_DETUNE = 14,
CC_PITCH_DECAY = 15,
CC_PITCH_ENV_DECAY = 15,
CC_PITCH_ENV_AMOUNT = 16,
CC_VIB_RATE = 76,
};
......
......@@ -50,11 +50,10 @@ mono_note_on(uint8_t n)
void
mono_set_pitch(void)
{
if (l_empty(notes)) {
return;
if (!l_empty(notes)) {
current_note = l_first(notes);
}
current_note = l_first(notes);
for_each_voice(v) {
int16_t detune = detune_factor[v] * detune_amount;
detune += vibrato_detune();
......@@ -118,7 +117,9 @@ mono_pitch_bend(uint16_t bend)
void
mono_update_clock(uint8_t delta)
{
(void) delta;
if (delta == 0) {
return;
}
mono_set_pitch();
}
......@@ -2,9 +2,13 @@
#include "voice.h"
#include "dac.h"
#include "safe_add.h"
#include <math.h>
static uint16_t _decay_counter[NUM_VOICES];
static uint8_t _decay;
static uint16_t _env_delta[NUM_VOICES];
static uint8_t _env_decay;
static uint8_t _env_amount;
enum { DECAY_SCALE = 10000 };
......@@ -14,6 +18,8 @@ pitch_init(void)
for_each_voice(v) {
_decay_counter[v] = 0;
}
_env_decay = 0;
_env_amount = 0;
}
void
......@@ -24,8 +30,9 @@ 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));
data = safe_add(data, _env_delta[voice]);
float bend_factor = ((int16_t) (bend & BEND_MAX) - BEND_CENTER) / ((float) BEND_MAX);
int16_t bend_data = dac_pitch_delta(4) * bend_factor;
......@@ -37,32 +44,48 @@ pitch_set_note(uint8_t voice, uint8_t note, int16_t detune, uint16_t bend)
void
pitch_update_clock(uint8_t delta)
{
if (delta == 0) {
return;
}
for_each_voice(v) {
if (_decay_counter[v] <= delta) {
_decay_counter[v] = 0;
_env_delta[v] = 0;
continue;
}
_decay_counter[v] = _decay_counter[v] - delta;
_env_delta[v] = -log((DECAY_SCALE - _decay_counter[v] + 1) / ((float) DECAY_SCALE)) * (_env_amount / 127.0) * dac_pitch_delta(5);
}
}
void
pitch_envelope_set_decay(uint8_t decay)
pitch_env_set_decay(uint8_t decay)
{
if (decay > 127) {
decay = 127;
}
_decay = decay;
_env_decay = decay;
}
void
pitch_env_set_amount(uint8_t amount)
{
if (amount > 127) {
amount = 127;
}
_env_amount = amount;
}
void
pitch_envelope_trigger(uint8_t voice)
pitch_env_trigger(uint8_t voice)
{
if (voice >= NUM_VOICES) {
return;
}
_decay_counter[voice] = (_decay/127.0) * DECAY_SCALE;
_decay_counter[voice] = (_env_decay/127.0) * DECAY_SCALE;
}
......@@ -6,8 +6,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);
void pitch_env_set_decay(uint8_t decay);
void pitch_env_set_amount(uint8_t amount);
void pitch_env_trigger(uint8_t voice);
void pitch_init(void);
enum {
......
......@@ -9,6 +9,7 @@
static uint8_t playing[NUM_VOICES];
static uint16_t current_bend;
static uint8_t current_note[NUM_VOICES];
INIT_LIST(avail, NUM_VOICES);
INIT_LIST(want_play, NUM_NOTES);
......@@ -22,6 +23,7 @@ poly2_init(void)
l_flush(avail);
for_each_voice(i) {
playing[i] = NUM_NOTES;
current_note[i] = NUM_NOTES;
l_push(avail, i);
}
......@@ -86,6 +88,7 @@ poly2_update_playing(void)
poly2_set_pitch(v, j);
gate_on(v);
playing[v] = j;
current_note[v] = j;
l_delete(avail, v);
}
}
......@@ -119,16 +122,18 @@ poly2_pitch_bend(uint16_t bend)
{
current_bend = bend;
for_each_voice(v) {
poly2_set_pitch(v, playing[v]);
poly2_set_pitch(v, current_note[v]);
}
}
void
poly2_update_clock(uint8_t delta)
{
(void) delta;
if (delta == 0) {
return;
}
for_each_voice(v) {
poly2_set_pitch(v, playing[v]);
poly2_set_pitch(v, current_note[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