Audio Processing Series Part III : Designing an Echo effect
Echo Algorithm & Parameter Tuning
The echo effect is a widely used audio processing technique that mimics sound reflections in different environments. Fundamentally, the echo algorithm works by capturing an audio input, introducing a delay to the signal, and blending it back with the original sound. This method requires fine-tuning parameters like delay time, feedback, and amplitude, enabling the generation of multiple repetitions that can differ in intensity and timing. Delay time specifies how long it takes for the echoed sound to return, while feedback determines the amount of the output signal that is redirected back into the input.
The delay time is set to 300 milliseconds, determining the duration between the original signal and its echoed repetition. This value creates a noticeable gap that allows listeners to perceive the echo distinctly. The feedback level is configured to 0.7, which indicates that 70% of the output signal is fed back into the input.
Reading Audio Data from Flash
This function reads audio data from flash memory and converts it from a 16-bit integer PCM format to floating-point format. By interpreting each pair of bytes as a single audio sample, the function scales these samples to a range of -1 to 1, making them suitable for floating-point processing. After retrieving and converting the data, the function stores the processed values in the provided array, ready for further audio processing or playback.
Processing Audio to create an Echo effect
These definitions set up key parameters for the echo effect in the project. SAMPLE_RATE specifies the audio sampling rate at 16,000 samples per second, which determines the resolution of audio data. DELAY_MS sets the echo delay time to 300 milliseconds, defining how long after the original sound the echo will be heard. BUFFER_SIZE specifies a buffer size of 128 samples to handle audio data during processing. FEEDBACK, set to 0.7, controls the intensity of repeated echoes, with each subsequent echo being 70% as loud as the previous one. The calculated DELAY_SAMPLES converts the delay from milliseconds to samples, providing a precise delay length that fits within the defined sample rate (16,000 Hz), resulting in 4,800 samples for the delay duration.
This function applies an echo effect to audio data by combining the current audio sample with a delayed version stored in a buffer. For each sample in the input block, it retrieves a delayed sample from the delayBuffer, scales it by the FEEDBACK factor, and adds it to the current input sample to produce the echo effect in the output. The function then stores the current input sample in the buffer to be used as a delayed sample in future iterations. The delayIndex is incremented with each sample, and when it reaches the defined delay length (DELAY_SAMPLES), it wraps back to the start of the buffer, creating a continuous loop for the echo effect.
Writing processed Audio to Flash Memory
This function writes audio data to flash memory by converting each floating-point sample in the range [-1, 1] to a 16-bit integer PCM format. Each floating-point sample is scaled and offset to fit into the 16-bit range [0, 65535], clamping any values that exceed this range to avoid distortion. The processed samples are then stored as pairs of bytes, which represent the low and high parts of each 16-bit integer. Finally, the function writes the converted data to the specified page in flash memory, making it available for future retrieval.
Analysis of Audio Playback: Original v/s Processed
Plot of the Input PCM file |
The plots of the Input and the Echo Processed PCM file reveal subtle differences in waveform characteristics, indicating the impact of the echo processing on the audio signal like the variations in amplitude and timing, highlight how the echo effect modifies the original sound.
The 300 ms delay means that each sound will be repeated after a quarter of a second, allowing listeners to perceive a distinct echo. With 70% feedback, the echoes are not just single repetitions; they will continue to bounce back into the mix multiple times, gradually diminishing in volume with each iteration.