/* MicroMDAEPiano MicroMDAEPiano is a port of the MDA-EPiano sound engine (https://sourceforge.net/projects/mda-vst/) for the Teensy-3.5/3.6 with audio shield. (c)2019 H. Wirtz This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include "EEPROMAnything.h" #include "mdaEPiano.h" #include "effect_modulated_delay.h" #ifdef USE_XFADE_DATA #include "mdaEPianoDataXfade.h" #else #include "mdaEPianoData.h" #endif #include "UI.hpp" #include "midi_devices.hpp" #include "config.h" //************************************************************************************************* //* GLOBAL VARIABLES //************************************************************************************************* // Audio configuration AudioPlayQueue queue_r; AudioPlayQueue queue_l; AudioAnalyzePeak peak_r; AudioAnalyzePeak peak_l; AudioEffectFreeverb freeverb_r; AudioEffectFreeverb freeverb_l; AudioMixer4 mixer_r; AudioMixer4 mixer_l; AudioAmplifier volume_r; AudioAmplifier volume_l; AudioAmplifier inverter; AudioEffectModulatedDelay modchorus_r; AudioEffectModulatedDelay modchorus_l; AudioSynthWaveform modulator; AudioConnection patchCord0(queue_r, peak_r); AudioConnection patchCord1(queue_l, peak_l); AudioConnection patchCord2(queue_r, freeverb_r); AudioConnection patchCord3(queue_l, freeverb_l); AudioConnection patchCord4(queue_r, 0, modchorus_r, 0); AudioConnection patchCord5(queue_l, 0, modchorus_l, 0); AudioConnection patchCord6(modulator, 0, modchorus_r, 1); AudioConnection patchCord7(modulator, inverter); AudioConnection patchCord8(inverter, 0, modchorus_l, 1); AudioConnection patchCord9(queue_r, 0, mixer_r, 0); AudioConnection patchCord10(queue_l, 0, mixer_l, 0); AudioConnection patchCord11(modchorus_r, 0, mixer_r, 2); AudioConnection patchCord12(modchorus_l, 0, mixer_l, 2); AudioConnection patchCord13(freeverb_r, 0, mixer_r, 1); AudioConnection patchCord14(freeverb_l, 0, mixer_l, 1); AudioConnection patchCord15(mixer_r, volume_r); AudioConnection patchCord16(mixer_l, volume_l); AudioOutputUSB usb1; AudioConnection patchCord17(volume_r, 0, usb1, 0); AudioConnection patchCord18(volume_l, 0, usb1, 1); AudioOutputI2S i2s1; AudioConnection patchCord19(volume_r, 0, i2s1, 0); AudioConnection patchCord20(volume_l, 0, i2s1, 1); AudioControlSGTL5000 sgtl5000_1; // Objects mdaEPiano* ep; extern void init_menus(void); extern int32_t encoder_value[NUM_ENCODER]; extern Bounce but[NUM_ENCODER]; // more variables uint8_t sound = 1; uint32_t xrun = 0; uint32_t overload = 0; uint32_t peak = 0; uint16_t render_time_max = 0; elapsedMicros fill_audio_buffer; elapsedMillis control_rate; const uint16_t audio_block_time_us = 1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES); config_t configuration = { 0xffff, // checksum ENC_DECAY_DEFAULT, // decay ENC_RELEASE_DEFAULT, // release ENC_HARDNESS_DEFAULT, // hardness ENC_TREBLE_DEFAULT, // treble ENC_STEREO_DEFAULT, // stereo ENC_TRANSPOSE_DEFAULT, // transpose ENC_TUNE_DEFAULT, // tune ENC_DETUNE_DEFAULT, // detune ENC_VELOCITY_SENSE_DEFAULT, // velocity_sense ENC_PAN_TREM_FREQUENCY_DEFAULT, // pan_trem_frequency ENC_PAN_TREM_LEVEL_DEFAULT, // pan_trem_level ENC_OVERDRIVE_DEFAULT, // overdrive ENC_COMP_GAIN_DEFAULT, // comp_gain ENC_COMP_RESPONSE_DEFAULT, // comp_response ENC_COMP_LIMIT_DEFAULT, // comp_limit ENC_COMP_THRESHOLD_DEFAULT, // comp_threshold ENC_COMP_ATTACK_DEFAULT, // comp_attack ENC_COMP_DECAY_DEFAULT, // comp_decay ENC_REVERB_ROOMSIZE_DEFAULT, // reverb_roomsize ENC_REVERB_DAMPING_DEFAULT, // reverb_damping ENC_REVERB_LEVEL_DEFAULT, // reverb_level ENC_CHORUS_FREQUENCY_DEFAULT, // chorus_frequency ENC_CHORUS_DELAY_DEFAULT, // chorus_delay ENC_CHORUS_INTENSITY_DEFAULT, // chorus_intensity ENC_CHORUS_LEVEL_DEFAULT, // chorus_level ENC_BASS_LR_LEVEL_DEFAULT, // bass_lr_level ENC_BASS_MONO_LEVEL_DEFAULT, // bass_mono_level ENC_EQ_BASS_DEFAULT, // eq_bass ENC_EQ_TREBLE_DEFAULT, // eq_treble ENC_LOUDNESS_DEFAULT, // loudness ENC_MIDI_CHANNEL_DEFAULT, // midi_channel ENC_MIDI_SOFT_THRU_DEFAULT, // midi_soft_thru ENC_MAX_POLY_DEFAULT, // max_poly ENC_MASTER_PAN_DEFAULT // pan }; uint8_t master_volume = ENC_MASTER_VOLUME_DEFAULT; int8_t pan = ENC_MASTER_PAN_DEFAULT; uint8_t eeprom_config_update_flag = 0; bool eeprom_master_volume_update_flag = false; elapsedMillis eeprom_master_volume_update_timer; #ifdef SHOW_CPU_LOAD_MSEC elapsedMillis cpu_mem_millis; #endif // Allocate the delay lines for left and right channels short l_delayline[CHORUS_DELAY_LENGTH_SAMPLES]; short r_delayline[CHORUS_DELAY_LENGTH_SAMPLES]; enum { VOL_MAIN, VOL_REVERB, VOL_CHORUS }; //************************************************************************************************* //* SETUP FUNCTION //************************************************************************************************* void setup() { Serial.begin(SERIAL_SPEED); pinMode(BUT_L_PIN, INPUT_PULLUP); pinMode(BUT_R_PIN, INPUT_PULLUP); init_menus(); // Debug output Serial.println(F("MicroMDAEPiano based on https://sourceforge.net/projects/mda-vst")); Serial.println(F("(c)2018/2019 H. Wirtz ")); Serial.println(F("https://codeberg.org/dcoredump/MicroMDAEPiano")); Serial.print(F("Data in PROGMEM: ")); Serial.print(sizeof(epianoDataXfade), DEC); Serial.println(F(" bytes")); Serial.println(); Serial.println(F("")); // create EPiano object ep = new mdaEPiano(); set_complete_configuration(); initial_values_from_eeprom(); setup_midi_devices(); // start audio card AudioNoInterrupts(); AudioMemory(AUDIO_MEM); sgtl5000_1.enable(); sgtl5000_1.dacVolumeRamp(); sgtl5000_1.dacVolume(1.0); sgtl5000_1.unmuteHeadphone(); sgtl5000_1.volume(0.5, 0.5); // Headphone volume sgtl5000_1.unmuteLineout(); sgtl5000_1.lineOutLevel(SGTL5000_LINEOUT_LEVEL); sgtl5000_1.audioPostProcessorEnable(); sgtl5000_1.eqSelect(TONE_CONTROLS); sgtl5000_1.autoVolumeEnable(); sgtl5000_1.enhanceBassEnable(); Serial.println(F("Teensy-Audio-Board enabled.")); #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC) // Initialize processor and memory measurements AudioProcessorUsageMaxReset(); AudioMemoryUsageMaxReset(); #endif AudioInterrupts(); Serial.print(F("AUDIO_BLOCK_SAMPLES=")); Serial.print(AUDIO_BLOCK_SAMPLES); Serial.print(F(" (Time per block=")); Serial.print(audio_block_time_us); Serial.println(F("us)")); if (!modchorus_r.begin(r_delayline, CHORUS_DELAY_LENGTH_SAMPLES)) { Serial.println(F("AudioEffectModulatedDelay - right channel begin failed")); while (1); } if (!modchorus_l.begin(l_delayline, CHORUS_DELAY_LENGTH_SAMPLES)) { Serial.println(F("AudioEffectModulatedDelay - left channel begin failed")); while (1); } // chorus modulation fixed modulator.begin(CHORUS_WAVEFORM); modulator.phase(0); modulator.offset(0.0); inverter.gain(-1.0); // change phase for second moduleated delay // internal mixing of original signal(0), reverb(1) and chorus(2) mixer_r.gain(VOL_MAIN, 0.4); mixer_l.gain(VOL_MAIN, 0.4); mixer_r.gain(VOL_REVERB, 0.2); mixer_l.gain(VOL_REVERB, 0.2); mixer_r.gain(VOL_CHORUS, 0.2); mixer_l.gain(VOL_CHORUS, 0.2); // set master volume set_master_volume(master_volume); Serial.println(F("")); #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC) Serial.println(); show_cpu_and_mem_usage(); cpu_mem_millis = 0; #endif } //************************************************************************************************* //* MAIN LOOP //************************************************************************************************* void loop() { int16_t* audio_buffer_r; // pointer to AUDIO_BLOCK_SAMPLES * sizeof(int16_t) int16_t* audio_buffer_l; // pointer to AUDIO_BLOCK_SAMPLES * sizeof(int16_t) // Main sound calculation if (queue_r.available() && queue_l.available() && fill_audio_buffer > audio_block_time_us - 10) { fill_audio_buffer = 0; #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC) if (cpu_mem_millis > SHOW_CPU_LOAD_MSEC) { show_cpu_and_mem_usage(); cpu_mem_millis = 0; } #endif audio_buffer_r = queue_r.getBuffer(); if (audio_buffer_r == NULL) { Serial.println(F("E: audio_buffer_r allocation problems!")); } audio_buffer_l = queue_l.getBuffer(); if (audio_buffer_l == NULL) { Serial.println(F("E: audio_buffer_l allocation problems!")); } elapsedMicros t1; ep->process(audio_buffer_r, audio_buffer_l); uint32_t t2 = t1; if (t2 > audio_block_time_us) // everything greater 2.9ms is a buffer underrun! xrun++; if (t2 > render_time_max) render_time_max = t2; if (peak_r.available()) { if (peak_r.read() > 1.00) peak++; } if (peak_l.available()) { if (peak_l.read() > 1.00) peak++; } queue_r.playBuffer(); queue_l.playBuffer(); } check_midi_devices(); // CONTROL-RATE-EVENT-HANDLING if (control_rate > CONTROL_RATE_MS) { control_rate = 0; handle_ui(); if ( eeprom_config_update_flag > 0 && ep->getActiveVoices() == 0) // write only to eeprom when no voice is active eeprom_config_update(); if (eeprom_master_volume_update_flag == true && eeprom_master_volume_update_timer > STORE_MASTER_VOLUME_MS) eeprom_master_volume_update(); } } //************************************************************************************************* //* PROGRAM FUNCTIONS //************************************************************************************************* void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity) { if (checkMidiChannel(inChannel)) { ep->noteOn(inNumber + configuration.transpose, inVelocity); } } void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity) { if (checkMidiChannel(inChannel)) { ep->noteOn(inNumber + configuration.transpose, 0); } } void handleControlChange(byte inChannel, byte inData1, byte inData2) { if (checkMidiChannel(inChannel)) { switch (inData1) { case 10: // Panorama configuration.pan = mapfloat(float(inData2), 0, 127, 0.0, 1.0); break; case 91: // Reverb level set_reverb_level(map(inData2, 0, 127, ENC_REVERB_LEVEL_MIN, ENC_REVERB_LEVEL_MAX)); break; case 92: // Tremolo level (same as modwheel) inData1 = 1; // now it's modwheel and can be processd by ep->processMidiController :-) break; case 93: // Chorus level set_chorus_level(map(inData2, 0, 127, ENC_CHORUS_LEVEL_MIN, ENC_CHORUS_LEVEL_MAX)); break; case 94: // Detune level ep->setDetune(mapfloat(float(inData2), 0, 127, 0.0, 1.0)); break; default: ep->processMidiController(inData1, inData2); break; } } } void handleAfterTouch(byte inChannel, byte inPressure) { ; } void handlePitchBend(byte inChannel, int inPitch) { ; } void handleProgramChange(byte inChannel, byte inProgram) { if (checkMidiChannel(inChannel)) { sound = inProgram; load_sound(); if (menu_system.get_currentScreen() == &load_sound_screen) menu_system.update(); } } void handleSystemExclusive(byte * data, uint len) { ; } void handleSystemExclusiveChunk(const byte * data, uint16_t len, bool last) { ; } void handleTimeCodeQuarterFrame(byte data) { ; } void handleAfterTouchPoly(byte inChannel, byte inNumber, byte inVelocity) { ; } void handleSongSelect(byte inSong) { ; } void handleTuneRequest(void) { ; } void handleClock(void) { ; } void handleStart(void) { ; } void handleContinue(void) { ; } void handleStop(void) { ; } void handleActiveSensing(void) { ; } void handleSystemReset(void) { ; } void handleRealTimeSystem(void) { ; } bool checkMidiChannel(byte inChannel) { // check for MIDI channel if (configuration.midi_channel == MIDI_CHANNEL_OMNI) { return (true); } else if (inChannel != configuration.midi_channel) { #ifdef SHOW_DEBUG Serial.print(F("Ignoring MIDI data on channel ")); Serial.print(inChannel); Serial.print(F("(listening on ")); Serial.print(configuration.midi_channel); Serial.println(F(")")); #endif return (false); } return (true); } void set_master_volume(uint8_t value) { //configuration.pan = 0; // BAD HACK! uint16_t tmp = map(value, ENC_MASTER_VOLUME_MIN, ENC_MASTER_VOLUME_MAX, 0, 0x3ff); float tmp2 = mapfloat(configuration.pan, ENC_MASTER_PAN_MIN, ENC_MASTER_PAN_MAX, 0.0, 1.0); float tmp3 = (float)(tmp * (tmp + 2)) / (float)(1 << 20); #ifdef SHOW_DEBUG Serial.print(F("Setting volume: VOL=")); Serial.print(value, DEC); Serial.print(F("[")); Serial.print(tmp3, 3); Serial.print(F("] PAN=")); Serial.print(configuration.pan, DEC); Serial.print(F("[")); Serial.print(tmp2, 3); Serial.print(F("] ")); Serial.print(tmp3 * sinf(tmp2 * PI / 2), 3); Serial.print(F("/")); Serial.println(tmp3 * cosf(tmp2 * PI / 2), 3); #endif // float v = (float)(a * (a + 2))/(float)(1 << 20); // (pseudo-) logarithmic curve for volume control // http://files.csound-tutorial.net/floss_manual/Release03/Cs_FM_03_ScrapBook/b-panning-and-spatialization.html volume_r.gain(tmp3 * sinf(tmp2 * PI / 2)); volume_l.gain(tmp3 * cosf(tmp2 * PI / 2)); eeprom_master_volume_update_flag = true; eeprom_master_volume_update_timer = 0; if (menu_system.get_currentScreen() == &master_volume_screen) menu_system.update(); } /****************************************************************************** EEPROM HELPER ******************************************************************************/ void config_from_eeprom(void) { uint32_t checksum; config_t tmp_conf; EEPROM_readAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), tmp_conf); checksum = crc32((byte*)&tmp_conf + 4, sizeof(tmp_conf) - 4); #ifdef SHOW_DEBUG Serial.print(F("Reading sound ")); Serial.print(sound, DEC); Serial.print(F(" from 0x")); Serial.print(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), HEX); Serial.print(F(" EEPROM checksum: 0x")); Serial.print(tmp_conf.checksum, HEX); Serial.print(F(" / 0x")); Serial.print(checksum, HEX); #endif if (checksum == tmp_conf.checksum) { EEPROM_readAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), configuration); #ifdef SHOW_DEBUG Serial.print(F(" - OK")); #endif } else { #ifdef SHOW_DEBUG Serial.println(F(" - mismatch -> loading initial configuration.")); #endif EEPROM.update(EEPROM_SOUND, sound); } set_complete_configuration(); #ifdef SHOW_DEBUG show_sound(); #endif } void initial_values_from_eeprom(void) { master_volume = EEPROM.read(EEPROM_MASTER_VOLUME); sound = EEPROM.read(EEPROM_SOUND); load_sound(); } void eeprom_config_write(uint8_t value) { eeprom_config_update_flag = value; } void eeprom_config_update(void) { configuration.checksum = crc32((byte*)&configuration + 4, sizeof(configuration) - 4); Serial.print(F("Updating EEPROM configuration for sound ")); Serial.print(eeprom_config_update_flag, DEC); Serial.print(F(" with checksum 0x")); Serial.print(configuration.checksum, HEX); Serial.print(F(" at 0x")); Serial.println(EEPROM_CONFIGURATIONS + sizeof(config_t) * (eeprom_config_update_flag - 1), HEX); EEPROM_writeAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (eeprom_config_update_flag - 1), configuration); eeprom_config_update_flag = 0; EEPROM.update(EEPROM_SOUND, sound); } void eeprom_master_volume_write(void) { eeprom_master_volume_update_flag = true; } void eeprom_master_volume_update(void) { eeprom_master_volume_update_flag = false; EEPROM.update(EEPROM_MASTER_VOLUME, master_volume); Serial.println(F("Updating EEPROM with master_volume")); } uint32_t crc32(byte * calc_start, uint16_t calc_bytes) // base code from https://www.arduino.cc/en/Tutorial/EEPROMCrc { const uint32_t crc_table[16] = { 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; uint32_t crc = ~0L; for (byte* index = calc_start ; index < (calc_start + calc_bytes) ; ++index) { crc = crc_table[(crc ^ *index) & 0x0f] ^ (crc >> 4); crc = crc_table[(crc ^ (*index >> 4)) & 0x0f] ^ (crc >> 4); crc = ~crc; } return (crc); } //************************************************************************************************* //* DEBUG FUNCTIONS //************************************************************************************************* #if defined (SHOW_DEBUG) && defined (SHOW_CPU_LOAD_MSEC) void show_cpu_and_mem_usage(void) { Serial.print(F("CPU: ")); Serial.print(AudioProcessorUsage(), DEC); Serial.print(F(" CPU MAX: ")); Serial.print(AudioProcessorUsageMax(), DEC); Serial.print(F(" MEM: ")); Serial.print(AudioMemoryUsage(), DEC); Serial.print(F(" MEM MAX: ")); Serial.print(AudioMemoryUsageMax(), DEC); Serial.print(F(" RENDER_TIME_MAX: ")); Serial.print(render_time_max, DEC); Serial.print(F(" XRUN: ")); Serial.print(xrun, DEC); Serial.print(F(" OVERLOAD: ")); Serial.print(overload, DEC); Serial.print(F(" PEAK: ")); Serial.print(peak, DEC); Serial.print(F(" ACTIVE_VOICES: ")); Serial.print(ep->getActiveVoices(), DEC); Serial.println(); AudioProcessorUsageMaxReset(); AudioMemoryUsageMaxReset(); render_time_max = 0; } void show_sound(void) { Serial.print(F("Master Volume: ")); Serial.println(master_volume, DEC); Serial.print(F("Sound: ")); Serial.println(sound, DEC); Serial.print(F("Checksum: 0x")); Serial.println(configuration.checksum, HEX); Serial.print(F("Decay: ")); Serial.println(configuration.decay, DEC); Serial.print(F("Release: ")); Serial.println(configuration.release, DEC); Serial.print(F("Hardness: ")); Serial.println(configuration.hardness, DEC); Serial.print(F("Treble: ")); Serial.println(configuration.treble, DEC); Serial.print(F("Stereo: ")); Serial.println(configuration.stereo, DEC); Serial.print(F("Transpose: ")); Serial.println(configuration.transpose, DEC); Serial.print(F("Tune: ")); Serial.println(configuration.tune, DEC); Serial.print(F("Detune: ")); Serial.println(configuration.detune, DEC); Serial.print(F("Velocity Sense: ")); Serial.println(configuration.velocity_sense, DEC); Serial.print(F("Pan Tremolo Frequency: ")); Serial.println(configuration.pan_trem_frequency, DEC); Serial.print(F("Pan Tremolo Level: ")); Serial.println(configuration.pan_trem_level, DEC); Serial.print(F("Ovedrive: ")); Serial.println(configuration.overdrive, DEC); Serial.print(F("Compressor Gain: ")); Serial.println(configuration.comp_gain, DEC); Serial.print(F("Compressor Respone: ")); Serial.println(configuration.comp_response, DEC); Serial.print(F("Compressor Limit: ")); Serial.println(configuration.comp_limit, DEC); Serial.print(F("Compressor Threshold: ")); Serial.println(configuration.comp_threshold, DEC); Serial.print(F("Compressor Attack: ")); Serial.println(configuration.comp_attack, DEC); Serial.print(F("Compressor Decay: ")); Serial.println(configuration.comp_decay, DEC); Serial.print(F("Reverb Roomsize: ")); Serial.println(configuration.reverb_roomsize, DEC); Serial.print(F("Reverb Damping: ")); Serial.println(configuration.reverb_damping, DEC); Serial.print(F("Reverb Level: ")); Serial.println(configuration.reverb_level, DEC); Serial.print(F("Chorus Frequency: ")); Serial.println(configuration.chorus_frequency, DEC); Serial.print(F("Chorus Delay: ")); Serial.println(configuration.chorus_delay, DEC); Serial.print(F("Chorus Intensity: ")); Serial.println(configuration.chorus_intensity, DEC); Serial.print(F("Chorus Level: ")); Serial.println(configuration.chorus_level, DEC); Serial.print(F("Bass L/R Level: ")); Serial.println(configuration.bass_lr_level, DEC); Serial.print(F("Bass Mono Level: ")); Serial.println(configuration.bass_mono_level, DEC); Serial.print(F("EQ Bass: ")); Serial.println(configuration.eq_bass, DEC); Serial.print(F("EQ Treble: ")); Serial.println(configuration.eq_treble, DEC); Serial.print(F("Loudness: ")); Serial.println(configuration.loudness, DEC); Serial.print(F("MIDI Channel: ")); Serial.println(configuration.midi_channel, DEC); Serial.print(F("MIDI Soft-Thru: ")); Serial.println(configuration.midi_soft_thru, DEC); Serial.print(F("Maximum Polyphony: ")); Serial.println(configuration.max_poly, DEC); Serial.print(F("Panorama: ")); Serial.println(configuration.pan, DEC); } #endif