%% Cell ONE - Single Delay Based Effects

% I have created two cells. The first cell (this one) deals with single
% (one) delay based effects. Most of the scipt here is sourced and inspired
% by Ella Manor's tutorial on Time Delay Modulation, week 11 May 21st 2014.
% 
%
% There are four scripts in total (including this one). flan.m, vibrato.m
% and iircomb.m.
%
% flang.m is sourced from Mathworks file exchange, it is written by Steven 
% G. McGovern. Vibrato.m is sourced from the 2002 DAFx by Udo Zölzer, chapter 3 source
% material, and iircomb.m is sourced from the Sydney University Library
% eScholarship Repository, by Samuel Johnson, DESC9115, 2012. His code was
% based off the 2002 DAFx chapters by Udo Zölzer.
%
% 
% Simply click "Run Selection" to begin this cell. The user is promted with
% a selection of three effects. The input is case sensitive. The user can
% follow the prompts on screen to create a simple effect. The result effect
% is written to an audiofile. If the script is re-run, the audio file is
% overwitten. 
%
% The second cell is multi-delay based effects that combine two effects
% together. For selection, simply type 1, 2 or 3 when prompted for desired
% effect. 


clear all


[guitar, fs] = audioread('matlab2.wav');
% normalise the signal
guitar = guitar./max(abs((guitar)));


display = '\n Flange, Vibrato or Delay?\n\n >';
% assign the selected string input to variable effect_select
effect_select = input(display,'s');


% FLANGE
if strcmp('Flange',effect_select)
%flan = flange(fs,v,guitar,r);
%Normalise
%[guitarflan, fs] = audioread('matlab2.wav');
%guitarflan = guitarflan./max(abs((guitarflan)));
%effect1 = guitarflan;
filename = 'guitar';


% VIBRATO
elseif strcmp('Vibrato',effect_select)
%Vib = vibrato(guitar,Modfreq,Width,fs);
% Normalise
%[guitarvib, fs] = audioread('matlab2.wav');
%guitarvib = guitarvib./max(abs((guitarvib)));
%effect1 = guitarvib;
filename = 'guitar';

% DELAY
elseif strcmp('Delay',effect_select)
%Delay = iircomb(guitar,delay_in_ms, g, fs);
% Normalise
%[guitardelay, fs] = audioread('matlab2.wav');
%guitardelay = guitardelay./max(abs((guitardelay)));
%effect1 = guitardelay;
filename = 'guitar';

else 
error('Your selection is invalid');    
end

% first variable for Flange
if strcmp('Flange',effect_select); 
    display = '\n Select the first Flange variable between 1 and 8 \n\n >';
% assign the selected input to variable v
    v = input(display);
    v = v./1000; %convert to seconds)
% Select delay-line width (milliseconds) value, use the range 5-15 ms
    display = '\n select the rate in seconds (0.4-0.8 recommended) \n\n >';
    r = input(display);


elseif strcmp('Vibrato',effect_select);
    display = '\n Input Modulation Frequency between 1 and 5Hz \n\n >';
    Modfreq = input(display);
    
    display = '\n Input Width in milliseconds value, range 1-10 \n\n >';
    Width = input(display);
    Width = Width./1000; 
  
elseif strcmp('Delay',effect_select);
    display = '\n Delay in Milliseconds (500 recommended, sounds awesome) \n\n >';
    delay_in_ms = input(display);
    
    display = '\n Gain (feedback, <1) \n\n >';
    g = input(display);
end


if strcmp('Flange',effect_select);
    out_flange = flang(fs,v,guitar,r);
    out_flange = out_flange./max(abs(out_flange));
    sound(out_flange,fs);
    
audiowrite(strcat(filename,'_flange.wav'),out_flange,fs);
    

elseif strcmp('Vibrato',effect_select);
    out_vib = vibrato(guitar,fs,Modfreq,Width);
    out_vib = out_vib./max(abs(out_vib));
    sound(out_vib,fs);
audiowrite(strcat(filename,'_vibrato.wav'),out_vib,fs);
    
elseif strcmp('Delay',effect_select);
    out_delay = iircomb(guitar,delay_in_ms, g, fs);
    out_delay = out_delay./max(abs(out_delay));
    sound(out_delay,fs);
audiowrite(strcat(filename,'_delay.wav'),out_delay,fs);
end



%% Combining Delay Based Effects

%
% Run Selection, type 1, 2 or 3 based of desired combination of effects. 
% Follow the prompts and input desired variables, the user may go outside
% of these parimeters if wished.
%

clear all

[guitar, fs] = audioread('matlab2.wav');
% normalise the signal
guitar = guitar./max(abs((guitar)));

display = '\n Flange and Vibrato (select 1), Vibrato and Delay (select 2), Flange and Delay (select 3) \n\n >';
% assign the selected string input to variable effect_select
effect_select = input(display,'s');


