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.

872 lines
28 KiB

  1. /*
  2. MicroMDAEPiano
  3. MicroMDAEPiano is a port of the MDA-EPiano sound engine
  4. (https://sourceforge.net/projects/mda-vst/) for the Teensy-3.5/3.6 with audio shield.
  5. (c)2019 H. Wirtz <wirtz@parasitstudio.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <Audio.h>
  19. #include <Wire.h>
  20. #include <SPI.h>
  21. #include <MIDI.h>
  22. #include <EEPROM.h>
  23. #include "EEPROMAnything.h"
  24. #include "mdaEPiano.h"
  25. #include "effect_modulated_delay.h"
  26. #ifdef USE_XFADE_DATA
  27. #include "mdaEPianoDataXfade.h"
  28. #else
  29. #include "mdaEPianoData.h"
  30. #endif
  31. #include "UI.hpp"
  32. #include "midi_devices.hpp"
  33. #include "config.h"
  34. //*************************************************************************************************
  35. //* GLOBAL VARIABLES
  36. //*************************************************************************************************
  37. // Audio configuration
  38. AudioPlayQueue queue_r;
  39. AudioPlayQueue queue_l;
  40. AudioAnalyzePeak peak_r;
  41. AudioAnalyzePeak peak_l;
  42. AudioEffectFreeverb freeverb_r;
  43. AudioEffectFreeverb freeverb_l;
  44. AudioMixer4 mixer_r;
  45. AudioMixer4 mixer_l;
  46. AudioAmplifier volume_r;
  47. AudioAmplifier volume_l;
  48. AudioAmplifier inverter;
  49. AudioEffectModulatedDelay modchorus_r;
  50. AudioEffectModulatedDelay modchorus_l;
  51. #if MOD_FILTER_OUTPUT != MOD_NO_FILTER_OUTPUT
  52. AudioFilterBiquad modchorus_filter_r;
  53. AudioFilterBiquad modchorus_filter_l;
  54. #endif
  55. AudioSynthWaveform modulator;
  56. AudioConnection patchCord0(queue_r, peak_r);
  57. AudioConnection patchCord1(queue_l, peak_l);
  58. AudioConnection patchCord2(queue_r, freeverb_r);
  59. AudioConnection patchCord3(queue_l, freeverb_l);
  60. AudioConnection patchCord4(queue_r, 0, modchorus_r, 0);
  61. AudioConnection patchCord5(queue_l, 0, modchorus_l, 0);
  62. AudioConnection patchCord6(modulator, 0, modchorus_r, 1);
  63. AudioConnection patchCord7(modulator, inverter);
  64. AudioConnection patchCord8(inverter, 0, modchorus_l, 1);
  65. AudioConnection patchCord9(queue_r, 0, mixer_r, 0);
  66. AudioConnection patchCord10(queue_l, 0, mixer_l, 0);
  67. #if MOD_FILTER_OUTPUT != MOD_NO_FILTER_OUTPUT
  68. AudioConnection patchCord11(modchorus_r, modchorus_filter_r);
  69. AudioConnection patchCord12(modchorus_l, modchorus_filter_l);
  70. AudioConnection patchCord13(modchorus_filter_r, 0, mixer_r, 2);
  71. AudioConnection patchCord14(modchorus_filter_l, 0, mixer_l, 2);
  72. #else
  73. AudioConnection patchCord11(modchorus_r, mixer_r);
  74. AudioConnection patchCord12(modchorus_l, mixer_l);
  75. #endif
  76. AudioConnection patchCord15(freeverb_r, 0, mixer_r, 1);
  77. AudioConnection patchCord16(freeverb_l, 0, mixer_l, 1);
  78. AudioConnection patchCord17(mixer_r, volume_r);
  79. AudioConnection patchCord18(mixer_l, volume_l);
  80. #ifdef USB_AUDIO
  81. AudioOutputUSB usb1;
  82. AudioConnection patchCord19(volume_r, 0, usb1, 0);
  83. AudioConnection patchCord20(volume_l, 0, usb1, 1);
  84. #endif
  85. AudioOutputI2S i2s1;
  86. AudioConnection patchCord21(volume_r, 0, i2s1, 0);
  87. AudioConnection patchCord22(volume_l, 0, i2s1, 1);
  88. AudioControlSGTL5000 sgtl5000_1;
  89. // Objects
  90. mdaEPiano* ep;
  91. extern void init_menus(void);
  92. extern int32_t encoder_value[NUM_ENCODER];
  93. extern Bounce but[NUM_ENCODER];
  94. // more variables
  95. uint8_t sound = 1;
  96. uint32_t xrun = 0;
  97. uint32_t overload = 0;
  98. uint32_t peak = 0;
  99. uint16_t render_time_max = 0;
  100. elapsedMicros fill_audio_buffer;
  101. elapsedMillis control_rate;
  102. const uint16_t audio_block_time_us = 1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES);
  103. config_t configuration = {
  104. 0xffff, // checksum
  105. ENC_DECAY_DEFAULT, // decay
  106. ENC_RELEASE_DEFAULT, // release
  107. ENC_HARDNESS_DEFAULT, // hardness
  108. ENC_TREBLE_DEFAULT, // treble
  109. ENC_STEREO_DEFAULT, // stereo
  110. ENC_TRANSPOSE_DEFAULT, // transpose
  111. ENC_TUNE_DEFAULT, // tune
  112. ENC_DETUNE_DEFAULT, // detune
  113. ENC_VELOCITY_SENSE_DEFAULT, // velocity_sense
  114. ENC_PAN_TREM_FREQUENCY_DEFAULT, // pan_trem_frequency
  115. ENC_PAN_TREM_LEVEL_DEFAULT, // pan_trem_level
  116. ENC_OVERDRIVE_DEFAULT, // overdrive
  117. ENC_COMP_GAIN_DEFAULT, // comp_gain
  118. ENC_COMP_RESPONSE_DEFAULT, // comp_response
  119. ENC_COMP_LIMIT_DEFAULT, // comp_limit
  120. ENC_COMP_THRESHOLD_DEFAULT, // comp_threshold
  121. ENC_COMP_ATTACK_DEFAULT, // comp_attack
  122. ENC_COMP_DECAY_DEFAULT, // comp_decay
  123. ENC_REVERB_ROOMSIZE_DEFAULT, // reverb_roomsize
  124. ENC_REVERB_DAMPING_DEFAULT, // reverb_damping
  125. ENC_REVERB_LEVEL_DEFAULT, // reverb_level
  126. ENC_CHORUS_FREQUENCY_DEFAULT, // chorus_frequency
  127. ENC_CHORUS_INTENSITY_DEFAULT, // chorus_intensity
  128. ENC_CHORUS_WAVEFORM_DEFAULT, // chorus_waveform
  129. ENC_CHORUS_LEVEL_DEFAULT, // chorus_level
  130. ENC_BASS_LR_LEVEL_DEFAULT, // bass_lr_level
  131. ENC_BASS_MONO_LEVEL_DEFAULT, // bass_mono_level
  132. ENC_EQ_BASS_DEFAULT, // eq_bass
  133. ENC_EQ_TREBLE_DEFAULT, // eq_treble
  134. ENC_LOUDNESS_DEFAULT, // loudness
  135. ENC_MIDI_CHANNEL_DEFAULT, // midi_channel
  136. ENC_MIDI_SOFT_THRU_DEFAULT, // midi_soft_thru
  137. ENC_MAX_POLY_DEFAULT, // max_poly
  138. ENC_MONO_DEFAULT, // mono
  139. ENC_MASTER_PAN_DEFAULT // pan
  140. };
  141. uint8_t master_volume = ENC_MASTER_VOLUME_DEFAULT;
  142. int8_t pan = ENC_MASTER_PAN_DEFAULT;
  143. uint8_t eeprom_config_update_flag = 0;
  144. bool eeprom_master_volume_update_flag = false;
  145. elapsedMillis eeprom_master_volume_update_timer;
  146. #ifdef SHOW_CPU_LOAD_MSEC
  147. elapsedMillis cpu_mem_millis;
  148. #endif
  149. #ifdef DEBUG_AUDIO
  150. elapsedMillis debug_audio_timer;
  151. #endif
  152. // Allocate the delay lines for left and right channels
  153. short l_delayline[MOD_DELAY_SAMPLE_BUFFER];
  154. short r_delayline[MOD_DELAY_SAMPLE_BUFFER];
  155. enum { VOL_MAIN, VOL_REVERB, VOL_CHORUS };
  156. //*************************************************************************************************
  157. //* SETUP FUNCTION
  158. //*************************************************************************************************
  159. void setup()
  160. {
  161. Serial.begin(SERIAL_SPEED);
  162. pinMode(BUT_L_PIN, INPUT_PULLUP);
  163. pinMode(BUT_R_PIN, INPUT_PULLUP);
  164. init_menus();
  165. // Debug output
  166. Serial.println(F("MicroMDAEPiano based on https://sourceforge.net/projects/mda-vst"));
  167. Serial.println(F("(c)2018/2019 H. Wirtz <wirtz@parasitstudio.de>"));
  168. Serial.println(F("https://codeberg.org/dcoredump/MicroMDAEPiano"));
  169. Serial.print(F("Data in PROGMEM: "));
  170. Serial.print(sizeof(epianoDataXfade), DEC);
  171. Serial.println(F(" bytes"));
  172. Serial.println();
  173. Serial.println(F("<setup start>"));
  174. // create EPiano object
  175. ep = new mdaEPiano();
  176. // set initial init configuration
  177. set_complete_configuration();
  178. setup_midi_devices();
  179. // start audio card
  180. AudioNoInterrupts();
  181. AudioMemory(AUDIO_MEM);
  182. sgtl5000_1.enable();
  183. sgtl5000_1.dacVolumeRamp();
  184. sgtl5000_1.dacVolume(1.0);
  185. sgtl5000_1.unmuteHeadphone();
  186. sgtl5000_1.volume(0.5, 0.5); // Headphone volume
  187. sgtl5000_1.unmuteLineout();
  188. sgtl5000_1.lineOutLevel(SGTL5000_LINEOUT_LEVEL);
  189. sgtl5000_1.audioPostProcessorEnable();
  190. sgtl5000_1.eqSelect(TONE_CONTROLS);
  191. sgtl5000_1.autoVolumeEnable();
  192. sgtl5000_1.enhanceBassEnable();
  193. Serial.println(F("Teensy-Audio-Board enabled."));
  194. #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
  195. // Initialize processor and memory measurements
  196. AudioProcessorUsageMaxReset();
  197. AudioMemoryUsageMaxReset();
  198. #endif
  199. AudioInterrupts();
  200. Serial.print(F("AUDIO_BLOCK_SAMPLES="));
  201. Serial.print(AUDIO_BLOCK_SAMPLES);
  202. Serial.print(F(" (Time per block="));
  203. Serial.print(audio_block_time_us);
  204. Serial.println(F("us)"));
  205. if (!modchorus_r.begin(r_delayline, MOD_DELAY_SAMPLE_BUFFER)) {
  206. Serial.println(F("AudioEffectModulatedDelay - right channel begin failed"));
  207. while (1);
  208. }
  209. if (!modchorus_l.begin(l_delayline, MOD_DELAY_SAMPLE_BUFFER)) {
  210. Serial.println(F("AudioEffectModulatedDelay - left channel begin failed"));
  211. while (1);
  212. }
  213. #ifdef DEBUG
  214. Serial.print(F("MOD_DELAY_SAMPLE_BUFFER="));
  215. Serial.print(MOD_DELAY_SAMPLE_BUFFER, DEC);
  216. Serial.println(F(" samples"));
  217. #endif
  218. // chorus modulation fixed
  219. modulator.begin(MOD_WAVEFORM);
  220. modulator.phase(0);
  221. modulator.amplitude(0.5);
  222. modulator.offset(0.0);
  223. #if MOD_FILTER_OUTPUT == MOD_BUTTERWORTH_FILTER_OUTPUT
  224. // Butterworth filter, 12 db/octave
  225. modchorus_filter_r.setLowpass(0, MOD_FILTER_CUTOFF_HZ, 0.707);
  226. modchorus_filter_l.setLowpass(0, MOD_FILTER_CUTOFF_HZ, 0.707);
  227. #elif MOD_FILTER_OUTPUT == MOD_LINKWITZ_RILEY_FILTER_OUTPUT
  228. // Linkwitz-Riley filter, 48 dB/octave
  229. modchorus_filter_r.setLowpass(0, MOD_FILTER_CUTOFF_HZ, 0.54);
  230. modchorus_filter_r.setLowpass(1, MOD_FILTER_CUTOFF_HZ, 1.3);
  231. modchorus_filter_r.setLowpass(2, MOD_FILTER_CUTOFF_HZ, 0.54);
  232. modchorus_filter_r.setLowpass(3, MOD_FILTER_CUTOFF_HZ, 1.3);
  233. modchorus_filter_l.setLowpass(0, MOD_FILTER_CUTOFF_HZ, 0.54);
  234. modchorus_filter_l.setLowpass(1, MOD_FILTER_CUTOFF_HZ, 1.3);
  235. modchorus_filter_l.setLowpass(2, MOD_FILTER_CUTOFF_HZ, 0.54);
  236. modchorus_filter_l.setLowpass(3, MOD_FILTER_CUTOFF_HZ, 1.3);
  237. #endif
  238. // internal mixing of original signal(0), reverb(1) and chorus(2)
  239. mixer_r.gain(VOL_MAIN, 0.5);
  240. mixer_l.gain(VOL_MAIN, 0.5);
  241. mixer_r.gain(VOL_REVERB, 0.2);
  242. mixer_l.gain(VOL_REVERB, 0.2);
  243. mixer_r.gain(VOL_CHORUS, 0.2);
  244. mixer_l.gain(VOL_CHORUS, 0.2);
  245. // Stereo/Mono initial setup
  246. if (configuration.mono == 0)
  247. {
  248. inverter.gain(-1.0); // change phase for second modulated delay (faked stereo mode)
  249. }
  250. else
  251. {
  252. inverter.gain(1.0);
  253. configuration.pan = ENC_MASTER_PAN_DEFAULT;
  254. }
  255. // set master volume
  256. set_master_volume(master_volume);
  257. // load last configuration used
  258. initial_values_from_eeprom();
  259. // init random generator
  260. srand(analogRead(A0));
  261. Serial.println(F("<setup end>"));
  262. #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
  263. Serial.println();
  264. show_cpu_and_mem_usage();
  265. cpu_mem_millis = 0;
  266. #endif
  267. }
  268. //*************************************************************************************************
  269. //* MAIN LOOP
  270. //*************************************************************************************************
  271. void loop()
  272. {
  273. int16_t* audio_buffer_r; // pointer to AUDIO_BLOCK_SAMPLES * sizeof(int16_t)
  274. int16_t* audio_buffer_l; // pointer to AUDIO_BLOCK_SAMPLES * sizeof(int16_t)
  275. // Main sound calculation
  276. if (queue_r.available() && queue_l.available() && fill_audio_buffer > audio_block_time_us - 10)
  277. {
  278. fill_audio_buffer = 0;
  279. #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
  280. if (cpu_mem_millis > SHOW_CPU_LOAD_MSEC)
  281. {
  282. show_cpu_and_mem_usage();
  283. cpu_mem_millis = 0;
  284. }
  285. #endif
  286. audio_buffer_r = queue_r.getBuffer();
  287. if (audio_buffer_r == NULL)
  288. {
  289. Serial.println(F("E: audio_buffer_r allocation problems!"));
  290. }
  291. audio_buffer_l = queue_l.getBuffer();
  292. if (audio_buffer_l == NULL)
  293. {
  294. Serial.println(F("E: audio_buffer_l allocation problems!"));
  295. }
  296. elapsedMicros t1;
  297. ep->process(audio_buffer_r, audio_buffer_l);
  298. uint32_t t2 = t1;
  299. if (t2 > audio_block_time_us) // everything greater 2.9ms is a buffer underrun!
  300. xrun++;
  301. if (t2 > render_time_max)
  302. render_time_max = t2;
  303. if (peak_r.available())
  304. {
  305. if (peak_r.read() > 1.00)
  306. peak++;
  307. }
  308. if (peak_l.available())
  309. {
  310. if (peak_l.read() > 1.00)
  311. peak++;
  312. }
  313. queue_r.playBuffer();
  314. queue_l.playBuffer();
  315. }
  316. check_midi_devices();
  317. // CONTROL-RATE-EVENT-HANDLING
  318. if (control_rate > CONTROL_RATE_MS)
  319. {
  320. control_rate = 0;
  321. handle_ui();
  322. if ( eeprom_config_update_flag > 0 && ep->getActiveVoices() == 0) // write only to eeprom when no voice is active
  323. eeprom_config_update();
  324. if (eeprom_master_volume_update_flag == true && eeprom_master_volume_update_timer > STORE_MASTER_VOLUME_MS && ep->getActiveVoices() == 0)
  325. eeprom_master_volume_update();
  326. }
  327. #ifdef DEBUG_AUDIO
  328. if (debug_audio_timer > DEBUG_AUDIO)
  329. {
  330. ep->noteOn(60 + rand() % 108, rand() % 128);
  331. debug_audio_timer = 0;
  332. }
  333. #endif
  334. }
  335. //*************************************************************************************************
  336. //* PROGRAM FUNCTIONS
  337. //*************************************************************************************************
  338. void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity)
  339. {
  340. if (checkMidiChannel(inChannel))
  341. {
  342. ep->noteOn(inNumber + configuration.transpose, inVelocity);
  343. }
  344. }
  345. void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity)
  346. {
  347. if (checkMidiChannel(inChannel))
  348. {
  349. ep->noteOn(inNumber + configuration.transpose, 0);
  350. }
  351. }
  352. void handleControlChange(byte inChannel, byte inData1, byte inData2)
  353. {
  354. if (checkMidiChannel(inChannel))
  355. {
  356. switch (inData1)
  357. {
  358. // Standard MIDI-CC
  359. case MIDI_CC_PANORAMA: // Panorama
  360. configuration.pan = map(inData2, 0, 127, ENC_MASTER_PAN_MIN, ENC_MASTER_PAN_MAX);
  361. break;
  362. case MIDI_CC_REVERB_SEND: // Reverb level
  363. set_reverb_level(map(inData2, 0, 127, ENC_REVERB_LEVEL_MIN, ENC_REVERB_LEVEL_MAX));
  364. break;
  365. case MIDI_CC_TREMOLO_DEPTH: // Tremolo level (same as modwheel)
  366. inData1 = 1; // now it's modwheel and can be processd by ep->processMidiController :-)
  367. break;
  368. case MIDI_CC_CHORUS_SEND: // Chorus level
  369. set_chorus_level(map(inData2, 0, 127, ENC_CHORUS_LEVEL_MIN, ENC_CHORUS_LEVEL_MAX));
  370. break;
  371. case MIDI_CC_DETUNE_DEPTH: // Detune level
  372. ep->setDetune(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  373. break;
  374. // Own MIDI-CC mapping
  375. case MIDI_CC_EP_DECAY:
  376. ep->setDecay(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  377. break;
  378. case MIDI_CC_EP_RELEASE:
  379. ep->setRelease(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  380. break;
  381. case MIDI_CC_EP_HARDNESS:
  382. ep->setHardness(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  383. break;
  384. case MIDI_CC_EP_TREBLE:
  385. ep->setTreble(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  386. break;
  387. case MIDI_CC_EP_STEREO:
  388. ep->setStereo(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  389. break;
  390. case MIDI_CC_EP_TUNE:
  391. ep->setTune(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  392. break;
  393. case MIDI_CC_EP_VELOCITY_SENSE:
  394. ep->setVelocitySense(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  395. break;
  396. case MIDI_CC_EP_TREM_FRQ:
  397. ep->setPanLFO(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  398. break;
  399. case MIDI_CC_EP_OVERDRIVE:
  400. ep->setOverdrive(mapfloat(float(inData2), 0, 127, 0.0, 1.0));
  401. break;
  402. case MIDI_CC_COMP_GAIN:
  403. set_comp_gain(map(inData2, 0, 127, ENC_COMP_GAIN_MIN, ENC_COMP_GAIN_MAX));
  404. break;
  405. case MIDI_CC_COMP_REPOSNE:
  406. set_comp_response(map(inData2, 0, 127, ENC_COMP_RESPONSE_MIN, ENC_COMP_RESPONSE_MAX));
  407. break;
  408. case MIDI_CC_COMP_LIMIT:
  409. set_comp_limit(map(inData2, 0, 127, ENC_COMP_LIMIT_MIN, ENC_COMP_LIMIT_MAX));
  410. break;
  411. case MIDI_CC_COMP_THRESHOLD:
  412. set_comp_threshold(map(inData2, 0, 127, ENC_COMP_THRESHOLD_MIN, ENC_COMP_THRESHOLD_MAX));
  413. break;
  414. case MIDI_CC_COMP_ATTACK:
  415. set_comp_attack(map(inData2, 0, 127, ENC_COMP_ATTACK_MIN, ENC_COMP_ATTACK_MAX));
  416. break;
  417. case MIDI_CC_COMP_DECAY:
  418. set_comp_decay(map(inData2, 0, 127, ENC_COMP_DECAY_MIN, ENC_COMP_DECAY_MAX));
  419. break;
  420. case MIDI_CC_REVERB_ROOMSIZE:
  421. set_reverb_roomsize(map(inData2, 0, 127, ENC_REVERB_ROOMSIZE_MIN, ENC_REVERB_ROOMSIZE_MAX));
  422. break;
  423. case MIDI_CC_REVERB_DAMPING:
  424. set_reverb_damping(map(inData2, 0, 127, ENC_REVERB_DAMPING_MIN, ENC_REVERB_DAMPING_MAX));
  425. break;
  426. case MIDI_CC_CHORUS_FREQUENCY:
  427. set_chorus_frequency(map(inData2, 0, 127, ENC_CHORUS_FREQUENCY_MIN, ENC_CHORUS_FREQUENCY_MAX));
  428. break;
  429. case MIDI_CC_CHORUS_INTENSITY:
  430. set_chorus_intensity(map(inData2, 0, 127, ENC_CHORUS_INTENSITY_MIN, ENC_CHORUS_INTENSITY_MAX));
  431. break;
  432. case MIDI_CC_CHORUS_WAVEFORM:
  433. set_chorus_waveform(map(inData2, 0, 127, ENC_CHORUS_WAVEFORM_MIN, ENC_CHORUS_WAVEFORM_MAX));
  434. break;
  435. case MIDI_CC_BASS_LR_LEVEL:
  436. set_bass_lr_level(map(inData2, 0, 127, ENC_BASS_LR_LEVEL_MIN, ENC_BASS_LR_LEVEL_MAX));
  437. break;
  438. case MIDI_CC_BASS_MONO_LEVEL:
  439. set_bass_mono_level(map(inData2, 0, 127, ENC_BASS_MONO_LEVEL_MIN, ENC_BASS_MONO_LEVEL_MAX));
  440. break;
  441. case MIDI_CC_EQ_BASS:
  442. set_eq_bass(map(inData2, 0, 127, ENC_EQ_BASS_MIN, ENC_EQ_BASS_MAX));
  443. break;
  444. case MIDI_CC_EQ_TREBLE:
  445. set_eq_treble(map(inData2, 0, 127, ENC_EQ_TREBLE_MIN, ENC_EQ_TREBLE_MAX));
  446. break;
  447. case MIDI_CC_MIDI_SOFT_THRU:
  448. set_midi_soft_thru(map(inData2, 0, 127, ENC_MIDI_SOFT_THRU_MIN, ENC_MIDI_SOFT_THRU_MAX));
  449. break;
  450. case MIDI_CC_MONO:
  451. set_mono(map(inData2, 0, 127, ENC_MONO_MIN, ENC_MONO_MAX));
  452. break;
  453. default:
  454. ep->processMidiController(inData1, inData2);
  455. break;
  456. }
  457. }
  458. }
  459. void handleAfterTouch(byte inChannel, byte inPressure)
  460. {
  461. ;
  462. }
  463. void handlePitchBend(byte inChannel, int inPitch)
  464. {
  465. ;
  466. }
  467. void handleProgramChange(byte inChannel, byte inProgram)
  468. {
  469. if (checkMidiChannel(inChannel))
  470. {
  471. sound = inProgram;
  472. load_sound();
  473. if (menu_system.get_currentScreen() == &load_sound_screen)
  474. menu_system.update();
  475. }
  476. }
  477. void handleSystemExclusive(byte * data, uint len)
  478. {
  479. ;
  480. }
  481. void handleSystemExclusiveChunk(const byte * data, uint16_t len, bool last)
  482. {
  483. ;
  484. }
  485. void handleTimeCodeQuarterFrame(byte data)
  486. {
  487. ;
  488. }
  489. void handleAfterTouchPoly(byte inChannel, byte inNumber, byte inVelocity)
  490. {
  491. ;
  492. }
  493. void handleSongSelect(byte inSong)
  494. {
  495. ;
  496. }
  497. void handleTuneRequest(void)
  498. {
  499. ;
  500. }
  501. void handleClock(void)
  502. {
  503. ;
  504. }
  505. void handleStart(void)
  506. {
  507. ;
  508. }
  509. void handleContinue(void)
  510. {
  511. ;
  512. }
  513. void handleStop(void)
  514. {
  515. ;
  516. }
  517. void handleActiveSensing(void)
  518. {
  519. ;
  520. }
  521. void handleSystemReset(void)
  522. {
  523. ;
  524. }
  525. void handleRealTimeSystem(void)
  526. {
  527. ;
  528. }
  529. bool checkMidiChannel(byte inChannel)
  530. {
  531. // check for MIDI channel
  532. if (configuration.midi_channel == MIDI_CHANNEL_OMNI)
  533. {
  534. return (true);
  535. }
  536. else if (inChannel != configuration.midi_channel)
  537. {
  538. #ifdef SHOW_DEBUG
  539. Serial.print(F("Ignoring MIDI data on channel "));
  540. Serial.print(inChannel);
  541. Serial.print(F("(listening on "));
  542. Serial.print(configuration.midi_channel);
  543. Serial.println(F(")"));
  544. #endif
  545. return (false);
  546. }
  547. return (true);
  548. }
  549. void set_master_volume(uint8_t value)
  550. {
  551. //configuration.pan = 0; // BAD HACK!
  552. uint16_t tmp = map(value, ENC_MASTER_VOLUME_MIN, ENC_MASTER_VOLUME_MAX, 0, 0x3ff);
  553. float tmp2 = mapfloat(configuration.pan, ENC_MASTER_PAN_MIN, ENC_MASTER_PAN_MAX, 0.0, 1.0);
  554. float tmp3 = (float)(tmp * (tmp + 2)) / (float)(1 << 20);
  555. #ifdef SHOW_DEBUG
  556. Serial.print(F("Setting volume: VOL="));
  557. Serial.print(value, DEC);
  558. Serial.print(F("["));
  559. Serial.print(tmp3, 3);
  560. Serial.print(F("] PAN="));
  561. Serial.print(configuration.pan, DEC);
  562. Serial.print(F("["));
  563. Serial.print(tmp2, 3);
  564. Serial.print(F("] "));
  565. Serial.print(tmp3 * sinf(tmp2 * PI / 2), 3);
  566. Serial.print(F("/"));
  567. Serial.println(tmp3 * cosf(tmp2 * PI / 2), 3);
  568. #endif
  569. // float v = (float)(a * (a + 2))/(float)(1 << 20); // (pseudo-) logarithmic curve for volume control
  570. // http://files.csound-tutorial.net/floss_manual/Release03/Cs_FM_03_ScrapBook/b-panning-and-spatialization.html
  571. volume_r.gain(tmp3 * sinf(tmp2 * PI / 2));
  572. volume_l.gain(tmp3 * cosf(tmp2 * PI / 2));
  573. if (configuration.mono == 2)
  574. volume_l.gain(0.0);
  575. else if (configuration.mono == 3)
  576. volume_r.gain(0.0);
  577. eeprom_master_volume_update_flag = true;
  578. eeprom_master_volume_update_timer = 0;
  579. if (menu_system.get_currentScreen() == &master_volume_screen)
  580. menu_system.update();
  581. }
  582. /******************************************************************************
  583. EEPROM HELPER
  584. ******************************************************************************/
  585. void config_from_eeprom(void)
  586. {
  587. uint32_t checksum;
  588. config_t tmp_conf;
  589. EEPROM_readAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), tmp_conf);
  590. checksum = crc32((byte*)&tmp_conf + 4, sizeof(tmp_conf) - 4);
  591. #ifdef SHOW_DEBUG
  592. Serial.print(F("Reading sound "));
  593. Serial.print(sound, DEC);
  594. Serial.print(F(" from 0x"));
  595. Serial.print(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), HEX);
  596. Serial.print(F(" EEPROM checksum: 0x"));
  597. Serial.print(tmp_conf.checksum, HEX);
  598. Serial.print(F(" / 0x"));
  599. Serial.print(checksum, HEX);
  600. #endif
  601. if (checksum == tmp_conf.checksum)
  602. {
  603. EEPROM_readAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), configuration);
  604. #ifdef SHOW_DEBUG
  605. Serial.println(F(" - OK"));
  606. #endif
  607. }
  608. else
  609. {
  610. #ifdef SHOW_DEBUG
  611. Serial.println(F(" - mismatch -> loading initial configuration."));
  612. #endif
  613. EEPROM.update(EEPROM_SOUND, sound);
  614. }
  615. set_complete_configuration();
  616. #ifdef SHOW_DEBUG
  617. show_sound();
  618. #endif
  619. }
  620. void initial_values_from_eeprom(void)
  621. {
  622. master_volume = EEPROM.read(EEPROM_MASTER_VOLUME);
  623. sound = EEPROM.read(EEPROM_SOUND);
  624. load_sound();
  625. }
  626. void eeprom_config_write(uint8_t value)
  627. {
  628. eeprom_config_update_flag = value;
  629. }
  630. void eeprom_config_update(void)
  631. {
  632. configuration.checksum = crc32((byte*)&configuration + 4, sizeof(configuration) - 4);
  633. Serial.print(F("Updating EEPROM configuration for sound "));
  634. Serial.print(eeprom_config_update_flag, DEC);
  635. Serial.print(F(" with checksum 0x"));
  636. Serial.print(configuration.checksum, HEX);
  637. Serial.print(F(" at 0x"));
  638. Serial.println(EEPROM_CONFIGURATIONS + sizeof(config_t) * (eeprom_config_update_flag - 1), HEX);
  639. EEPROM_writeAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (eeprom_config_update_flag - 1), configuration);
  640. eeprom_config_update_flag = 0;
  641. EEPROM.update(EEPROM_SOUND, sound);
  642. }
  643. void eeprom_master_volume_write(void)
  644. {
  645. eeprom_master_volume_update_flag = true;
  646. }
  647. void eeprom_master_volume_update(void)
  648. {
  649. eeprom_master_volume_update_flag = false;
  650. EEPROM.update(EEPROM_MASTER_VOLUME, master_volume);
  651. Serial.println(F("Updating EEPROM with master_volume"));
  652. }
  653. uint32_t crc32(byte * calc_start, uint16_t calc_bytes) // base code from https://www.arduino.cc/en/Tutorial/EEPROMCrc
  654. {
  655. const uint32_t crc_table[16] =
  656. {
  657. 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
  658. 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
  659. 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
  660. 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
  661. };
  662. uint32_t crc = ~0L;
  663. for (byte* index = calc_start ; index < (calc_start + calc_bytes) ; ++index)
  664. {
  665. crc = crc_table[(crc ^ *index) & 0x0f] ^ (crc >> 4);
  666. crc = crc_table[(crc ^ (*index >> 4)) & 0x0f] ^ (crc >> 4);
  667. crc = ~crc;
  668. }
  669. return (crc);
  670. }
  671. //*************************************************************************************************
  672. //* DEBUG FUNCTIONS
  673. //*************************************************************************************************
  674. #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
  675. void show_cpu_and_mem_usage(void)
  676. {
  677. Serial.print(F("CPU: "));
  678. Serial.print(AudioProcessorUsage(), DEC);
  679. Serial.print(F(" CPU MAX: "));
  680. Serial.print(AudioProcessorUsageMax(), DEC);
  681. Serial.print(F(" MEM: "));
  682. Serial.print(AudioMemoryUsage(), DEC);
  683. Serial.print(F(" MEM MAX: "));
  684. Serial.print(AudioMemoryUsageMax(), DEC);
  685. Serial.print(F(" RENDER_TIME_MAX: "));
  686. Serial.print(render_time_max, DEC);
  687. Serial.print(F(" XRUN: "));
  688. Serial.print(xrun, DEC);
  689. Serial.print(F(" OVERLOAD: "));
  690. Serial.print(overload, DEC);
  691. Serial.print(F(" PEAK: "));
  692. Serial.print(peak, DEC);
  693. Serial.print(F(" ACTIVE_VOICES: "));
  694. Serial.print(ep->getActiveVoices(), DEC);
  695. Serial.println();
  696. AudioProcessorUsageMaxReset();
  697. AudioMemoryUsageMaxReset();
  698. render_time_max = 0;
  699. }
  700. void show_sound(void)
  701. {
  702. Serial.println(F("======SHOW=SOUND=CONFIGURATION======"));
  703. Serial.print(F("Master Volume: "));
  704. Serial.println(master_volume, DEC);
  705. Serial.print(F("Sound: "));
  706. Serial.println(sound, DEC);
  707. Serial.print(F("Checksum: 0x"));
  708. Serial.println(configuration.checksum, HEX);
  709. Serial.print(F("Decay: "));
  710. Serial.println(configuration.decay, DEC);
  711. Serial.print(F("Release: "));
  712. Serial.println(configuration.release, DEC);
  713. Serial.print(F("Hardness: "));
  714. Serial.println(configuration.hardness, DEC);
  715. Serial.print(F("Treble: "));
  716. Serial.println(configuration.treble, DEC);
  717. Serial.print(F("Stereo: "));
  718. Serial.println(configuration.stereo, DEC);
  719. Serial.print(F("Transpose: "));
  720. Serial.println(configuration.transpose, DEC);
  721. Serial.print(F("Tune: "));
  722. Serial.println(configuration.tune, DEC);
  723. Serial.print(F("Detune: "));
  724. Serial.println(configuration.detune, DEC);
  725. Serial.print(F("Velocity Sense: "));
  726. Serial.println(configuration.velocity_sense, DEC);
  727. Serial.print(F("Pan Tremolo Frequency: "));
  728. Serial.println(configuration.pan_trem_frequency, DEC);
  729. Serial.print(F("Pan Tremolo Level: "));
  730. Serial.println(configuration.pan_trem_level, DEC);
  731. Serial.print(F("Ovedrive: "));
  732. Serial.println(configuration.overdrive, DEC);
  733. Serial.print(F("Compressor Gain: "));
  734. Serial.println(configuration.comp_gain, DEC);
  735. Serial.print(F("Compressor Respone: "));
  736. Serial.println(configuration.comp_response, DEC);
  737. Serial.print(F("Compressor Limit: "));
  738. Serial.println(configuration.comp_limit, DEC);
  739. Serial.print(F("Compressor Threshold: "));
  740. Serial.println(configuration.comp_threshold, DEC);
  741. Serial.print(F("Compressor Attack: "));
  742. Serial.println(configuration.comp_attack, DEC);
  743. Serial.print(F("Compressor Decay: "));
  744. Serial.println(configuration.comp_decay, DEC);
  745. Serial.print(F("Reverb Roomsize: "));
  746. Serial.println(configuration.reverb_roomsize, DEC);
  747. Serial.print(F("Reverb Damping: "));
  748. Serial.println(configuration.reverb_damping, DEC);
  749. Serial.print(F("Reverb Level: "));
  750. Serial.println(configuration.reverb_level, DEC);
  751. Serial.print(F("Chorus Frequency: "));
  752. Serial.println(configuration.chorus_frequency, DEC);
  753. Serial.print(F("Chorus Intensity: "));
  754. Serial.println(configuration.chorus_intensity, DEC);
  755. Serial.print(F("Chorus Waveform: "));
  756. Serial.println(configuration.chorus_waveform, DEC);
  757. Serial.print(F("Chorus Level: "));
  758. Serial.println(configuration.chorus_level, DEC);
  759. Serial.print(F("Bass L/R Level: "));
  760. Serial.println(configuration.bass_lr_level, DEC);
  761. Serial.print(F("Bass Mono Level: "));
  762. Serial.println(configuration.bass_mono_level, DEC);
  763. Serial.print(F("EQ Bass: "));
  764. Serial.println(configuration.eq_bass, DEC);
  765. Serial.print(F("EQ Treble: "));
  766. Serial.println(configuration.eq_treble, DEC);
  767. Serial.print(F("Loudness: "));
  768. Serial.println(configuration.loudness, DEC);
  769. Serial.print(F("MIDI Channel: "));
  770. Serial.println(configuration.midi_channel, DEC);
  771. Serial.print(F("MIDI Soft-Thru: "));
  772. Serial.println(configuration.midi_soft_thru, DEC);
  773. Serial.print(F("Maximum Polyphony: "));
  774. Serial.println(configuration.max_poly, DEC);
  775. Serial.print(F("Audio-Mono: "));
  776. Serial.println(configuration.mono, DEC);
  777. Serial.print(F("Panorama: "));
  778. Serial.println(configuration.pan, DEC);
  779. Serial.println(F("======END=OF=SOUND=CONFIGURATION======="));
  780. }
  781. #endif