double frequency = 0;
int volume = 0;
long position = 0;
-int duty_place = 1;
-int duty_counter = 0;
double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
bool repeat = true;
uint8_t * sample;
uint16_t sample_length = 0;
-
+double freq = 0;
bool notes = false;
bool note = false;
uint8_t current_note = 0;
uint8_t rest_counter = 0;
+uint8_t vibrato_counter = 0;
+float vibrato_strength = 0;
+
+float polyphony_rate = 0;
+
audio_config_t audio_config;
voices--;
if (voices < 0)
voices = 0;
+ if (voice_place >= voices) {
+ voice_place = 0;
+ }
if (voices == 0) {
#ifdef PWM_AUDIO
TIMSK3 &= ~_BV(OCIE3A);
frequency = 0;
volume = 0;
note = false;
- } else {
- double freq = frequencies[voices - 1];
- int vol = volumes[voices - 1];
- double starting_f = frequency;
- if (frequency < freq) {
- sliding = true;
- for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
- frequency = f;
- }
- sliding = false;
- } else if (frequency > freq) {
- sliding = true;
- for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
- frequency = f;
- }
- sliding = false;
- }
- frequency = freq;
- volume = vol;
}
}
}
#endif
}
+float mod(float a, int b)
+{
+ float r = fmod(a, b);
+ return r < 0 ? r + b : r;
+}
ISR(TIMER3_COMPA_vect) {
if (note) {
OCR4A = sum;
}
#else
- if (frequency > 0) {
- // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
- // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
- voice_place %= voices;
- if (place > (frequencies[voice_place] / 50)) {
- voice_place = (voice_place + 1) % voices;
- place = 0.0;
+ if (voices > 0) {
+ if (false && polyphony_rate > 0) {
+ if (voices > 1) {
+ voice_place %= voices;
+ if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER / voices)) {
+ 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);
+ } 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);
+ }
+ } 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);
+ } else {
+ freq = frequency;
+ }
}
- ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period
- OCR3A = (int)((((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
- //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period
- place++;
- // if (duty_counter > (frequencies[voice_place] / 500)) {
- // duty_place = (duty_place % 3) + 1;
- // duty_counter = 0;
- // }
- // duty_counter++;
+ ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
+ OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
}
#endif
}
place -= SINE_LENGTH;
#else
if (note_frequency > 0) {
- ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period
- OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
+ 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);
+ } else {
+ freq = note_frequency;
+ }
+
+ ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
+ OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
} else {
ICR3 = 0;
OCR3A = 0;
freq = freq / SAMPLE_RATE;
#endif
if (freq > 0) {
- if (frequency != 0) {
- double starting_f = frequency;
- if (frequency < freq) {
- for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
- frequency = f;
- }
- } else if (frequency > freq) {
- for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
- frequency = f;
- }
- }
- }
- frequency = freq;
- volume = vol;
-
- frequencies[voices] = frequency;
- volumes[voices] = volume;
+ frequencies[voices] = freq;
+ volumes[voices] = vol;
voices++;
}