% Kevin Ziemer
% Flange Efffect Algorithm
% EECS 452

% This code was written with the goal of implimentation on a Motorola 56303 DSP.
% The code was origionally written with 5x oversampling using linear 
% interpolation.  It was later found that linear interpolation was not needed, 
% as this code (without oversampling) sounded sufficiently smooth.  The values
% in the 'Variables' section can be changed to modify the effect, however if 
% they are drastically changed, the code may need to be revised (the maximum
% value for delay size is > 2000 (you would have to change the parameters of
% the for loop)).

%*******************************************************************************
% Note: This code was written for a mono-wave file.  Also, the typical variable
%       values are for a sample frequency of 44.1 kHz (the 'sine_size' and 
%       'delay_size' would need to be adjusted accordingly).
%*******************************************************************************

% Variables
%===============================================================================
inp = 'audio2.wav';     % The name of the audio file to be processed

mix = 50;               % Variable for mixing processed signal with origional.
                        % 0 = all origional, 100 = all processed, 50 = 50/50
                   
sine_int = 20;          % Modulates how fast flange is.  
                        % (lower number -> faster) 
                        % 20 works well.  This will be the number of points
                        % interpolated between the varying delay look-up values.
                        
delay_size = 400;       % Modulates the maximum variance of delay.  
                        % (higher number -> more effect)
                        % 200 - 600 should work well. 
                        % 2000 gives a scratch effect.
                        % The audio buffer will need to be set up with this 
                        % number of available positions.
%===============================================================================

sine_size = 256;        % Size of sine look-up table (for varying delay).  
[x,fs]=wavread(inp);       
xlen=length(x);
delay_half = delay_size/2;
delay1 = linspace(-pi, pi, sine_size);     
delay = delay_half .* sin(delay1);                  
out2=zeros(xlen,1);
index = 1;              % index will be a pointer to a value in the sine look-up table
between_sine = 1;       % between_sine is the interpolated position for the sine values
for t=1001:(xlen-1001);
    ind2 = index + 1;       % ind2 is index of the following value in sine table
    if ind2 > length(delay)
        ind2 = 1;
    end
    dval = round((1 - (between_sine/sine_int)) * delay(index) + ((between_sine/sine_int) * (delay(ind2))));
    out2(t) = x(t - dval);
    between_sine = between_sine + 1;
    if (between_sine > sine_int)        % this big if-statement accounts for circular buffers
        between_sine = 1;
        index = index + 1;
        if index > length(delay)
            index = 1;
        end
    end
end
mix = mix/100;
out2 = (mix * out2) + ((1 - mix) * x);                
sound(out2,fs)
