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.c 11KB

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