From 1b2e43bd1b2907b97b944dd09238853a11582315 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Fri, 3 May 2019 08:52:26 +0200 Subject: [PATCH] Moved volume handling to internal configuration. --- MicroMDAEPiano.ino | 40 ++++++++++++++++++++++------------------ config.h | 4 +++- mdaEPiano.cpp | 4 ++-- mdaEPiano.h | 2 ++ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/MicroMDAEPiano.ino b/MicroMDAEPiano.ino index 31174d5..8544e36 100644 --- a/MicroMDAEPiano.ino +++ b/MicroMDAEPiano.ino @@ -100,9 +100,6 @@ uint32_t xrun = 0; uint32_t overload = 0; uint32_t peak = 0; uint16_t render_time_max = 0; -float vol = VOLUME; -float vol_right = 1.0; -float vol_left = 1.0; elapsedMicros fill_audio_buffer; elapsedMillis control_rate; elapsedMillis autostore; @@ -197,7 +194,7 @@ void setup() #endif //set_volume(vol, vol_left, vol_right); - set_volume(1.0, 1.0, 1.0); + set_volume(1.0, 0.5); #if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC) // Initialize processor and memory measurements @@ -293,14 +290,6 @@ void loop() peak++; } -#ifndef TEENSY_AUDIO_BOARD - for (uint8_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++) - { - audio_buffer_r[i] *= vol; - audio_buffer_l[i] *= vol; - } -#endif - queue_r.playBuffer(); queue_l.playBuffer(); } @@ -311,13 +300,9 @@ void loop() if (control_rate > CONTROL_RATE_MS) { control_rate = 0; - - - } } - //************************************************************************************************* //* PROGRAM FUNCTIONS //************************************************************************************************* @@ -446,9 +431,28 @@ bool checkMidiChannel(byte inChannel) return (true); } -void set_volume(float v, float vr, float vl) +void set_volume(float v, float p) { - ; + configuration.vol = v; + configuration.pan = p; + +#ifdef DEBUG + Serial.print(F("Setting volume: VOL=")); + Serial.print(v, DEC); + Serial.print(F("[")); + Serial.print(configuration.vol, DEC); + Serial.print(F("] PAN=")); + Serial.print(F("[")); + Serial.print(configuration.pan, DEC); + Serial.print(F("] ")); + Serial.print(pow(configuration.vol * sinf(configuration.pan * PI / 2), VOLUME_CURVE), 3); + Serial.print(F("/")); + Serial.println(pow(configuration.vol * cosf( configuration.pan * PI / 2), VOLUME_CURVE), 3); +#endif + + // http://files.csound-tutorial.net/floss_manual/Release03/Cs_FM_03_ScrapBook/b-panning-and-spatialization.html + mixer_r.gain(0,sinf(p * PI / 2)); + mixer_l.gain(0,cosf(p * PI / 2)); } /****************************************************************************** diff --git a/config.h b/config.h index 7524cb7..9126c4a 100644 --- a/config.h +++ b/config.h @@ -41,7 +41,7 @@ // AUDIO // If nothing is defined PT8211 is used as audio output device! -//#define TEENSY_AUDIO_BOARD 1 +#define TEENSY_AUDIO_BOARD 1 //#define TGA_AUDIO_BOARD 1 //************************************************************************************************* @@ -56,6 +56,7 @@ //************************************************************************************************* #define VOLUME 0.8 +#define VOLUME_CURVE 0.07 #define AUDIO_MEM 128 #define SAMPLE_RATE 44100 #define REDUCE_LOUDNESS 0 @@ -75,6 +76,7 @@ //************************************************************************************************* // Teensy Audio Shield: +#define SGTL5000_LINEOUT_LEVEL 29 //#define SDCARD_CS_PIN 10 //#define SDCARD_MOSI_PIN 7 //#define SDCARD_SCK_PIN 14 diff --git a/mdaEPiano.cpp b/mdaEPiano.cpp index 835e4d2..3aad90f 100644 --- a/mdaEPiano.cpp +++ b/mdaEPiano.cpp @@ -240,8 +240,8 @@ void mdaEPiano::process(int16_t* outputs_r, int16_t* outputs_l) l = 1.0; else if (l < -1.0) l = -1.0; - outputs_l[frame] = static_cast(l * vol * 0x7fff) >> REDUCE_LOUDNESS; - outputs_r[frame] = static_cast(r * vol * 0x7fff) >> REDUCE_LOUDNESS; + outputs_l[frame] = static_cast(l * configuration.vol * 0x7fff) >> REDUCE_LOUDNESS; + outputs_r[frame] = static_cast(r * configuration.vol * 0x7fff) >> REDUCE_LOUDNESS; } if (fabs(tl) < 1.0e-10) tl = 0.0f; //anti-denormal diff --git a/mdaEPiano.h b/mdaEPiano.h index 3fa035b..b593301 100644 --- a/mdaEPiano.h +++ b/mdaEPiano.h @@ -34,6 +34,8 @@ #define SILENCE 0.0001f //voice choking #define WAVELEN 422414 //wave data bytes +extern config_t configuration; + class mdaEPianoProgram { friend class mdaEPiano;