audio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. dprintf("audio stop all notes");
  84. if (!audio_initialized) {
  85. audio_init();
  86. }
  87. voices = 0;
  88. DISABLE_AUDIO_COUNTER_3_ISR;
  89. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  90. playing_notes = false;
  91. playing_note = false;
  92. frequency = 0;
  93. volume = 0;
  94. for (uint8_t i = 0; i < 8; i++)
  95. {
  96. frequencies[i] = 0;
  97. volumes[i] = 0;
  98. }
  99. }
  100. void stop_note(float freq)
  101. {
  102. dprintf("audio stop note freq=%d", (int)freq);
  103. if (playing_note) {
  104. if (!audio_initialized) {
  105. audio_init();
  106. }
  107. for (int i = 7; i >= 0; i--) {
  108. if (frequencies[i] == freq) {
  109. frequencies[i] = 0;
  110. volumes[i] = 0;
  111. for (int j = i; (j < 7); j++) {
  112. frequencies[j] = frequencies[j+1];
  113. frequencies[j+1] = 0;
  114. volumes[j] = volumes[j+1];
  115. volumes[j+1] = 0;
  116. }
  117. break;
  118. }
  119. }
  120. voices--;
  121. if (voices < 0)
  122. voices = 0;
  123. if (voice_place >= voices) {
  124. voice_place = 0;
  125. }
  126. if (voices == 0) {
  127. DISABLE_AUDIO_COUNTER_3_ISR;
  128. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  129. frequency = 0;
  130. volume = 0;
  131. playing_note = false;
  132. }
  133. }
  134. }
  135. #ifdef VIBRATO_ENABLE
  136. float mod(float a, int b)
  137. {
  138. float r = fmod(a, b);
  139. return r < 0 ? r + b : r;
  140. }
  141. float vibrato(float average_freq) {
  142. #ifdef VIBRATO_STRENGTH_ENABLE
  143. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  144. #else
  145. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  146. #endif
  147. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  148. return vibrated_freq;
  149. }
  150. #endif
  151. ISR(TIMER3_COMPA_vect)
  152. {
  153. float freq;
  154. if (playing_note) {
  155. if (voices > 0) {
  156. if (polyphony_rate > 0) {
  157. if (voices > 1) {
  158. voice_place %= voices;
  159. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  160. voice_place = (voice_place + 1) % voices;
  161. place = 0.0;
  162. }
  163. }
  164. #ifdef VIBRATO_ENABLE
  165. if (vibrato_strength > 0) {
  166. freq = vibrato(frequencies[voice_place]);
  167. } else {
  168. freq = frequencies[voice_place];
  169. }
  170. #else
  171. freq = frequencies[voice_place];
  172. #endif
  173. } else {
  174. if (glissando) {
  175. 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 if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  178. frequency = frequency * pow(2, -440/frequency/12/2);
  179. } else {
  180. frequency = frequencies[voices - 1];
  181. }
  182. } else {
  183. frequency = frequencies[voices - 1];
  184. }
  185. #ifdef VIBRATO_ENABLE
  186. if (vibrato_strength > 0) {
  187. freq = vibrato(frequency);
  188. } else {
  189. freq = frequency;
  190. }
  191. #else
  192. freq = frequency;
  193. #endif
  194. }
  195. if (envelope_index < 65535) {
  196. envelope_index++;
  197. }
  198. freq = voice_envelope(freq);
  199. if (freq < 30.517578125) {
  200. freq = 30.52;
  201. }
  202. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  203. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  204. }
  205. }
  206. if (playing_notes) {
  207. if (note_frequency > 0) {
  208. #ifdef VIBRATO_ENABLE
  209. if (vibrato_strength > 0) {
  210. freq = vibrato(note_frequency);
  211. } else {
  212. freq = note_frequency;
  213. }
  214. #else
  215. freq = note_frequency;
  216. #endif
  217. if (envelope_index < 65535) {
  218. envelope_index++;
  219. }
  220. freq = voice_envelope(freq);
  221. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  222. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  223. } else {
  224. TIMER_3_PERIOD = 0;
  225. TIMER_3_DUTY_CYCLE = 0;
  226. }
  227. note_position++;
  228. bool end_of_note = false;
  229. if (TIMER_3_PERIOD > 0) {
  230. end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
  231. } else {
  232. end_of_note = (note_position >= (note_length * 0x7FF));
  233. }
  234. if (end_of_note) {
  235. current_note++;
  236. if (current_note >= notes_count) {
  237. if (notes_repeat) {
  238. current_note = 0;
  239. } else {
  240. DISABLE_AUDIO_COUNTER_3_ISR;
  241. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  242. playing_notes = false;
  243. return;
  244. }
  245. }
  246. if (!note_resting && (notes_rest > 0)) {
  247. note_resting = true;
  248. note_frequency = 0;
  249. note_length = notes_rest;
  250. current_note--;
  251. } else {
  252. note_resting = false;
  253. envelope_index = 0;
  254. note_frequency = (*notes_pointer)[current_note][0];
  255. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  256. }
  257. note_position = 0;
  258. }
  259. }
  260. if (!audio_config.enable) {
  261. playing_notes = false;
  262. playing_note = false;
  263. }
  264. }
  265. void play_note(float freq, int vol) {
  266. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  267. if (!audio_initialized) {
  268. audio_init();
  269. }
  270. if (audio_config.enable && voices < 8) {
  271. DISABLE_AUDIO_COUNTER_3_ISR;
  272. // Cancel notes if notes are playing
  273. if (playing_notes)
  274. stop_all_notes();
  275. playing_note = true;
  276. envelope_index = 0;
  277. if (freq > 0) {
  278. frequencies[voices] = freq;
  279. volumes[voices] = vol;
  280. voices++;
  281. }
  282. ENABLE_AUDIO_COUNTER_3_ISR;
  283. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  284. }
  285. }
  286. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
  287. {
  288. if (!audio_initialized) {
  289. audio_init();
  290. }
  291. if (audio_config.enable) {
  292. DISABLE_AUDIO_COUNTER_3_ISR;
  293. // Cancel note if a note is playing
  294. if (playing_note)
  295. stop_all_notes();
  296. playing_notes = true;
  297. notes_pointer = np;
  298. notes_count = n_count;
  299. notes_repeat = n_repeat;
  300. notes_rest = n_rest;
  301. place = 0;
  302. current_note = 0;
  303. note_frequency = (*notes_pointer)[current_note][0];
  304. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  305. note_position = 0;
  306. ENABLE_AUDIO_COUNTER_3_ISR;
  307. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  308. }
  309. }
  310. bool is_playing_notes(void) {
  311. return playing_notes;
  312. }
  313. bool is_audio_on(void) {
  314. return (audio_config.enable != 0);
  315. }
  316. void audio_toggle(void) {
  317. audio_config.enable ^= 1;
  318. eeconfig_update_audio(audio_config.raw);
  319. if (audio_config.enable)
  320. audio_on_user();
  321. }
  322. void audio_on(void) {
  323. audio_config.enable = 1;
  324. eeconfig_update_audio(audio_config.raw);
  325. audio_on_user();
  326. }
  327. void audio_off(void) {
  328. audio_config.enable = 0;
  329. eeconfig_update_audio(audio_config.raw);
  330. }
  331. #ifdef VIBRATO_ENABLE
  332. // Vibrato rate functions
  333. void set_vibrato_rate(float rate) {
  334. vibrato_rate = rate;
  335. }
  336. void increase_vibrato_rate(float change) {
  337. vibrato_rate *= change;
  338. }
  339. void decrease_vibrato_rate(float change) {
  340. vibrato_rate /= change;
  341. }
  342. #ifdef VIBRATO_STRENGTH_ENABLE
  343. void set_vibrato_strength(float strength) {
  344. vibrato_strength = strength;
  345. }
  346. void increase_vibrato_strength(float change) {
  347. vibrato_strength *= change;
  348. }
  349. void decrease_vibrato_strength(float change) {
  350. vibrato_strength /= change;
  351. }
  352. #endif /* VIBRATO_STRENGTH_ENABLE */
  353. #endif /* VIBRATO_ENABLE */
  354. // Polyphony functions
  355. void set_polyphony_rate(float rate) {
  356. polyphony_rate = rate;
  357. }
  358. void enable_polyphony() {
  359. polyphony_rate = 5;
  360. }
  361. void disable_polyphony() {
  362. polyphony_rate = 0;
  363. }
  364. void increase_polyphony_rate(float change) {
  365. polyphony_rate *= change;
  366. }
  367. void decrease_polyphony_rate(float change) {
  368. polyphony_rate /= change;
  369. }
  370. // Timbre function
  371. void set_timbre(float timbre) {
  372. note_timbre = timbre;
  373. }
  374. // Tempo functions
  375. void set_tempo(uint8_t tempo) {
  376. note_tempo = tempo;
  377. }
  378. void decrease_tempo(uint8_t tempo_change) {
  379. note_tempo += tempo_change;
  380. }
  381. void increase_tempo(uint8_t tempo_change) {
  382. if (note_tempo - tempo_change < 10) {
  383. note_tempo = 10;
  384. } else {
  385. note_tempo -= tempo_change;
  386. }
  387. }