aboutsummaryrefslogtreecommitdiff
path: root/quantum/audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/audio.c')
-rw-r--r--quantum/audio.c56
1 files changed, 43 insertions, 13 deletions
diff --git a/quantum/audio.c b/quantum/audio.c
index 119bd9229..9b9589f13 100644
--- a/quantum/audio.c
+++ b/quantum/audio.c
@@ -4,7 +4,7 @@
4#include <avr/pgmspace.h> 4#include <avr/pgmspace.h>
5#include <avr/interrupt.h> 5#include <avr/interrupt.h>
6#include <avr/io.h> 6#include <avr/io.h>
7 7#include "print.h"
8#include "audio.h" 8#include "audio.h"
9#include "keymap_common.h" 9#include "keymap_common.h"
10 10
@@ -57,9 +57,11 @@ bool notes = false;
57bool note = false; 57bool note = false;
58float note_frequency = 0; 58float note_frequency = 0;
59float note_length = 0; 59float note_length = 0;
60float note_tempo = TEMPO_DEFAULT;
61float note_timbre = TIMBRE_DEFAULT;
60uint16_t note_position = 0; 62uint16_t note_position = 0;
61float (* notes_pointer)[][2]; 63float (* notes_pointer)[][2];
62uint8_t notes_length; 64uint8_t notes_count;
63bool notes_repeat; 65bool notes_repeat;
64float notes_rest; 66float notes_rest;
65bool note_resting = false; 67bool note_resting = false;
@@ -255,7 +257,8 @@ ISR(TIMER3_COMPA_vect) {
255 place = 0.0; 257 place = 0.0;
256 } 258 }
257 ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period 259 ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period
258 OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period 260 OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
261 //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period
259 place++; 262 place++;
260 // if (duty_counter > (frequencies[voice_place] / 500)) { 263 // if (duty_counter > (frequencies[voice_place] / 500)) {
261 // duty_place = (duty_place % 3) + 1; 264 // duty_place = (duty_place % 3) + 1;
@@ -288,8 +291,7 @@ ISR(TIMER3_COMPA_vect) {
288 #else 291 #else
289 if (note_frequency > 0) { 292 if (note_frequency > 0) {
290 ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period 293 ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period
291 //OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period 294 OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
292 OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 2; // Set compare to half the period
293 } else { 295 } else {
294 ICR3 = 0; 296 ICR3 = 0;
295 OCR3A = 0; 297 OCR3A = 0;
@@ -305,7 +307,7 @@ ISR(TIMER3_COMPA_vect) {
305 end_of_note = (note_position >= (note_length * 0x7FF)); 307 end_of_note = (note_position >= (note_length * 0x7FF));
306 if (end_of_note) { 308 if (end_of_note) {
307 current_note++; 309 current_note++;
308 if (current_note >= notes_length) { 310 if (current_note >= notes_count) {
309 if (notes_repeat) { 311 if (notes_repeat) {
310 current_note = 0; 312 current_note = 0;
311 } else { 313 } else {
@@ -328,10 +330,10 @@ ISR(TIMER3_COMPA_vect) {
328 note_resting = false; 330 note_resting = false;
329 #ifdef PWM_AUDIO 331 #ifdef PWM_AUDIO
330 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; 332 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
331 note_length = (*notes_pointer)[current_note][1]; 333 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
332 #else 334 #else
333 note_frequency = (*notes_pointer)[current_note][0]; 335 note_frequency = (*notes_pointer)[current_note][0];
334 note_length = (*notes_pointer)[current_note][1] / 4; 336 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
335 #endif 337 #endif
336 } 338 }
337 note_position = 0; 339 note_position = 0;
@@ -345,7 +347,7 @@ ISR(TIMER3_COMPA_vect) {
345 } 347 }
346} 348}
347 349
348void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat, float n_rest) { 350void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) {
349 351
350if (audio_config.enable) { 352if (audio_config.enable) {
351 353
@@ -354,7 +356,7 @@ if (audio_config.enable) {
354 notes = true; 356 notes = true;
355 357
356 notes_pointer = np; 358 notes_pointer = np;
357 notes_length = n_length; 359 notes_count = n_count;
358 notes_repeat = n_repeat; 360 notes_repeat = n_repeat;
359 notes_rest = n_rest; 361 notes_rest = n_rest;
360 362
@@ -362,10 +364,10 @@ if (audio_config.enable) {
362 current_note = 0; 364 current_note = 0;
363 #ifdef PWM_AUDIO 365 #ifdef PWM_AUDIO
364 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; 366 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
365 note_length = (*notes_pointer)[current_note][1]; 367 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
366 #else 368 #else
367 note_frequency = (*notes_pointer)[current_note][0]; 369 note_frequency = (*notes_pointer)[current_note][0];
368 note_length = (*notes_pointer)[current_note][1] / 4; 370 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
369 #endif 371 #endif
370 note_position = 0; 372 note_position = 0;
371 373
@@ -439,4 +441,32 @@ if (audio_config.enable && voices < 8) {
439 441
440} 442}
441 443
442} \ No newline at end of file 444}
445
446void set_timbre(float timbre)
447{
448 note_timbre = timbre;
449}
450
451void set_tempo(float tempo)
452{
453 note_tempo = tempo;
454}
455
456void decrease_tempo(uint8_t tempo_change)
457{
458 note_tempo += (float) tempo_change;
459}
460
461void increase_tempo(uint8_t tempo_change)
462{
463 if (note_tempo - (float) tempo_change < 10)
464 {
465 note_tempo = 10;
466 }
467 else
468 {
469 note_tempo -= (float) tempo_change;
470 }
471}
472