function spread_out = spreader_mod( signal , delayL , delayR , pitchL , pitchR , mixer , fs )

% Spreads a mono audio signal through the stereo image by applying different delays to the left and right channels, as well as pitch shifting.

%   spread_out = spreader_mod( signal , delayL , delayR , pitchL , pitchR , mixer , fs )

% Inputs:

%   in_signal   = mono input vector to be "spread" ***has to be a mono signal
%   delayL = delay time in miliseconds for the left side
%   delayR = delay time in miliseconds for the right side
%   pitchL = pitch shift in cents for left side
%   pitchR = pitch shift in cents for right side
%   mixer  = fraction of dry sound in final mix from 0 to 1
%   fs     = sample rate in Hz

% Output:
%   spreader_out = stereo matrix (2 columns)


%   Example: proc_vox = spreader_mod( vox , 15 , 17 , -3 , +3 , 0.1 , fs );
%   
% Function by: Arturo Fdz

  signal = signal' ; % audio signal has to be transposed to one row as the delayed signal is being treated in rows
  
  cent = 2^(1/1200) ;            % ratio of one cent
  rL = cent^pitchL ;             % adjusts the pitch of the left channel signal acording to its ratio
  rR = cent^pitchR ;             % adjusts the pitch of the right channel signal acording to its ratio
  
  nodelayL = [ signal zeros( 1 , delayL/1000*fs ) ] ; % non delayed signal left
  delayedL = [ zeros( 1 , delayL/1000*fs ) signal ] ; % left delayed signal
  
  nodelayR = [ signal zeros( 1 , delayR/1000*fs ) ] ; % non delayed signal right
  delayedR = [ zeros( 1 , delayR/1000*fs ) signal ] ; % right delayed signal
  
  pL = pitch_shift(delayedL', rL);        % calls pitch_shift function on the left channel
  pR = pitch_shift(delayedR', rR);        % calls pitch_shift function on the right channel
  
  sL = mixer*nodelayL+pL(1:length(nodelayL));  % mixes the dry signal with the delayed signal
  sL=sL/max(abs(sL));            % normalize
  
  sR = mixer*nodelayR+pR(1:length(nodelayR));  % mixes the dry signal with the delayed signal
  sR=sR/max(abs(sR));            % normalize
    
  sL = sL' ;    % transposes rows to columns 
  sR = sR' ;    % transposes rows to columns
  
  lensL = length(sL) ;  % measures the length of sL in samples
  lensR = length(sR) ;  % measures the length of sR in samples
  if lensL == lensR     % if lensL is the same length as lensR then...
    sL = sL ;           % ...sL will stay the same and...
    sR = sR ;           % ...sR will stay the same
  elseif lensL < lensR        % if lensL is shorter than lensR then...
    diff = lensR - lensL ;    % difference between the longest and the shortest one in samples
    sL = sL ;                 % sL stays the same length
    sR = sR(1:lensR-diff) ;   % sR gets shortened by the difference therefore matching in length with sL
  else                        % last if statement, if lensL is longer than lensR then...
    diff = lensL - lensR ;    % difference between the longest and the shortest one in samples
    sL = sL(1:lensL-diff) ;   % sL gets shortened by the difference therefore matching in length with sR
    sR = sR ;                 % sR stays the same length
  end
  
  spread_out=[ sL' ;sR' ]' ;           % generates stereo processed signal
 
end