MATLAB Answers

Implementing the Chorus Effect

39 views (last 30 days)
Vincent Abraham
Vincent Abraham on 18 Nov 2020
Commented: Mathieu NOE on 23 Nov 2020
I have implemented a code for flanging. Flanging uses a delay of 0-10ms (in this case 0-3ms) to produce the desired output. Now, I want to implement this same code for creating a chorus effect, which uses a delay of 25-30 or 30-50ms. Please suggest what I should do
clear all;
close all;
infile='acoustic.wav';
outfile='out_chorus.wav';
% current sample is 11kHz so 0-3 ms is 0 - 33 samples
% read the sample waveform
[x,Fs] = audioread(infile);
% parameters to vary the effect %
min_time_delay=0.010; % 10ms min delay in seconds
max_time_delay=0.025; % 25ms max delay in seconds
rate=1; %rate of flange in Hz
index=1:length(x);
% sin reference to create oscillating delay
sin_ref = (sin(2*pi*index*(rate/Fs)))'; % sin(2pi*fa/fs);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
min_samp_delay=round(min_time_delay*Fs); %convert delay in ms to min delay in samples
max_samp_delay=round(max_time_delay*Fs); %convert delay in ms to max delay in samples
y = zeros(length(x),1); % create empty out vector
y(1:max_samp_delay)=x(1:max_samp_delay); % to avoid referencing of negative samples
amp=0.7; % suggested coefficient from page 71 DAFX
% for each sample
for i = (max_samp_delay+1):length(x)
cur_sin=abs(sin_ref(i)); %abs of current sin val 0-1
cur_delay=ceil(cur_sin*max_samp_delay); % generate delay from 1-max_samp_delay and ensure whole number
y(i) = (amp*x(i)) + amp*(x(i-cur_delay)); % add delayed sample
end
% write output
audiowrite(outfile, y, Fs);
figure(1)
hold on
plot(x,'r');
plot(y,'b');
title('Flanger and original Signal');

  6 Comments

Show 3 older comments
Vincent Abraham
Vincent Abraham on 19 Nov 2020
And i have seen those examples, but they are implemented on Simulink
Mathieu NOE
Mathieu NOE on 19 Nov 2020
and you don't have Simulink ?
Vincent Abraham
Vincent Abraham on 19 Nov 2020
I do. But i need to implement this in code

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 19 Nov 2020
hello again
so this could be considered as a vectorized version of the original code. You can add multiple delayed versions of the original signal
just make sure the dimensions of rate and amp are consistent
hope it matches your musical expectations...
infile='DirectGuitar.wav';
outfile='out_chorus.wav';
% current sample is 11kHz so 0-3 ms is 0 - 33 samples
% read the sample waveform
[x,Fs] = audioread(infile);
% normalize x to +/- 1 amplitude
x = x ./ (max(abs(x)));
samples = length(x);
% parameters to vary the effect %
min_time_delay=0.010; % 10ms min delay in seconds
max_time_delay=0.025; % 25ms max delay in seconds
% nb_effects = 4; % assume we do 4 delayed versions of the signal that will be summed
rate=[1 0.9 0.8 0.7] ; %rate of flange in Hz (as many as chorus effects)
nb_effects = length(rate); % assume we do 4 delayed versions of the signal that will be summed in the final section
% amplitude is now a vector
amp=[0.7 0.6 0.5 0.4 0.3];; % suggested coefficient from page 71 DAFX;
% Nb the first value applies to x and the remaining values apply to the 4
% delayed versions of x) so make sure this vector has length = nb_effects +1
index=(1:samples)';
% sin reference to create oscillating delay
sin_ref = (sin(2*pi*index*(rate/Fs)))'; % sin(2pi*fa/fs); % make sure this is a matrix dimensins = (nb_effects , samples)
% sin_ref = 0.5*(1+sin(2*pi*index*(rate/Fs)))'; % sin(2pi*fa/fs); % make sure this is a matrix dimensins = (nb_effects , samples)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
min_samp_delay=round(min_time_delay*Fs); %convert delay in ms to min delay in samples
max_samp_delay=round(max_time_delay*Fs); %convert delay in ms to max delay in samples
y = zeros(samples,1); % create empty out vector
y(1:max_samp_delay)=x(1:max_samp_delay); % to avoid referencing of negative samples
% amp=0.7; % suggested coefficient from page 71 DAFX
% for each sample
for i = (max_samp_delay+1):length(x)
cur_sin=abs(sin_ref(:,i)); %abs of current sin val 0-1
cur_delay=ceil(cur_sin*max_samp_delay); % generate delay from 1-max_samp_delay and ensure whole number
% add the amp weigthed multiple delayed x values
y(i) = amp(1)*x(i) + sum(amp(2:nb_effects+1)'.*x(i-cur_delay)); % add all 4 delayed sample (in one shot !)
end
% write output
% normalize y to +/- 1 amplitude
y = y ./ (max(abs(y)));
audiowrite(outfile, y, Fs);
figure(1)
hold on
plot(x,'r');
plot(y,'b');
title('Chorus and original Signal');

  19 Comments

Vincent Abraham
Vincent Abraham on 20 Nov 2020
Were you still able to generate chorus with the fixed delay ?
Mathieu NOE
Mathieu NOE on 23 Nov 2020
hello
yes , for the fixed dealy (4 effects) the sound is good
as soon as we make the delay variable, it's horrible... don't know what we are doing wrong here
Mathieu NOE
Mathieu NOE on 23 Nov 2020
so what do we do next ??

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!