audio_arm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 "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. int voices = 0;
  25. int voice_place = 0;
  26. float frequency = 0;
  27. float frequency_alt = 0;
  28. int volume = 0;
  29. long position = 0;
  30. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  31. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  32. bool sliding = false;
  33. float place = 0;
  34. uint8_t * sample;
  35. uint16_t sample_length = 0;
  36. bool playing_notes = false;
  37. bool playing_note = false;
  38. float note_frequency = 0;
  39. float note_length = 0;
  40. uint8_t note_tempo = TEMPO_DEFAULT;
  41. float note_timbre = TIMBRE_DEFAULT;
  42. uint16_t note_position = 0;
  43. float (* notes_pointer)[][2];
  44. uint16_t notes_count;
  45. bool notes_repeat;
  46. bool note_resting = false;
  47. uint16_t current_note = 0;
  48. uint8_t rest_counter = 0;
  49. #ifdef VIBRATO_ENABLE
  50. float vibrato_counter = 0;
  51. float vibrato_strength = .5;
  52. float vibrato_rate = 0.125;
  53. #endif
  54. float polyphony_rate = 0;
  55. static bool audio_initialized = false;
  56. audio_config_t audio_config;
  57. uint16_t envelope_index = 0;
  58. bool glissando = true;
  59. #ifndef STARTUP_SONG
  60. #define STARTUP_SONG SONG(STARTUP_SOUND)
  61. #endif
  62. float startup_song[][2] = STARTUP_SONG;
  63. #define DAC_BUFFER_SIZE 256U
  64. #ifndef DAC_SAMPLE_MAX
  65. #define DAC_SAMPLE_MAX 4095U
  66. #endif
  67. #define DAC_SAMPLE_RATE 44100U
  68. GPTConfig gpt7cfg1 = {
  69. .frequency = DAC_SAMPLE_RATE,
  70. .callback = NULL,
  71. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  72. .dier = 0U
  73. };
  74. static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
  75. // First half is max, second half is 0
  76. // [0 ... DAC_BUFFER_SIZE/2-1] = DAC_SAMPLE_MAX,
  77. // [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE -1] = 0,
  78. // 100 values, max 4095
  79. // 0x800,0x880,0x900,0x97f,0x9fd,0xa78,0xaf1,0xb67,
  80. // 0xbda,0xc49,0xcb3,0xd19,0xd79,0xdd4,0xe29,0xe78,
  81. // 0xec0,0xf02,0xf3c,0xf6f,0xf9b,0xfbf,0xfdb,0xfef,
  82. // 0xffb,0xfff,0xffb,0xfef,0xfdb,0xfbf,0xf9b,0xf6f,
  83. // 0xf3c,0xf02,0xec0,0xe78,0xe29,0xdd4,0xd79,0xd19,
  84. // 0xcb3,0xc49,0xbda,0xb67,0xaf1,0xa78,0x9fd,0x97f,
  85. // 0x900,0x880,0x800,0x77f,0x6ff,0x680,0x602,0x587,
  86. // 0x50e,0x498,0x425,0x3b6,0x34c,0x2e6,0x286,0x22b,
  87. // 0x1d6,0x187,0x13f,0xfd,0xc3,0x90,0x64,0x40,
  88. // 0x24,0x10,0x4,0x0,0x4,0x10,0x24,0x40,
  89. // 0x64,0x90,0xc3,0xfd,0x13f,0x187,0x1d6,0x22b,
  90. // 0x286,0x2e6,0x34c,0x3b6,0x425,0x498,0x50e,0x587,
  91. // 0x602,0x680,0x6ff,0x77f,0x800
  92. // 256 values, max 4095
  93. 0x800,0x832,0x864,0x896,0x8c8,0x8fa,0x92c,0x95e,
  94. 0x98f,0x9c0,0x9f1,0xa22,0xa52,0xa82,0xab1,0xae0,
  95. 0xb0f,0xb3d,0xb6b,0xb98,0xbc5,0xbf1,0xc1c,0xc47,
  96. 0xc71,0xc9a,0xcc3,0xceb,0xd12,0xd39,0xd5f,0xd83,
  97. 0xda7,0xdca,0xded,0xe0e,0xe2e,0xe4e,0xe6c,0xe8a,
  98. 0xea6,0xec1,0xedc,0xef5,0xf0d,0xf24,0xf3a,0xf4f,
  99. 0xf63,0xf76,0xf87,0xf98,0xfa7,0xfb5,0xfc2,0xfcd,
  100. 0xfd8,0xfe1,0xfe9,0xff0,0xff5,0xff9,0xffd,0xffe,
  101. 0xfff,0xffe,0xffd,0xff9,0xff5,0xff0,0xfe9,0xfe1,
  102. 0xfd8,0xfcd,0xfc2,0xfb5,0xfa7,0xf98,0xf87,0xf76,
  103. 0xf63,0xf4f,0xf3a,0xf24,0xf0d,0xef5,0xedc,0xec1,
  104. 0xea6,0xe8a,0xe6c,0xe4e,0xe2e,0xe0e,0xded,0xdca,
  105. 0xda7,0xd83,0xd5f,0xd39,0xd12,0xceb,0xcc3,0xc9a,
  106. 0xc71,0xc47,0xc1c,0xbf1,0xbc5,0xb98,0xb6b,0xb3d,
  107. 0xb0f,0xae0,0xab1,0xa82,0xa52,0xa22,0x9f1,0x9c0,
  108. 0x98f,0x95e,0x92c,0x8fa,0x8c8,0x896,0x864,0x832,
  109. 0x800,0x7cd,0x79b,0x769,0x737,0x705,0x6d3,0x6a1,
  110. 0x670,0x63f,0x60e,0x5dd,0x5ad,0x57d,0x54e,0x51f,
  111. 0x4f0,0x4c2,0x494,0x467,0x43a,0x40e,0x3e3,0x3b8,
  112. 0x38e,0x365,0x33c,0x314,0x2ed,0x2c6,0x2a0,0x27c,
  113. 0x258,0x235,0x212,0x1f1,0x1d1,0x1b1,0x193,0x175,
  114. 0x159,0x13e,0x123,0x10a,0xf2,0xdb,0xc5,0xb0,
  115. 0x9c,0x89,0x78,0x67,0x58,0x4a,0x3d,0x32,
  116. 0x27,0x1e,0x16,0xf,0xa,0x6,0x2,0x1,
  117. 0x0,0x1,0x2,0x6,0xa,0xf,0x16,0x1e,
  118. 0x27,0x32,0x3d,0x4a,0x58,0x67,0x78,0x89,
  119. 0x9c,0xb0,0xc5,0xdb,0xf2,0x10a,0x123,0x13e,
  120. 0x159,0x175,0x193,0x1b1,0x1d1,0x1f1,0x212,0x235,
  121. 0x258,0x27c,0x2a0,0x2c6,0x2ed,0x314,0x33c,0x365,
  122. 0x38e,0x3b8,0x3e3,0x40e,0x43a,0x467,0x494,0x4c2,
  123. 0x4f0,0x51f,0x54e,0x57d,0x5ad,0x5dd,0x60e,0x63f,
  124. 0x670,0x6a1,0x6d3,0x705,0x737,0x769,0x79b,0x7cd
  125. };
  126. #define DAC_VOICES_MAX 5
  127. dacsample_t dac_buffer_lr[DAC_BUFFER_SIZE] = { DAC_SAMPLE_MAX / 2 };
  128. float dac_if[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  129. /*
  130. * DAC streaming callback.
  131. */
  132. static void end_cb1(DACDriver * dacp, dacsample_t * samples, size_t rows) {
  133. (void)dacp;
  134. //(void)dac_buffer;
  135. int working_voices = voices;
  136. if (working_voices > DAC_VOICES_MAX)
  137. working_voices = DAC_VOICES_MAX;
  138. for (int s = 0; s < rows; s++) {
  139. if (working_voices > 0) {
  140. uint16_t sample_sum = 0;
  141. for (int i = 0; i < working_voices; i++) {
  142. dac_if[i] = dac_if[i] + ((frequencies[i]*(float)DAC_BUFFER_SIZE)/(float)DAC_SAMPLE_RATE*1.5);
  143. while(dac_if[i] >= DAC_BUFFER_SIZE)
  144. dac_if[i] = dac_if[i] - DAC_BUFFER_SIZE;
  145. // sine
  146. sample_sum += dac_buffer[(uint16_t)round(dac_if[i])] / working_voices;
  147. // rising triangle
  148. // sample_sum += (uint16_t)round((dac_if[i] * DAC_SAMPLE_MAX) / DAC_BUFFER_SIZE / working_voices );
  149. // square (max 5 voices)
  150. // sample_sum += ((dac_if[i] > (DAC_BUFFER_SIZE / 2)) ? DAC_SAMPLE_MAX / working_voices: 0);
  151. }
  152. samples[s] = sample_sum;
  153. } else {
  154. samples[s] = DAC_SAMPLE_MAX / 2;
  155. }
  156. }
  157. }
  158. /*
  159. * DAC error callback.
  160. */
  161. static void error_cb1(DACDriver *dacp, dacerror_t err) {
  162. (void)dacp;
  163. (void)err;
  164. chSysHalt("DAC failure");
  165. }
  166. static const DACConfig dac1cfg1 = {
  167. .init = DAC_SAMPLE_MAX,
  168. .datamode = DAC_DHRM_12BIT_RIGHT
  169. };
  170. static const DACConversionGroup dacgrpcfg1 = {
  171. .num_channels = 1U,
  172. .end_cb = end_cb1,
  173. .error_cb = error_cb1,
  174. .trigger = DAC_TRG(0)
  175. };
  176. void audio_init() {
  177. if (audio_initialized) {
  178. return;
  179. }
  180. // Check EEPROM
  181. #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
  182. if (!eeconfig_is_enabled()) {
  183. eeconfig_init();
  184. }
  185. audio_config.raw = eeconfig_read_audio();
  186. #else // ARM EEPROM
  187. audio_config.enable = true;
  188. #ifdef AUDIO_CLICKY_ON
  189. audio_config.clicky_enable = true;
  190. #endif
  191. #endif // ARM EEPROM
  192. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG );
  193. // palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG );
  194. palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL );
  195. palSetPad(GPIOA, 4);
  196. // dacStart(&DACD1, &dac1cfg1);
  197. // dacStartConversion(&DACD1, &dacgrpcfg1, dac_buffer_lr, 1);
  198. dacStart(&DACD2, &dac1cfg1);
  199. dacStartConversion(&DACD2, &dacgrpcfg1, dac_buffer_lr, DAC_BUFFER_SIZE);
  200. gptStart(&GPTD6, &gpt7cfg1);
  201. gptStartContinuous(&GPTD6, 2U);
  202. // gptStart(&GPTD7, &gpt7cfg1);
  203. // gptStartContinuous(&GPTD7, 2U);
  204. audio_initialized = true;
  205. if (audio_config.enable) {
  206. PLAY_SONG(startup_song);
  207. } else {
  208. stop_all_notes();
  209. }
  210. }
  211. void stop_all_notes() {
  212. dprintf("audio stop all notes");
  213. if (!audio_initialized) {
  214. audio_init();
  215. }
  216. voices = 0;
  217. gptStopTimer(&GPTD8);
  218. playing_notes = false;
  219. playing_note = false;
  220. frequency = 0;
  221. frequency_alt = 0;
  222. volume = 0;
  223. for (uint8_t i = 0; i < 8; i++)
  224. {
  225. frequencies[i] = 0;
  226. volumes[i] = 0;
  227. }
  228. }
  229. void stop_note(float freq) {
  230. dprintf("audio stop note freq=%d", (int)freq);
  231. if (playing_note) {
  232. if (!audio_initialized) {
  233. audio_init();
  234. }
  235. for (int i = 7; i >= 0; i--) {
  236. if (frequencies[i] == freq) {
  237. frequencies[i] = 0;
  238. volumes[i] = 0;
  239. for (int j = i; (j < 7); j++) {
  240. frequencies[j] = frequencies[j+1];
  241. frequencies[j+1] = 0;
  242. volumes[j] = volumes[j+1];
  243. volumes[j+1] = 0;
  244. }
  245. break;
  246. }
  247. }
  248. voices--;
  249. if (voices < 0) {
  250. voices = 0;
  251. }
  252. if (voice_place >= voices) {
  253. voice_place = 0;
  254. }
  255. if (voices == 0) {
  256. frequency = 0;
  257. frequency_alt = 0;
  258. volume = 0;
  259. playing_note = false;
  260. }
  261. }
  262. }
  263. #ifdef VIBRATO_ENABLE
  264. float mod(float a, int b) {
  265. float r = fmod(a, b);
  266. return r < 0 ? r + b : r;
  267. }
  268. float vibrato(float average_freq) {
  269. #ifdef VIBRATO_STRENGTH_ENABLE
  270. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  271. #else
  272. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  273. #endif
  274. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  275. return vibrated_freq;
  276. }
  277. #endif
  278. void play_note(float freq, int vol) {
  279. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  280. if (!audio_initialized) {
  281. audio_init();
  282. }
  283. if (audio_config.enable && voices < 8) {
  284. // Cancel notes if notes are playing
  285. if (playing_notes) {
  286. stop_all_notes();
  287. }
  288. playing_note = true;
  289. envelope_index = 0;
  290. if (freq > 0) {
  291. frequencies[voices] = freq;
  292. volumes[voices] = vol;
  293. voices++;
  294. }
  295. }
  296. }
  297. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
  298. if (!audio_initialized) {
  299. audio_init();
  300. }
  301. if (audio_config.enable) {
  302. // Cancel note if a note is playing
  303. if (playing_note) {
  304. stop_all_notes();
  305. }
  306. playing_notes = true;
  307. notes_pointer = np;
  308. notes_count = n_count;
  309. notes_repeat = n_repeat;
  310. place = 0;
  311. current_note = 0;
  312. note_frequency = (*notes_pointer)[current_note][0];
  313. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  314. note_position = 0;
  315. }
  316. }
  317. bool is_playing_notes(void) {
  318. return playing_notes;
  319. }
  320. bool is_audio_on(void) {
  321. return (audio_config.enable != 0);
  322. }
  323. void audio_toggle(void) {
  324. audio_config.enable ^= 1;
  325. eeconfig_update_audio(audio_config.raw);
  326. if (audio_config.enable) {
  327. audio_on_user();
  328. }
  329. }
  330. void audio_on(void) {
  331. audio_config.enable = 1;
  332. eeconfig_update_audio(audio_config.raw);
  333. audio_on_user();
  334. }
  335. void audio_off(void) {
  336. stop_all_notes();
  337. audio_config.enable = 0;
  338. eeconfig_update_audio(audio_config.raw);
  339. }
  340. #ifdef VIBRATO_ENABLE
  341. // Vibrato rate functions
  342. void set_vibrato_rate(float rate) {
  343. vibrato_rate = rate;
  344. }
  345. void increase_vibrato_rate(float change) {
  346. vibrato_rate *= change;
  347. }
  348. void decrease_vibrato_rate(float change) {
  349. vibrato_rate /= change;
  350. }
  351. #ifdef VIBRATO_STRENGTH_ENABLE
  352. void set_vibrato_strength(float strength) {
  353. vibrato_strength = strength;
  354. }
  355. void increase_vibrato_strength(float change) {
  356. vibrato_strength *= change;
  357. }
  358. void decrease_vibrato_strength(float change) {
  359. vibrato_strength /= change;
  360. }
  361. #endif /* VIBRATO_STRENGTH_ENABLE */
  362. #endif /* VIBRATO_ENABLE */
  363. // Polyphony functions
  364. void set_polyphony_rate(float rate) {
  365. polyphony_rate = rate;
  366. }
  367. void enable_polyphony() {
  368. polyphony_rate = 5;
  369. }
  370. void disable_polyphony() {
  371. polyphony_rate = 0;
  372. }
  373. void increase_polyphony_rate(float change) {
  374. polyphony_rate *= change;
  375. }
  376. void decrease_polyphony_rate(float change) {
  377. polyphony_rate /= change;
  378. }
  379. // Timbre function
  380. void set_timbre(float timbre) {
  381. note_timbre = timbre;
  382. }
  383. // Tempo functions
  384. void set_tempo(uint8_t tempo) {
  385. note_tempo = tempo;
  386. }
  387. void decrease_tempo(uint8_t tempo_change) {
  388. note_tempo += tempo_change;
  389. }
  390. void increase_tempo(uint8_t tempo_change) {
  391. if (note_tempo - tempo_change < 10) {
  392. note_tempo = 10;
  393. } else {
  394. note_tempo -= tempo_change;
  395. }
  396. }