function     RES = d3audithp(recdir,prefix,tcue,RES,NS,CH,recoveries)
%
%     R = d3audit(recdir,prefix,tcue,R)
%     Audit tool for dtag 3.
%     tag is the tag deployment string e.g., 'sw03_207a'
%     tcue is the time in seconds-since-tag-on to start displaying from
%     R is an optional audit structure to edit or augment
%     Output:
%        R is the audit structure made in the session. Use saveaudit
%        to save this to a file.
%
%     OPERATION
%     Type or click on the display for the following functions:
%     - type 'f' to go to the next block
%     - type 'b' to go to the previous block
%     - click on the graph to get the time cue, depth, time-to-last
%       and frequency of an event. Time-to-last is the elapsed time
%       between the current click point and the point last clicked.
%       Results display in the matlab command window.
%     - type 's' to select the current segment and add it to the audit.
%       You will be prompted to enter a sound type on the matlab command
%       window. Enter a single word and type return when complete.
%     - type 'l' to select the currect cursor position and add it to the
%       audit as a 0-length event. You will be prompted to enter a sound
%       type on the matlab command window. Enter a single word and type
%       return when complete.
%     - type 'x' to delete the audit entry at the cursor position.
%       If there is no audit entry at the cursor, nothing happens.
%       If there is more than one audit entry overlapping the cursor, one
%       will be deleted (the first one encountered in the audit structure).
%     - type 'p' to play the displayed sound segment
%       through the computer speaker/headphone jack.
%     - type 'q' or press the right hand mouse button to finish auditing.
%     - type 'a' to report the angle of arrival of the selected segment
%
%     mark johnson, WHOI
%     majohnson@whoi.edu
%     last modified March 2005
%     added buttons and updated audit structure