% FLANGE AND VIBRATO
if strcmp('1',effect_select)
%flan = flange(fs,v,guitar,r);
%Normalise
%[FlanVib, fs] = audioread('matlab2.wav');
%FlanVib = FlanVib./max(abs((FlanVib)));
%effect1 = FlanVib;
filename = 'guitar';


% VIBRATO AND DELAY
elseif strcmp('2',effect_select)
%Vib = vibrato(guitar,Modfreq,Width,fs);
% Normalise
%[VibDel, fs] = audioread('matlab2.wav');
%VibDel = VibDel./max(abs((VibDel)));
%effect1 = VibDel;
filename = 'guitar';

% FLANGE AND DELAY
elseif strcmp('3',effect_select)
%Delay = iircomb(guitar,delay_in_ms, g, fs);
% Normalise
%[FlaDel, fs] = audioread('matlab2.wav');
%FlaDel = FlaDel./max(abs((FlaDel)));
%effect1 = FlaDel;
filename = 'guitar';

else 
error('Your selection is invalid');    
end

% first variable for Flange and Vibrato (1)
if strcmp('1',effect_select); 
    display = '\n Select the first Flange variable between 1 and 8 \n\n >';
    v = input(display);
    v = v./1000; %convert to seconds
    display = '\n select the rate in seconds (0.4-0.8 recommended) \n\n >';
    r = input(display);    
    %Vibrato: 
    display = '\n Input Modulation Frequency between 1 and 5Hz \n\n >';
    Modfreq = input(display);    
    display = '\n Input Width in milliseconds value, range 0.1-10 \n\n >';
    Width = input(display);
    Width = Width./1000; 
    

% Variables for Vibrato and Delay (2)
elseif strcmp('2',effect_select);
    display = '\n Input Modulation Frequency between 1 and 5Hz \n\n >';
    Modfreq = input(display);    
    display = '\n Input Width in milliseconds value, range 0.1-10 \n\n >';
    Width = input(display);
    Width = Width./1000; 
    % Delay:
    display = '\n Delay in Milliseconds (200-650 recommended, 500 sounds awesome) \n\n >';
    delay_in_ms = input(display);    
    display = '\n Gain (feedback, <1) \n\n >';
    g = input(display);
    
  
% Variables for Flange and Delay (3)
elseif strcmp('3',effect_select);
    % Flange:
    display = '\n Select the first Flange variable between 1 and 8 \n\n >';
    v = input(display);
    v = v./1000;
    display = '\n select the rate in seconds (0.4-0.8 recommended) \n\n >';
    r = input(display);
    %Delay:
    display = '\n Delay in Milliseconds (200-650 recommended, 500 sounds awesome) \n\n >';
    delay_in_ms = input(display);    
    display = '\n Gain (feedback, <1) \n\n >';
    g = input(display);
end


if strcmp('1',effect_select);
    % ga=0.01; attempted gain reduction, does not work
    out_flange = flang(fs,v,guitar,r);
    out_flange = out_flange./max(abs(out_flange));    
    out_flanvib = vibrato(out_flange,fs,Modfreq,Width);
    out_flanvib = out_flanvib./max(abs(out_flanvib));
    sound(out_flanvib,fs); % play out_vib, the combined signal of the two effects
audiowrite(strcat(filename,'_flange_vibrato.wav'),out_flanvib,fs);
   
    % out_vib is combined with the out_flange variable, so out_vib is the
    % vibrato signal with the already made flange signal
  
    

elseif strcmp('2',effect_select);
    out_vib = vibrato(guitar,fs,Modfreq,Width);
    out_vib = out_vib./max(abs(out_vib));
    out_vibdelay = iircomb(out_vib,delay_in_ms, g, fs);
    out_vibdelay = out_vibdelay./max(abs(out_vibdelay));
    sound(out_vibdelay,fs);
audiowrite(strcat(filename,'_vibrato_delay.wav'),out_vibdelay,fs);    
    % This partitular one takes some time to generate, up to 20-30 seconds
    % of computer time. As selection 1, the out_delay has replaced the
    % guitar variable (the clean unfiltered signal) with the modulated
    % vibrato out_vib (the modulated, filtered signal). The delay is then
    % added to that signal. Similarily, it could be the other way around.
    % Delay then vibrato, crazy.
    
    
elseif strcmp('3',effect_select);
    out_flange = flang(fs,v,guitar,r);
    out_flange = out_flange./max(abs(out_flange));
    out_flandelay = iircomb(out_flange,delay_in_ms, g, fs);
    out_flandelay = out_flandelay./max(abs(out_flandelay));
    sound(out_flandelay,fs);
audiowrite(strcat(filename,'_flange_delay.wav'),out_flandelay,fs);    
    % Again, this can take some time to compute, expect half a minute of
    % matlab 'Busy' before the variables have completed. 
end

