update music functionality

audio_out
Jack Humbert 7 years ago
parent 8582faab6f
commit 7923376de1

@ -39,6 +39,7 @@
/* override number of MIDI tone keycodes (each octave adds 12 keycodes and allocates 12 bytes) */ /* override number of MIDI tone keycodes (each octave adds 12 keycodes and allocates 12 bytes) */
//#define MIDI_TONE_KEYCODE_OCTAVES 2 //#define MIDI_TONE_KEYCODE_OCTAVES 2
#define C6_AUDIO
#define B7_AUDIO #define B7_AUDIO
#undef BACKLIGHT_PIN #undef BACKLIGHT_PIN

@ -127,7 +127,7 @@ bool playing_note = false;
float note_frequency = 0; float note_frequency = 0;
float note_length = 0; float note_length = 0;
uint8_t note_tempo = TEMPO_DEFAULT; uint8_t note_tempo = TEMPO_DEFAULT;
float note_timbre = TIMBRE_DEFAULT; float note_timbre[NUMBER_OF_TIMERS] = {TIMBRE_DEFAULT};
uint16_t note_position = 0; uint16_t note_position = 0;
float (* notes_pointer)[][2]; float (* notes_pointer)[][2];
uint16_t notes_count; uint16_t notes_count;
@ -149,8 +149,8 @@ static bool audio_initialized = false;
audio_config_t audio_config; audio_config_t audio_config;
uint16_t envelope_index = 0; uint16_t envelope_index[NUMBER_OF_TIMERS] = {0};
bool glissando = true; bool glissando[NUMBER_OF_TIMERS] = {true};
#ifndef STARTUP_SONG #ifndef STARTUP_SONG
#define STARTUP_SONG SONG(STARTUP_SOUND) #define STARTUP_SONG SONG(STARTUP_SOUND)
@ -211,7 +211,7 @@ void audio_init()
DISABLE_AUDIO_COUNTER_3_ISR; DISABLE_AUDIO_COUNTER_3_ISR;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR; DISABLE_AUDIO_COUNTER_1_ISR;
#endif #endif
@ -223,14 +223,17 @@ void audio_init()
#ifdef C6_AUDIO #ifdef C6_AUDIO
TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre[TIMER_3_INDEX]);
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10); TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10); TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER)); TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre); TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre[TIMER_1_INDEX]);
#endif #endif
audio_initialized = true; audio_initialized = true;
@ -257,7 +260,7 @@ void stop_all_notes()
DISABLE_AUDIO_COUNTER_3_OUTPUT; DISABLE_AUDIO_COUNTER_3_OUTPUT;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR; DISABLE_AUDIO_COUNTER_1_ISR;
DISABLE_AUDIO_COUNTER_1_OUTPUT; DISABLE_AUDIO_COUNTER_1_OUTPUT;
#endif #endif
@ -302,12 +305,18 @@ void stop_note(float freq)
if (voice_place >= voices) { if (voice_place >= voices) {
voice_place = 0; voice_place = 0;
} }
if (voices == 1) {
#if defined(C6_AUDIO) && defined(B_AUDIO)
DISABLE_AUDIO_COUNTER_1_ISR;
DISABLE_AUDIO_COUNTER_1_OUTPUT;
#endif
}
if (voices == 0) { if (voices == 0) {
#ifdef C6_AUDIO #ifdef C6_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR; DISABLE_AUDIO_COUNTER_3_ISR;
DISABLE_AUDIO_COUNTER_3_OUTPUT; DISABLE_AUDIO_COUNTER_3_OUTPUT;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR; DISABLE_AUDIO_COUNTER_1_ISR;
DISABLE_AUDIO_COUNTER_1_OUTPUT; DISABLE_AUDIO_COUNTER_1_OUTPUT;
#endif #endif
@ -347,11 +356,11 @@ ISR(TIMER3_COMPA_vect)
if (playing_note) { if (playing_note) {
if (voices > 0) { if (voices > 0) {
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
float freq_alt = 0; float freq_alt = 0;
if (voices > 1) { if (voices > 1) {
if (polyphony_rate == 0) { if (polyphony_rate == 0) {
if (glissando) { if (glissando[TIMER_1_INDEX] && NUMBER_OF_TIMERS == 1) {
if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) { if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2); frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
} else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) { } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
@ -374,18 +383,18 @@ ISR(TIMER3_COMPA_vect)
#endif #endif
} }
if (envelope_index < 65535) { if (envelope_index[TIMER_1_INDEX] < 65535) {
envelope_index++; envelope_index[TIMER_1_INDEX]++;
} }
freq_alt = voice_envelope(freq_alt); freq_alt = voice_envelope(freq_alt,TIMER_1_INDEX);
if (freq_alt < 30.517578125) { if (freq_alt < 30.517578125) {
freq_alt = 30.52; freq_alt = 30.52;
} }
TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER)); TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre); TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre[TIMER_1_INDEX]);
} }
#endif #endif
@ -408,7 +417,7 @@ ISR(TIMER3_COMPA_vect)
freq = frequencies[voice_place]; freq = frequencies[voice_place];
#endif #endif
} else { } else {
if (glissando) { if (glissando[TIMER_3_INDEX] && NUMBER_OF_TIMERS == 1) {
if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { 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); 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)) { } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
@ -431,18 +440,18 @@ ISR(TIMER3_COMPA_vect)
#endif #endif
} }
if (envelope_index < 65535) { if (envelope_index[TIMER_3_INDEX] < 65535) {
envelope_index++; envelope_index[TIMER_3_INDEX]++;
} }
freq = voice_envelope(freq); freq = voice_envelope(freq, TIMER_3_INDEX);
if (freq < 30.517578125) { if (freq < 30.517578125) {
freq = 30.52; freq = 30.52;
} }
TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre[TIMER_3_INDEX]);
} }
} }
@ -458,13 +467,13 @@ ISR(TIMER3_COMPA_vect)
freq = note_frequency; freq = note_frequency;
#endif #endif
if (envelope_index < 65535) { if (envelope_index[TIMER_3_INDEX] < 65535) {
envelope_index++; envelope_index[TIMER_3_INDEX]++;
} }
freq = voice_envelope(freq); freq = voice_envelope(freq, TIMER_3_INDEX);
TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre[TIMER_3_INDEX]);
} else { } else {
TIMER_3_PERIOD = 0; TIMER_3_PERIOD = 0;
TIMER_3_DUTY_CYCLE = 0; TIMER_3_DUTY_CYCLE = 0;
@ -505,7 +514,7 @@ ISR(TIMER3_COMPA_vect)
} }
} else { } else {
note_resting = false; note_resting = false;
envelope_index = 0; envelope_index[TIMER_3_INDEX] = 0;
note_frequency = (*notes_pointer)[current_note][0]; note_frequency = (*notes_pointer)[current_note][0];
note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
} }
@ -521,10 +530,16 @@ ISR(TIMER3_COMPA_vect)
} }
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
#ifdef B5_AUDIO
ISR(TIMER1_COMPA_vect) ISR(TIMER1_COMPA_vect)
#elif defined(B6_AUDIO)
ISR(TIMER1_COMPB_vect)
#elif defined(B7_AUDIO)
ISR(TIMER1_COMPC_vect)
#endif
{ {
#if (defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO)) && !defined(C6_AUDIO) #if defined(B_AUDIO) && !defined(C6_AUDIO)
float freq = 0; float freq = 0;
if (playing_note) { if (playing_note) {
@ -548,7 +563,7 @@ ISR(TIMER1_COMPA_vect)
freq = frequencies[voice_place]; freq = frequencies[voice_place];
#endif #endif
} else { } else {
if (glissando) { if (glissando[TIMER_1_INDEX] && NUMBER_OF_TIMERS == 1) {
if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { 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); 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)) { } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
@ -571,18 +586,18 @@ ISR(TIMER1_COMPA_vect)
#endif #endif
} }
if (envelope_index < 65535) { if (envelope_index[TIMER_1_INDEX] < 65535) {
envelope_index++; envelope_index[TIMER_1_INDEX]++;
} }
freq = voice_envelope(freq); freq = voice_envelope(freq, TIMER_1_INDEX);
if (freq < 30.517578125) { if (freq < 30.517578125) {
freq = 30.52; freq = 30.52;
} }
TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre[TIMER_1_INDEX]);
} }
} }
@ -598,13 +613,13 @@ ISR(TIMER1_COMPA_vect)
freq = note_frequency; freq = note_frequency;
#endif #endif
if (envelope_index < 65535) { if (envelope_index[TIMER_1_INDEX] < 65535) {
envelope_index++; envelope_index[TIMER_1_INDEX]++;
} }
freq = voice_envelope(freq); freq = voice_envelope(freq, TIMER_1_INDEX);
TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER)); TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre[TIMER_1_INDEX]);
} else { } else {
TIMER_1_PERIOD = 0; TIMER_1_PERIOD = 0;
TIMER_1_DUTY_CYCLE = 0; TIMER_1_DUTY_CYCLE = 0;
@ -645,7 +660,7 @@ ISR(TIMER1_COMPA_vect)
} }
} else { } else {
note_resting = false; note_resting = false;
envelope_index = 0; envelope_index[TIMER_1_INDEX] = 0;
note_frequency = (*notes_pointer)[current_note][0]; note_frequency = (*notes_pointer)[current_note][0];
note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
} }
@ -674,7 +689,7 @@ void play_note(float freq, int vol) {
#ifdef C6_AUDIO #ifdef C6_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR; DISABLE_AUDIO_COUNTER_3_ISR;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR; DISABLE_AUDIO_COUNTER_1_ISR;
#endif #endif
@ -684,7 +699,6 @@ void play_note(float freq, int vol) {
playing_note = true; playing_note = true;
envelope_index = 0;
if (freq > 0) { if (freq > 0) {
frequencies[voices] = freq; frequencies[voices] = freq;
@ -696,16 +710,23 @@ void play_note(float freq, int vol) {
ENABLE_AUDIO_COUNTER_3_ISR; ENABLE_AUDIO_COUNTER_3_ISR;
ENABLE_AUDIO_COUNTER_3_OUTPUT; ENABLE_AUDIO_COUNTER_3_OUTPUT;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO)
#ifdef B_AUDIO
#ifdef C6_AUDIO #ifdef C6_AUDIO
if (voices > 1) { if (voices > 1) {
envelope_index[TIMER_3_INDEX] = 0;
ENABLE_AUDIO_COUNTER_1_ISR; ENABLE_AUDIO_COUNTER_1_ISR;
ENABLE_AUDIO_COUNTER_1_OUTPUT; ENABLE_AUDIO_COUNTER_1_OUTPUT;
} else {
envelope_index[TIMER_3_INDEX] = 0;
} }
#else #else
envelope_index[TIMER_1_INDEX] = 0;
ENABLE_AUDIO_COUNTER_1_ISR; ENABLE_AUDIO_COUNTER_1_ISR;
ENABLE_AUDIO_COUNTER_1_OUTPUT; ENABLE_AUDIO_COUNTER_1_OUTPUT;
#endif #endif
#else
envelope_index[TIMER_3_INDEX] = 0;
#endif #endif
} }
@ -723,7 +744,7 @@ void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
#ifdef C6_AUDIO #ifdef C6_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR; DISABLE_AUDIO_COUNTER_3_ISR;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR; DISABLE_AUDIO_COUNTER_1_ISR;
#endif #endif
@ -749,7 +770,7 @@ void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
ENABLE_AUDIO_COUNTER_3_ISR; ENABLE_AUDIO_COUNTER_3_ISR;
ENABLE_AUDIO_COUNTER_3_OUTPUT; ENABLE_AUDIO_COUNTER_3_OUTPUT;
#endif #endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO) #ifdef B_AUDIO
#ifndef C6_AUDIO #ifndef C6_AUDIO
ENABLE_AUDIO_COUNTER_1_ISR; ENABLE_AUDIO_COUNTER_1_ISR;
ENABLE_AUDIO_COUNTER_1_OUTPUT; ENABLE_AUDIO_COUNTER_1_OUTPUT;
@ -847,8 +868,8 @@ void decrease_polyphony_rate(float change) {
// Timbre function // Timbre function
void set_timbre(float timbre) { void set_timbre(float timbre, uint8_t timer_index) {
note_timbre = timbre; note_timbre[timer_index] = timbre;
} }
// Tempo functions // Tempo functions

@ -36,6 +36,27 @@
// Enable vibrato strength/amplitude - slows down ISR too much // Enable vibrato strength/amplitude - slows down ISR too much
// #define VIBRATO_STRENGTH_ENABLE // #define VIBRATO_STRENGTH_ENABLE
#ifdef B_AUDIO
#error Please define B5_AUDIO, B6_AUDIO, or B7_AUDIO instead
#endif
#if defined(B5_AUDIO) || defined(B6_AUDIO) || defined(B7_AUDIO)
#define B_AUDIO
#endif
#if defined(C6_AUDIO) && defined (B_AUDIO)
#define NUMBER_OF_TIMERS 2
#elif defined(C6_AUDIO)
#define NUMBER_OF_TIMERS 1
#elif defined(B_AUDIO)
#define NUMBER_OF_TIMERS 1
#else
#define NUMBER_OF_TIMERS 0
#endif
#define TIMER_1_INDEX 0
#define TIMER_3_INDEX 1
typedef union { typedef union {
uint8_t raw; uint8_t raw;
struct { struct {
@ -75,7 +96,7 @@ void disable_polyphony(void);
void increase_polyphony_rate(float change); void increase_polyphony_rate(float change);
void decrease_polyphony_rate(float change); void decrease_polyphony_rate(float change);
void set_timbre(float timbre); void set_timbre(float timbre, uint8_t timer_index);
void set_tempo(uint8_t tempo); void set_tempo(uint8_t tempo);
void increase_tempo(uint8_t tempo_change); void increase_tempo(uint8_t tempo_change);

@ -18,73 +18,91 @@
#include "stdlib.h" #include "stdlib.h"
// these are imported from audio.c // these are imported from audio.c
extern uint16_t envelope_index; extern uint16_t envelope_index[NUMBER_OF_TIMERS];
extern float note_timbre; extern float note_timbre[NUMBER_OF_TIMERS];
extern float polyphony_rate; extern float polyphony_rate;
extern bool glissando; extern bool glissando[NUMBER_OF_TIMERS];
voice_type voice = default_voice; voice_type voice[NUMBER_OF_TIMERS] = {default_voice};
void set_voice(voice_type v) { void set_all_voices(voice_type v) {
voice = v; for (uint8_t i = 0; i < NUMBER_OF_TIMERS; i++) {
voice[i] = v;
}
}
void all_voices_iterate(void) {
for (uint8_t i = 0; i < NUMBER_OF_TIMERS; i++) {
voice[i] = (voice[i] + 1) % number_of_voices;
}
}
void all_voices_deiterate(void) {
for (uint8_t i = 0; i < NUMBER_OF_TIMERS; i++) {
voice[i] = (voice[i] - 1 + number_of_voices) % number_of_voices;
}
} }
void voice_iterate() { void set_voice(voice_type v, uint8_t timer_index) {
voice = (voice + 1) % number_of_voices; voice[timer_index] = v;
} }
void voice_deiterate() { void voice_iterate(uint8_t timer_index) {
voice = (voice - 1 + number_of_voices) % number_of_voices; voice[timer_index] = (voice[timer_index] + 1) % number_of_voices;
} }
float voice_envelope(float frequency) { void voice_deiterate(uint8_t timer_index) {
// envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz voice[timer_index] = (voice[timer_index] - 1 + number_of_voices) % number_of_voices;
}
float voice_envelope(float frequency, uint8_t timer_index) {
// envelope_index[timer_index] ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz
__attribute__ ((unused)) __attribute__ ((unused))
uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency)); uint16_t compensated_index = (uint16_t)((float)envelope_index[timer_index] * (880.0 / frequency));
switch (voice) { switch (voice[timer_index]) {
case default_voice: case default_voice:
glissando = false; glissando[timer_index] = false;
note_timbre = TIMBRE_50; note_timbre[timer_index] = TIMBRE_50;
polyphony_rate = 0; polyphony_rate = 0;
break; break;
#ifdef AUDIO_VOICES #ifdef AUDIO_VOICES
case something: case something:
glissando = false; glissando[timer_index] = false;
polyphony_rate = 0; polyphony_rate = 0;
switch (compensated_index) { switch (compensated_index) {
case 0 ... 9: case 0 ... 9:
note_timbre = TIMBRE_12; note_timbre[timer_index] = TIMBRE_12;
break; break;
case 10 ... 19: case 10 ... 19:
note_timbre = TIMBRE_25; note_timbre[timer_index] = TIMBRE_25;
break; break;
case 20 ... 200: case 20 ... 200:
note_timbre = .125 + .125; note_timbre[timer_index] = .125 + .125;
break; break;
default: default:
note_timbre = .125; note_timbre[timer_index] = .125;
break; break;
} }
break; break;
case drums: case drums:
glissando = false; glissando[timer_index] = false;
polyphony_rate = 0; polyphony_rate = 0;
// switch (compensated_index) { // switch (compensated_index) {
// case 0 ... 10: // case 0 ... 10:
// note_timbre = 0.5; // note_timbre[timer_index] = 0.5;
// break; // break;
// case 11 ... 20: // case 11 ... 20:
// note_timbre = 0.5 * (21 - compensated_index) / 10; // note_timbre[timer_index] = 0.5 * (21 - compensated_index) / 10;
// break; // break;
// default: // default:
// note_timbre = 0; // note_timbre[timer_index] = 0;
// break; // break;
// } // }
// frequency = (rand() % (int)(frequency * 1.2 - frequency)) + (frequency * 0.8); // frequency = (rand() % (int)(frequency * 1.2 - frequency)) + (frequency * 0.8);
@ -95,15 +113,15 @@ float voice_envelope(float frequency) {
// Bass drum: 60 - 100 Hz // Bass drum: 60 - 100 Hz
frequency = (rand() % (int)(40)) + 60; frequency = (rand() % (int)(40)) + 60;
switch (envelope_index) { switch (envelope_index[timer_index]) {
case 0 ... 10: case 0 ... 10:
note_timbre = 0.5; note_timbre[timer_index] = 0.5;
break; break;
case 11 ... 20: case 11 ... 20:
note_timbre = 0.5 * (21 - envelope_index) / 10; note_timbre[timer_index] = 0.5 * (21 - envelope_index[timer_index]) / 10;
break; break;
default: default:
note_timbre = 0; note_timbre[timer_index] = 0;
break; break;
} }
@ -112,15 +130,15 @@ float voice_envelope(float frequency) {
// Snare drum: 1 - 2 KHz // Snare drum: 1 - 2 KHz
frequency = (rand() % (int)(1000)) + 1000; frequency = (rand() % (int)(1000)) + 1000;
switch (envelope_index) { switch (envelope_index[timer_index]) {
case 0 ... 5: case 0 ... 5:
note_timbre = 0.5; note_timbre[timer_index] = 0.5;
break; break;
case 6 ... 20: case 6 ... 20:
note_timbre = 0.5 * (21 - envelope_index) / 15; note_timbre[timer_index] = 0.5 * (21 - envelope_index[timer_index]) / 15;
break; break;
default: default:
note_timbre = 0; note_timbre[timer_index] = 0;
break; break;
} }
@ -128,15 +146,15 @@ float voice_envelope(float frequency) {
// Closed Hi-hat: 3 - 5 KHz // Closed Hi-hat: 3 - 5 KHz
frequency = (rand() % (int)(2000)) + 3000; frequency = (rand() % (int)(2000)) + 3000;
switch (envelope_index) { switch (envelope_index[timer_index]) {
case 0 ... 15: case 0 ... 15:
note_timbre = 0.5; note_timbre[timer_index] = 0.5;
break; break;
case 16 ... 20: case 16 ... 20:
note_timbre = 0.5 * (21 - envelope_index) / 5; note_timbre[timer_index] = 0.5 * (21 - envelope_index[timer_index]) / 5;
break; break;
default: default:
note_timbre = 0; note_timbre[timer_index] = 0;
break; break;
} }
@ -144,96 +162,96 @@ float voice_envelope(float frequency) {
// Open Hi-hat: 3 - 5 KHz // Open Hi-hat: 3 - 5 KHz
frequency = (rand() % (int)(2000)) + 3000; frequency = (rand() % (int)(2000)) + 3000;
switch (envelope_index) { switch (envelope_index[timer_index]) {
case 0 ... 35: case 0 ... 35:
note_timbre = 0.5; note_timbre[timer_index] = 0.5;
break; break;
case 36 ... 50: case 36 ... 50:
note_timbre = 0.5 * (51 - envelope_index) / 15; note_timbre[timer_index] = 0.5 * (51 - envelope_index[timer_index]) / 15;
break; break;
default: default:
note_timbre = 0; note_timbre[timer_index] = 0;
break; break;
} }
} }
break; break;
case butts_fader: case butts_fader:
glissando = true; glissando[timer_index] = true;
polyphony_rate = 0; polyphony_rate = 0;
switch (compensated_index) { switch (compensated_index) {
case 0 ... 9: case 0 ... 9:
frequency = frequency / 4; frequency = frequency / 4;
note_timbre = TIMBRE_12; note_timbre[timer_index] = TIMBRE_12;
break; break;
case 10 ... 19: case 10 ... 19:
frequency = frequency / 2; frequency = frequency / 2;
note_timbre = TIMBRE_12; note_timbre[timer_index] = TIMBRE_12;
break; break;
case 20 ... 200: case 20 ... 200:
note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125; note_timbre[timer_index] = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125;
break; break;
default: default:
note_timbre = 0; note_timbre[timer_index] = 0;
break; break;
} }
break; break;
// case octave_crunch: case octave_crunch:
// polyphony_rate = 0; polyphony_rate = 0;
// switch (compensated_index) { switch (compensated_index) {
// case 0 ... 9: case 0 ... 9:
// case 20 ... 24: case 20 ... 24:
// case 30 ... 32: case 30 ... 32:
// frequency = frequency / 2; frequency = frequency / 2;
// note_timbre = TIMBRE_12; note_timbre[timer_index] = TIMBRE_12;
// break; break;
// case 10 ... 19: case 10 ... 19:
// case 25 ... 29: case 25 ... 29:
// case 33 ... 35: case 33 ... 35:
// frequency = frequency * 2; frequency = frequency * 2;
// note_timbre = TIMBRE_12; note_timbre[timer_index] = TIMBRE_12;
// break; break;
// default: default:
// note_timbre = TIMBRE_12; note_timbre[timer_index] = TIMBRE_12;
// break; break;
// } }
// break; break;
case duty_osc: case duty_osc:
// This slows the loop down a substantial amount, so higher notes may freeze // This slows the loop down a substantial amount, so higher notes may freeze
glissando = true; glissando[timer_index] = true;
polyphony_rate = 0; polyphony_rate = 0;
switch (compensated_index) { switch (compensated_index) {
default: default:
#define OCS_SPEED 10 #define OCS_SPEED 10
#define OCS_AMP .25 #define OCS_AMP .25
// sine wave is slow // sine wave is slow
// note_timbre = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5; // note_timbre[timer_index] = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5;
// triangle wave is a bit faster // triangle wave is a bit faster
note_timbre = (float)abs((compensated_index*OCS_SPEED % 3000) - 1500) * ( OCS_AMP / 1500 ) + (1 - OCS_AMP) / 2; note_timbre[timer_index] = (float)abs((compensated_index*OCS_SPEED % 3000) - 1500) * ( OCS_AMP / 1500 ) + (1 - OCS_AMP) / 2;
break; break;
} }
break; break;
case duty_octave_down: case duty_octave_down:
glissando = true; glissando[timer_index] = true;
polyphony_rate = 0; polyphony_rate = 0;
note_timbre = (envelope_index % 2) * .125 + .375 * 2; note_timbre[timer_index] = (envelope_index[timer_index] % 2) * .125 + .375 * 2;
if ((envelope_index % 4) == 0) if ((envelope_index[timer_index] % 4) == 0)
note_timbre = 0.5; note_timbre[timer_index] = 0.5;
if ((envelope_index % 8) == 0) if ((envelope_index[timer_index] % 8) == 0)
note_timbre = 0; note_timbre[timer_index] = 0;
break; break;
case delayed_vibrato: case delayed_vibrato:
glissando = true; glissando[timer_index] = true;
polyphony_rate = 0; polyphony_rate = 0;
note_timbre = TIMBRE_50; note_timbre[timer_index] = TIMBRE_50;
#define VOICE_VIBRATO_DELAY 150 #define VOICE_VIBRATO_DELAY 150
#define VOICE_VIBRATO_SPEED 50 #define VOICE_VIBRATO_SPEED 50
switch (compensated_index) { switch (compensated_index) {
@ -246,10 +264,10 @@ float voice_envelope(float frequency) {
break; break;
// case delayed_vibrato_octave: // case delayed_vibrato_octave:
// polyphony_rate = 0; // polyphony_rate = 0;
// if ((envelope_index % 2) == 1) { // if ((envelope_index[timer_index] % 2) == 1) {
// note_timbre = 0.55; // note_timbre[timer_index] = 0.55;
// } else { // } else {
// note_timbre = 0.45; // note_timbre[timer_index] = 0.45;
// } // }
// #define VOICE_VIBRATO_DELAY 150 // #define VOICE_VIBRATO_DELAY 150
// #define VOICE_VIBRATO_SPEED 50 // #define VOICE_VIBRATO_SPEED 50
@ -262,28 +280,28 @@ float voice_envelope(float frequency) {
// } // }
// break; // break;
// case duty_fifth_down: // case duty_fifth_down:
// note_timbre = 0.5; // note_timbre[timer_index] = 0.5;
// if ((envelope_index % 3) == 0) // if ((envelope_index[timer_index] % 3) == 0)
// note_timbre = 0.75; // note_timbre[timer_index] = 0.75;
// break; // break;
// case duty_fourth_down: // case duty_fourth_down:
// note_timbre = 0.0; // note_timbre[timer_index] = 0.0;
// if ((envelope_index % 12) == 0) // if ((envelope_index[timer_index] % 12) == 0)
// note_timbre = 0.75; // note_timbre[timer_index] = 0.75;
// if (((envelope_index % 12) % 4) != 1) // if (((envelope_index[timer_index] % 12) % 4) != 1)
// note_timbre = 0.75; // note_timbre[timer_index] = 0.75;
// break; // break;
// case duty_third_down: // case duty_third_down:
// note_timbre = 0.5; // note_timbre[timer_index] = 0.5;
// if ((envelope_index % 5) == 0) // if ((envelope_index[timer_index] % 5) == 0)
// note_timbre = 0.75; // note_timbre[timer_index] = 0.75;
// break; // break;
// case duty_fifth_third_down: // case duty_fifth_third_down:
// note_timbre = 0.5; // note_timbre[timer_index] = 0.5;
// if ((envelope_index % 5) == 0) // if ((envelope_index[timer_index] % 5) == 0)
// note_timbre = 0.75; // note_timbre[timer_index] = 0.75;
// if ((envelope_index % 3) == 0) // if ((envelope_index[timer_index] % 3) == 0)
// note_timbre = 0.25; // note_timbre[timer_index] = 0.25;
// break; // break;
#endif #endif

@ -24,7 +24,7 @@
#ifndef VOICES_H #ifndef VOICES_H
#define VOICES_H #define VOICES_H
float voice_envelope(float frequency); float voice_envelope(float frequency, uint8_t timer_index);
typedef enum { typedef enum {
default_voice, default_voice,
@ -45,8 +45,12 @@ typedef enum {
number_of_voices // important that this is last number_of_voices // important that this is last
} voice_type; } voice_type;
void set_voice(voice_type v); void set_all_voices(voice_type v);
void voice_iterate(void); void all_voices_iterate(void);
void voice_deiterate(void); void all_voices_deiterate(void);
void set_voice(voice_type v, uint8_t timer_index);
void voice_iterate(uint8_t timer_index);
void voice_deiterate(uint8_t timer_index);
#endif #endif

@ -38,13 +38,13 @@ bool process_audio(uint16_t keycode, keyrecord_t *record) {
} }
if (keycode == MUV_IN && record->event.pressed) { if (keycode == MUV_IN && record->event.pressed) {
voice_iterate(); all_voices_iterate();
PLAY_SONG(voice_change_song); PLAY_SONG(voice_change_song);
return false; return false;
} }
if (keycode == MUV_DE && record->event.pressed) { if (keycode == MUV_DE && record->event.pressed) {
voice_deiterate(); all_voices_deiterate();
PLAY_SONG(voice_change_song); PLAY_SONG(voice_change_song);
return false; return false;
} }

@ -28,7 +28,7 @@ bool music_activated = false;
bool midi_activated = false; bool midi_activated = false;
uint8_t music_starting_note = 0x0C; uint8_t music_starting_note = 0x0C;
int music_offset = 7; int music_offset = 7;
uint8_t music_mode = MUSIC_MODE_CHROMATIC; uint8_t music_mode = MUSIC_MODE_MAJOR;
// music sequencer // music sequencer
static bool music_sequence_recording = false; static bool music_sequence_recording = false;

Loading…
Cancel
Save