% Pernille Tønnesen
% May 18 2018

clear
close all

addpath(genpath('G:\Azores 2017\sw17'))                     % Adds path with dtag recordings

load('C:\MATLAB_pt\SW_AcousticScene\Clickdetection\Data\AZ_dive_times.mat')   % Struct consisting of continuous click start times in samples

recdir = {'G:\Azores 2017\sw17\sw17_196a','G:\Azores 2017\sw17\sw17_204a','G:\Azores 2017\sw17\sw17_205a'};
prefix = {'sw17_196a','sw17_204a','sw17_205a'};
fi = {'sw17_196a015','sw17_204a001','sw17_205a018'};

% get audio sampling rate
for jj= 1:length(fi)
    [sig,afs] = audioread([recdir{jj} '\' fi{jj} '.wav'],[100 200]);
    fs(jj)=afs;
    clear afs sig
end

% Click detection
% Remember to use new getxmldevid
jtag = 1;
jdive = 1;                  % CHANGE !!!!!
CL = d3findallclicks(recdir{jtag},prefix{jtag},[data(jtag).start_sample_cont(jdive)/fs(jtag) data(jtag).end_sample_cont(jdive)/fs(jtag)]);

% Save
pas = ['C:\MATLAB_pt\SW_AcousticScene\Clickdetection\Data\sw' prefix{jtag}(6:9) '\'];
save([pas 'CLsw17_' prefix{jtag}(end-3:end) '_dive' num2str(jdive)],'CL')

if jdive == 1
    whale = struct();
else
    load([pas recdir{jtag}(end-8:end) '_click'])
end

whale(jdive).dive_no = jdive;
whale(jdive).CLraw = CL;

% Check everything looks okay
CL = d3findmissedclicks_advanced(recdir{jtag},prefix{jtag},CL,[3e3,20e3],[0.4,0.5],data(jtag).start_sample_cont(jdive)/fs(jtag));
save([pas 'CLsw17_' prefix{jtag}(end-3:end) '_dive' num2str(jdive)],'CL')

whale(jdive).CLclean = CL;
save([pas recdir{jtag}(end-8:end) '_click'],'whale')

% Find buzzes
[buzz_start buzz_end] = find_buzz_fun(CL);
whale(jdive).buzz_start_raw = buzz_start;
whale(jdive).buzz_end_raw = buzz_end;

% Clean buzzes
for j=1:length(whale(jdive).buzz_start_raw)
    CL=d3findmissedclicks_buzz(recdir{jtag},prefix{jtag},CL,[3e3,20e3],[0.4,0.5],whale(jdive).buzz_start_raw(j));
end
whale(jdive).CLbuzzclean = CL;
save([pas 'CLsw17_' prefix{jtag}(end-3:end) '_dive' num2str(jdive)],'CL')
save([pas recdir{jtag}(end-8:end) '_click'],'whale')

% Find clean buzzes
[buzz_start buzz_end] = find_buzz_fun(CL);
whale(jdive).buzz_start_clean = buzz_start;
whale(jdive).buzz_end_clean = buzz_end;

% Get time for each click within a buzz end reg clicks
for jdive = 1:size(whale,2)
    whale(jdive).CLregclick=[];
    for j=1:length(whale(jdive).buzz_start_clean)
        [val_s(j),idx_s(j)]=min(abs(whale(jdive).CLbuzzclean - whale(jdive).buzz_start_clean(j)));
        [val_e(j),idx_e(j)]=min(abs(whale(jdive).CLbuzzclean - whale(jdive).buzz_end_clean(j)));
        whale(jdive).CLbuzz{j}=whale(jdive).CLbuzzclean(idx_s(j):idx_e(j));
        if j == 1
            regclick=whale(jdive).CLbuzzclean(1:idx_s(j));
        else
            regclick=whale(jdive).CLbuzzclean(idx_e(j-1):idx_s(j));
        end
        whale(jdive).CLregclick = [whale(jdive).CLregclick;regclick];
        clear regclick
    end
    regclick=whale(jdive).CLbuzzclean(idx_e(j):end);
    whale(jdive).CLregclick = [whale(jdive).CLregclick;regclick];
end


%check that is looks allright
figure
plot(whale(jdive).CLbuzzclean)
hold on
for j=1:size(whale(jdive).CLbuzz,2)
    plot(1:length(whale(jdive).CLbuzz{j}),whale(jdive).CLbuzz{j})
end
grid on

% Add buzz details
for jdive = 1:size(whale,2)
    whale(jdive).buzz_no = length(whale(jdive).buzz_start_clean);
    for j = 1:length(whale(jdive).CLbuzz)
        whale(jdive).buzz_dur_sec(j) = whale(jdive).CLbuzz{j}(end)-whale(jdive).CLbuzz{j}(1);
        whale(jdive).buzz_click_no(j) = length(whale(jdive).CLbuzz{j});
    end
end

save([pas recdir{jtag}(end-8:end) '_click'],'whale')