% NS = 5 ;          % number of seconds to display
BL = 512 ;         % specgram (fft) block size
CLIM = [-90 0] ;   % color axis limits in dB for specgram
% CH = 1 ;           % which channel to display if multichannel audio
THRESH = 0 ;       % click detector threshold, 0 to disable
volume = 20 ;      % amplification factor for audio output - often needed to
% hear weak signals (if volume>1, loud transients will
% be clipped when playing the sound cut
SOUND_FH = 0 ;     % high-pass filter for sound playback - 0 for no filter
SOUND_FL = 0 ;     % low-pass filter for sound playback - 0 for no filter
SOUND_DF = 1 ;     % decimation factor for playing sound
AOA_FH = 2e3 ;     % high-pass filter for angle-of-arrival measurement
AOA_SCF = 1500/0.025 ;     % v/h

MAXYONCLICKDISPLAY = 0.01 ;

% high-pass filter frequencies (Hz) for click detector
switch prefix(1:2),
    case 'zc',      % for ziphius use:
        FH = 20000 ;
        TC = 0.5e-3 ;           % power averaging time constant in seconds
    case 'md',      % for mesoplodon use:
        FH = 20000 ;
        TC = 0.5e-3 ;           % power averaging time constant in seconds
    case 'pw',      % for pilot whale use:
        FH = 10000 ;
        TC = 0.5e-3 ;           % power averaging time constant in seconds
    case 'sw',      % for sperm whale use:
        FH = 3000 ;
        TC = 2.5e-3 ;           % power averaging time constant in seconds
    case 'hp'
        FH = 100000 ;
        TC = 0.5e-3 ;
    otherwise,      % for others use:
        FH = 5000 ;
        TC = 0.5e-3 ;           % power averaging time constant in seconds
end

if nargin<5 | isempty(NS)
    NS = 5 ;          % number of seconds to display
end

if nargin<4 | isempty(RES),
    RES.cue = [] ;
    RES.comment = [] ;
end

k = loadprh(prefix,0,'p','fs') ;           % read p and fs from the sensor file
if k==0,
    fprintf('Unable to find a PRH file - continuing without\n') ;
    p = [] ; fs = [] ;
end

% check sampling rate
[x,afs] = d3wavread(tcue+[0 0.01],recdir,prefix) ;
if SOUND_FH > 0,
    [bs as] = butter(6,SOUND_FH/(afs/2),'high') ;
elseif SOUND_FL > 0,
    [bs as] = butter(6,SOUND_FL/(afs/2)) ;
else
    bs = [] ;
end

% high pass filter for envelope
[bh ah] = cheby1(6,0.5,FH/afs*2,'high') ;
% envelope smoothing filter
pp = 1/TC/afs ;

% angle-of-arrival filter
[baoa aaoa] = butter(4,AOA_FH/(afs/2),'high') ;

current = [0 0] ;
figure(100),clf
figure(1),clf
if ~isempty(p),
    kb = 1:floor(NS*fs) ;
    AXm = axes('position',[0.11,0.76,0.78,0.18]) ;
    AXc = axes('position',[0.11,0.70,0.78,0.05]) ;
    AXs = axes('position',[0.11,0.34,0.78,0.35]) ;
    AXp = axes('position',[0.11,0.11,0.78,0.2]) ;
else
    AXm = axes('position',[0.11,0.60,0.78,0.34]) ;
    AXc = axes('position',[0.11,0.52,0.78,0.07]) ;
    AXs = axes('position',[0.11,0.11,0.78,0.38]) ;
end

bc = get(gcf,'Color') ;
set(AXc,'XLim',[0 1],'YLim',[0 1]) ;
set(AXc,'Box','off','XTick',[],'YTick',[],'XColor',bc,'YColor',bc,'Color',bc) ;
cleanh = [] ;

while 1,
    [x,afs] = d3wavread(tcue+[0 NS],recdir,prefix) ;
    if isempty(x), return, end
    x = x-repmat(mean(x),size(x,1),1) ;
    [B F T] = specgram(x(:,CH),BL,afs,hamming(BL),BL/2) ;
    xx = filter(pp,[1 -(1-pp)],abs(filter(bh,ah,x(:,CH)))) ;
    
    kk = 1:5:length(xx) ;
    axes(AXm), 
    plot(tcue+kk/afs,xx(kk),'k') ; grid
    set(AXm,'XAxisLocation','top') ;
    yl = get(gca,'YLim') ;
    yl(2) = min([yl(2) MAXYONCLICKDISPLAY]) ;
    axis([tcue tcue+NS yl]) ;
    
    plotRES(AXc,RES,[tcue tcue+NS]) ;
    
    if ~isempty(p),
        ks = kb + round(tcue*fs) ;
        axes(AXp),plot(ks/fs,p(ks)), grid
        set(gca,'YDir','reverse') ;
        axis([tcue tcue+max(T) get(gca,'YLim')]) ;
        xlabel('Time, s')
        ylabel('Depth, m')
    end
    
    BB = adjust2Axis(20*log10(abs(B))) ;
    axes(AXs), imagesc(tcue+T,F/1000,BB,CLIM) ;
    axis xy, grid ;
    if ~isempty(p),
        set(AXs,'XTickLabel',[]) ;
    else
        xlabel('Time, s')
    end
    ylabel('Frequency, kHz')
    hold on
    hhh = plot([0 0],0.8*afs/2000*[1 1],'k*-') ;    % plot cursor
    hold off
    
    done = 0 ;
    while done == 0,
        axes(AXs) ; pause(0) ;
        [gx gy button] = ginput(1) ;
        if button>='A',
            button = lower(setstr(button)) ;
        end
        if button==3 | button=='q',
            %             save d3audit_RECOVER RES
            save(fullfile(recoveries,'d3audit_RECOVER.mat'), 'RES', '-v6')
            return
            
        elseif button=='s',
            ss = input(' Enter comment... ','s') ;
            cc = sort(current) ;
            RES.cue = [RES.cue;[cc(1) diff(cc)]] ;
            RES.stype{size(RES.cue,1)} = ss ;
            %             save d3audit_RECOVER RES
            save(fullfile(recoveries,'d3audit_RECOVER.mat'), 'RES', '-v6')
            plotRES(AXc,RES,[tcue tcue+NS]) ;
            
        elseif button=='r',
            chk = min(size(x,2),2) ;
            %             ss = input(' Enter comment... ','s') ;
            % disp('dupa')
            cc = sort(current) ;
            %          RES.cue = [RES.cue;[cc(1) diff(cc)]] ;
            %          RES.stype{size(RES.cue,1)} = ss ;
            %          save d3audit_RECOVER RES
            [B2, F2, T2] = specgram(x(:,CH),BL*4,afs,hamming(BL*4),BL*4/2) ;
            BB2 = adjust2Axis(20*log10(abs(B2))) ;
            figure(100),clf
            imagesc(tcue+T2,F2/1000,BB2,CLIM) ;
            axis xy, grid ;
            axis([cc(1) cc(2) 0 5])
            wavplay(volume*x((cc(1)-tcue)*afs:(cc(2)-tcue)*afs,1:chk),afs/SOUND_DF) ;
            figure(1),
            plotRES(AXc,RES,[tcue tcue+NS]) ;
            
            
        elseif button=='a',
            if size(x,2)>1,
                cc = sort(current)-tcue ;
                kcc = round(afs*cc(1)):round(afs*cc(2)) ;
                xf = filter(baoa,aaoa,x(kcc,:)) ;
                [aa,qq] = xc_tdoa(xf(:,1),xf(:,2)) ;
                fprintf(' Angle of arrival %3.1f, quality %1.2f\n',asin(aa*AOA_SCF/afs)*180/pi,qq) ;
            end
            
        elseif button=='l',
            ss = input(' Enter comment... ','s') ;
            RES.cue = [RES.cue;[gx 0]] ;
            RES.stype{size(RES.cue,1)} = ss ;
            %             save d3audit_RECOVER RES
            save(fullfile(recoveries,'d3audit_RECOVER.mat'), 'RES', '-v6')
            plotRES(AXc,RES,[tcue tcue+NS]) ;
            
        elseif button=='e',
            % Find all cues around synch cursor
            kres =(find(gx>=RES.cue(:,1)-0.1 & gx<sum(RES.cue')'+0.1)) ;
            if ~isempty(kres),
                % Find the type of cues and allow users to edit them
                ktype = RES.stype(kres) ;
                ktype=inputdlg(ktype,'Edit/remove cues',[1,40],ktype);
                % If cancel button is pressed, ktype will be empty cell
                if ~isempty(ktype)
                    % Otherwise, go through cell array of new sound types
                    for i=1:length(ktype)
                        RES.stype(kres(i))=ktype(i);
                        kempty(i)=isempty(ktype{i});
                    end
                    
                    % If there are empty sound types, remove them
                    kempty=kres(find(kempty));
                    kkeep = setxor(1:size(RES.cue,1),kempty) ;
                    RES.cue = RES.cue(kkeep,:) ;
                    RES.stype = {RES.stype{kkeep}} ;
                end
                plotRES(AXc,RES,[tcue tcue+NS]) ;
                clear kempty kres
                
            else
                fprintf(' No saved cue at cursor\n') ;
            end
            
        elseif button=='x',
            kres = min(find(gx>=RES.cue(:,1)-0.1 & gx<sum(RES.cue')'+0.1)) ;
            if ~isempty(kres),
                kkeep = setxor(1:size(RES.cue,1),kres) ;
                RES.cue = RES.cue(kkeep,:) ;
                RES.stype = {RES.stype{kkeep}} ;
                plotRES(AXc,RES,[tcue tcue+NS]) ;
            else
                fprintf(' No saved cue at cursor\n') ;
            end
            
        elseif button=='f',
            tcue = tcue+floor(NS)-0.5 ;
            done = 1 ;
            
        elseif button=='b',
            tcue = max([0 tcue-NS+0.5]) ;
            done = 1 ;
            
        elseif button=='p',
            chk = min(size(x,2),2) ;
            %chk=2;
            if ~isempty(bs),
                xf = filter(bs,as,x(:,1:chk)) ;
                %                sound(volume*xf,afs/SOUND_DF,16) ;
                wavplay(volume*xf,afs/SOUND_DF) ;
            else
                %                sound(volume*x(:,1:chk),afs/SOUND_DF,16) ;
                wavplay(volume*x(:,1:chk),afs/SOUND_DF) ;
            end
            
        elseif button==1,
            if gy<0 | gx<tcue | gx>tcue+NS
                fprintf('Invalid click: commands are f b s l p x q\n')
                
            else
                current = [current(2) gx] ;
                set(hhh,'XData',current) ;
                if ~isempty(p),
                    fprintf(' -> %6.1f\t\tdiff to last = %6.1f\t\tp = %6.1f\t\tfreq. = %4.2f kHz\n', ...
                        gx,diff(current),p(round(gx*fs)),gy) ;
                else
                    fprintf(' -> %6.1f\t\tdiff to last = %6.1f\t\tfreq. = %4.2f kHz\n', ...
                        gx,diff(current),gy) ;
                end
            end
        end
    end
end


function plotRES(AXc,RES,XLIMS) ;

axes(AXc)
if ~isempty(RES.cue),
    kk = find(sum(RES.cue')'>XLIMS(1) & RES.cue(:,1)<=XLIMS(2)) ;
    if ~isempty(kk),
        plot([RES.cue(kk,1) sum(RES.cue(kk,:)')']',0.2*ones(2,length(kk)),'k*-') ;
        for k=kk',
            text(max([XLIMS(1) RES.cue(k,1)+0.1]),0.6,RES.stype{k},'FontSize',10) ;
        end
    else
        plot(0,0,'k*-') ;
    end
else
    plot(0,0,'k*-') ;
end

set(AXc,'XLim',XLIMS,'YLim',[0 1]) ;
bc = get(gcf,'Color') ;
set(AXc,'Box','off','XTick',[],'YTick',[],'XColor',bc,'YColor',bc,'Color',bc) ;
return

