function [L_left, L_right] = grain_lengths(Density_L,Density_R,grain_lengthL,grain_lengthR,grain_length_variation,fs,Ly)

%Function determines grain lengths for left and right channels
%User inputs length in milliseconds
%User input variations of 1 = none to 4 = max

%Convert grain length times from milliseconds to samples
grain_lengthL = round(fs*(grain_lengthL/1000));  
grain_lengthR = round(fs*(grain_lengthR/1000)); 


if grain_length_variation == 1,
    L_left = round(grain_lengthL)*ones(1,Density_L); % Grain length in samples
    L_right = round(grain_lengthR)*ones(1,Density_R); % Grain length in samples

elseif grain_length_variation == 2,
    L_left = round((grain_lengthL)*(0.9+(1.1-0.9).*rand(1,Density_L))); % Grain length in samples
    L_right = round((grain_lengthR)*(0.9+(1.1-0.9).*rand(1,Density_R))); % Grain length in samples
elseif grain_length_variation == 3,
    L_left = round((grain_lengthL)*(0.8+(1.2-0.8).*rand(1,Density_L))); % Grain length in samples
    L_right = round((grain_lengthR)*(0.8+(1.2-0.8).*rand(1,Density_R))); % Grain length in samples      
elseif grain_length_variation == 4,
    L_left = round((grain_lengthL)*(0.7+(1.3-0.7).*rand(1,Density_L))); % Grain length in samples
    L_right = round((grain_lengthR)*(0.7+(1.3-0.7).*rand(1,Density_R))); % Grain length in samples   
end

if L_left >= Ly,
    L_left = Ly-1;
end
if L_right >= Ly,
    L_right = Ly-1;
end
