%%GRANULATION - InputParameters_FunctionCall
%Use this script to set all the input parameters for the granulation
%effect. It is recommended you run each section individually. The effect is
%often quick to evaluate, however, if you are using very high numbers of
%grains (in the order or millions) be prepared to wait a little longer.

[x,fs] = audioread ('gtr2_original');%loads the audio signal to which the effect will be applied.
                                         %Type the filename inside the parantheses eg [x,fs]=audioread('example.wav');
                                      
channelsIN = 2;%Is the input file mono or stereo?
                                                 %1; = mono 
                                                 %2; = stereo
                                                 
Read_Type = 1;%There are 3 options available for how grains are selected from the input file:
                                    %1; = Sequential through the input file
                                    %2; = Backwards through the input file
                                    %3; = Selected randomly from input file
                                   
Lout = 50;%Lout sets the length of the graulated output file. 
                 %You have the option of entering a value in seconds, or if you
                 %would like the effected audio to be the same length as 
                 %the input file, type: length(x);

W = 0.4;%Fade In/Out Length as Percentage of grain where 1 = 100%. 
        %Default window type is tapered cosine (tukey) window, however this
        %can be altered quite easily by editing the grain.m function.
        %Window type will affect the spectral content of output signal.

%Stereo Processing - if you want a mono output, comment out lines 30,32,34,49 and 53 then evaluate(use sound and write YR instead of Y, or edit the variable name)
GnL = 35000;%Number of grains in left channel

GDminL = 1;%Minimum grain duration for left channel in milliseconds. Ensure GDmin is shorter than GDmax

GDmaxL = 100;%Maximum grain duration for left channel in milliseconds. Ensure that GDmax is shorter than input signal(x).

GnR = 40000;%Number of grains in right channel

GDminR = 1;%Minimum grain duration for right channel in milliseconds. Ensure GDmin is shorter than GDmax

GDmaxR = 200;%Maximum grain duration for right channel in milliseconds. Ensure that GDmax is shorter than input signal(x).

ENV = 4;%Amplitude envelope over the length of the output signal. There are 4 options here:
                                    %1; = random amplitude coefficient for each grain
                                    %2; = gaussian (bell shape) envelope over length of output signal
                                    %3; = crescendo (amplitude increasing)over length of output signal
                                    %4; = diminuendo (amplitude decreasing)over length of output signal
 
%% Function call, do not edit these code lines, rather, set the input parameters above then evaluate. This calls the granules.m function.
YL = granules (x,fs,GnL,Lout,GDmaxL,GDminL,channelsIN,Read_Type,W,ENV);%Function call for left channel

YR = granules (x,fs,GnR,Lout,GDmaxR,GDminR,channelsIN,Read_Type,W,ENV);%Function call for right channel

Y = [YL, YR];%Concatenate YL and YR (make stereo)
%% Play the resulting audio signal
sound(Y,fs);
%% If you are happy with the result, save it as a wav file. Be sure to type filename.
wavwrite(Y,fs,'filename.wav');









