upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

audio_pwm.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. #include <stdio.h>
  2. #include <string.h>
  3. //#include <math.h>
  4. #include <avr/pgmspace.h>
  5. #include <avr/interrupt.h>
  6. #include <avr/io.h>
  7. #include "print.h"
  8. #include "audio.h"
  9. #include "keymap_common.h"
  10. #include "eeconfig.h"
  11. #define PI 3.14159265
  12. #define CPU_PRESCALER 8
  13. // Timer Abstractions
  14. // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
  15. // Turn on/off 3A interputs, stopping/enabling the ISR calls
  16. #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
  17. #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
  18. // TCCR3A: Timer/Counter #3 Control Register
  19. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  20. #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
  21. #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
  22. #define NOTE_PERIOD ICR3
  23. #define NOTE_DUTY_CYCLE OCR3A
  24. #ifdef PWM_AUDIO
  25. #include "wave.h"
  26. #define SAMPLE_DIVIDER 39
  27. #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
  28. // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
  29. float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  30. uint16_t place_int = 0;
  31. bool repeat = true;
  32. #endif
  33. void delay_us(int count) {
  34. while(count--) {
  35. _delay_us(1);
  36. }
  37. }
  38. int voices = 0;
  39. int voice_place = 0;
  40. float frequency = 0;
  41. int volume = 0;
  42. long position = 0;
  43. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  44. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  45. bool sliding = false;
  46. float place = 0;
  47. uint8_t * sample;
  48. uint16_t sample_length = 0;
  49. // float freq = 0;
  50. bool playing_notes = false;
  51. bool playing_note = false;
  52. float note_frequency = 0;
  53. float note_length = 0;
  54. uint8_t note_tempo = TEMPO_DEFAULT;
  55. float note_timbre = TIMBRE_DEFAULT;
  56. uint16_t note_position = 0;
  57. float (* notes_pointer)[][2];
  58. uint16_t notes_count;
  59. bool notes_repeat;
  60. float notes_rest;
  61. bool note_resting = false;
  62. uint8_t current_note = 0;
  63. uint8_t rest_counter = 0;
  64. #ifdef VIBRATO_ENABLE
  65. float vibrato_counter = 0;
  66. float vibrato_strength = .5;
  67. float vibrato_rate = 0.125;
  68. #endif
  69. float polyphony_rate = 0;
  70. static bool audio_initialized = false;
  71. audio_config_t audio_config;
  72. uint16_t envelope_index = 0;
  73. void audio_init() {
  74. // Check EEPROM
  75. if (!eeconfig_is_enabled())
  76. {
  77. eeconfig_init();
  78. }
  79. audio_config.raw = eeconfig_read_audio();
  80. #ifdef PWM_AUDIO
  81. PLLFRQ = _BV(PDIV2);
  82. PLLCSR = _BV(PLLE);
  83. while(!(PLLCSR & _BV(PLOCK)));
  84. PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
  85. /* Init a fast PWM on Timer4 */
  86. TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
  87. TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
  88. OCR4A = 0;
  89. /* Enable the OC4A output */
  90. DDRC |= _BV(PORTC6);
  91. DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs
  92. TCCR3A = 0x0; // Options not needed
  93. TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
  94. OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
  95. #else
  96. // Set port PC6 (OC3A and /OC4A) as output
  97. DDRC |= _BV(PORTC6);
  98. DISABLE_AUDIO_COUNTER_3_ISR;
  99. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  100. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  101. // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
  102. // Clock Select (CS3n) = 0b010 = Clock / 8
  103. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  104. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  105. #endif
  106. audio_initialized = true;
  107. }
  108. void stop_all_notes() {
  109. if (!audio_initialized) {
  110. audio_init();
  111. }
  112. voices = 0;
  113. #ifdef PWM_AUDIO
  114. DISABLE_AUDIO_COUNTER_3_ISR;
  115. #else
  116. DISABLE_AUDIO_COUNTER_3_ISR;
  117. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  118. #endif
  119. playing_notes = false;
  120. playing_note = false;
  121. frequency = 0;
  122. volume = 0;
  123. for (uint8_t i = 0; i < 8; i++)
  124. {
  125. frequencies[i] = 0;
  126. volumes[i] = 0;
  127. }
  128. }
  129. void stop_note(float freq)
  130. {
  131. if (playing_note) {
  132. if (!audio_initialized) {
  133. audio_init();
  134. }
  135. #ifdef PWM_AUDIO
  136. freq = freq / SAMPLE_RATE;
  137. #endif
  138. for (int i = 7; i >= 0; i--) {
  139. if (frequencies[i] == freq) {
  140. frequencies[i] = 0;
  141. volumes[i] = 0;
  142. for (int j = i; (j < 7); j++) {
  143. frequencies[j] = frequencies[j+1];
  144. frequencies[j+1] = 0;
  145. volumes[j] = volumes[j+1];
  146. volumes[j+1] = 0;
  147. }
  148. break;
  149. }
  150. }
  151. voices--;
  152. if (voices < 0)
  153. voices = 0;
  154. if (voice_place >= voices) {
  155. voice_place = 0;
  156. }
  157. if (voices == 0) {
  158. #ifdef PWM_AUDIO
  159. DISABLE_AUDIO_COUNTER_3_ISR;
  160. #else
  161. DISABLE_AUDIO_COUNTER_3_ISR;
  162. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  163. #endif
  164. frequency = 0;
  165. volume = 0;
  166. playing_note = false;
  167. }
  168. }
  169. }
  170. #ifdef VIBRATO_ENABLE
  171. float mod(float a, int b)
  172. {
  173. float r = fmod(a, b);
  174. return r < 0 ? r + b : r;
  175. }
  176. float vibrato(float average_freq) {
  177. #ifdef VIBRATO_STRENGTH_ENABLE
  178. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  179. #else
  180. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  181. #endif
  182. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  183. return vibrated_freq;
  184. }
  185. #endif
  186. ISR(TIMER3_COMPA_vect)
  187. {
  188. if (playing_note) {
  189. #ifdef PWM_AUDIO
  190. if (voices == 1) {
  191. // SINE
  192. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
  193. // SQUARE
  194. // if (((int)place) >= 1024){
  195. // OCR4A = 0xFF >> 2;
  196. // } else {
  197. // OCR4A = 0x00;
  198. // }
  199. // SAWTOOTH
  200. // OCR4A = (int)place / 4;
  201. // TRIANGLE
  202. // if (((int)place) >= 1024) {
  203. // OCR4A = (int)place / 2;
  204. // } else {
  205. // OCR4A = 2048 - (int)place / 2;
  206. // }
  207. place += frequency;
  208. if (place >= SINE_LENGTH)
  209. place -= SINE_LENGTH;
  210. } else {
  211. int sum = 0;
  212. for (int i = 0; i < voices; i++) {
  213. // SINE
  214. sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
  215. // SQUARE
  216. // if (((int)places[i]) >= 1024){
  217. // sum += 0xFF >> 2;
  218. // } else {
  219. // sum += 0x00;
  220. // }
  221. places[i] += frequencies[i];
  222. if (places[i] >= SINE_LENGTH)
  223. places[i] -= SINE_LENGTH;
  224. }
  225. OCR4A = sum;
  226. }
  227. #else
  228. if (voices > 0) {
  229. float freq;
  230. if (polyphony_rate > 0) {
  231. if (voices > 1) {
  232. voice_place %= voices;
  233. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  234. voice_place = (voice_place + 1) % voices;
  235. place = 0.0;
  236. }
  237. }
  238. #ifdef VIBRATO_ENABLE
  239. if (vibrato_strength > 0) {
  240. freq = vibrato(frequencies[voice_place]);
  241. } else {
  242. #else
  243. {
  244. #endif
  245. freq = frequencies[voice_place];
  246. }
  247. } else {
  248. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  249. frequency = frequency * pow(2, 440/frequency/12/2);
  250. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  251. frequency = frequency * pow(2, -440/frequency/12/2);
  252. } else {
  253. frequency = frequencies[voices - 1];
  254. }
  255. #ifdef VIBRATO_ENABLE
  256. if (vibrato_strength > 0) {
  257. freq = vibrato(frequency);
  258. } else {
  259. #else
  260. {
  261. #endif
  262. freq = frequency;
  263. }
  264. }
  265. if (envelope_index < 65535) {
  266. envelope_index++;
  267. }
  268. freq = voice_envelope(freq);
  269. if (freq < 30.517578125)
  270. freq = 30.52;
  271. NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  272. NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  273. }
  274. #endif
  275. }
  276. // SAMPLE
  277. // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
  278. // place_int++;
  279. // if (place_int >= sample_length)
  280. // if (repeat)
  281. // place_int -= sample_length;
  282. // else
  283. // DISABLE_AUDIO_COUNTER_3_ISR;
  284. if (playing_notes) {
  285. #ifdef PWM_AUDIO
  286. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
  287. place += note_frequency;
  288. if (place >= SINE_LENGTH)
  289. place -= SINE_LENGTH;
  290. #else
  291. if (note_frequency > 0) {
  292. float freq;
  293. #ifdef VIBRATO_ENABLE
  294. if (vibrato_strength > 0) {
  295. freq = vibrato(note_frequency);
  296. } else {
  297. #else
  298. {
  299. #endif
  300. freq = note_frequency;
  301. }
  302. if (envelope_index < 65535) {
  303. envelope_index++;
  304. }
  305. freq = voice_envelope(freq);
  306. NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  307. NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  308. } else {
  309. NOTE_PERIOD = 0;
  310. NOTE_DUTY_CYCLE = 0;
  311. }
  312. #endif
  313. note_position++;
  314. bool end_of_note = false;
  315. if (NOTE_PERIOD > 0)
  316. end_of_note = (note_position >= (note_length / NOTE_PERIOD * 0xFFFF));
  317. else
  318. end_of_note = (note_position >= (note_length * 0x7FF));
  319. if (end_of_note) {
  320. current_note++;
  321. if (current_note >= notes_count) {
  322. if (notes_repeat) {
  323. current_note = 0;
  324. } else {
  325. #ifdef PWM_AUDIO
  326. DISABLE_AUDIO_COUNTER_3_ISR;
  327. #else
  328. DISABLE_AUDIO_COUNTER_3_ISR;
  329. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  330. #endif
  331. playing_notes = false;
  332. return;
  333. }
  334. }
  335. if (!note_resting && (notes_rest > 0)) {
  336. note_resting = true;
  337. note_frequency = 0;
  338. note_length = notes_rest;
  339. current_note--;
  340. } else {
  341. note_resting = false;
  342. #ifdef PWM_AUDIO
  343. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  344. note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
  345. #else
  346. envelope_index = 0;
  347. note_frequency = (*notes_pointer)[current_note][0];
  348. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  349. #endif
  350. }
  351. note_position = 0;
  352. }
  353. }
  354. if (!audio_config.enable) {
  355. playing_notes = false;
  356. playing_note = false;
  357. }
  358. }
  359. void play_note(float freq, int vol) {
  360. if (!audio_initialized) {
  361. audio_init();
  362. }
  363. if (audio_config.enable && voices < 8) {
  364. DISABLE_AUDIO_COUNTER_3_ISR;
  365. // Cancel notes if notes are playing
  366. if (playing_notes)
  367. stop_all_notes();
  368. playing_note = true;
  369. envelope_index = 0;
  370. #ifdef PWM_AUDIO
  371. freq = freq / SAMPLE_RATE;
  372. #endif
  373. if (freq > 0) {
  374. frequencies[voices] = freq;
  375. volumes[voices] = vol;
  376. voices++;
  377. }
  378. #ifdef PWM_AUDIO
  379. ENABLE_AUDIO_COUNTER_3_ISR;
  380. #else
  381. ENABLE_AUDIO_COUNTER_3_ISR;
  382. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  383. #endif
  384. }
  385. }
  386. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
  387. {
  388. if (!audio_initialized) {
  389. audio_init();
  390. }
  391. if (audio_config.enable) {
  392. DISABLE_AUDIO_COUNTER_3_ISR;
  393. // Cancel note if a note is playing
  394. if (playing_note)
  395. stop_all_notes();
  396. playing_notes = true;
  397. notes_pointer = np;
  398. notes_count = n_count;
  399. notes_repeat = n_repeat;
  400. notes_rest = n_rest;
  401. place = 0;
  402. current_note = 0;
  403. #ifdef PWM_AUDIO
  404. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  405. note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
  406. #else
  407. note_frequency = (*notes_pointer)[current_note][0];
  408. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  409. #endif
  410. note_position = 0;
  411. #ifdef PWM_AUDIO
  412. ENABLE_AUDIO_COUNTER_3_ISR;
  413. #else
  414. ENABLE_AUDIO_COUNTER_3_ISR;
  415. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  416. #endif
  417. }
  418. }
  419. #ifdef PWM_AUDIO
  420. void play_sample(uint8_t * s, uint16_t l, bool r) {
  421. if (!audio_initialized) {
  422. audio_init();
  423. }
  424. if (audio_config.enable) {
  425. DISABLE_AUDIO_COUNTER_3_ISR;
  426. stop_all_notes();
  427. place_int = 0;
  428. sample = s;
  429. sample_length = l;
  430. repeat = r;
  431. ENABLE_AUDIO_COUNTER_3_ISR;
  432. }
  433. }
  434. #endif
  435. void audio_toggle(void) {
  436. audio_config.enable ^= 1;
  437. eeconfig_update_audio(audio_config.raw);
  438. }
  439. void audio_on(void) {
  440. audio_config.enable = 1;
  441. eeconfig_update_audio(audio_config.raw);
  442. }
  443. void audio_off(void) {
  444. audio_config.enable = 0;
  445. eeconfig_update_audio(audio_config.raw);
  446. }
  447. #ifdef VIBRATO_ENABLE
  448. // Vibrato rate functions
  449. void set_vibrato_rate(float rate) {
  450. vibrato_rate = rate;
  451. }
  452. void increase_vibrato_rate(float change) {
  453. vibrato_rate *= change;
  454. }
  455. void decrease_vibrato_rate(float change) {
  456. vibrato_rate /= change;
  457. }
  458. #ifdef VIBRATO_STRENGTH_ENABLE
  459. void set_vibrato_strength(float strength) {
  460. vibrato_strength = strength;
  461. }
  462. void increase_vibrato_strength(float change) {
  463. vibrato_strength *= change;
  464. }
  465. void decrease_vibrato_strength(float change) {
  466. vibrato_strength /= change;
  467. }
  468. #endif /* VIBRATO_STRENGTH_ENABLE */
  469. #endif /* VIBRATO_ENABLE */
  470. // Polyphony functions
  471. void set_polyphony_rate(float rate) {
  472. polyphony_rate = rate;
  473. }
  474. void enable_polyphony() {
  475. polyphony_rate = 5;
  476. }
  477. void disable_polyphony() {
  478. polyphony_rate = 0;
  479. }
  480. void increase_polyphony_rate(float change) {
  481. polyphony_rate *= change;
  482. }
  483. void decrease_polyphony_rate(float change) {
  484. polyphony_rate /= change;
  485. }
  486. // Timbre function
  487. void set_timbre(float timbre) {
  488. note_timbre = timbre;
  489. }
  490. // Tempo functions
  491. void set_tempo(uint8_t tempo) {
  492. note_tempo = tempo;
  493. }
  494. void decrease_tempo(uint8_t tempo_change) {
  495. note_tempo += tempo_change;
  496. }
  497. void increase_tempo(uint8_t tempo_change) {
  498. if (note_tempo - tempo_change < 10) {
  499. note_tempo = 10;
  500. } else {
  501. note_tempo -= tempo_change;
  502. }
  503. }
  504. //------------------------------------------------------------------------------
  505. // Override these functions in your keymap file to play different tunes on
  506. // startup and bootloader jump
  507. __attribute__ ((weak))
  508. void play_startup_tone()
  509. {
  510. }
  511. __attribute__ ((weak))
  512. void play_goodbye_tone()
  513. {
  514. }
  515. //------------------------------------------------------------------------------