%% Delay-Based Audio Effects
% This example shows how to design and use three audio effects that are based on varying delay: echo, chorus and flanger. The example also shows how the algorithms, developed in MATLAB, can be easily ported to Simulink.
%
%% Introduction
% Audio effects can be generated by adding a processed ('wet') signal to the original ('dry') audio signal. A simple effect, echo, adds a delayed version of the signal to the original. More complex effects, like chorus and flanger, modulate the delayed version of the signal.
%
%% Echo
% You can model the echo effect by delaying the audio signal and adding it back. Feedback is often added to the delay line to give a fading effect. The echo effect is implemented in the audioexample.Echo class. The block diagram shows a high-level implementation of an echo effect.
%
% The echo effect example has four tunable parameters that can be modified while the simulation is running:
% Delay - Delay applied to audio signal, in seconds
% Gain - Linear gain of the delayed audio
% FeedbackLevel - Feedback gain applied to delay line
% WetDryMix - Ratio of wet signal added to dry signal
% You can try out audioexample.Echo by running audioDelayEffectsExampleApp with 'echo' as input. The example reads an audio signal from a file, applies the echo effect, and then plays the processed signal through your audio output device. It also launches a UI that allows you to tune the parameters of the echo effect. You can pass an additional argument that determines duration to play the audio.

duration = 30; % in seconds
audioDelayEffectsExampleApp('echo',duration);

%
%
%
%% Chorus
% The chorus effect usually has multiple independent delays, each modulated by a low-frequency oscillator. audioexample.Chorus implements this effect. The block diagram shows a high-level implementation of a chorus effect.
%
% The chorus effect example has six tunable parameters that can be modified while the simulation is running:
% Delay - Base delay applied to audio signal, in seconds
% Depth 1 - Amplitude of modulator applied to first delay branch
% Rate 1 - Frequency of modulator applied to first delay branch, in Hz
% Depth 2 - Amplitude of modulator applied to second delay branch
% Rate 2 - Frequency of modulator applied to second delay branch, in Hz
% WetDryMix - Ratio of wet signal added to dry signal
% You can try out audioexample.Chorus by running audioDelayEffectsExampleApp with 'chorus' as input. The example reads an audio signal from a file, applies the chorus effect, then plays the processed signal through your audio output device. It also launches a UI that allows you to tune the parameters of the chorus effect. You can pass an additional argument that determines duration to play the audio.

duration = 30; % in seconds
audioDelayEffectsExampleApp('chorus',duration);

%
%
%
%% Flanger
% You can model the flanging effect by delaying the audio input by an amount that is modulated by a low-frequency oscillator (LFO). The delay line used in flanger can also have a feedback path. audioexample.Flanger implements this effect. The block diagram shows a high-level implementation of a flanger effect.
%
% The flanger effect example has five tunable parameters that can be modified while the simulation is running:
% Delay - Base delay applied to audio signal, in seconds
% Depth - Amplitude of LFO
% Rate - Frequency of LFO, in Hz
% FeedbackLevel - Feedback gain applied to delay line
% WetDryMix - Ratio of wet signal added to dry signal
% You can try out audioexample.Flanger by running audioDelayEffectsExampleApp with 'flanger' as input. The example reads an audio signal from a file, applies the flanger effect, then plays the processed signal through your audio output device. It also launches a UI that allows you to tune the parameters of the flanger effect. The second input to this function is optional, and decides how long the audio should be played. You can pass an additional argument that determines duration to play the audio.

duration = 30; % in seconds
audioDelayEffectsExampleApp('flanger',duration);

%
%
%
%% Audio Effects in Simulink
% You can use the System objects audioexample.Echo, audioexample.Chorus and audioexample.Flanger in Simulink by using the MATLAB System block. The model audiodelaybasedeffects has these effects ready for simulation.

open_system('audiodelaybasedeffects')

%
% You can select the effect to be applied by double-clicking on the Effect Selector block.
%
% Once the effect has been selected, you can click on Launch Parameter Tuning UI button to bring up the dialog that has all tunable parameters of the effect.
%
% This dialog will remain available even during simulation. You can run the model and tune properties of the effect to listen to how they affect the audio output.
% Copyright 2015-2019 The MathWorks, Inc.