% Harmonizer Script

[direct_signal,fs] = audioread('tenor_sax2_dry.wav'); 


N = 4096;

w =blackmanharris(N+1);

t = -150;

nH = 150;

minf0 = 50;

maxf0 = 1000;

f0et = 5;

maxhd = .2;

stocf = 1;

timemapping = [0 length(direct_signal)/fs;0 length(direct_signal)/fs];

option = 3;  %1. G Mixolydian Harmonization (upper)
%             2. G Mixolydian Harmonization (lower)
%             3. G Pentatonic Minor
%             4. Blues Hybrid Scale

[y,yh,ys] = hpsmodelparams_Harmonizer(direct_signal,fs,w,N,t,nH,minf0,maxf0,f0et,...
                                    maxhd,stocf,timemapping,option);
       


%%%%%%%%%%%%%%%% Reverb Script %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


x = y; % The harmony line is put through the reverb

%     tn = the number of taps of the tapped delay line
%     Number of early reflections e.g. 4
tn = 4;
%
%     tg = a vector which contains the gain of each tap of the tapped delay line
%     Early reflection coefficients (linear not dB) e.g. [0.7, 0.5, 0.8, 0.6] for -3.1dB,-6dB -1.9dB & -4.4dB  
tg = [0.6, 0.5, 0.8, 0.9];
%
%     td = a vector which contains the delay length of each tap of the tapped delay line
%     Early reflection delays (in samples not time) [59, 100, 195, 225] for 3.7, 6.3, 12.2 14.1 ms reflections @ FS=16k

delayinms = [24,33,44,49];

td = round(fs.*(delayinms/1000));

%
%     cg = cg2/(1-cg1), where g2 is the feedback gain of the comb filter a vector of length 4 which contains the gain of each of the comb filters (should be less than 1 for stability)
%     Comb filter (recursive echo) coefficients (linear not dB) e.g. [0.7, 0.6, 0.8, 0.7] for -3.1dB,-4.4dB -1.9dB & -3.1dB    
cg = [0.5, 0.4, 0.7, 0.5];

%     cg1 = the feedback gain of the low pass filter (this should be less than 1 for stability) 

cg1 = [0.7, 0.5, 0.9, 0.8];

%     cd = a vector of length 4 which contains the delay of each of the comb filters
% Comb filter (recursive echo) delays (in samples not time) e.g. [400, 531, 640, 734] for 25 33.2 40 45.9 ms reflections @ FS=16k


cd = [508, 1307, 999, 707];
%     ag = the gain of the allpass filters (this should be less than 1 for stability)
%     All Pass filter (more recursive echoes) coefficient (linear not dB) e.g. 0.7 for -3.1 dB reflections
ag = 0.5;
%
%     ad = a vector of length 2 which contains the delay of each of the allpass filters
%     All Pass filter (more recursive echoes) delays (in samples not time) e.g. [520, 495] for 32.5 & 30.9 ms reflections
delayinms = [33,44, 77];

ad = round(fs.*(delayinms/1000));



z = 1.7; %z = the gain factor of the direct signal added to the cascaded allpass filters
%     Direct signal level (linear not dB) e.g. 0.5 for -6dB

oy = schroeder_fft_lp_stereo_max(x,tn,tg,td,cg,cg1,cd,ag,ad,direct_signal,z);

%%%%%%% Write the Audio %%%%%%%%%%%%%%%

audiowrite('Tenor_Sax2.wav',oy, fs)