audio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /* Copyright 2016 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. //#include <math.h>
  19. #include <avr/pgmspace.h>
  20. #include <avr/interrupt.h>
  21. #include <avr/io.h>
  22. #include "print.h"
  23. #include "audio.h"
  24. #include "keymap.h"
  25. #include "eeconfig.h"
  26. #define CPU_PRESCALER 8
  27. // -----------------------------------------------------------------------------
  28. // Timer Abstractions
  29. // -----------------------------------------------------------------------------
  30. // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
  31. // Turn on/off 3A interputs, stopping/enabling the ISR calls
  32. #ifdef C6_AUDIO
  33. #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
  34. #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
  35. #endif
  36. #ifdef B5_AUDIO
  37. #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
  38. #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
  39. #endif
  40. // TCCR3A: Timer/Counter #3 Control Register
  41. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  42. #ifdef C6_AUDIO
  43. #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
  44. #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
  45. #endif
  46. #ifdef B5_AUDIO
  47. #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
  48. #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
  49. #endif
  50. // Fast PWM Mode Controls
  51. #ifdef C6_AUDIO
  52. #define TIMER_3_PERIOD ICR3
  53. #define TIMER_3_DUTY_CYCLE OCR3A
  54. #endif
  55. #ifdef B5_AUDIO
  56. #define TIMER_1_PERIOD ICR1
  57. #define TIMER_1_DUTY_CYCLE OCR1A
  58. #endif
  59. // -----------------------------------------------------------------------------
  60. int voices = 0;
  61. int voice_place = 0;
  62. float frequency = 0;
  63. int volume = 0;
  64. long position = 0;
  65. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  66. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  67. bool sliding = false;
  68. float place = 0;
  69. uint8_t * sample;
  70. uint16_t sample_length = 0;
  71. bool playing_notes = false;
  72. bool playing_note = false;
  73. float note_frequency = 0;
  74. float note_length = 0;
  75. uint8_t note_tempo = TEMPO_DEFAULT;
  76. float note_timbre = TIMBRE_DEFAULT;
  77. uint16_t note_position = 0;
  78. float (* notes_pointer)[][2];
  79. uint16_t notes_count;
  80. bool notes_repeat;
  81. float notes_rest;
  82. bool note_resting = false;
  83. uint8_t current_note = 0;
  84. uint8_t rest_counter = 0;
  85. #ifdef VIBRATO_ENABLE
  86. float vibrato_counter = 0;
  87. float vibrato_strength = .5;
  88. float vibrato_rate = 0.125;
  89. #endif
  90. float polyphony_rate = 0;
  91. static bool audio_initialized = false;
  92. audio_config_t audio_config;
  93. uint16_t envelope_index = 0;
  94. bool glissando = true;
  95. void audio_init()
  96. {
  97. // Check EEPROM
  98. if (!eeconfig_is_enabled())
  99. {
  100. eeconfig_init();
  101. }
  102. audio_config.raw = eeconfig_read_audio();
  103. // Set port PC6 (OC3A and /OC4A) as output
  104. #ifdef C6_AUDIO
  105. DDRC |= _BV(PORTC6);
  106. #else
  107. DDRC |= _BV(PORTC6);
  108. PORTC &= ~_BV(PORTC6);
  109. #endif
  110. #ifdef B5_AUDIO
  111. DDRB |= _BV(PORTB5);
  112. #else
  113. DDRB |= _BV(PORTB5);
  114. PORTB &= ~_BV(PORTB5);
  115. #endif
  116. #ifdef C6_AUDIO
  117. DISABLE_AUDIO_COUNTER_3_ISR;
  118. #endif
  119. #ifdef B5_AUDIO
  120. DISABLE_AUDIO_COUNTER_1_ISR;
  121. #endif
  122. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  123. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  124. // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
  125. // Clock Select (CS3n) = 0b010 = Clock / 8
  126. #ifdef C6_AUDIO
  127. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  128. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  129. #endif
  130. #ifdef B5_AUDIO
  131. TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
  132. TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
  133. #endif
  134. audio_initialized = true;
  135. }
  136. void stop_all_notes()
  137. {
  138. dprintf("audio stop all notes");
  139. if (!audio_initialized) {
  140. audio_init();
  141. }
  142. voices = 0;
  143. #ifdef C6_AUDIO
  144. DISABLE_AUDIO_COUNTER_3_ISR;
  145. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  146. #endif
  147. #ifdef B5_AUDIO
  148. DISABLE_AUDIO_COUNTER_1_ISR;
  149. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  150. #endif
  151. playing_notes = false;
  152. playing_note = false;
  153. frequency = 0;
  154. volume = 0;
  155. for (uint8_t i = 0; i < 8; i++)
  156. {
  157. frequencies[i] = 0;
  158. volumes[i] = 0;
  159. }
  160. }
  161. void stop_note(float freq)
  162. {
  163. dprintf("audio stop note freq=%d", (int)freq);
  164. if (playing_note) {
  165. if (!audio_initialized) {
  166. audio_init();
  167. }
  168. for (int i = 7; i >= 0; i--) {
  169. if (frequencies[i] == freq) {
  170. frequencies[i] = 0;
  171. volumes[i] = 0;
  172. for (int j = i; (j < 7); j++) {
  173. frequencies[j] = frequencies[j+1];
  174. frequencies[j+1] = 0;
  175. volumes[j] = volumes[j+1];
  176. volumes[j+1] = 0;
  177. }
  178. break;
  179. }
  180. }
  181. voices--;
  182. if (voices < 0)
  183. voices = 0;
  184. if (voice_place >= voices) {
  185. voice_place = 0;
  186. }
  187. if (voices == 0) {
  188. #ifdef C6_AUDIO
  189. DISABLE_AUDIO_COUNTER_3_ISR;
  190. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  191. #endif
  192. #ifdef B5_AUDIO
  193. DISABLE_AUDIO_COUNTER_1_ISR;
  194. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  195. #endif
  196. frequency = 0;
  197. volume = 0;
  198. playing_note = false;
  199. }
  200. }
  201. }
  202. #ifdef VIBRATO_ENABLE
  203. float mod(float a, int b)
  204. {
  205. float r = fmod(a, b);
  206. return r < 0 ? r + b : r;
  207. }
  208. float vibrato(float average_freq) {
  209. #ifdef VIBRATO_STRENGTH_ENABLE
  210. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  211. #else
  212. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  213. #endif
  214. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  215. return vibrated_freq;
  216. }
  217. #endif
  218. #ifdef C6_AUDIO
  219. ISR(TIMER3_COMPA_vect)
  220. {
  221. float freq;
  222. if (playing_note) {
  223. if (voices > 0) {
  224. if (polyphony_rate > 0) {
  225. if (voices > 1) {
  226. voice_place %= voices;
  227. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  228. voice_place = (voice_place + 1) % voices;
  229. place = 0.0;
  230. }
  231. }
  232. #ifdef VIBRATO_ENABLE
  233. if (vibrato_strength > 0) {
  234. freq = vibrato(frequencies[voice_place]);
  235. } else {
  236. freq = frequencies[voice_place];
  237. }
  238. #else
  239. freq = frequencies[voice_place];
  240. #endif
  241. } else {
  242. if (glissando) {
  243. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  244. frequency = frequency * pow(2, 440/frequency/12/2);
  245. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  246. frequency = frequency * pow(2, -440/frequency/12/2);
  247. } else {
  248. frequency = frequencies[voices - 1];
  249. }
  250. } else {
  251. frequency = frequencies[voices - 1];
  252. }
  253. #ifdef VIBRATO_ENABLE
  254. if (vibrato_strength > 0) {
  255. freq = vibrato(frequency);
  256. } else {
  257. freq = frequency;
  258. }
  259. #else
  260. freq = frequency;
  261. #endif
  262. }
  263. if (envelope_index < 65535) {
  264. envelope_index++;
  265. }
  266. freq = voice_envelope(freq);
  267. if (freq < 30.517578125) {
  268. freq = 30.52;
  269. }
  270. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  271. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  272. }
  273. }
  274. if (playing_notes) {
  275. if (note_frequency > 0) {
  276. #ifdef VIBRATO_ENABLE
  277. if (vibrato_strength > 0) {
  278. freq = vibrato(note_frequency);
  279. } else {
  280. freq = note_frequency;
  281. }
  282. #else
  283. freq = note_frequency;
  284. #endif
  285. if (envelope_index < 65535) {
  286. envelope_index++;
  287. }
  288. freq = voice_envelope(freq);
  289. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  290. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  291. } else {
  292. TIMER_3_PERIOD = 0;
  293. TIMER_3_DUTY_CYCLE = 0;
  294. }
  295. note_position++;
  296. bool end_of_note = false;
  297. if (TIMER_3_PERIOD > 0) {
  298. end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
  299. } else {
  300. end_of_note = (note_position >= (note_length * 0x7FF));
  301. }
  302. if (end_of_note) {
  303. current_note++;
  304. if (current_note >= notes_count) {
  305. if (notes_repeat) {
  306. current_note = 0;
  307. } else {
  308. DISABLE_AUDIO_COUNTER_3_ISR;
  309. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  310. playing_notes = false;
  311. return;
  312. }
  313. }
  314. if (!note_resting && (notes_rest > 0)) {
  315. note_resting = true;
  316. note_frequency = 0;
  317. note_length = notes_rest;
  318. current_note--;
  319. } else {
  320. note_resting = false;
  321. envelope_index = 0;
  322. note_frequency = (*notes_pointer)[current_note][0];
  323. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  324. }
  325. note_position = 0;
  326. }
  327. }
  328. if (!audio_config.enable) {
  329. playing_notes = false;
  330. playing_note = false;
  331. }
  332. }
  333. #endif
  334. #ifdef B5_AUDIO
  335. ISR(TIMER1_COMPA_vect)
  336. {
  337. float freq;
  338. if (playing_note) {
  339. if (voices > 0) {
  340. if (polyphony_rate > 0) {
  341. if (voices > 1) {
  342. voice_place %= voices;
  343. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  344. voice_place = (voice_place + 1) % voices;
  345. place = 0.0;
  346. }
  347. }
  348. #ifdef VIBRATO_ENABLE
  349. if (vibrato_strength > 0) {
  350. freq = vibrato(frequencies[voice_place]);
  351. } else {
  352. freq = frequencies[voice_place];
  353. }
  354. #else
  355. freq = frequencies[voice_place];
  356. #endif
  357. } else {
  358. if (glissando) {
  359. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  360. frequency = frequency * pow(2, 440/frequency/12/2);
  361. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  362. frequency = frequency * pow(2, -440/frequency/12/2);
  363. } else {
  364. frequency = frequencies[voices - 1];
  365. }
  366. } else {
  367. frequency = frequencies[voices - 1];
  368. }
  369. #ifdef VIBRATO_ENABLE
  370. if (vibrato_strength > 0) {
  371. freq = vibrato(frequency);
  372. } else {
  373. freq = frequency;
  374. }
  375. #else
  376. freq = frequency;
  377. #endif
  378. }
  379. if (envelope_index < 65535) {
  380. envelope_index++;
  381. }
  382. freq = voice_envelope(freq);
  383. if (freq < 30.517578125) {
  384. freq = 30.52;
  385. }
  386. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  387. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  388. }
  389. }
  390. if (playing_notes) {
  391. if (note_frequency > 0) {
  392. #ifdef VIBRATO_ENABLE
  393. if (vibrato_strength > 0) {
  394. freq = vibrato(note_frequency);
  395. } else {
  396. freq = note_frequency;
  397. }
  398. #else
  399. freq = note_frequency;
  400. #endif
  401. if (envelope_index < 65535) {
  402. envelope_index++;
  403. }
  404. freq = voice_envelope(freq);
  405. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  406. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  407. } else {
  408. TIMER_1_PERIOD = 0;
  409. TIMER_1_DUTY_CYCLE = 0;
  410. }
  411. note_position++;
  412. bool end_of_note = false;
  413. if (TIMER_1_PERIOD > 0) {
  414. end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF));
  415. } else {
  416. end_of_note = (note_position >= (note_length * 0x7FF));
  417. }
  418. if (end_of_note) {
  419. current_note++;
  420. if (current_note >= notes_count) {
  421. if (notes_repeat) {
  422. current_note = 0;
  423. } else {
  424. DISABLE_AUDIO_COUNTER_1_ISR;
  425. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  426. playing_notes = false;
  427. return;
  428. }
  429. }
  430. if (!note_resting && (notes_rest > 0)) {
  431. note_resting = true;
  432. note_frequency = 0;
  433. note_length = notes_rest;
  434. current_note--;
  435. } else {
  436. note_resting = false;
  437. envelope_index = 0;
  438. note_frequency = (*notes_pointer)[current_note][0];
  439. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  440. }
  441. note_position = 0;
  442. }
  443. }
  444. if (!audio_config.enable) {
  445. playing_notes = false;
  446. playing_note = false;
  447. }
  448. }
  449. #endif
  450. void play_note(float freq, int vol) {
  451. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  452. if (!audio_initialized) {
  453. audio_init();
  454. }
  455. if (audio_config.enable && voices < 8) {
  456. #ifdef C6_AUDIO
  457. DISABLE_AUDIO_COUNTER_3_ISR;
  458. #endif
  459. #ifdef B5_AUDIO
  460. DISABLE_AUDIO_COUNTER_1_ISR;
  461. #endif
  462. // Cancel notes if notes are playing
  463. if (playing_notes)
  464. stop_all_notes();
  465. playing_note = true;
  466. envelope_index = 0;
  467. if (freq > 0) {
  468. frequencies[voices] = freq;
  469. volumes[voices] = vol;
  470. voices++;
  471. }
  472. #ifdef C6_AUDIO
  473. ENABLE_AUDIO_COUNTER_3_ISR;
  474. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  475. #endif
  476. #ifdef B5_AUDIO
  477. ENABLE_AUDIO_COUNTER_1_ISR;
  478. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  479. #endif
  480. }
  481. }
  482. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
  483. {
  484. if (!audio_initialized) {
  485. audio_init();
  486. }
  487. if (audio_config.enable) {
  488. #ifdef C6_AUDIO
  489. DISABLE_AUDIO_COUNTER_3_ISR;
  490. #endif
  491. #ifdef B5_AUDIO
  492. DISABLE_AUDIO_COUNTER_1_ISR;
  493. #endif
  494. // Cancel note if a note is playing
  495. if (playing_note)
  496. stop_all_notes();
  497. playing_notes = true;
  498. notes_pointer = np;
  499. notes_count = n_count;
  500. notes_repeat = n_repeat;
  501. notes_rest = n_rest;
  502. place = 0;
  503. current_note = 0;
  504. note_frequency = (*notes_pointer)[current_note][0];
  505. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  506. note_position = 0;
  507. #ifdef C6_AUDIO
  508. ENABLE_AUDIO_COUNTER_3_ISR;
  509. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  510. #endif
  511. #ifdef B5_AUDIO
  512. ENABLE_AUDIO_COUNTER_1_ISR;
  513. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  514. #endif
  515. }
  516. }
  517. bool is_playing_notes(void) {
  518. return playing_notes;
  519. }
  520. bool is_audio_on(void) {
  521. return (audio_config.enable != 0);
  522. }
  523. void audio_toggle(void) {
  524. audio_config.enable ^= 1;
  525. eeconfig_update_audio(audio_config.raw);
  526. if (audio_config.enable)
  527. audio_on_user();
  528. }
  529. void audio_on(void) {
  530. audio_config.enable = 1;
  531. eeconfig_update_audio(audio_config.raw);
  532. audio_on_user();
  533. }
  534. void audio_off(void) {
  535. audio_config.enable = 0;
  536. eeconfig_update_audio(audio_config.raw);
  537. }
  538. #ifdef VIBRATO_ENABLE
  539. // Vibrato rate functions
  540. void set_vibrato_rate(float rate) {
  541. vibrato_rate = rate;
  542. }
  543. void increase_vibrato_rate(float change) {
  544. vibrato_rate *= change;
  545. }
  546. void decrease_vibrato_rate(float change) {
  547. vibrato_rate /= change;
  548. }
  549. #ifdef VIBRATO_STRENGTH_ENABLE
  550. void set_vibrato_strength(float strength) {
  551. vibrato_strength = strength;
  552. }
  553. void increase_vibrato_strength(float change) {
  554. vibrato_strength *= change;
  555. }
  556. void decrease_vibrato_strength(float change) {
  557. vibrato_strength /= change;
  558. }
  559. #endif /* VIBRATO_STRENGTH_ENABLE */
  560. #endif /* VIBRATO_ENABLE */
  561. // Polyphony functions
  562. void set_polyphony_rate(float rate) {
  563. polyphony_rate = rate;
  564. }
  565. void enable_polyphony() {
  566. polyphony_rate = 5;
  567. }
  568. void disable_polyphony() {
  569. polyphony_rate = 0;
  570. }
  571. void increase_polyphony_rate(float change) {
  572. polyphony_rate *= change;
  573. }
  574. void decrease_polyphony_rate(float change) {
  575. polyphony_rate /= change;
  576. }
  577. // Timbre function
  578. void set_timbre(float timbre) {
  579. note_timbre = timbre;
  580. }
  581. // Tempo functions
  582. void set_tempo(uint8_t tempo) {
  583. note_tempo = tempo;
  584. }
  585. void decrease_tempo(uint8_t tempo_change) {
  586. note_tempo += tempo_change;
  587. }
  588. void increase_tempo(uint8_t tempo_change) {
  589. if (note_tempo - tempo_change < 10) {
  590. note_tempo = 10;
  591. } else {
  592. note_tempo -= tempo_change;
  593. }
  594. }