|
|
@ -10,20 +10,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include "eeconfig.h"
|
|
|
|
#include "eeconfig.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
#include "vibrato_lut.h"
|
|
|
|
#include "vibrato_lut.h"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define PI 3.14159265
|
|
|
|
#define PI 3.14159265
|
|
|
|
|
|
|
|
|
|
|
|
#define CPU_PRESCALER 8
|
|
|
|
#define CPU_PRESCALER 8
|
|
|
|
|
|
|
|
|
|
|
|
// Largely untested PWM audio mode (doesn't sound as good)
|
|
|
|
|
|
|
|
// #define PWM_AUDIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
#include "wave.h"
|
|
|
|
#include "wave.h"
|
|
|
|
#define SAMPLE_DIVIDER 39
|
|
|
|
#define SAMPLE_DIVIDER 39
|
|
|
|
#define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
|
|
|
|
#define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
|
|
|
|
// Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
|
|
|
|
// Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
|
|
|
|
uint16_t place_int = 0;
|
|
|
|
|
|
|
|
bool repeat = true;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void delay_us(int count) {
|
|
|
|
void delay_us(int count) {
|
|
|
@ -34,25 +37,21 @@ void delay_us(int count) {
|
|
|
|
|
|
|
|
|
|
|
|
int voices = 0;
|
|
|
|
int voices = 0;
|
|
|
|
int voice_place = 0;
|
|
|
|
int voice_place = 0;
|
|
|
|
double frequency = 0;
|
|
|
|
float frequency = 0;
|
|
|
|
int volume = 0;
|
|
|
|
int volume = 0;
|
|
|
|
long position = 0;
|
|
|
|
long position = 0;
|
|
|
|
|
|
|
|
|
|
|
|
double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
bool sliding = false;
|
|
|
|
bool sliding = false;
|
|
|
|
|
|
|
|
|
|
|
|
int max = 0xFF;
|
|
|
|
int max = 0xFF;
|
|
|
|
float sum = 0;
|
|
|
|
float sum = 0;
|
|
|
|
int value = 128;
|
|
|
|
|
|
|
|
float place = 0;
|
|
|
|
float place = 0;
|
|
|
|
float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t place_int = 0;
|
|
|
|
|
|
|
|
bool repeat = true;
|
|
|
|
|
|
|
|
uint8_t * sample;
|
|
|
|
uint8_t * sample;
|
|
|
|
uint16_t sample_length = 0;
|
|
|
|
uint16_t sample_length = 0;
|
|
|
|
double freq = 0;
|
|
|
|
// float freq = 0;
|
|
|
|
|
|
|
|
|
|
|
|
bool notes = false;
|
|
|
|
bool notes = false;
|
|
|
|
bool note = false;
|
|
|
|
bool note = false;
|
|
|
@ -62,7 +61,7 @@ float note_tempo = TEMPO_DEFAULT;
|
|
|
|
float note_timbre = TIMBRE_DEFAULT;
|
|
|
|
float note_timbre = TIMBRE_DEFAULT;
|
|
|
|
uint16_t note_position = 0;
|
|
|
|
uint16_t note_position = 0;
|
|
|
|
float (* notes_pointer)[][2];
|
|
|
|
float (* notes_pointer)[][2];
|
|
|
|
uint8_t notes_count;
|
|
|
|
uint16_t notes_count;
|
|
|
|
bool notes_repeat;
|
|
|
|
bool notes_repeat;
|
|
|
|
float notes_rest;
|
|
|
|
float notes_rest;
|
|
|
|
bool note_resting = false;
|
|
|
|
bool note_resting = false;
|
|
|
@ -70,9 +69,11 @@ bool note_resting = false;
|
|
|
|
uint8_t current_note = 0;
|
|
|
|
uint8_t current_note = 0;
|
|
|
|
uint8_t rest_counter = 0;
|
|
|
|
uint8_t rest_counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
float vibrato_counter = 0;
|
|
|
|
float vibrato_counter = 0;
|
|
|
|
float vibrato_strength = .5;
|
|
|
|
float vibrato_strength = .5;
|
|
|
|
float vibrato_rate = 0.125;
|
|
|
|
float vibrato_rate = 0.125;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
float polyphony_rate = 0;
|
|
|
|
float polyphony_rate = 0;
|
|
|
|
|
|
|
|
|
|
|
@ -96,6 +97,7 @@ void audio_off(void) {
|
|
|
|
eeconfig_write_audio(audio_config.raw);
|
|
|
|
eeconfig_write_audio(audio_config.raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
// Vibrato rate functions
|
|
|
|
// Vibrato rate functions
|
|
|
|
|
|
|
|
|
|
|
|
void set_vibrato_rate(float rate) {
|
|
|
|
void set_vibrato_rate(float rate) {
|
|
|
@ -126,6 +128,8 @@ void decrease_vibrato_strength(float change) {
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Polyphony functions
|
|
|
|
// Polyphony functions
|
|
|
|
|
|
|
|
|
|
|
|
void set_polyphony_rate(float rate) {
|
|
|
|
void set_polyphony_rate(float rate) {
|
|
|
@ -209,6 +213,7 @@ void audio_init() {
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
inited = true;
|
|
|
|
inited = true;
|
|
|
|
|
|
|
|
_delay_ms(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void stop_all_notes() {
|
|
|
|
void stop_all_notes() {
|
|
|
@ -233,7 +238,7 @@ void stop_all_notes() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void stop_note(double freq) {
|
|
|
|
void stop_note(float freq) {
|
|
|
|
if (note) {
|
|
|
|
if (note) {
|
|
|
|
if (!inited) {
|
|
|
|
if (!inited) {
|
|
|
|
audio_init();
|
|
|
|
audio_init();
|
|
|
@ -274,6 +279,8 @@ void stop_note(double freq) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
|
|
|
|
|
|
|
|
float mod(float a, int b)
|
|
|
|
float mod(float a, int b)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
float r = fmod(a, b);
|
|
|
|
float r = fmod(a, b);
|
|
|
@ -290,6 +297,8 @@ float vibrato(float average_freq) {
|
|
|
|
return vibrated_freq;
|
|
|
|
return vibrated_freq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
ISR(TIMER3_COMPA_vect) {
|
|
|
|
ISR(TIMER3_COMPA_vect) {
|
|
|
|
if (note) {
|
|
|
|
if (note) {
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
@ -341,6 +350,7 @@ ISR(TIMER3_COMPA_vect) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
if (voices > 0) {
|
|
|
|
if (voices > 0) {
|
|
|
|
|
|
|
|
float freq;
|
|
|
|
if (polyphony_rate > 0) {
|
|
|
|
if (polyphony_rate > 0) {
|
|
|
|
if (voices > 1) {
|
|
|
|
if (voices > 1) {
|
|
|
|
voice_place %= voices;
|
|
|
|
voice_place %= voices;
|
|
|
@ -349,9 +359,13 @@ ISR(TIMER3_COMPA_vect) {
|
|
|
|
place = 0.0;
|
|
|
|
place = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
if (vibrato_strength > 0) {
|
|
|
|
if (vibrato_strength > 0) {
|
|
|
|
freq = vibrato(frequencies[voice_place]);
|
|
|
|
freq = vibrato(frequencies[voice_place]);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
#endif
|
|
|
|
freq = frequencies[voice_place];
|
|
|
|
freq = frequencies[voice_place];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -363,9 +377,14 @@ ISR(TIMER3_COMPA_vect) {
|
|
|
|
frequency = frequencies[voices - 1];
|
|
|
|
frequency = frequencies[voices - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
if (vibrato_strength > 0) {
|
|
|
|
if (vibrato_strength > 0) {
|
|
|
|
freq = vibrato(frequency);
|
|
|
|
freq = vibrato(frequency);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
#endif
|
|
|
|
freq = frequency;
|
|
|
|
freq = frequency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -398,9 +417,13 @@ ISR(TIMER3_COMPA_vect) {
|
|
|
|
if (note_frequency > 0) {
|
|
|
|
if (note_frequency > 0) {
|
|
|
|
float freq;
|
|
|
|
float freq;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VIBRATO_ENABLE
|
|
|
|
if (vibrato_strength > 0) {
|
|
|
|
if (vibrato_strength > 0) {
|
|
|
|
freq = vibrato(note_frequency);
|
|
|
|
freq = vibrato(note_frequency);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
#endif
|
|
|
|
freq = note_frequency;
|
|
|
|
freq = note_frequency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -461,13 +484,45 @@ ISR(TIMER3_COMPA_vect) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) {
|
|
|
|
void play_note(float freq, int vol) {
|
|
|
|
|
|
|
|
|
|
|
|
if (audio_config.enable) {
|
|
|
|
if (!inited) {
|
|
|
|
|
|
|
|
audio_init();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (audio_config.enable && voices < 8) {
|
|
|
|
TIMSK3 &= ~_BV(OCIE3A);
|
|
|
|
TIMSK3 &= ~_BV(OCIE3A);
|
|
|
|
|
|
|
|
// Cancel notes if notes are playing
|
|
|
|
|
|
|
|
if (notes)
|
|
|
|
|
|
|
|
stop_all_notes();
|
|
|
|
|
|
|
|
note = true;
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
|
|
|
|
freq = freq / SAMPLE_RATE;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (freq > 0) {
|
|
|
|
|
|
|
|
frequencies[voices] = freq;
|
|
|
|
|
|
|
|
volumes[voices] = vol;
|
|
|
|
|
|
|
|
voices++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
|
|
|
|
TIMSK3 |= _BV(OCIE3A);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
TIMSK3 |= _BV(OCIE3A);
|
|
|
|
|
|
|
|
TCCR3A |= _BV(COM3A1);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
|
|
|
|
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
if (!inited) {
|
|
|
|
audio_init();
|
|
|
|
audio_init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (audio_config.enable) {
|
|
|
|
|
|
|
|
TIMSK3 &= ~_BV(OCIE3A);
|
|
|
|
// Cancel note if a note is playing
|
|
|
|
// Cancel note if a note is playing
|
|
|
|
if (note)
|
|
|
|
if (note)
|
|
|
|
stop_all_notes();
|
|
|
|
stop_all_notes();
|
|
|
@ -500,57 +555,24 @@ if (audio_config.enable) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
void play_sample(uint8_t * s, uint16_t l, bool r) {
|
|
|
|
void play_sample(uint8_t * s, uint16_t l, bool r) {
|
|
|
|
|
|
|
|
|
|
|
|
if (audio_config.enable) {
|
|
|
|
|
|
|
|
TIMSK3 &= ~_BV(OCIE3A);
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
if (!inited) {
|
|
|
|
audio_init();
|
|
|
|
audio_init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (audio_config.enable) {
|
|
|
|
|
|
|
|
TIMSK3 &= ~_BV(OCIE3A);
|
|
|
|
stop_all_notes();
|
|
|
|
stop_all_notes();
|
|
|
|
place_int = 0;
|
|
|
|
place_int = 0;
|
|
|
|
sample = s;
|
|
|
|
sample = s;
|
|
|
|
sample_length = l;
|
|
|
|
sample_length = l;
|
|
|
|
repeat = r;
|
|
|
|
repeat = r;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
|
|
|
|
TIMSK3 |= _BV(OCIE3A);
|
|
|
|
TIMSK3 |= _BV(OCIE3A);
|
|
|
|
#else
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void play_note(double freq, int vol) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (audio_config.enable && voices < 8) {
|
|
|
|
|
|
|
|
TIMSK3 &= ~_BV(OCIE3A);
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
|
|
|
|
audio_init();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel notes if notes are playing
|
|
|
|
|
|
|
|
if (notes)
|
|
|
|
|
|
|
|
stop_all_notes();
|
|
|
|
|
|
|
|
note = true;
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
|
|
|
|
freq = freq / SAMPLE_RATE;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (freq > 0) {
|
|
|
|
|
|
|
|
frequencies[voices] = freq;
|
|
|
|
|
|
|
|
volumes[voices] = vol;
|
|
|
|
|
|
|
|
voices++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PWM_AUDIO
|
|
|
|
|
|
|
|
TIMSK3 |= _BV(OCIE3A);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
TIMSK3 |= _BV(OCIE3A);
|
|
|
|
|
|
|
|
TCCR3A |= _BV(COM3A1);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Override these functions in your keymap file to play different tunes on
|
|
|
|
// Override these functions in your keymap file to play different tunes on
|
|
|
|