|
| 1 | +// FormantShifter_FD |
| 2 | +// |
| 3 | +// Demonstrate formant shifting via frequency domain processin. |
| 4 | +// |
| 5 | +// Created: Chip Audette (OpenAudio) March 2019 |
| 6 | +// |
| 7 | +// Approach: |
| 8 | +// * Take samples in the time domain |
| 9 | +// * Take FFT to convert to frequency domain |
| 10 | +// * Manipulate the frequency bins to do the formant shifting |
| 11 | +// * Take IFFT to convert back to time domain |
| 12 | +// * Send samples back to the audio interface |
| 13 | +// |
| 14 | +// The amount of formant shifting is controled via the Serial link. |
| 15 | +// It defaults to a modest upward shifting of the formants |
| 16 | +// |
| 17 | +// Built for the Tympan library for Teensy 3.6-based hardware |
| 18 | +// |
| 19 | +// MIT License. Use at your own risk. |
| 20 | +// |
| 21 | + |
| 22 | +#include <Tympan_Library.h> |
| 23 | +#include "AudioEffectFormantShiftFD_F32.h" //the local file holding your custom function |
| 24 | +#include "SerialManager.h" |
| 25 | + |
| 26 | +//set the sample rate and block size |
| 27 | +const float sample_rate_Hz = 44117.f; ; //24000 or 44117 (or other frequencies in the table in AudioOutputI2S_F32) |
| 28 | +const int audio_block_samples = 128; //for freq domain processing choose a power of 2 (16, 32, 64, 128) but no higher than 128 |
| 29 | +AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples); |
| 30 | + |
| 31 | +//create audio library objects for handling the audio |
| 32 | +Tympan audioHardware(TympanRev::D); //do TympanRev::C or TympanRev::D |
| 33 | +AudioInputI2S_F32 i2s_in(audio_settings); //Digital audio *from* the Tympan AIC. |
| 34 | +AudioEffectFormantShiftFD_F32 formantShift(audio_settings); //create the frequency-domain processing block |
| 35 | +AudioEffectGain_F32 gain1; //Applies digital gain to audio data. |
| 36 | +AudioOutputI2S_F32 i2s_out(audio_settings); //Digital audio out *to* the Tympan AIC. |
| 37 | + |
| 38 | +//Make all of the audio connections |
| 39 | +AudioConnection_F32 patchCord1(i2s_in, 0, formantShift, 0); //use the Left input |
| 40 | +AudioConnection_F32 patchCord2(formantShift, 0, gain1, 0); //connect to gain |
| 41 | +AudioConnection_F32 patchCord3(gain1, 0, i2s_out, 0); //connect to the left output |
| 42 | +AudioConnection_F32 patchCord4(gain1, 0, i2s_out, 1); //connect to the right output |
| 43 | + |
| 44 | + |
| 45 | +//control display and serial interaction |
| 46 | +bool enable_printCPUandMemory = false; |
| 47 | +void togglePrintMemoryAndCPU(void) { enable_printCPUandMemory = !enable_printCPUandMemory; }; |
| 48 | +SerialManager serialManager(audioHardware); |
| 49 | +#define mySerial audioHardware //audioHardware is a printable stream! |
| 50 | + |
| 51 | +//inputs and levels |
| 52 | +float input_gain_dB = 20.0f; //gain on the microphone |
| 53 | +float formant_shift_gain_correction_dB = 0.0; //will be used to adjust for gain in formant shifter |
| 54 | +float vol_knob_gain_dB = 0.0; //will be overridden by volume knob |
| 55 | +void switchToPCBMics(void) { |
| 56 | + mySerial.println("Switching to PCB Mics."); |
| 57 | + audioHardware.inputSelect(TYMPAN_INPUT_ON_BOARD_MIC); // use the microphone jack - defaults to mic bias OFF |
| 58 | + audioHardware.setInputGain_dB(input_gain_dB); |
| 59 | +} |
| 60 | +void switchToLineInOnMicJack(void) { |
| 61 | + mySerial.println("Switching to Line-in on Mic Jack."); |
| 62 | + audioHardware.inputSelect(TYMPAN_INPUT_JACK_AS_LINEIN); // use the microphone jack - defaults to mic bias OFF |
| 63 | + audioHardware.setInputGain_dB(0.0); |
| 64 | +} |
| 65 | +void switchToMicInOnMicJack(void) { |
| 66 | + mySerial.println("Switching to Mic-In on Mic Jack."); |
| 67 | + audioHardware.inputSelect(TYMPAN_INPUT_JACK_AS_MIC); // use the microphone jack - defaults to mic bias OFF |
| 68 | + audioHardware.setInputGain_dB(input_gain_dB); |
| 69 | +} |
| 70 | + |
| 71 | +// define the setup() function, the function that is called once when the device is booting |
| 72 | +void setup() { |
| 73 | + audioHardware.beginBothSerial(); delay(1000); |
| 74 | + mySerial.println("FormantShifter: starting setup()..."); |
| 75 | + mySerial.print(" : sample rate (Hz) = "); mySerial.println(audio_settings.sample_rate_Hz); |
| 76 | + mySerial.print(" : block size (samples) = "); mySerial.println(audio_settings.audio_block_samples); |
| 77 | + |
| 78 | + // Audio connections require memory to work. For more |
| 79 | + // detailed information, see the MemoryAndCpuUsage example |
| 80 | + AudioMemory_F32(40, audio_settings); |
| 81 | + |
| 82 | + // Configure the frequency-domain algorithm |
| 83 | + int overlap_factor = 4; //set to 4 or 8 or either 75% overlap (4x) or 87.5% overlap (8x) |
| 84 | + int N_FFT = audio_block_samples * overlap_factor; |
| 85 | + formantShift.setup(audio_settings, N_FFT); //do after AudioMemory_F32(); |
| 86 | + formantShift.setScaleFactor(1.5); //1.0 is no formant shifting. |
| 87 | + if (overlap_factor == 4) { |
| 88 | + formant_shift_gain_correction_dB = -3.0; |
| 89 | + } else if (overlap_factor == 8) { |
| 90 | + formant_shift_gain_correction_dB = -9.0; |
| 91 | + } |
| 92 | + |
| 93 | + //Enable the Tympan to start the audio flowing! |
| 94 | + audioHardware.enable(); // activate AIC |
| 95 | + |
| 96 | + //setup DC-blocking highpass filter running in the ADC hardware itself |
| 97 | + float cutoff_Hz = 60.0; //set the default cutoff frequency for the highpass filter |
| 98 | + audioHardware.setHPFonADC(true,cutoff_Hz,audio_settings.sample_rate_Hz); //set to false to disble |
| 99 | + |
| 100 | + //Choose the desired input |
| 101 | + switchToPCBMics(); //use PCB mics as input |
| 102 | + //switchToMicInOnMicJack(); //use Mic jack as mic input (ie, with mic bias) |
| 103 | + //switchToLineInOnMicJack(); //use Mic jack as line input (ie, no mic bias) |
| 104 | + |
| 105 | + //Set the desired volume levels |
| 106 | + audioHardware.volume_dB(0); // headphone amplifier. -63.6 to +24 dB in 0.5dB steps. |
| 107 | + |
| 108 | + // configure the blue potentiometer |
| 109 | + servicePotentiometer(millis(),0); //update based on the knob setting the "0" is not relevant here. |
| 110 | + |
| 111 | + //finish the setup by printing the help menu to the serial connections |
| 112 | + serialManager.printHelp(); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +// define the loop() function, the function that is repeated over and over for the life of the device |
| 117 | +void loop() { |
| 118 | + |
| 119 | + //respond to Serial commands |
| 120 | + while (Serial.available()) serialManager.respondToByte((char)Serial.read()); //USB Serial |
| 121 | + //while (Serial1.available()) serialManager.respondToByte((char)Serial1.read()); //BT Serial |
| 122 | + |
| 123 | + //check the potentiometer |
| 124 | + servicePotentiometer(millis(), 100); //service the potentiometer every 100 msec |
| 125 | + |
| 126 | + //check to see whether to print the CPU and Memory Usage |
| 127 | + if (enable_printCPUandMemory) printCPUandMemory(millis(), 3000); //print every 3000 msec |
| 128 | + |
| 129 | +} //end loop(); |
| 130 | + |
| 131 | + |
| 132 | +// ///////////////// Servicing routines |
| 133 | + |
| 134 | +//servicePotentiometer: listens to the blue potentiometer and sends the new pot value |
| 135 | +// to the audio processing algorithm as a control parameter |
| 136 | +void servicePotentiometer(unsigned long curTime_millis, const unsigned long updatePeriod_millis) { |
| 137 | + //static unsigned long updatePeriod_millis = 100; //how many milliseconds between updating the potentiometer reading? |
| 138 | + static unsigned long lastUpdate_millis = 0; |
| 139 | + static float prev_val = -1.0; |
| 140 | + |
| 141 | + //has enough time passed to update everything? |
| 142 | + if (curTime_millis < lastUpdate_millis) lastUpdate_millis = 0; //handle wrap-around of the clock |
| 143 | + if ((curTime_millis - lastUpdate_millis) > updatePeriod_millis) { //is it time to update the user interface? |
| 144 | + |
| 145 | + //read potentiometer |
| 146 | + float val = float(audioHardware.readPotentiometer()) / 1023.0; //0.0 to 1.0 |
| 147 | + val = (1.0/9.0) * (float)((int)(9.0 * val + 0.5)); //quantize so that it doesn't chatter...0 to 1.0 |
| 148 | + |
| 149 | + //use the potentiometer value to control something interesting |
| 150 | + if (abs(val - prev_val) > 0.05) { //is it different than befor? |
| 151 | + prev_val = val; //save the value for comparison for the next time around |
| 152 | + |
| 153 | + #if 0 |
| 154 | + //set the volume of the system |
| 155 | + setVolKnobGain_dB(val*45.0f - 10.0f - input_gain_dB); |
| 156 | + #else |
| 157 | + //set the amount of formant shifting |
| 158 | + float new_scale_fac = powf(2.0,(val-0.5)*2.0); |
| 159 | + formantShift.setScaleFactor(new_scale_fac); |
| 160 | + #endif |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + lastUpdate_millis = curTime_millis; |
| 165 | + } // end if |
| 166 | +} //end servicePotentiometer(); |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +//This routine prints the current and maximum CPU usage and the current usage of the AudioMemory that has been allocated |
| 171 | +void printCPUandMemory(unsigned long curTime_millis, unsigned long updatePeriod_millis) { |
| 172 | + //static unsigned long updatePeriod_millis = 3000; //how many milliseconds between updating gain reading? |
| 173 | + static unsigned long lastUpdate_millis = 0; |
| 174 | + |
| 175 | + //has enough time passed to update everything? |
| 176 | + if (curTime_millis < lastUpdate_millis) lastUpdate_millis = 0; //handle wrap-around of the clock |
| 177 | + if ((curTime_millis - lastUpdate_millis) > updatePeriod_millis) { //is it time to update the user interface? |
| 178 | + mySerial.print("printCPUandMemory: "); |
| 179 | + mySerial.print("CPU Cur/Peak: "); |
| 180 | + mySerial.print(audio_settings.processorUsage()); |
| 181 | + //mySerial.print(AudioProcessorUsage()); //if not using AudioSettings_F32 |
| 182 | + mySerial.print("%/"); |
| 183 | + mySerial.print(audio_settings.processorUsageMax()); |
| 184 | + //mySerial.print(AudioProcessorUsageMax()); //if not using AudioSettings_F32 |
| 185 | + mySerial.print("%, "); |
| 186 | + mySerial.print("Dyn MEM Float32 Cur/Peak: "); |
| 187 | + mySerial.print(AudioMemoryUsage_F32()); |
| 188 | + mySerial.print("/"); |
| 189 | + mySerial.print(AudioMemoryUsageMax_F32()); |
| 190 | + mySerial.println(); |
| 191 | + |
| 192 | + lastUpdate_millis = curTime_millis; //we will use this value the next time around. |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +void printGainSettings(void) { |
| 197 | + mySerial.print("Gain (dB): "); |
| 198 | + mySerial.print("Vol Knob = "); mySerial.print(vol_knob_gain_dB,1); |
| 199 | + //mySerial.print(", Input PGA = "); mySerial.print(input_gain_dB,1); |
| 200 | + mySerial.println(); |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +void incrementKnobGain(float increment_dB) { //"extern" to make it available to other files, such as SerialManager.h |
| 205 | + setVolKnobGain_dB(vol_knob_gain_dB+increment_dB); |
| 206 | +} |
| 207 | + |
| 208 | +void setVolKnobGain_dB(float gain_dB) { |
| 209 | + vol_knob_gain_dB = gain_dB; |
| 210 | + gain1.setGain_dB(vol_knob_gain_dB+formant_shift_gain_correction_dB); |
| 211 | + printGainSettings(); |
| 212 | +} |
| 213 | + |
| 214 | +float incrementFormantShift(float incr_factor) { |
| 215 | + float cur_scale_factor = formantShift.getScaleFactor(); |
| 216 | + return formantShift.setScaleFactor(cur_scale_factor*incr_factor); |
| 217 | +} |
| 218 | + |
| 219 | + |
0 commit comments