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.

503 lines
13 KiB

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