#include "eeconfig.h"
+#include "vibrato_lut.h"
+
#define PI 3.14159265
#define CPU_PRESCALER 8
+// Largely untested PWM audio mode (doesn't sound as good)
// #define PWM_AUDIO
#ifdef PWM_AUDIO
uint8_t current_note = 0;
uint8_t rest_counter = 0;
-uint8_t vibrato_counter = 0;
-float vibrato_strength = 0;
+float vibrato_counter = 0;
+float vibrato_strength = .5;
+float vibrato_rate = 0.125;
-float polyphony_rate = 0;
+float polyphony_rate = .5;
audio_config_t audio_config;
eeconfig_write_audio(audio_config.raw);
}
+// Vibrato rate functions
+
+void set_vibrato_rate(float rate) {
+ vibrato_rate = rate;
+}
+
+void increase_vibrato_rate(float change) {
+ vibrato_rate *= change;
+}
+
+void decrease_vibrato_rate(float change) {
+ vibrato_rate /= change;
+}
+
+#ifdef VIBRATO_STRENGTH_ENABLE
+
+void set_vibrato_strength(float strength) {
+ vibrato_strength = strength;
+}
+
+void increase_vibrato_strength(float change) {
+ vibrato_strength *= change;
+}
+
+void decrease_vibrato_strength(float change) {
+ vibrato_strength /= change;
+}
+
+#endif
+
+// Polyphony functions
+
+void set_polyphony_rate(float rate) {
+ polyphony_rate = rate;
+}
+
+void enable_polyphony() {
+ polyphony_rate = 5;
+}
+
+void disable_polyphony() {
+ polyphony_rate = 0;
+}
+
+void increase_polyphony_rate(float change) {
+ polyphony_rate *= change;
+}
+
+void decrease_polyphony_rate(float change) {
+ polyphony_rate /= change;
+}
+
+// Timbre function
+
+void set_timbre(float timbre) {
+ note_timbre = timbre;
+}
+
+// Tempo functions
+
+void set_tempo(float tempo) {
+ note_tempo = tempo;
+}
+
+void decrease_tempo(uint8_t tempo_change) {
+ note_tempo += (float) tempo_change;
+}
+
+void increase_tempo(uint8_t tempo_change) {
+ if (note_tempo - (float) tempo_change < 10) {
+ note_tempo = 10;
+ } else {
+ note_tempo -= (float) tempo_change;
+ }
+}
void stop_all_notes() {
voices = 0;
void stop_note(double freq) {
if (note) {
+ cli();
#ifdef PWM_AUDIO
freq = freq / SAMPLE_RATE;
#endif
volumes[j] = volumes[j+1];
volumes[j+1] = 0;
}
+ break;
}
}
voices--;
volume = 0;
note = false;
}
+ sei();
}
}
return r < 0 ? r + b : r;
}
+float vibrato(float average_freq) {
+ #ifdef VIBRATO_STRENGTH_ENABLE
+ float vibrated_freq = average_freq * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength);
+ #else
+ float vibrated_freq = average_freq * VIBRATO_LUT[(int)vibrato_counter];
+ #endif
+ vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
+ return vibrated_freq;
+}
+
ISR(TIMER3_COMPA_vect) {
if (note) {
#ifdef PWM_AUDIO
}
#else
if (voices > 0) {
- if (false && polyphony_rate > 0) {
+ if (polyphony_rate > 0) {
if (voices > 1) {
voice_place %= voices;
- if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER / voices)) {
+ if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
voice_place = (voice_place + 1) % voices;
place = 0.0;
}
}
if (vibrato_strength > 0) {
- freq = frequencies[voice_place] * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength);
- vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH);
+ freq = vibrato(frequencies[voice_place]);
} else {
freq = frequencies[voice_place];
}
} else {
- if (frequency != 0) {
- if (frequency < frequencies[voices - 1]) {
- frequency = frequency * pow(2, 440/frequencies[voices - 1]/12/4);
- } else if (frequency > frequencies[voices - 1]) {
- frequency = frequency * pow(2, -440/frequencies[voices - 1]/12/4);
- }
+ if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
+ frequency = frequency * pow(2, 440/frequency/12/2);
+ } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
+ frequency = frequency * pow(2, -440/frequency/12/2);
} else {
frequency = frequencies[voices - 1];
}
- if (false && vibrato_strength > 0) {
- freq = frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength);
- vibrato_counter = mod((vibrato_counter + 1 + 440/frequencies[voices - 1]), VIBRATO_LUT_LENGTH);
+ if (vibrato_strength > 0) {
+ freq = vibrato(frequency);
} else {
freq = frequency;
}
if (note_frequency > 0) {
float freq;
- if (false && vibrato_strength > 0) {
- freq = note_frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength);
- vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH);
+ if (vibrato_strength > 0) {
+ freq = vibrato(note_frequency);
} else {
freq = note_frequency;
}
void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) {
if (audio_config.enable) {
-
+ cli();
// Cancel note if a note is playing
if (note)
stop_all_notes();
TIMSK3 |= _BV(OCIE3A);
TCCR3A |= _BV(COM3A1);
#endif
-
+ sei();
}
}
void play_note(double freq, int vol) {
if (audio_config.enable && voices < 8) {
-
+ cli();
// Cancel notes if notes are playing
if (notes)
stop_all_notes();
TIMSK3 |= _BV(OCIE3A);
TCCR3A |= _BV(COM3A1);
#endif
-
-}
-
-}
-
-void set_timbre(float timbre)
-{
- note_timbre = timbre;
+ sei();
}
-void set_tempo(float tempo)
-{
- note_tempo = tempo;
-}
-
-void decrease_tempo(uint8_t tempo_change)
-{
- note_tempo += (float) tempo_change;
-}
-
-void increase_tempo(uint8_t tempo_change)
-{
- if (note_tempo - (float) tempo_change < 10)
- {
- note_tempo = 10;
- }
- else
- {
- note_tempo -= (float) tempo_change;
- }
}
//------------------------------------------------------------------------------
#ifndef AUDIO_H
#define AUDIO_H
+// Enable vibrato strength/amplitude - slows down ISR too much
+// #define VIBRATO_STRENGTH_ENABLE
+
typedef union {
uint8_t raw;
struct {
void audio_on(void);
void audio_off(void);
+// Vibrato rate functions
+
+void set_vibrato_rate(float rate);
+void increase_vibrato_rate(float change);
+void decrease_vibrato_rate(float change);
+
+#ifdef VIBRATO_STRENGTH_ENABLE
+
+void set_vibrato_strength(float strength);
+void increase_vibrato_strength(float change);
+void decrease_vibrato_strength(float change);
+
+#endif
+
+// Polyphony functions
+
+void set_polyphony_rate(float rate);
+void enable_polyphony();
+void disable_polyphony();
+void increase_polyphony_rate(float change);
+void decrease_polyphony_rate(float change);
+
+void set_timbre(float timbre);
+void set_tempo(float tempo);
+
+void increase_tempo(uint8_t tempo_change);
+void decrease_tempo(uint8_t tempo_change);
+
void play_sample(uint8_t * s, uint16_t l, bool r);
void play_note(double freq, int vol);
void stop_note(double freq);
void init_notes(void);
void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest);
-void set_timbre(float timbre);
-void set_tempo(float tempo);
-void increase_tempo(uint8_t tempo_change);
-void decrease_tempo(uint8_t tempo_change);
-
#define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
void play_goodbye_tone(void);
void play_startup_tone(void);
-#define VIBRATO_LUT (float []) { \
-1.00090714186239, \
-1.00181152169061, \
-1.00270955652027, \
-1.00359767896099, \
-1.00447235162891, \
-1.00533008160601, \
-1.00616743486158, \
-1.00698105056935, \
-1.00776765525194, \
-1.00852407668313, \
-1.0092472574777, \
-1.00993426829815, \
-1.01058232060837, \
-1.01118877890462, \
-1.01175117235612, \
-1.01226720578933, \
-1.01273476995269, \
-1.01315195100182, \
-1.0135170391489, \
-1.01382853642434, \
-1.01408516350345, \
-1.01428586555648, \
-1.0144298170856, \
-1.0145164257189, \
-1.01454533493752, \
-1.0145164257189, \
-1.0144298170856, \
-1.01428586555648, \
-1.01408516350345, \
-1.01382853642434, \
-1.0135170391489, \
-1.01315195100182, \
-1.01273476995269, \
-1.01226720578933, \
-1.01175117235612, \
-1.01118877890462, \
-1.01058232060837, \
-1.00993426829815, \
-1.0092472574777, \
-1.00852407668313, \
-1.00776765525194, \
-1.00698105056935, \
-1.00616743486158, \
-1.00533008160601, \
-1.00447235162891, \
-1.00359767896099, \
-1.00270955652027, \
-1.00181152169061, \
-1.00090714186239, \
-1, \
-0.999093680298157, \
-0.998191753986265, \
-0.997297765337276, \
-0.996415217934032, \
-0.995547561242821, \
-0.99469817754036, \
-0.993870369236874, \
-0.993067346634376, \
-0.992292216155724, \
-0.991547969076588, \
-0.990837470789065, \
-0.990163450622494, \
-0.989528492243954, \
-0.988935024658062, \
-0.988385313823004, \
-0.98788145489731, \
-0.987425365129624, \
-0.987018777401739, \
-0.986663234433381, \
-0.986360083655655, \
-0.986110472758728, \
-0.985915345918143, \
-0.985775440703176, \
-0.985691285669809, \
-0.985663198640188, \
-0.985691285669809, \
-0.985775440703176, \
-0.985915345918143, \
-0.986110472758728, \
-0.986360083655655, \
-0.986663234433381, \
-0.987018777401739, \
-0.987425365129624, \
-0.98788145489731, \
-0.988385313823004, \
-0.988935024658062, \
-0.989528492243954, \
-0.990163450622494, \
-0.990837470789065, \
-0.991547969076588, \
-0.992292216155724, \
-0.993067346634376, \
-0.993870369236874, \
-0.99469817754036, \
-0.99554756124282, \
-0.996415217934032, \
-0.997297765337276, \
-0.998191753986265, \
-0.999093680298157, \
-1, \
-}
-#define VIBRATO_LUT_LENGTH NOTE_ARRAY_SIZE(VIBRATO_LUT)
-
#endif
\ No newline at end of file