audio_arm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* Copyright 2016-2019 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 "audio.h"
  17. #include "ch.h"
  18. #include "hal.h"
  19. #include <string.h>
  20. #include "print.h"
  21. #include "keymap.h"
  22. #include "eeconfig.h"
  23. // -----------------------------------------------------------------------------
  24. uint8_t dac_voices = 0;
  25. float dac_frequencies[8] = { 0.0 };
  26. bool playing_notes = false;
  27. bool playing_note = false;
  28. float note_frequency = 0;
  29. float note_length = 0;
  30. uint8_t note_tempo = TEMPO_DEFAULT;
  31. float note_timbre = TIMBRE_DEFAULT;
  32. uint32_t note_position = 0;
  33. float (* notes_pointer)[][2];
  34. uint16_t notes_count;
  35. bool notes_repeat;
  36. uint16_t current_note = 0;
  37. #ifdef VIBRATO_ENABLE
  38. float vibrato_counter = 0;
  39. float vibrato_strength = .5;
  40. float vibrato_rate = 0.125;
  41. #endif
  42. float polyphony_rate = 0;
  43. static bool audio_initialized = false;
  44. audio_config_t audio_config;
  45. uint16_t envelope_index = 0;
  46. bool glissando = true;
  47. #ifndef STARTUP_SONG
  48. #define STARTUP_SONG SONG(STARTUP_SOUND)
  49. #endif
  50. float startup_song[][2] = STARTUP_SONG;
  51. static const dacsample_t dac_buffer_sine[DAC_BUFFER_SIZE] = {
  52. // 256 values, max 4095
  53. 0x800,0x832,0x864,0x896,0x8c8,0x8fa,0x92c,0x95e,
  54. 0x98f,0x9c0,0x9f1,0xa22,0xa52,0xa82,0xab1,0xae0,
  55. 0xb0f,0xb3d,0xb6b,0xb98,0xbc5,0xbf1,0xc1c,0xc47,
  56. 0xc71,0xc9a,0xcc3,0xceb,0xd12,0xd39,0xd5f,0xd83,
  57. 0xda7,0xdca,0xded,0xe0e,0xe2e,0xe4e,0xe6c,0xe8a,
  58. 0xea6,0xec1,0xedc,0xef5,0xf0d,0xf24,0xf3a,0xf4f,
  59. 0xf63,0xf76,0xf87,0xf98,0xfa7,0xfb5,0xfc2,0xfcd,
  60. 0xfd8,0xfe1,0xfe9,0xff0,0xff5,0xff9,0xffd,0xffe,
  61. 0xfff,0xffe,0xffd,0xff9,0xff5,0xff0,0xfe9,0xfe1,
  62. 0xfd8,0xfcd,0xfc2,0xfb5,0xfa7,0xf98,0xf87,0xf76,
  63. 0xf63,0xf4f,0xf3a,0xf24,0xf0d,0xef5,0xedc,0xec1,
  64. 0xea6,0xe8a,0xe6c,0xe4e,0xe2e,0xe0e,0xded,0xdca,
  65. 0xda7,0xd83,0xd5f,0xd39,0xd12,0xceb,0xcc3,0xc9a,
  66. 0xc71,0xc47,0xc1c,0xbf1,0xbc5,0xb98,0xb6b,0xb3d,
  67. 0xb0f,0xae0,0xab1,0xa82,0xa52,0xa22,0x9f1,0x9c0,
  68. 0x98f,0x95e,0x92c,0x8fa,0x8c8,0x896,0x864,0x832,
  69. 0x800,0x7cd,0x79b,0x769,0x737,0x705,0x6d3,0x6a1,
  70. 0x670,0x63f,0x60e,0x5dd,0x5ad,0x57d,0x54e,0x51f,
  71. 0x4f0,0x4c2,0x494,0x467,0x43a,0x40e,0x3e3,0x3b8,
  72. 0x38e,0x365,0x33c,0x314,0x2ed,0x2c6,0x2a0,0x27c,
  73. 0x258,0x235,0x212,0x1f1,0x1d1,0x1b1,0x193,0x175,
  74. 0x159,0x13e,0x123,0x10a,0xf2, 0xdb, 0xc5, 0xb0,
  75. 0x9c, 0x89, 0x78, 0x67, 0x58, 0x4a, 0x3d, 0x32,
  76. 0x27, 0x1e, 0x16, 0xf, 0xa, 0x6, 0x2, 0x1,
  77. 0x0, 0x1, 0x2, 0x6, 0xa, 0xf, 0x16, 0x1e,
  78. 0x27, 0x32, 0x3d, 0x4a, 0x58, 0x67, 0x78, 0x89,
  79. 0x9c, 0xb0, 0xc5, 0xdb, 0xf2, 0x10a,0x123,0x13e,
  80. 0x159,0x175,0x193,0x1b1,0x1d1,0x1f1,0x212,0x235,
  81. 0x258,0x27c,0x2a0,0x2c6,0x2ed,0x314,0x33c,0x365,
  82. 0x38e,0x3b8,0x3e3,0x40e,0x43a,0x467,0x494,0x4c2,
  83. 0x4f0,0x51f,0x54e,0x57d,0x5ad,0x5dd,0x60e,0x63f,
  84. 0x670,0x6a1,0x6d3,0x705,0x737,0x769,0x79b,0x7cd
  85. };
  86. static const dacsample_t dac_buffer_triangle[DAC_BUFFER_SIZE] = {
  87. // 256 values, max 4095
  88. 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0, 0x100,
  89. 0x120,0x140,0x160,0x180,0x1a0,0x1c0,0x1e0,0x200,
  90. 0x220,0x240,0x260,0x280,0x2a0,0x2c0,0x2e0,0x300,
  91. 0x320,0x340,0x360,0x380,0x3a0,0x3c0,0x3e0,0x400,
  92. 0x420,0x440,0x460,0x480,0x4a0,0x4c0,0x4e0,0x500,
  93. 0x520,0x540,0x560,0x580,0x5a0,0x5c0,0x5e0,0x600,
  94. 0x620,0x640,0x660,0x680,0x6a0,0x6c0,0x6e0,0x700,
  95. 0x720,0x740,0x760,0x780,0x7a0,0x7c0,0x7e0,0x800,
  96. 0x81f,0x83f,0x85f,0x87f,0x89f,0x8bf,0x8df,0x8ff,
  97. 0x91f,0x93f,0x95f,0x97f,0x99f,0x9bf,0x9df,0x9ff,
  98. 0xa1f,0xa3f,0xa5f,0xa7f,0xa9f,0xabf,0xadf,0xaff,
  99. 0xb1f,0xb3f,0xb5f,0xb7f,0xb9f,0xbbf,0xbdf,0xbff,
  100. 0xc1f,0xc3f,0xc5f,0xc7f,0xc9f,0xcbf,0xcdf,0xcff,
  101. 0xd1f,0xd3f,0xd5f,0xd7f,0xd9f,0xdbf,0xddf,0xdff,
  102. 0xe1f,0xe3f,0xe5f,0xe7f,0xe9f,0xebf,0xedf,0xeff,
  103. 0xf1f,0xf3f,0xf5f,0xf7f,0xf9f,0xfbf,0xfdf,0xfff,
  104. 0xfdf,0xfbf,0xf9f,0xf7f,0xf5f,0xf3f,0xf1f,0xeff,
  105. 0xedf,0xebf,0xe9f,0xe7f,0xe5f,0xe3f,0xe1f,0xdff,
  106. 0xddf,0xdbf,0xd9f,0xd7f,0xd5f,0xd3f,0xd1f,0xcff,
  107. 0xcdf,0xcbf,0xc9f,0xc7f,0xc5f,0xc3f,0xc1f,0xbff,
  108. 0xbdf,0xbbf,0xb9f,0xb7f,0xb5f,0xb3f,0xb1f,0xaff,
  109. 0xadf,0xabf,0xa9f,0xa7f,0xa5f,0xa3f,0xa1f,0x9ff,
  110. 0x9df,0x9bf,0x99f,0x97f,0x95f,0x93f,0x91f,0x8ff,
  111. 0x8df,0x8bf,0x89f,0x87f,0x85f,0x83f,0x81f,0x800,
  112. 0x7e0,0x7c0,0x7a0,0x780,0x760,0x740,0x720,0x700,
  113. 0x6e0,0x6c0,0x6a0,0x680,0x660,0x640,0x620,0x600,
  114. 0x5e0,0x5c0,0x5a0,0x580,0x560,0x540,0x520,0x500,
  115. 0x4e0,0x4c0,0x4a0,0x480,0x460,0x440,0x420,0x400,
  116. 0x3e0,0x3c0,0x3a0,0x380,0x360,0x340,0x320,0x300,
  117. 0x2e0,0x2c0,0x2a0,0x280,0x260,0x240,0x220,0x200,
  118. 0x1e0,0x1c0,0x1a0,0x180,0x160,0x140,0x120,0x100,
  119. 0xe0, 0xc0, 0xa0, 0x80, 0x60, 0x40, 0x20, 0x0
  120. };
  121. static const dacsample_t dac_buffer_square[DAC_BUFFER_SIZE] = {
  122. // First half is max, second half is 0
  123. [0 ... DAC_BUFFER_SIZE/2-1] = DAC_SAMPLE_MAX,
  124. [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE -1] = 0,
  125. };
  126. static dacsample_t dac_buffer_empty[DAC_BUFFER_SIZE] = { DAC_OFF_VALUE };
  127. static float dac_if[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  128. /**
  129. * Generation of the waveform being passed to the callback. Declared weak so users
  130. * can override it with their own waveforms/noises.
  131. */
  132. __attribute__ ((weak))
  133. uint16_t dac_value_generate(void) {
  134. uint16_t value = DAC_OFF_VALUE;
  135. uint8_t working_voices = dac_voices;
  136. if (working_voices > DAC_VOICES_MAX)
  137. working_voices = DAC_VOICES_MAX;
  138. if (working_voices > 0) {
  139. uint16_t value_avg = 0;
  140. for (uint8_t i = 0; i < working_voices; i++) {
  141. dac_if[i] = dac_if[i] + ((dac_frequencies[i] * DAC_BUFFER_SIZE) / DAC_SAMPLE_RATE);
  142. // Needed because % doesn't work with floats
  143. while (dac_if[i] >= (DAC_BUFFER_SIZE))
  144. dac_if[i] = dac_if[i] - DAC_BUFFER_SIZE;
  145. // Wavetable generation/lookup
  146. uint16_t dac_i = (uint16_t)dac_if[i];
  147. // SINE
  148. value_avg += dac_buffer_sine[dac_i] / working_voices / 3;
  149. // TRIANGLE
  150. value_avg += dac_buffer_triangle[dac_i] / working_voices / 3;
  151. // SQUARE
  152. value_avg += dac_buffer_square[dac_i] / working_voices / 3;
  153. }
  154. value = value_avg;
  155. }
  156. return value;
  157. }
  158. /**
  159. * DAC streaming callback. Does all of the main computing for playing songs.
  160. */
  161. static void dac_end(DACDriver * dacp, dacsample_t * sample_p, size_t sample_count) {
  162. (void)dacp;
  163. for (uint8_t s = 0; s < sample_count; s++) {
  164. sample_p[s] = dac_value_generate();
  165. }
  166. if (playing_notes) {
  167. note_position += sample_count;
  168. // End of the note - 35 is arbitary here, but gets us close to AVR's timing
  169. if ((note_position >= (note_length*DAC_SAMPLE_RATE/35))) {
  170. stop_note((*notes_pointer)[current_note][0]);
  171. current_note++;
  172. if (current_note >= notes_count) {
  173. if (notes_repeat) {
  174. current_note = 0;
  175. } else {
  176. playing_notes = false;
  177. return;
  178. }
  179. }
  180. play_note((*notes_pointer)[current_note][0], 15);
  181. envelope_index = 0;
  182. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  183. // Skip forward in the next note's length if we've over shot the last, so
  184. // the overall length of the song is the same
  185. note_position = note_position - (note_length*DAC_SAMPLE_RATE/35);
  186. }
  187. }
  188. }
  189. static void dac_error(DACDriver *dacp, dacerror_t err) {
  190. (void)dacp;
  191. (void)err;
  192. chSysHalt("DAC failure. halp");
  193. }
  194. static const GPTConfig gpt6cfg1 = {
  195. .frequency = DAC_SAMPLE_RATE * 3,
  196. .callback = NULL,
  197. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  198. .dier = 0U
  199. };
  200. static const DACConfig dac_conf = {
  201. .init = DAC_SAMPLE_MAX,
  202. .datamode = DAC_DHRM_12BIT_RIGHT
  203. };
  204. /**
  205. * @note The DAC_TRG(0) here selects the Timer 6 TRGO event, which is triggered
  206. * on the rising edge after 3 APB1 clock cycles, causing our gpt6cfg1.frequency
  207. * to be a third of what we expect.
  208. *
  209. * Here are all the values for DAC_TRG (TSEL in the ref manual)
  210. * TIM15_TRGO 0b011
  211. * TIM2_TRGO 0b100
  212. * TIM3_TRGO 0b001
  213. * TIM6_TRGO 0b000
  214. * TIM7_TRGO 0b010
  215. * EXTI9 0b110
  216. * SWTRIG 0b111
  217. */
  218. static const DACConversionGroup dac_conv_cfg = {
  219. .num_channels = 1U,
  220. .end_cb = dac_end,
  221. .error_cb = dac_error,
  222. .trigger = DAC_TRG(0b000)
  223. };
  224. void audio_init() {
  225. if (audio_initialized) {
  226. return;
  227. }
  228. // Check EEPROM
  229. #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
  230. if (!eeconfig_is_enabled()) {
  231. eeconfig_init();
  232. }
  233. audio_config.raw = eeconfig_read_audio();
  234. #else // ARM EEPROM
  235. audio_config.enable = true;
  236. #ifdef AUDIO_CLICKY_ON
  237. audio_config.clicky_enable = true;
  238. #endif
  239. #endif // ARM EEPROM
  240. #if defined(A4_AUDIO)
  241. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG );
  242. dacStart(&DACD1, &dac_conf);
  243. dacStartConversion(&DACD1, &dac_conv_cfg, dac_buffer_empty, DAC_BUFFER_SIZE);
  244. #endif
  245. #if defined(A5_AUDIO)
  246. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG );
  247. dacStart(&DACD2, &dac_conf);
  248. dacStartConversion(&DACD2, &dac_conv_cfg, dac_buffer_empty, DAC_BUFFER_SIZE);
  249. #endif
  250. gptStart(&GPTD6, &gpt6cfg1);
  251. gptStartContinuous(&GPTD6, 2U);
  252. audio_initialized = true;
  253. if (audio_config.enable) {
  254. PLAY_SONG(startup_song);
  255. } else {
  256. stop_all_notes();
  257. }
  258. }
  259. void stop_all_notes() {
  260. dprintf("audio stop all notes");
  261. if (!audio_initialized) {
  262. audio_init();
  263. }
  264. dac_voices = 0;
  265. playing_notes = false;
  266. playing_note = false;
  267. for (uint8_t i = 0; i < 8; i++) {
  268. dac_frequencies[i] = 0;
  269. }
  270. }
  271. void stop_note(float freq) {
  272. dprintf("audio stop note freq=%d", (int)freq);
  273. if (playing_note) {
  274. if (!audio_initialized) {
  275. audio_init();
  276. }
  277. for (int i = 7; i >= 0; i--) {
  278. if (dac_frequencies[i] == freq) {
  279. dac_frequencies[i] = 0;
  280. for (int j = i; (j < 7); j++) {
  281. dac_frequencies[j] = dac_frequencies[j+1];
  282. dac_frequencies[j+1] = 0;
  283. }
  284. break;
  285. }
  286. }
  287. dac_voices--;
  288. if (dac_voices < 0) {
  289. dac_voices = 0;
  290. }
  291. if (dac_voices == 0) {
  292. playing_note = false;
  293. }
  294. }
  295. }
  296. #ifdef VIBRATO_ENABLE
  297. float mod(float a, int b) {
  298. float r = fmod(a, b);
  299. return r < 0 ? r + b : r;
  300. }
  301. float vibrato(float average_freq) {
  302. #ifdef VIBRATO_STRENGTH_ENABLE
  303. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  304. #else
  305. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  306. #endif
  307. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  308. return vibrated_freq;
  309. }
  310. #endif
  311. void play_note(float freq, int vol) {
  312. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  313. if (!audio_initialized) {
  314. audio_init();
  315. }
  316. if (audio_config.enable && dac_voices < 8) {
  317. playing_note = true;
  318. if (freq > 0) {
  319. envelope_index = 0;
  320. dac_frequencies[dac_voices] = freq;
  321. dac_voices++;
  322. }
  323. }
  324. }
  325. __attribute__ ((weak))
  326. void dac_setup_note(void) {
  327. dac_if[dac_voices] = 0.0f;
  328. }
  329. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
  330. if (!audio_initialized) {
  331. audio_init();
  332. }
  333. if (audio_config.enable) {
  334. playing_notes = true;
  335. notes_pointer = np;
  336. notes_count = n_count;
  337. notes_repeat = n_repeat;
  338. current_note = 0;
  339. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  340. note_position = 0;
  341. play_note((*notes_pointer)[current_note][0], 15);
  342. }
  343. }
  344. bool is_playing_note(void) {
  345. return playing_note;
  346. }
  347. bool is_playing_notes(void) {
  348. return playing_notes;
  349. }
  350. bool is_audio_on(void) {
  351. return (audio_config.enable != 0);
  352. }
  353. void audio_toggle(void) {
  354. audio_config.enable ^= 1;
  355. eeconfig_update_audio(audio_config.raw);
  356. if (audio_config.enable) {
  357. audio_on_user();
  358. }
  359. }
  360. void audio_on(void) {
  361. audio_config.enable = 1;
  362. eeconfig_update_audio(audio_config.raw);
  363. audio_on_user();
  364. }
  365. void audio_off(void) {
  366. stop_all_notes();
  367. audio_config.enable = 0;
  368. eeconfig_update_audio(audio_config.raw);
  369. }
  370. #ifdef VIBRATO_ENABLE
  371. // Vibrato rate functions
  372. void set_vibrato_rate(float rate) {
  373. vibrato_rate = rate;
  374. }
  375. void increase_vibrato_rate(float change) {
  376. vibrato_rate *= change;
  377. }
  378. void decrease_vibrato_rate(float change) {
  379. vibrato_rate /= change;
  380. }
  381. #ifdef VIBRATO_STRENGTH_ENABLE
  382. void set_vibrato_strength(float strength) {
  383. vibrato_strength = strength;
  384. }
  385. void increase_vibrato_strength(float change) {
  386. vibrato_strength *= change;
  387. }
  388. void decrease_vibrato_strength(float change) {
  389. vibrato_strength /= change;
  390. }
  391. #endif /* VIBRATO_STRENGTH_ENABLE */
  392. #endif /* VIBRATO_ENABLE */
  393. // Polyphony functions
  394. void set_polyphony_rate(float rate) {
  395. polyphony_rate = rate;
  396. }
  397. void enable_polyphony() {
  398. polyphony_rate = 5;
  399. }
  400. void disable_polyphony() {
  401. polyphony_rate = 0;
  402. }
  403. void increase_polyphony_rate(float change) {
  404. polyphony_rate *= change;
  405. }
  406. void decrease_polyphony_rate(float change) {
  407. polyphony_rate /= change;
  408. }
  409. // Timbre function
  410. void set_timbre(float timbre) {
  411. note_timbre = timbre;
  412. }
  413. // Tempo functions
  414. void set_tempo(uint8_t tempo) {
  415. note_tempo = tempo;
  416. }
  417. void decrease_tempo(uint8_t tempo_change) {
  418. note_tempo += tempo_change;
  419. }
  420. void increase_tempo(uint8_t tempo_change) {
  421. if (note_tempo - tempo_change < 10) {
  422. note_tempo = 10;
  423. } else {
  424. note_tempo -= tempo_change;
  425. }
  426. }
  427. uint8_t dac_number_of_voices(void) {
  428. return dac_voices;
  429. }
  430. float dac_get_frequency(uint8_t index) {
  431. return dac_frequencies[index];
  432. }