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.
MicroDexed/MicroDexed.ino

1512 lines
55 KiB

/*
MicroDexed
MicroDexed is a port of the Dexed sound engine
(https://github.com/asb2m10/dexed) for the Teensy-3.5/3.6 with audio shield.
Dexed ist heavily based on https://github.com/google/music-synthesizer-for-android
(c)2018,2019 H. Wirtz <wirtz@parasitstudio.de>
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 <limits.h>
#include "config.h"
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <MIDI.h>
#include <EEPROM.h>
#include "midi_devices.hpp"
#include "dexed.h"
#include "dexed_sysex.h"
#include "effect_modulated_delay.h"
#include "effect_stereo_mono.h"
#include "PluginFx.h"
#include "SoftenValue.hpp"
#include "UI.hpp"
#include "source_microdexed.h"
// Audio engines
AudioAnalyzePeak peak1;
AudioSourceMicroDexed* MicroDexed[NUM_DEXED];
AudioEffectDelay delay1;
AudioEffectModulatedDelay modchorus;
AudioSynthWaveform modulator;
AudioMixer4 chorus_mixer;
AudioMixer4 delay_mixer;
AudioMixer4 delay_fb_mixer;
AudioMixer4 master_mixer_r;
AudioMixer4 master_mixer_l;
AudioAmplifier volume_r;
AudioAmplifier volume_l;
AudioAmplifier modchorus_inverter;
#if defined(AUDIO_DEVICE_USB)
AudioOutputUSB usb1;
#endif
AudioEffectStereoMono stereomono1;
// Audio connections
AudioConnection patchCord1(modulator, 0, modchorus, 1);
AudioConnection patchCord2(chorus_mixer, 0, modchorus, 0);
AudioConnection patchCord3(delay_mixer, 0, delay_fb_mixer, 0);
AudioConnection patchCord4(delay_fb_mixer, delay1);
AudioConnection patchCord5(delay1, 0, delay_fb_mixer, 1);
AudioConnection patchCord6(delay_mixer, 0, master_mixer_r, 2);
AudioConnection patchCord7(delay_mixer, 0, master_mixer_l, 2);
AudioConnection patchCord8(master_mixer_r, volume_r);
AudioConnection patchCord9(master_mixer_l, volume_l);
AudioConnection patchCord10(volume_r, 0, stereomono1, 0);
AudioConnection patchCord11(volume_l, 0, stereomono1, 1);
#if MOD_FILTER_OUTPUT != MOD_NO_FILTER_OUTPUT
AudioFilterBiquad modchorus_filter;
AudioConnection patchCord12(modchorus, modchorus_filter);
AudioConnection patchCord13(modchorus_filter, 0, master_mixer_r, 3);
AudioConnection patchCord14(modchorus_filter, modchorus_inverter);
AudioConnection patchCord15(modchorus_inverter, 0, master_mixer_l, 3);
#else
AudioConnection patchCord16(modchorus, 0, master_mixer_r, 3);
AudioConnection patchCord17(modchorus, modchorus_inverter);
AudioConnection patchCord18(modchorus_inverter, 0, master_mixer_l, 3);
#endif
#if defined(USE_REVERB)
AudioMixer4 reverb_mixer;
AudioEffectFreeverbStereo freeverbs1;
AudioConnection patchCord19(reverb_mixer, 0, freeverbs1, 0);
AudioConnection patchCord20(freeverbs1, 0, master_mixer_r, 1);
AudioConnection patchCord21(freeverbs1, 1, master_mixer_l, 1);
#endif
#ifdef AUDIO_DEVICE_USB
AudioConnection patchCord22(stereomono1, 0, usb1, 0);
AudioConnection patchCord23(stereomono1, 1, usb1, 1);
#endif
#if defined(TEENSY_AUDIO_BOARD)
AudioOutputI2S i2s1;
AudioConnection patchCord24(stereomono1, 0, i2s1, 0);
AudioConnection patchCord25(stereomono1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
#elif defined (I2S_AUDIO_ONLY)
AudioOutputI2S i2s1;
AudioConnection patchCord26(stereomono1, 0, i2s1, 0);
AudioConnection patchCord27(stereomono1, 1, i2s1, 1);
#elif defined(TGA_AUDIO_BOARD)
AudioOutputI2S i2s1;
AudioConnection patchCord28(stereomono1, 0, i2s1, 0);
AudioConnection patchCord29(stereomono1, 1, i2s1, 1);
AudioControlWM8731master wm8731_1;
#elif defined(PT8211_AUDIO)
AudioOutputPT8211 pt8211_1;
AudioConnection patchCord30(stereomono1, 0, pt8211_1, 0);
AudioConnection patchCord31(stereomono1, 1, pt8211_1, 1);
#elif defined(TEENSY_DAC_SYMMETRIC)
AudioOutputAnalogStereo dacOut;
AudioMixer4 invMixer;
AudioConnection patchCord32(stereomono1, 0, dacOut , 0);
AudioConnection patchCord33(stereomono1, 1, invMixer, 0);
AudioConnection patchCord34(invMixer, 0, dacOut , 1);
#else
AudioOutputAnalogStereo dacOut;
AudioConnection patchCord35(stereomono1, 0, dacOut, 0);
AudioConnection patchCord36(stereomono1, 1, dacOut, 1);
#endif
//
// Dynamic patching
//
uint8_t nDynamic = 0;
#ifdef USE_REVERB
AudioConnection * dynamicConnections[NUM_DEXED * 7];
#else
AudioConnection * dynamicConnections[NUM_DEXED * 6];
#endif
void create_audio_connections(AudioSourceMicroDexed &d, uint8_t instance_id)
{
dynamicConnections[nDynamic++] = new AudioConnection(d, peak1);
#if defined(USE_REVERB)
dynamicConnections[nDynamic++] = new AudioConnection(d, 0, reverb_mixer, instance_id);
#endif
dynamicConnections[nDynamic++] = new AudioConnection(d, 0, delay_mixer, instance_id);
dynamicConnections[nDynamic++] = new AudioConnection(d, 0, chorus_mixer, instance_id);
dynamicConnections[nDynamic++] = new AudioConnection(d, 0 , master_mixer_r, 0);
dynamicConnections[nDynamic++] = new AudioConnection(d, 0 , master_mixer_l, 0);
}
bool sd_card_available = false;
uint8_t max_loaded_banks = 0;
char bank_name[NUM_DEXED][BANK_NAME_LEN];
char voice_name[NUM_DEXED][VOICE_NAME_LEN];
char bank_names[NUM_DEXED][MAX_BANKS][BANK_NAME_LEN];
char voice_names[NUM_DEXED][MAX_VOICES][VOICE_NAME_LEN];
elapsedMillis autostore;
uint8_t midi_timing_counter = 0; // 24 per qarter
elapsedMillis midi_timing_timestep;
uint16_t midi_timing_quarter = 0;
elapsedMillis long_button_pressed;
SoftenValue <uint8_t> soften_volume;
SoftenValue <uint8_t> soften_filter_res[NUM_DEXED];
SoftenValue <uint8_t> soften_filter_cut[NUM_DEXED];
//elapsedMicros fill_audio_buffer;
elapsedMillis control_rate;
uint8_t active_voices = 0;
#ifdef SHOW_CPU_LOAD_MSEC
elapsedMillis cpu_mem_millis;
#endif
//uint32_t overload = 0;
uint32_t peak = 0;
bool eeprom_update_flag = false;
config_t configuration;
uint8_t selected_dexed_instance = 0;
// Allocate the delay lines for chorus
int16_t delayline[MOD_DELAY_SAMPLE_BUFFER];
#ifdef ENABLE_LCD_UI
/***********************************************************************
LCDMenuLib2
***********************************************************************/
extern LCDMenuLib2 LCDML;
extern uint8_t menu_state;
#endif
#ifdef DISPLAY_LCD_SPI
void change_disp_sd(bool disp)
{
digitalWrite(SDCARD_CS_PIN, disp);
digitalWrite(U8X8_CS_PIN, !disp);
}
#endif
void setup()
{
// Start audio system
AudioNoInterrupts();
AudioMemory(AUDIO_MEM);
Serial.begin(SERIAL_SPEED);
//while (!Serial) ; // wait for Serial Monitor
#ifdef DISPLAY_LCD_SPI
pinMode(SDCARD_CS_PIN, OUTPUT);
pinMode(U8X8_CS_PIN, OUTPUT);
#endif
delay(320); // necessary, because before this time no serial output is done :(
#ifdef ENABLE_LCD_UI
setup_ui();
#else
Serial.println(F("NO LCD DISPLAY ENABLED!"));
#endif
Serial.println(F("MicroDexed based on https://github.com/asb2m10/dexed"));
Serial.println(F("(c)2018,2019 H. Wirtz <wirtz@parasitstudio.de>"));
Serial.println(F("https://codeberg.org/dcoredump/MicroDexed"));
Serial.print(F("Version: "));
Serial.println(VERSION);
Serial.println(F("<setup start>"));
Serial.flush();
setup_midi_devices();
for (uint8_t i = 0; i < NUM_DEXED; i++)
{
Serial.print(F("Creating MicroDexed instance "));
Serial.print(i, DEC);
MicroDexed[i] = new AudioSourceMicroDexed(SAMPLE_RATE);
create_audio_connections(*MicroDexed[i], i);
Serial.println(F("... created"));
}
// Init EEPROM if both buttons are pressed at startup
/* if (digitalRead(BUT_R_PIN) == LOW)
{
Serial.println(F("Init EEPROM"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("INIT"));
lcd.setCursor(0, 1);
lcd.print(F("EEPROM"));
initial_values_from_eeprom(true);
}
else*/
initial_values_from_eeprom(false);
#if defined(TEENSY_AUDIO_BOARD)
sgtl5000_1.enable();
sgtl5000_1.dacVolumeRamp();
//sgtl5000_1.dacVolumeRampLinear();
//sgtl5000_1.dacVolumeRampDisable();
sgtl5000_1.unmuteHeadphone();
sgtl5000_1.unmuteLineout();
sgtl5000_1.autoVolumeDisable(); // turn off AGC
sgtl5000_1.volume(0.5, 0.5); // Headphone volume
sgtl5000_1.lineOutLevel(SGTL5000_LINEOUT_LEVEL);
sgtl5000_1.audioPostProcessorEnable();
sgtl5000_1.autoVolumeControl(1, 1, 1, 0.9, 0.01, 0.05);
sgtl5000_1.autoVolumeEnable();
sgtl5000_1.surroundSoundEnable();
sgtl5000_1.surroundSound(7, 2); // Configures virtual surround width from 0 (mono) to 7 (widest). select may be set to 1 (disable), 2 (mono input) or 3 (stereo input).
sgtl5000_1.enhanceBassEnable();
sgtl5000_1.enhanceBass(1.0, 0.2, 1, 2); // Configures the bass enhancement by setting the levels of the original stereo signal and the bass-enhanced mono level which will be mixed together. The high-pass filter may be enabled (0) or bypassed (1).
/* The cutoff frequency is specified as follows:
value frequency
0 80Hz
1 100Hz
2 125Hz
3 150Hz
4 175Hz
5 200Hz
6 225Hz
*/
//sgtl5000_1.eqBands(bass, mid_bass, midrange, mid_treble, treble);
Serial.println(F("Teensy-Audio-Board enabled."));
#elif defined(TGA_AUDIO_BOARD)
wm8731_1.enable();
wm8731_1.volume(1.0);
Serial.println(F("TGA board enabled."));
#elif defined(I2S_AUDIO_ONLY)
Serial.println(F("I2S enabled."));
#elif defined(PT8211_AUDIO)
Serial.println(F("PT8211 enabled."));
#elif defined(TEENSY_DAC_SYMMETRIC)
invMixer.gain(0, -1.f);
Serial.println(F("Internal DAC using symmetric outputs enabled."));
#else
Serial.println(F("Internal DAC enabled."));
#endif
// start SD card
#ifndef TEENSY4
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
#endif
#ifdef DISPLAY_LCD_SPI
change_disp_sd(false);
#endif
if (!SD.begin(SDCARD_CS_PIN))
{
Serial.println(F("SD card not accessable."));
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
strcpy(bank_name[instance_id], "Default");
strcpy(voice_name[instance_id], "Default");
}
}
else
{
Serial.println(F("SD card found."));
sd_card_available = true;
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
// read all bank names
max_loaded_banks = get_bank_names(instance_id);
strip_extension(bank_names[instance_id][configuration.dexed[instance_id].bank], bank_name[instance_id]);
// read all voice name for actual bank
get_voice_names_from_bank(configuration.dexed[instance_id].bank, instance_id);
#ifdef DEBUG
Serial.print(F("Bank ["));
Serial.print(bank_names[instance_id][configuration.dexed[instance_id].bank]);
Serial.print(F("/"));
Serial.print(bank_name[instance_id]);
Serial.println(F("]"));
for (uint8_t n = 0; n < MAX_VOICES; n++)
{
if (n < 10)
Serial.print(F(" "));
Serial.print(F(" "));
Serial.print(n, DEC);
Serial.print(F("["));
Serial.print(voice_names[instance_id][n]);
Serial.println(F("]"));
}
#endif
// load default SYSEX data
load_sysex(configuration.dexed[instance_id].bank, configuration.dexed[instance_id].voice, instance_id);
}
}
#ifdef DISPLAY_LCD_SPI
change_disp_sd(true);
#endif
// Init effects
memset(delayline, 0, sizeof(delayline));
if (!modchorus.begin(delayline, MOD_DELAY_SAMPLE_BUFFER)) {
Serial.println(F("AudioEffectModulatedDelay - begin failed"));
while (1);
}
#ifdef DEBUG
Serial.print(F("MOD_DELAY_SAMPLE_BUFFER="));
Serial.print(MOD_DELAY_SAMPLE_BUFFER, DEC);
Serial.println(F(" samples"));
#endif
master_mixer_r.gain(DEXED, 1.0);
master_mixer_l.gain(DEXED, 1.0);
#ifdef USE_REVERB
// INIT REVERB
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
reverb_mixer.gain(instance_id, 0.0);
master_mixer_r.gain(REVERB, 1.0);
master_mixer_l.gain(REVERB, 1.0);
freeverbs1.roomsize(mapfloat(configuration.reverb_roomsize, REVERB_ROOMSIZE_MIN, REVERB_ROOMSIZE_MAX, 0.0, 1.0));
freeverbs1.damping(mapfloat(configuration.reverb_damping, REVERB_DAMPING_MIN, REVERB_DAMPING_MAX, 0.0, 1.0));
#endif
// INIT DELAY
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
delay_mixer.gain(instance_id, 0.0);
master_mixer_r.gain(DELAY, 1.0);
master_mixer_l.gain(DELAY, 1.0);
delay1.delay(0, mapfloat(configuration.delay_time, DELAY_TIME_MIN, DELAY_TIME_MAX, 0.0, float(DELAY_TIME_MAX)));
// delay_fb_mixer is the feedback-adding mixer
//delay_fb_mixer.gain(0, 1.0 - (mapfloat(configuration.delay_feedback, DELAY_FEEDBACK_MIN, DELAY_FEEDBACK_MAX, 0.0, 1.0))); // original signal
delay_fb_mixer.gain(0, 1.0); // original signal
delay_fb_mixer.gain(1, mapfloat(configuration.delay_feedback, DELAY_FEEDBACK_MIN, DELAY_FEEDBACK_MAX, 0.0, 1.0)); // amount of feedback
// INIT CHORUS
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
chorus_mixer.gain(instance_id, 0.0);
master_mixer_r.gain(CHORUS, 1.0);
master_mixer_l.gain(CHORUS, 1.0);
switch (configuration.chorus_waveform)
{
case 0:
modulator.begin(WAVEFORM_TRIANGLE);
break;
case 1:
modulator.begin(WAVEFORM_SINE);
break;
default:
modulator.begin(WAVEFORM_TRIANGLE);
}
modulator.phase(0);
modulator.amplitude(mapfloat(configuration.chorus_depth, CHORUS_DEPTH_MIN, CHORUS_DEPTH_MAX, 0.0, 1.0));
modulator.offset(0.0);
#if MOD_FILTER_OUTPUT == MOD_BUTTERWORTH_FILTER_OUTPUT
// Butterworth filter, 12 db/octave
modchorus_filter.setLowpass(0, MOD_FILTER_CUTOFF_HZ, 0.707);
#elif MOD_FILTER_OUTPUT == MOD_LINKWITZ_RILEY_FILTER_OUTPUT
// Linkwitz-Riley filter, 48 dB/octave
modchorus_filter.setLowpass(0, MOD_FILTER_CUTOFF_HZ, 0.54);
modchorus_filter.setLowpass(1, MOD_FILTER_CUTOFF_HZ, 1.3);
modchorus_filter.setLowpass(2, MOD_FILTER_CUTOFF_HZ, 0.54);
modchorus_filter.setLowpass(3, MOD_FILTER_CUTOFF_HZ, 1.3);
#endif
if (configuration.mono == 0)
modchorus_inverter.gain(-1.0); // stereo mode
else
modchorus_inverter.gain(1.0); // mono mode
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
soften_filter_res[instance_id].init(configuration.dexed[instance_id].filter_resonance);
soften_filter_cut[instance_id].init(configuration.dexed[instance_id].filter_cutoff);
MicroDexed[instance_id]->fx.Gain = mapfloat(configuration.dexed[instance_id].loudness, LOUDNESS_MIN, LOUDNESS_MAX, 0.0, 1.0);
MicroDexed[instance_id]->fx.Reso = mapfloat(FILTER_RESONANCE_MAX - configuration.dexed[instance_id].filter_resonance, FILTER_RESONANCE_MIN, FILTER_RESONANCE_MAX, 1.0, 0.0);
MicroDexed[instance_id]->fx.Cutoff = mapfloat(FILTER_CUTOFF_MAX - configuration.dexed[instance_id].filter_cutoff, FILTER_CUTOFF_MIN, FILTER_CUTOFF_MAX, 1.0, 0.0);
MicroDexed[instance_id]->doRefreshVoice();
}
// set initial volume and pan (read from EEPROM)
set_volume(configuration.vol, configuration.pan, configuration.mono);
soften_volume.init(configuration.vol);
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
// Initialize processor and memory measurements
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
#endif
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
#ifdef DEBUG
Serial.print(F("Bank/Voice from EEPROM ["));
Serial.print(configuration.dexed[instance_id].bank, DEC);
Serial.print(F("/"));
Serial.print(configuration.dexed[instance_id].voice, DEC);
Serial.println(F("]"));
show_patch(instance_id);
#endif
}
Serial.print(F("AUDIO_BLOCK_SAMPLES="));
Serial.print(AUDIO_BLOCK_SAMPLES);
Serial.print(F(" (Time per block="));
Serial.print(1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES));
Serial.println(F("ms)"));
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
show_cpu_and_mem_usage();
#endif
AudioInterrupts();
Serial.println(F("<setup end>"));
}
void loop()
{
#ifdef OLD
int16_t* audio_buffer; // pointer to AUDIO_BLOCK_SAMPLES * int16_t
const uint16_t audio_block_time_us = 1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES);
#endif
//while (42 == 42)
//{
#ifdef OLD
// Main sound calculation
if (queue1.available() && fill_audio_buffer > audio_block_time_us - 10)
{
fill_audio_buffer = 0;
audio_buffer = queue1.getBuffer();
elapsedMicros t1;
for (uint8_t i = 0; i < NUM_DEXED; i++)
{
MicroDexed[i]->getSamples(AUDIO_BLOCK_SAMPLES, audio_buffer);
}
if (t1 > audio_block_time_us) // everything greater 2.9ms is a buffer underrun!
xrun++;
if (t1 > render_time_max)
render_time_max = t1;
if (peak1.available())
{
if (peak1.read() > 0.99)
peak++;
}
queue1.playBuffer();
}
#endif
// MIDI input handling
check_midi_devices();
// CONTROL-RATE-EVENT-HANDLING
if (control_rate > CONTROL_RATE_MS)
{
control_rate = 0;
#ifdef ENABLE_LCD_UI
// LCD Menu
LCDML.loop();
// initial starts voice selection menu as default
if (menu_state == MENU_START)
{
menu_state = MENU_VOICE;
UI_func_voice_selection(0);
}
#endif
// EEPROM update handling
//if (autostore >= AUTOSTORE_MS && active_voices == 0 && eeprom_update_flag == true)
if (autostore >= AUTOSTORE_MS && eeprom_update_flag == true)
{
// only store configuration data to EEPROM when AUTOSTORE_MS is reached and no voices are activated anymore
eeprom_update();
}
// check for value changes and unused voices
soften_volume.tick();
for (uint8_t i = 0; i < NUM_DEXED; i++)
{
active_voices = MicroDexed[i]->getNumNotesPlaying();
soften_filter_res[i].tick();
soften_filter_cut[i].tick();
if (soften_filter_res[i].running())
{
// soften filter resonance value
MicroDexed[i]->fx.Reso = (FILTER_RESONANCE_MAX - soften_filter_res[i].value()) / 100.0;
#ifdef DEBUG
Serial.print(F("Filter-Resonance: "));
Serial.print(MicroDexed[i]->fx.Reso, 5);
Serial.print(F(" Filter-Resonance step: "));
Serial.print(soften_filter_res[i].steps());
Serial.print(F(" Filter-Resonance diff: "));
Serial.println(soften_filter_res[i].diff(), 5);
#endif
}
// soften filter cutoff value
if (soften_filter_cut[i].running())
{
MicroDexed[i]->fx.Cutoff = (FILTER_CUTOFF_MAX - soften_filter_cut[i].value()) / 100.0;
#ifdef DEBUG
Serial.print(F("Filter-Cutoff: "));
Serial.print(MicroDexed[i]->fx.Cutoff, 5);
Serial.print(F(" Filter-Cutoff step: "));
Serial.print(soften_filter_cut[i].steps());
Serial.print(F(" Filter-Cutoff diff: "));
Serial.println(soften_filter_cut[i].diff(), 5);
#endif
}
}
if (soften_volume.running())
{
set_volume(soften_volume.value(), configuration.pan, configuration.mono);
#ifdef DEBUG
Serial.print(F("Volume: "));
Serial.print(configuration.vol, DEC);
Serial.print(F(" step: "));
Serial.print(soften_volume.steps());
Serial.print(F(" diff: "));
Serial.println(soften_volume.diff(), 5);
#endif
}
}
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
if (cpu_mem_millis >= SHOW_CPU_LOAD_MSEC)
{
if (peak1.available())
{
if (peak1.read() > 0.99)
peak++;
}
cpu_mem_millis -= SHOW_CPU_LOAD_MSEC;
show_cpu_and_mem_usage();
}
#endif
//}
}
/******************************************************************************
MIDI MESSAGE HANDLER
******************************************************************************/
void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (checkMidiChannel(inChannel, instance_id))
{
MicroDexed[instance_id]->keydown(inNumber, inVelocity);
}
}
}
void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (checkMidiChannel(inChannel, instance_id))
{
MicroDexed[instance_id]->keyup(inNumber);
}
}
}
void handleControlChange(byte inChannel, byte inCtrl, byte inValue)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (checkMidiChannel(inChannel, instance_id))
{
#ifdef DEBUG
Serial.print(F("INSTANCE "));
Serial.print(instance_id, DEC);
Serial.print(F(": CC#"));
Serial.print(inCtrl, DEC);
Serial.print(F(":"));
Serial.println(inValue, DEC);
#endif
switch (inCtrl) {
case 0:
if (inValue < MAX_BANKS)
{
configuration.dexed[instance_id].bank = inValue;
}
break;
case 1:
MicroDexed[instance_id]->controllers.modwheel_cc = inValue;
MicroDexed[instance_id]->controllers.refresh();
break;
case 2:
MicroDexed[instance_id]->controllers.breath_cc = inValue;
MicroDexed[instance_id]->controllers.refresh();
break;
case 4:
MicroDexed[instance_id]->controllers.foot_cc = inValue;
MicroDexed[instance_id]->controllers.refresh();
break;
case 7: // Volume
configuration.vol = map(inValue, 0, 0x7f, VOLUME_MIN, VOLUME_MAX);
soften_volume.update(configuration.vol, SOFTEN_VALUE_CHANGE_STEPS);
break;
case 10: // Pan
configuration.pan = map(inValue, 0, 0x7f, PANORAMA_MIN, PANORAMA_MAX);
set_volume(configuration.vol, configuration.pan, configuration.mono);
break;
case 32: // BankSelect LSB
configuration.dexed[instance_id].bank = inValue;
break;
case 64:
MicroDexed[instance_id]->setSustain(inValue > 63);
if (!MicroDexed[instance_id]->getSustain())
{
for (uint8_t note = 0; note < MicroDexed[instance_id]->getMaxNotes(); note++)
{
if (MicroDexed[instance_id]->voices[note].sustained && !MicroDexed[instance_id]->voices[note].keydown)
{
MicroDexed[instance_id]->voices[note].dx7_note->keyup();
MicroDexed[instance_id]->voices[note].sustained = false;
}
}
}
break;
case 103: // CC 103: filter resonance
configuration.dexed[instance_id].filter_resonance = map(inValue, 0, 0x7f, FILTER_RESONANCE_MIN, FILTER_RESONANCE_MAX);
//MicroDexed[instance_id]->fx.Reso = (100 - configuration.dexed[instance_id].filter_resonance) / 100.0;
soften_filter_res[instance_id].update(soften_filter_res[instance_id].value() + (FILTER_RESONANCE_MAX - FILTER_RESONANCE_MIN) / FILTER_RESONANCE_ENC_STEPS, SOFTEN_VALUE_CHANGE_STEPS);
break;
case 104: // CC 104: filter cutoff
configuration.dexed[instance_id].filter_cutoff = map(inValue, 0, 0x7f, FILTER_CUTOFF_MIN, FILTER_CUTOFF_MAX);
//MicroDexed[instance_id]->fx.Cutoff = (100 - configuration.dexed[instance_id].filter_cutoff) / 100.0;
soften_filter_cut[instance_id].update(soften_filter_cut[instance_id].value() + (FILTER_CUTOFF_MAX - FILTER_CUTOFF_MIN) / FILTER_CUTOFF_ENC_STEPS, SOFTEN_VALUE_CHANGE_STEPS);
break;
case 105: // CC 105: delay time
configuration.delay_time = map(inValue, 0, 0x7f, DELAY_TIME_MIN, DELAY_TIME_MAX);
delay1.delay(0, configuration.delay_time * 10);
case 106: // CC 106: delay feedback
configuration.delay_feedback = map(inValue, 0, 0x7f, DELAY_FEEDBACK_MIN , DELAY_FEEDBACK_MAX);
delay_fb_mixer.gain(1, configuration.delay_feedback / 100.0 ); // amount of feedback
delay_fb_mixer.gain(0, 1.0 - configuration.delay_feedback / 100.0); // original signalbreak;
break;
case 107: // CC 107: delay volume
configuration.dexed[instance_id].delay_send = map(inValue, 0, 0x7f, DELAY_SEND_MIN, DELAY_SEND_MAX);
master_mixer_r.gain(DELAY, configuration.dexed[instance_id].delay_send / 100.0);
master_mixer_l.gain(2, configuration.dexed[instance_id].delay_send / 100.0);
break;
case 120:
MicroDexed[instance_id]->panic();
break;
case 121:
MicroDexed[instance_id]->resetControllers();
break;
case 123:
MicroDexed[instance_id]->notesOff();
break;
case 126:
MicroDexed[instance_id]->setMonoMode(true);
break;
case 127:
MicroDexed[instance_id]->setMonoMode(false);
break;
}
}
}
}
void handleAfterTouch(byte inChannel, byte inPressure)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (checkMidiChannel(inChannel, instance_id))
{
MicroDexed[instance_id]->controllers.aftertouch_cc = inPressure;
MicroDexed[instance_id]->controllers.refresh();
}
}
}
void handlePitchBend(byte inChannel, int inPitch)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (checkMidiChannel(inChannel, instance_id))
{
MicroDexed[instance_id]->controllers.values_[kControllerPitch] = inPitch + 0x2000; // -8192 to +8191 --> 0 to 16383
}
}
}
void handleProgramChange(byte inChannel, byte inProgram)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (checkMidiChannel(inChannel, instance_id))
{
if (inProgram < MAX_VOICES)
{
#ifdef DISPLAY_LCD_SPI
change_disp_sd(false);
#endif
load_sysex(configuration.dexed[instance_id].bank, inProgram, instance_id);
#ifdef DISPLAY_LCD_SPI
change_disp_sd(true);
#endif
}
}
}
}
void handleSystemExclusive(byte * sysex, uint len)
{
/*
SYSEX MESSAGE: Parameter Change
-------------------------------
bits hex description
11110000 F0 Status byte - start sysex
0iiiiiii 43 ID # (i=67; Yamaha)
0sssnnnn 10 Sub-status (s=1) & channel number (n=0; ch 1)
0gggggpp ** parameter group # (g=0; voice, g=2; function)
0ppppppp ** parameter # (these are listed in next section)
Note that voice parameter #'s can go over 128 so
the pp bits in the group byte are either 00 for
par# 0-127 or 01 for par# 128-155. In the latter case
you add 128 to the 0ppppppp byte to compute par#.
0ddddddd ** data byte
11110111 F7 Status - end sysex
*/
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
if (!checkMidiChannel((sysex[2] & 0x0f) + 1 , instance_id))
{
#ifdef DEBUG
Serial.print(F("INSTANCE "));
Serial.print(instance_id, DEC);
Serial.println(F(": SYSEX-MIDI-Channel mismatch"));
#endif
return;
}
#ifdef DEBUG
Serial.print(F("INSTANCE "));
Serial.print(instance_id, DEC);
Serial.print(F(": SYSEX-Data["));
Serial.print(len, DEC);
Serial.print(F("]"));
for (uint8_t i = 0; i < len; i++)
{
Serial.print(F(" "));
Serial.print(sysex[i], DEC);
}
Serial.println();
#endif
if (sysex[1] != 0x43) // check for Yamaha sysex
{
#ifdef DEBUG
Serial.println(F("E: SysEx vendor not Yamaha."));
#endif
return;
}
#ifdef DEBUG
Serial.print(F("Substatus: ["));
Serial.print((sysex[2] & 0x70) >> 4);
Serial.println(F("]"));
#endif
// parse parameter change
if (len == 7)
{
if (((sysex[3] & 0x7c) >> 2) != 0 && ((sysex[3] & 0x7c) >> 2) != 2)
{
#ifdef DEBUG
Serial.println(F("E: Not a SysEx parameter or function parameter change."));
#endif
return;
}
if (sysex[6] != 0xf7)
{
#ifdef DEBUG
Serial.println(F("E: SysEx end status byte not detected."));
#endif
return;
}
sysex[4] &= 0x7f;
sysex[5] &= 0x7f;
uint8_t data_index;
if (((sysex[3] & 0x7c) >> 2) == 0)
{
MicroDexed[instance_id]->notesOff();
MicroDexed[instance_id]->data[sysex[4] + ((sysex[3] & 0x03) * 128)] = sysex[5]; // set parameter
MicroDexed[instance_id]->doRefreshVoice();
data_index = sysex[4] + ((sysex[3] & 0x03) * 128);
}
else
{
MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET - 63 + sysex[4]] = sysex[5]; // set function parameter
MicroDexed[instance_id]->controllers.values_[kControllerPitchRange] = MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_PITCHBEND_RANGE];
MicroDexed[instance_id]->controllers.values_[kControllerPitchStep] = MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_PITCHBEND_STEP];
MicroDexed[instance_id]->controllers.wheel.setRange(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_MODWHEEL_RANGE]);
MicroDexed[instance_id]->controllers.wheel.setTarget(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_MODWHEEL_ASSIGN]);
MicroDexed[instance_id]->controllers.foot.setRange(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_FOOTCTRL_RANGE]);
MicroDexed[instance_id]->controllers.foot.setTarget(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_FOOTCTRL_ASSIGN]);
MicroDexed[instance_id]->controllers.breath.setRange(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_BREATHCTRL_RANGE]);
MicroDexed[instance_id]->controllers.breath.setTarget(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_BREATHCTRL_ASSIGN]);
MicroDexed[instance_id]->controllers.at.setRange(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_AT_RANGE]);
MicroDexed[instance_id]->controllers.at.setTarget(MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_AT_ASSIGN]);
MicroDexed[instance_id]->controllers.masterTune = (MicroDexed[instance_id]->data[DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_MASTER_TUNE] * 0x4000 << 11) * (1.0 / 12);
MicroDexed[instance_id]->controllers.refresh();
data_index = DEXED_GLOBAL_PARAMETER_OFFSET - 63 + sysex[4];
}
#ifdef DEBUG
Serial.print(F("SysEx"));
if (((sysex[3] & 0x7c) >> 2) == 0)
Serial.print(F(" function"));
Serial.print(F(" parameter "));
Serial.print(sysex[4], DEC);
Serial.print(F(" = "));
Serial.print(sysex[5], DEC);
Serial.print(F(", data_index = "));
Serial.println(data_index, DEC);
#endif
}
else if (len == 163)
{
int32_t bulk_checksum_calc = 0;
int8_t bulk_checksum = sysex[161];
// 1 Voice bulk upload
#ifdef DEBUG
Serial.println(F("One Voice bulk upload"));
#endif
if (sysex[162] != 0xf7)
{
#ifdef DEBUG
Serial.println(F("E: Found no SysEx end marker."));
#endif
return;
}
if ((sysex[3] & 0x7f) != 0)
{
#ifdef DEBUG
Serial.println(F("E: Not a SysEx voice bulk upload."));
#endif
return;
}
if (((sysex[4] << 7) | sysex[5]) != 0x9b)
{
#ifdef DEBUG
Serial.println(F("E: Wrong length for SysEx voice bulk upload (not 155)."));
#endif
return;
}
// checksum calculation
for (uint8_t i = 0; i < 155 ; i++)
{
bulk_checksum_calc -= sysex[i + 6];
}
bulk_checksum_calc &= 0x7f;
if (bulk_checksum_calc != bulk_checksum)
{
#ifdef DEBUG
Serial.print(F("E: Checksum error for one voice [0x"));
Serial.print(bulk_checksum, HEX);
Serial.print(F("/0x"));
Serial.print(bulk_checksum_calc, HEX);
Serial.println(F("]"));
#endif
return;
}
// load sysex-data into voice memory
MicroDexed[instance_id]->loadVoiceParameters(&sysex[6]);
//MicroDexed[instance_id]->initGlobalParameters();
// manipulate UI names and numbers
strncpy(voice_name[instance_id], (char *)&sysex[151], sizeof(voice_name[instance_id]) - 1);
Serial.print(F("Got voice ["));
Serial.print(voice_name[instance_id]);
Serial.println(F("]."));
}
#ifdef DEBUG
else
Serial.println(F("E: SysEx parameter length wrong."));
#endif
}
}
void handleTimeCodeQuarterFrame(byte data)
{
;
}
void handleAfterTouchPoly(byte inChannel, byte inNumber, byte inVelocity)
{
;
}
void handleSongSelect(byte inSong)
{
;
}
void handleTuneRequest(void)
{
;
}
void handleClock(void)
{
midi_timing_counter++;
if (midi_timing_counter % 24 == 0)
{
midi_timing_quarter = midi_timing_timestep;
midi_timing_counter = 0;
midi_timing_timestep = 0;
// Adjust delay control here
#ifdef DEBUG
Serial.print(F("MIDI Clock: "));
Serial.print(60000 / midi_timing_quarter, DEC);
Serial.print(F("bpm ("));
Serial.print(midi_timing_quarter, DEC);
Serial.println(F("ms per quarter)"));
#endif
}
}
void handleStart(void)
{
;
}
void handleContinue(void)
{
;
}
void handleStop(void)
{
;
}
void handleActiveSensing(void)
{
;
}
void handleSystemReset(void)
{
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
#ifdef DEBUG
Serial.println(F("MIDI SYSEX RESET"));
#endif
MicroDexed[instance_id]->notesOff();
MicroDexed[instance_id]->panic();
MicroDexed[instance_id]->resetControllers();
}
}
/******************************************************************************
MIDI HELPER
******************************************************************************/
bool checkMidiChannel(byte inChannel, uint8_t instance_id)
{
// check for MIDI channel
if (configuration.dexed[instance_id].midi_channel == MIDI_CHANNEL_OMNI)
{
return (true);
}
else if (inChannel != configuration.dexed[instance_id].midi_channel)
{
#ifdef DEBUG
Serial.print(F("INSTANCE "));
Serial.print(instance_id, DEC);
Serial.print(F(": Ignoring MIDI data on channel "));
Serial.print(inChannel);
Serial.print(F("(listening on "));
Serial.print(configuration.dexed[instance_id].midi_channel);
Serial.println(F(")"));
#endif
return (false);
}
return (true);
}
/******************************************************************************
VOLUME HELPER
******************************************************************************/
void set_volume(uint8_t v, int8_t p, uint8_t m)
{
float tmp2;
configuration.vol = v;
if (configuration.vol > 100)
configuration.vol = 100;
//configuration.dexed[instance_id].pan = p; // TODO: Fixing pan per Dexed
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
configuration.pan = p;
}
configuration.mono = m;
uint16_t tmp = v / 100.0 * 1023.0 + 0.5;
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
tmp2 = mapfloat(configuration.pan, PANORAMA_MIN, PANORAMA_MAX, 0.0, 1.0);
}
float tmp3 = (float)(tmp * (tmp + 2)) / (float)(1 << 20);
#ifdef DEBUG
Serial.print(F("Setting volume: VOL="));
Serial.print(v, DEC);
Serial.print(F("["));
Serial.print(tmp3, 3);
Serial.print(F("] PAN="));
Serial.print(p, 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));
switch (m)
{
case 0: // stereo
stereomono1.stereo(true);
modchorus_inverter.gain(-1.0); // stereo mode
break;
case 1: // mono both
stereomono1.stereo(false);
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
configuration.pan = 0.5;
}
modchorus_inverter.gain(1.0); // stereo mode
break;
case 2: // mono right
volume_l.gain(0.0);
stereomono1.stereo(false);
configuration.pan = 0.5;
modchorus_inverter.gain(1.0); // stereo mode
break;
case 3: // mono left
volume_r.gain(0.0);
stereomono1.stereo(false);
configuration.pan = 0.5;
modchorus_inverter.gain(1.0); // stereo mode
break;
}
}
// https://www.dr-lex.be/info-stuff/volumecontrols.html#table1
inline float logvol(float x)
{
return (0.001 * expf(6.908 * x));
}
/******************************************************************************
EEPROM HELPER
******************************************************************************/
void initial_values_from_eeprom(bool init)
{
uint32_t checksum;
config_t tmp_conf;
if (init == true)
init_configuration();
else
{
EEPROM.get(EEPROM_START_ADDRESS, tmp_conf);
checksum = crc32((byte*)&tmp_conf + 4, sizeof(tmp_conf) - 4);
#ifdef DEBUG
Serial.print(F("EEPROM checksum: 0x"));
Serial.print(tmp_conf.checksum, HEX);
Serial.print(F(" / 0x"));
Serial.println(checksum, HEX);
#endif
if (checksum != tmp_conf.checksum)
{
#ifdef DEBUG
Serial.println(F("Checksum mismatch -> initializing EEPROM!"));
#endif
init_configuration();
}
else
{
EEPROM.get(EEPROM_START_ADDRESS, configuration);
/*
configuration.instances %= INSTANCES_MAX;
configuration.instance_mode %= INSTANCE_MODE_MAX;
configuration.instance_splitpoint %= INSTANCE_SPLITPOINT_MAX;
configuration.vol %= VOLUME_MAX;
configuration.mono %= MONO_MAX;
configuration.reverb_roomsize %= REVERB_ROOMSIZE_MAX;
configuration.reverb_damping %= REVERB_DAMPING_MAX;
configuration.chorus_frequency %= CHORUS_FREQUENCY_MAX;
configuration.chorus_waveform %= CHORUS_WAVEFORM_MAX;
configuration.chorus_depth %= CHORUS_DEPTH_MAX;
configuration.delay_time %= DELAY_TIME_MAX;
configuration.delay_feedback %= DELAY_FEEDBACK_MAX;
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
configuration.dexed[instance_id].midi_channel %= MIDI_CHANNEL_MAX;
configuration.dexed[instance_id].bank %= MAX_BANKS;
configuration.dexed[instance_id].voice %= MAX_VOICES;
configuration.dexed[instance_id].pan %= PANORAMA_MAX;
configuration.dexed[instance_id].reverb_send %= REVERB_SEND_MAX;
configuration.dexed[instance_id].chorus_send %= CHORUS_SEND_MAX;
configuration.dexed[instance_id].delay_send %= DELAY_SEND_MAX;
configuration.dexed[instance_id].filter_cutoff %= FILTER_CUTOFF_MAX;
configuration.dexed[instance_id].filter_resonance %= FILTER_RESONANCE_MAX;
configuration.dexed[instance_id].loudness %= LOUDNESS_MAX;
configuration.dexed[instance_id].polyphony %= POLYPHONY_MAX;
configuration.dexed[instance_id].engine %= ENGINE_MAX;
configuration.dexed[instance_id].monopoly %= MONOPOLY_MAX;
configuration.dexed[instance_id].pb_range %= PB_RANGE_MAX;
configuration.dexed[instance_id].pb_step %= PB_STEP_MAX;
configuration.dexed[instance_id].mw_range %= MW_RANGE_MAX;
configuration.dexed[instance_id].mw_assign %= MW_ASSIGN_MAX;
configuration.dexed[instance_id].fc_range %= FC_RANGE_MAX;
configuration.dexed[instance_id].fc_assign %= FC_ASSIGN_MAX;
configuration.dexed[instance_id].bc_range %= BC_RANGE_MAX;
configuration.dexed[instance_id].bc_assign %= BC_ASSIGN_MAX;
configuration.dexed[instance_id].at_range %= AT_RANGE_MAX;
configuration.dexed[instance_id].at_assign %= AT_ASSIGN_MAX;
configuration.dexed[instance_id].op_enabled %= 0x3f; */
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
MicroDexed[instance_id]->setPBController(configuration.dexed[instance_id].pb_range, configuration.dexed[instance_id].pb_step);
MicroDexed[instance_id]->setMWController(configuration.dexed[instance_id].mw_range, configuration.dexed[instance_id].mw_assign);
MicroDexed[instance_id]->setFCController(configuration.dexed[instance_id].fc_range, configuration.dexed[instance_id].fc_assign);
MicroDexed[instance_id]->setBCController(configuration.dexed[instance_id].bc_range, configuration.dexed[instance_id].bc_assign);
MicroDexed[instance_id]->setATController(configuration.dexed[instance_id].at_range, configuration.dexed[instance_id].at_assign);
MicroDexed[instance_id]->setOPs(configuration.dexed[instance_id].op_enabled);
MicroDexed[instance_id]->doRefreshVoice();
}
Serial.println(F("OK, loaded!"));
}
}
#ifdef DEBUG
show_configuration();
#endif
if (configuration.vol > 100)
configuration.vol = 100;
}
void init_configuration(void)
{
#ifdef DEBUG
Serial.print(F("Initializing configuration"));
#endif
configuration.checksum = 0xffff;
configuration.instances = INSTANCES_DEFAULT;
configuration.instance_mode = INSTANCE_MODE_DEFAULT;
configuration.instance_splitpoint = INSTANCE_SPLITPOINT_DEFAULT;
configuration.vol = VOLUME_DEFAULT;
configuration.pan = PANORAMA_DEFAULT;
configuration.mono = MONO_DEFAULT;
configuration.reverb_roomsize = REVERB_ROOMSIZE_DEFAULT;
configuration.reverb_damping = REVERB_DAMPING_DEFAULT;
configuration.chorus_frequency = CHORUS_FREQUENCY_DEFAULT;
configuration.chorus_waveform = CHORUS_WAVEFORM_DEFAULT;
configuration.chorus_depth = CHORUS_DEPTH_DEFAULT;
configuration.delay_time = DELAY_TIME_DEFAULT;
configuration.delay_feedback = DELAY_FEEDBACK_DEFAULT;
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
configuration.dexed[instance_id].midi_channel = DEFAULT_MIDI_CHANNEL;
configuration.dexed[instance_id].bank = SYSEXBANK_DEFAULT;
configuration.dexed[instance_id].voice = SYSEXSOUND_DEFAULT;
configuration.dexed[instance_id].reverb_send = REVERB_SEND_DEFAULT;
configuration.dexed[instance_id].chorus_send = CHORUS_SEND_DEFAULT;
configuration.dexed[instance_id].delay_send = DELAY_SEND_DEFAULT;
configuration.dexed[instance_id].filter_cutoff = FILTER_CUTOFF_DEFAULT;
configuration.dexed[instance_id].filter_resonance = FILTER_RESONANCE_DEFAULT;
configuration.dexed[instance_id].loudness = LOUDNESS_DEFAULT;
configuration.dexed[instance_id].polyphony = POLYPHONY_DEFAULT;
configuration.dexed[instance_id].engine = ENGINE_DEFAULT;
configuration.dexed[instance_id].monopoly = MONOPOLY_DEFAULT;
configuration.dexed[instance_id].pb_range = PB_RANGE_DEFAULT;
configuration.dexed[instance_id].pb_step = PB_STEP_DEFAULT;
configuration.dexed[instance_id].mw_range = MW_RANGE_DEFAULT;
configuration.dexed[instance_id].mw_assign = MW_ASSIGN_DEFAULT;
configuration.dexed[instance_id].fc_range = FC_RANGE_DEFAULT;
configuration.dexed[instance_id].fc_assign = FC_ASSIGN_DEFAULT;
configuration.dexed[instance_id].bc_range = BC_RANGE_DEFAULT;
configuration.dexed[instance_id].bc_assign = BC_ASSIGN_DEFAULT;
configuration.dexed[instance_id].at_range = AT_RANGE_DEFAULT;
configuration.dexed[instance_id].at_assign = AT_ASSIGN_DEFAULT;
configuration.dexed[instance_id].op_enabled = OP_ENABLED_DEFAULT;
}
eeprom_update();
}
void eeprom_write(void)
{
autostore = 0;
eeprom_update_flag = true;
}
void eeprom_update(void)
{
eeprom_update_flag = false;
configuration.checksum = crc32((byte*)&configuration + 4, sizeof(configuration) - 4);
EEPROM.put(EEPROM_START_ADDRESS, configuration);
Serial.println(F("Updating EEPROM"));
show_configuration();
}
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 HELPER
******************************************************************************/
#if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC)
void show_cpu_and_mem_usage(void)
{
uint32_t sum_xrun = 0;
uint16_t sum_render_time_max = 0;
uint32_t sum_overload = 0;
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
sum_xrun += MicroDexed[instance_id]->xrun;
sum_render_time_max += MicroDexed[instance_id]->render_time_max;
sum_overload += MicroDexed[instance_id]->overload;
MicroDexed[instance_id]->render_time_max = 0;
}
Serial.print(F("CPU: "));
Serial.print(AudioProcessorUsage(), 2);
Serial.print(F("% CPU MAX: "));
Serial.print(AudioProcessorUsageMax(), 2);
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(sum_render_time_max, DEC);
Serial.print(F(" XRUN: "));
Serial.print(sum_xrun, DEC);
Serial.print(F(" OVERLOAD: "));
Serial.print(sum_overload, DEC);
Serial.print(F(" PEAK: "));
Serial.print(peak, DEC);
Serial.print(F(" BLOCKSIZE: "));
Serial.print(AUDIO_BLOCK_SAMPLES, DEC);
Serial.print(F(" ACTIVE_VOICES: "));
Serial.print(active_voices, DEC);
Serial.println();
Serial.flush();
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
}
#endif
#ifdef DEBUG
void show_configuration(void)
{
Serial.println();
Serial.println(F("CONFIGURATION:"));
Serial.print(F("Checksum 0x")); Serial.println(configuration.checksum, HEX);
Serial.print(F("Instances ")); Serial.println(configuration.instances, DEC);
Serial.print(F("Instance Mode ")); Serial.println(configuration.instance_mode, DEC);
Serial.print(F("Instance Splitpoint ")); Serial.println(configuration.instance_splitpoint, DEC);
Serial.print(F("Volume ")); Serial.println(configuration.vol, DEC);
Serial.print(F("Panorama ")); Serial.println(configuration.pan, DEC);
Serial.print(F("Mono ")); Serial.println(configuration.mono, 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("Chorus Frequency ")); Serial.println(configuration.chorus_frequency, DEC);
Serial.print(F("Chorus Waveform ")); Serial.println(configuration.chorus_waveform, DEC);
Serial.print(F("Chorus Depth ")); Serial.println(configuration.chorus_depth, DEC);
Serial.print(F("Delay Time ")); Serial.println(configuration.delay_time, DEC);
Serial.print(F("Delay Feedback ")); Serial.println(configuration.delay_feedback, DEC);
Serial.flush();
for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++)
{
Serial.print(F("=== DEXED INSTANCE "));
Serial.print(instance_id, DEC);
Serial.println(" ===");
Serial.print(F(" MIDI-Channel ")); Serial.println(configuration.dexed[instance_id].midi_channel, DEC);
Serial.print(F(" Bank ")); Serial.println(configuration.dexed[instance_id].bank, DEC);
Serial.print(F(" Voice ")); Serial.println(configuration.dexed[instance_id].voice, DEC);
Serial.print(F(" Reverb Send ")); Serial.println(configuration.dexed[instance_id].reverb_send, DEC);
Serial.print(F(" Chorus Send ")); Serial.println(configuration.dexed[instance_id].chorus_send, DEC);
Serial.print(F(" Delay Send ")); Serial.println(configuration.dexed[instance_id].delay_send, DEC);
Serial.print(F(" Filter Cutoff ")); Serial.println(configuration.dexed[instance_id].filter_cutoff, DEC);
Serial.print(F(" Filter Resonance ")); Serial.println(configuration.dexed[instance_id].filter_resonance, DEC);
Serial.print(F(" Loudness ")); Serial.println(configuration.dexed[instance_id].loudness, DEC);
Serial.print(F(" Polyphony ")); Serial.println(configuration.dexed[instance_id].polyphony, DEC);
Serial.print(F(" Engine ")); Serial.println(configuration.dexed[instance_id].engine, DEC);
Serial.print(F(" Mono/Poly ")); Serial.println(configuration.dexed[instance_id].monopoly, DEC);
Serial.print(F(" Pitchbend Range ")); Serial.println(configuration.dexed[instance_id].pb_range, DEC);
Serial.print(F(" Pitchbend Step ")); Serial.println(configuration.dexed[instance_id].pb_step, DEC);
Serial.print(F(" Modwheel Range ")); Serial.println(configuration.dexed[instance_id].mw_range, DEC);
Serial.print(F(" Modwheel Assign ")); Serial.println(configuration.dexed[instance_id].mw_assign, DEC);
Serial.print(F(" Footctrl Range ")); Serial.println(configuration.dexed[instance_id].fc_range, DEC);
Serial.print(F(" Footctrl Assign ")); Serial.println(configuration.dexed[instance_id].fc_assign, DEC);
Serial.print(F(" BreathCtrl Range ")); Serial.println(configuration.dexed[instance_id].bc_range, DEC);
Serial.print(F(" Breathctrl Assign ")); Serial.println(configuration.dexed[instance_id].bc_assign, DEC);
Serial.print(F(" Aftertouch Range ")); Serial.println(configuration.dexed[instance_id].at_range, DEC);
Serial.print(F(" Aftertouch Assign ")); Serial.println(configuration.dexed[instance_id].at_assign, DEC);
Serial.print(F(" OP Enabled ")); Serial.println(configuration.dexed[instance_id].op_enabled, DEC);
Serial.flush();
}
Serial.println();
}
void show_patch(uint8_t instance_id)
{
uint8_t i;
char voicename[VOICE_NAME_LEN];
Serial.print(F("INSTANCE "));
Serial.println(instance_id, DEC);
memset(voicename, 0, sizeof(voicename));
for (i = 0; i < 6; i++)
{
Serial.print(F("OP"));
Serial.print(6 - i, DEC);
Serial.println(F(": "));
Serial.println(F("R1 | R2 | R3 | R4 | L1 | L2 | L3 | L4 LEV_SCL_BRK_PT | SCL_LEFT_DEPTH | SCL_RGHT_DEPTH"));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_R1], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_R2], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_R3], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_R4], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_L1], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_L2], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_L3], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_EG_L4], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_LEV_SCL_BRK_PT], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_SCL_LEFT_DEPTH], DEC);
Serial.print(F(" "));
Serial.println(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_SCL_RGHT_DEPTH], DEC);
Serial.println(F("SCL_L_CURVE | SCL_R_CURVE | RT_SCALE | AMS | KVS | OUT_LEV | OP_MOD | FRQ_C | FRQ_F | DETUNE"));
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_SCL_LEFT_CURVE], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_SCL_RGHT_CURVE], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_OSC_RATE_SCALE], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_AMP_MOD_SENS], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_KEY_VEL_SENS], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_OUTPUT_LEV], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_OSC_MODE], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_FREQ_COARSE], DEC);
Serial.print(F(" "));
Serial.print(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_FREQ_FINE], DEC);
Serial.print(F(" "));
Serial.println(MicroDexed[instance_id]->data[(i * 21) + DEXED_OP_OSC_DETUNE], DEC);
Serial.flush();
}
Serial.println(F("PR1 | PR2 | PR3 | PR4 | PL1 | PL2 | PL3 | PL4"));
Serial.print(F(" "));
for (i = 0; i < 8; i++)
{
Serial.print(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + i], DEC);
Serial.print(F(" "));
}
Serial.println();
Serial.print(F("ALG: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_ALGORITHM], DEC);
Serial.print(F("FB: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_FEEDBACK], DEC);
Serial.print(F("OKS: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_OSC_KEY_SYNC], DEC);
Serial.print(F("LFO SPD: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_SPEED], DEC);
Serial.print(F("LFO_DLY: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_DELAY], DEC);
Serial.print(F("LFO PMD: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_PITCH_MOD_DEP], DEC);
Serial.print(F("LFO_AMD: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_AMP_MOD_DEP], DEC);
Serial.print(F("LFO_SYNC: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_SYNC], DEC);
Serial.print(F("LFO_WAVEFRM: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_WAVE], DEC);
Serial.print(F("LFO_PMS: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_LFO_PITCH_MOD_SENS], DEC);
Serial.print(F("TRNSPSE: "));
Serial.println(MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_TRANSPOSE], DEC);
Serial.print(F("NAME: "));
strncpy(voicename, (char *)&MicroDexed[instance_id]->data[DEXED_VOICE_OFFSET + DEXED_NAME], sizeof(voicename) - 1);
Serial.print(F("["));
Serial.print(voicename);
Serial.println(F("]"));
Serial.flush();
for (i = DEXED_GLOBAL_PARAMETER_OFFSET; i <= DEXED_GLOBAL_PARAMETER_OFFSET + DEXED_MAX_NOTES; i++)
{
Serial.print(i, DEC);
Serial.print(F(": "));
Serial.println(MicroDexed[instance_id]->data[i]);
}
Serial.println();
}
#endif