From 7c64ae6d2e938dc2bc82ad449578cbdf037e4730 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Wed, 25 Jul 2018 09:01:21 +0200 Subject: [PATCH] Improved volume and pan management. Storing last volume and pan in EEPROM. --- MicroDexed.ino | 35 ++++++++++++++++------------------- config.h | 6 ++++-- dexed.cpp | 19 +++++++++++-------- dexed.h | 6 ++++-- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/MicroDexed.ino b/MicroDexed.ino index 4d1c574..c21918c 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -53,15 +53,11 @@ Bounce but1 = Bounce(BUT1_PIN, 10); // 10 ms debounce // GUItool: begin automatically generated code AudioPlayQueue queue1; //xy=708,349 -AudioAmplifier amp1; //xy=904,314 -AudioAmplifier amp2; //xy=909,373 AudioAnalyzePeak peak1; //xy=909,436 AudioOutputI2S i2s1; //xy=1055,343 -AudioConnection patchCord1(queue1, amp1); -AudioConnection patchCord2(queue1, amp2); -AudioConnection patchCord3(queue1, peak1); -AudioConnection patchCord4(amp1, 0, i2s1, 0); -AudioConnection patchCord5(amp2, 0, i2s1, 1); +AudioConnection patchCord1(queue1, peak1); +AudioConnection patchCord2(queue1, 0, i2s1, 0); +AudioConnection patchCord3(queue1, 0, i2s1, 1); AudioControlSGTL5000 sgtl5000_1; //xy=1055,398 // GUItool: end automatically generated code @@ -73,6 +69,9 @@ uint32_t xrun = 0; uint32_t overload = 0; uint32_t peak = 0; uint16_t render_time_max = 0; +float vol = float(EEPROM.read(EEPROM_MASTER_VOLUME_ADDR)) / 256; +float vol_right = float(EEPROM.read(EEPROM_VOLUME_RIGHT_ADDR)) / 256; +float vol_left = float(EEPROM.read(EEPROM_VOLUME_LEFT_ADDR)) / 256; #ifdef MASTER_KEY_MIDI bool master_key_enabled = false; @@ -132,15 +131,13 @@ void setup() // start audio card AudioMemory(AUDIO_MEM); - amp1.gain(1.0); // normal audio - amp2.gain(1.0); // normal audio sgtl5000_1.enable(); //sgtl5000_1.dacVolumeRamp(); sgtl5000_1.dacVolumeRampLinear(); sgtl5000_1.unmuteHeadphone(); sgtl5000_1.autoVolumeDisable(); // turn off AGC - sgtl5000_1.volume(1.0,1.0); - sgtl5000_1.dacVolume(VOLUME,VOLUME); + sgtl5000_1.volume(1.0, 1.0); + set_volume(vol, vol_left, vol_right); // start SD card SPI.setMOSI(SDCARD_MOSI_PIN); @@ -381,7 +378,7 @@ bool handle_master_key(uint8_t data) Serial.print(F("Loading voice number ")); Serial.println(num, DEC); #endif - store_voice_number(bank, num); + EEPROM.update(EEPROM_VOICE_ADDR, num); } #ifdef DEBUG else @@ -399,8 +396,7 @@ bool handle_master_key(uint8_t data) num = abs(num); if (num <= 10) { - //sgtl5000_1.volume(num * 0.1, num * 0.1); - sgtl5000_1.dacVolume(num * 0.1, num * 0.1); + set_volume(num * 0.1, vol_left, vol_right); #ifdef DEBUG Serial.print(F("Volume changed to: ")); Serial.println(num * 0.1, DEC); @@ -409,6 +405,7 @@ bool handle_master_key(uint8_t data) else if (num > 10 && num <= 20) { bank = num - 10; + EEPROM.update(EEPROM_BANK_ADDR, bank); #ifdef DEBUG Serial.print(F("Bank switch to: ")); Serial.println(bank, DEC); @@ -533,12 +530,12 @@ int8_t num_key_base_c(uint8_t midi_note) } #endif -void store_voice_number(uint8_t bank, uint8_t voice) +void set_volume(float master_volume, float volume_right, float volume_left) { - if (EEPROM.read(EEPROM_BANK_ADDR) != bank) - EEPROM.write(EEPROM_BANK_ADDR, bank); - if (EEPROM.read(EEPROM_VOICE_ADDR) != voice) - EEPROM.write(EEPROM_VOICE_ADDR, voice); + EEPROM.update(EEPROM_MASTER_VOLUME_ADDR, uint8_t(master_volume * 256)); + EEPROM.update(EEPROM_VOLUME_RIGHT_ADDR, uint8_t(volume_right * 256)); + EEPROM.update(EEPROM_VOLUME_LEFT_ADDR, uint8_t(volume_left * 256)); + sgtl5000_1.dacVolume(master_volume * volume_left, master_volume * volume_right); } void handle_sysex_parameter(const uint8_t* sysex, uint8_t len) diff --git a/config.h b/config.h index 127c57b..0478e43 100644 --- a/config.h +++ b/config.h @@ -31,7 +31,7 @@ // Initial values #define MIDI_DEVICE Serial1 #define USE_ONBOARD_USB_HOST 1 -#define VOLUME 0.5 +#define VOLUME 1.0 #define DEFAULT_MIDI_CHANNEL MIDI_CHANNEL_OMNI #define DEFAULT_SYSEXBANK 0 #define DEFAULT_SYSEXSOUND 0 @@ -84,4 +84,6 @@ // EEPROM address #define EEPROM_BANK_ADDR 0 #define EEPROM_VOICE_ADDR 1 - +#define EEPROM_MASTER_VOLUME_ADDR 2 +#define EEPROM_VOLUME_RIGHT_ADDR 3 +#define EEPROM_VOLUME_LEFT_ADDR 4 diff --git a/dexed.cpp b/dexed.cpp index 99d2844..5efbbae 100644 --- a/dexed.cpp +++ b/dexed.cpp @@ -220,24 +220,27 @@ bool Dexed::processMidiMessage(uint8_t type, uint8_t data1, uint8_t data2) controllers.refresh(); break; case 7: // Volume - //sgtl5000_1.volume(value / 0x7f, value / 0x7f); - sgtl5000_1.dacVolume(value / 0x7f, value / 0x7f); + vol = value / 0x7f; + sgtl5000_1.dacVolume(vol * vol_left, vol * vol_right); break; case 10: // Pan if (value < 64) { - amp1.gain(1.0); - amp2.gain(float(value) / 0x40); + vol_left = 1.0; + vol_right = float(value) / 0x40; + set_volume(vol, vol_left, vol_right); } else if (value > 64) { - amp1.gain(float(0x7f - value) / 0x40); - amp2.gain(1.0); + vol_left = float(0x7f - value) / 0x40; + vol_right = 1.0; + set_volume(vol, vol_left, vol_right); } else { - amp1.gain(1.0); - amp2.gain(1.0); + vol_left = 1.0; + vol_right = 1.0; + set_volume(vol, vol_left, vol_right); } break; case 32: // BankSelect LSB diff --git a/dexed.h b/dexed.h index 015c5ad..574125a 100644 --- a/dexed.h +++ b/dexed.h @@ -40,8 +40,10 @@ extern uint8_t bank; extern uint32_t overload; extern bool load_sysex(uint8_t bank, uint8_t voice_number); extern AudioControlSGTL5000 sgtl5000_1; -extern AudioAmplifier amp1; -extern AudioAmplifier amp2; +extern float vol; +extern float vol_right; +extern float vol_left; +extern void set_volume(float master_volume, float volume_right, float volume_left); struct ProcessorVoice { uint8_t midi_note;