audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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.h"
  10. #include "eeconfig.h"
  11. #define CPU_PRESCALER 8
  12. // -----------------------------------------------------------------------------
  13. // Timer Abstractions
  14. // -----------------------------------------------------------------------------
  15. // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
  16. // Turn on/off 3A interputs, stopping/enabling the ISR calls
  17. #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
  18. #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
  19. // TCCR3A: Timer/Counter #3 Control Register
  20. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  21. #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
  22. #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
  23. // Fast PWM Mode Controls
  24. #define TIMER_3_PERIOD ICR3
  25. #define TIMER_3_DUTY_CYCLE OCR3A
  26. // -----------------------------------------------------------------------------
  27. int voices = 0;
  28. int voice_place = 0;
  29. float frequency = 0;
  30. int volume = 0;
  31. long position = 0;
  32. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  33. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  34. bool sliding = false;
  35. float place = 0;
  36. uint8_t * sample;
  37. uint16_t sample_length = 0;
  38. bool playing_notes = false;
  39. bool playing_note = false;
  40. float note_frequency = 0;
  41. float note_length = 0;
  42. uint8_t note_tempo = TEMPO_DEFAULT;
  43. float note_timbre = TIMBRE_DEFAULT;
  44. uint16_t note_position = 0;
  45. float (* notes_pointer)[][2];
  46. uint16_t notes_count;
  47. bool notes_repeat;
  48. float notes_rest;
  49. bool note_resting = false;
  50. uint8_t current_note = 0;
  51. uint8_t rest_counter = 0;
  52. #ifdef VIBRATO_ENABLE
  53. float vibrato_counter = 0;
  54. float vibrato_strength = .5;
  55. float vibrato_rate = 0.125;
  56. #endif
  57. float polyphony_rate = 0;
  58. static bool audio_initialized = false;
  59. audio_config_t audio_config;
  60. uint16_t envelope_index = 0;
  61. bool glissando = true;
  62. void audio_init()
  63. {
  64. // Check EEPROM
  65. if (!eeconfig_is_enabled())
  66. {
  67. eeconfig_init();
  68. }
  69. audio_config.raw = eeconfig_read_audio();
  70. // Set port PC6 (OC3A and /OC4A) as output
  71. DDRC |= _BV(PORTC6);
  72. DISABLE_AUDIO_COUNTER_3_ISR;
  73. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  74. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  75. // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
  76. // Clock Select (CS3n) = 0b010 = Clock / 8
  77. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  78. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  79. audio_initialized = true;
  80. }
  81. void stop_all_notes()
  82. {
  83. if (!audio_initialized) {
  84. audio_init();
  85. }
  86. voices = 0;
  87. DISABLE_AUDIO_COUNTER_3_ISR;
  88. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  89. playing_notes = false;
  90. playing_note = false;
  91. frequency = 0;
  92. volume = 0;
  93. for (uint8_t i = 0; i < 8; i++)
  94. {
  95. frequencies[i] = 0;
  96. volumes[i] = 0;
  97. }
  98. }
  99. void stop_note(float freq)
  100. {
  101. if (playing_note) {
  102. if (!audio_initialized) {
  103. audio_init();
  104. }
  105. for (int i = 7; i >= 0; i--) {
  106. if (frequencies[i] == freq) {
  107. frequencies[i] = 0;
  108. volumes[i] = 0;
  109. for (int j = i; (j < 7); j++) {
  110. frequencies[j] = frequencies[j+1];
  111. frequencies[j+1] = 0;
  112. volumes[j] = volumes[j+1];
  113. volumes[j+1] = 0;
  114. }
  115. break;
  116. }
  117. }
  118. voices--;
  119. if (voices < 0)
  120. voices = 0;
  121. if (voice_place >= voices) {
  122. voice_place = 0;
  123. }
  124. if (voices == 0) {
  125. DISABLE_AUDIO_COUNTER_3_ISR;
  126. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  127. frequency = 0;
  128. volume = 0;
  129. playing_note = false;
  130. }
  131. }
  132. }
  133. #ifdef VIBRATO_ENABLE
  134. float mod(float a, int b)
  135. {
  136. float r = fmod(a, b);
  137. return r < 0 ? r + b : r;
  138. }
  139. float vibrato(float average_freq) {
  140. #ifdef VIBRATO_STRENGTH_ENABLE
  141. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  142. #else
  143. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  144. #endif
  145. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  146. return vibrated_freq;
  147. }
  148. #endif
  149. ISR(TIMER3_COMPA_vect)
  150. {
  151. float freq;
  152. if (playing_note) {
  153. if (voices > 0) {
  154. if (polyphony_rate > 0) {
  155. if (voices > 1) {
  156. voice_place %= voices;
  157. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  158. voice_place = (voice_place + 1) % voices;
  159. place = 0.0;
  160. }
  161. }
  162. #ifdef VIBRATO_ENABLE
  163. if (vibrato_strength > 0) {
  164. freq = vibrato(frequencies[voice_place]);
  165. } else {
  166. freq = frequencies[voice_place];
  167. }
  168. #else
  169. freq = frequencies[voice_place];
  170. #endif
  171. } else {
  172. if (glissando) {
  173. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  174. frequency = frequency * pow(2, 440/frequency/12/2);
  175. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  176. frequency = frequency * pow(2, -440/frequency/12/2);
  177. } else {
  178. frequency = frequencies[voices - 1];
  179. }
  180. } else {
  181. frequency = frequencies[voices - 1];
  182. }
  183. #ifdef VIBRATO_ENABLE
  184. if (vibrato_strength > 0) {
  185. freq = vibrato(frequency);
  186. } else {
  187. freq = frequency;
  188. }
  189. #else
  190. freq = frequency;
  191. #endif
  192. }
  193. if (envelope_index < 65535) {
  194. envelope_index++;
  195. }
  196. freq = voice_envelope(freq);
  197. if (freq < 30.517578125) {
  198. freq = 30.52;
  199. }
  200. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  201. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  202. }
  203. }
  204. if (playing_notes) {
  205. if (note_frequency > 0) {
  206. #ifdef VIBRATO_ENABLE
  207. if (vibrato_strength > 0) {
  208. freq = vibrato(note_frequency);
  209. } else {
  210. freq = note_frequency;
  211. }
  212. #else
  213. freq = note_frequency;
  214. #endif
  215. if (envelope_index < 65535) {
  216. envelope_index++;
  217. }
  218. freq = voice_envelope(freq);
  219. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  220. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  221. } else {
  222. TIMER_3_PERIOD = 0;
  223. TIMER_3_DUTY_CYCLE = 0;
  224. }
  225. note_position++;
  226. bool end_of_note = false;
  227. if (TIMER_3_PERIOD > 0) {
  228. end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
  229. } else {
  230. end_of_note = (note_position >= (note_length * 0x7FF));
  231. }
  232. if (end_of_note) {
  233. current_note++;
  234. if (current_note >= notes_count) {
  235. if (notes_repeat) {
  236. current_note = 0;
  237. } else {
  238. DISABLE_AUDIO_COUNTER_3_ISR;
  239. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  240. playing_notes = false;
  241. return;
  242. }
  243. }
  244. if (!note_resting && (notes_rest > 0)) {
  245. note_resting = true;
  246. note_frequency = 0;
  247. note_length = notes_rest;
  248. current_note--;
  249. } else {
  250. note_resting = false;
  251. envelope_index = 0;
  252. note_frequency = (*notes_pointer)[current_note][0];
  253. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  254. }
  255. note_position = 0;
  256. }
  257. }
  258. if (!audio_config.enable) {
  259. playing_notes = false;
  260. playing_note = false;
  261. }
  262. }
  263. void play_note(float freq, int vol) {
  264. if (!audio_initialized) {
  265. audio_init();
  266. }
  267. if (audio_config.enable && voices < 8) {
  268. DISABLE_AUDIO_COUNTER_3_ISR;
  269. // Cancel notes if notes are playing
  270. if (playing_notes)
  271. stop_all_notes();
  272. playing_note = true;
  273. envelope_index = 0;
  274. if (freq > 0) {
  275. frequencies[voices] = freq;
  276. volumes[voices] = vol;
  277. voices++;
  278. }
  279. ENABLE_AUDIO_COUNTER_3_ISR;
  280. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  281. }
  282. }
  283. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
  284. {
  285. if (!audio_initialized) {
  286. audio_init();
  287. }
  288. if (audio_config.enable) {
  289. DISABLE_AUDIO_COUNTER_3_ISR;
  290. // Cancel note if a note is playing
  291. if (playing_note)
  292. stop_all_notes();
  293. playing_notes = true;
  294. notes_pointer = np;
  295. notes_count = n_count;
  296. notes_repeat = n_repeat;
  297. notes_rest = n_rest;
  298. place = 0;
  299. current_note = 0;
  300. note_frequency = (*notes_pointer)[current_note][0];
  301. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  302. note_position = 0;
  303. ENABLE_AUDIO_COUNTER_3_ISR;
  304. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  305. }
  306. }
  307. bool is_playing_notes(void) {
  308. return playing_notes;
  309. }
  310. bool is_audio_on(void) {
  311. return (audio_config.enable != 0);
  312. }
  313. void audio_toggle(void) {
  314. audio_config.enable ^= 1;
  315. eeconfig_update_audio(audio_config.raw);
  316. if (audio_config.enable)
  317. audio_on_user();
  318. }
  319. void audio_on(void) {
  320. audio_config.enable = 1;
  321. eeconfig_update_audio(audio_config.raw);
  322. audio_on_user();
  323. }
  324. void audio_off(void) {
  325. audio_config.enable = 0;
  326. eeconfig_update_audio(audio_config.raw);
  327. }
  328. #ifdef VIBRATO_ENABLE
  329. // Vibrato rate functions
  330. void set_vibrato_rate(float rate) {
  331. vibrato_rate = rate;
  332. }
  333. void increase_vibrato_rate(float change) {
  334. vibrato_rate *= change;
  335. }
  336. void decrease_vibrato_rate(float change) {
  337. vibrato_rate /= change;
  338. }
  339. #ifdef VIBRATO_STRENGTH_ENABLE
  340. void set_vibrato_strength(float strength) {
  341. vibrato_strength = strength;
  342. }
  343. void increase_vibrato_strength(float change) {
  344. vibrato_strength *= change;
  345. }
  346. void decrease_vibrato_strength(float change) {
  347. vibrato_strength /= change;
  348. }
  349. #endif /* VIBRATO_STRENGTH_ENABLE */
  350. #endif /* VIBRATO_ENABLE */
  351. // Polyphony functions
  352. void set_polyphony_rate(float rate) {
  353. polyphony_rate = rate;
  354. }
  355. void enable_polyphony() {
  356. polyphony_rate = 5;
  357. }
  358. void disable_polyphony() {
  359. polyphony_rate = 0;
  360. }
  361. void increase_polyphony_rate(float change) {
  362. polyphony_rate *= change;
  363. }
  364. void decrease_polyphony_rate(float change) {
  365. polyphony_rate /= change;
  366. }
  367. // Timbre function
  368. void set_timbre(float timbre) {
  369. note_timbre = timbre;
  370. }
  371. // Tempo functions
  372. void set_tempo(uint8_t tempo) {
  373. note_tempo = tempo;
  374. }
  375. void decrease_tempo(uint8_t tempo_change) {
  376. note_tempo += tempo_change;
  377. }
  378. void increase_tempo(uint8_t tempo_change) {
  379. if (note_tempo - tempo_change < 10) {
  380. note_tempo = 10;
  381. } else {
  382. note_tempo -= tempo_change;
  383. }
  384. }