% [total_solo, total_solo_correct,total_non_solo,...
%        total_non_solo_correct, figNum,time_block] = ...
%    process_wav(figNum,num,is_training,do_output)
%
%  Runs the classifier on a single .wav file.
%
%  Inputs:
%    figNum - first figure number for plots
%    num - sample number.  e.g. 2 for 2.wav and 2.txt (label file)
%    is_training - set to true if this is part of the training set
%      and false otherwise.
%    do_output - set to true if you want the plots and text output
%
%  Outputs:
%    total_solo - the total number of time slices that are truly
%      solo
%    total_solo_correct - the number of time slices that the
%      classifier got correct
%    figNum - the next available figure number.  (One more than
%      the last figure number this function used.)
%    time_block - the time in seconds of each time slice
%
% Dec-14-2006 ces2130@columbia.edu
function [total_solo, total_solo_correct,total_non_solo,...
        total_non_solo_correct, figNum,time_block] = ...
    process_wav(figNum,num,is_training,do_output)

% get the file names
[wav_file info_file] = get_files(num,is_training);

[wav sampling_rate] = wavread(wav_file);

% add the left and right tracks together
wav = wav(:,1) + wav(:,2);

% take the spectrogram
[B,F,T] = specgram(wav,2048,sampling_rate);

len = length(wav);
t = [0:len-1]./sampling_rate;

% read the label file
truth = process_label(info_file,T);

% judging from the spectrograms, it seems that most of the action
% happens under 4000 Hz.
max_frequency = 4000;

% we're going to use the 10th percentile as the cutoff for the
% zeros
percentile = 10;

% find out where the zeros are
[figNum calculated]= calc_zeros(figNum,B,F,T,percentile,...
    max_frequency,do_output);

if do_output
    % see what we have
    figure(figNum);
    figNum = figNum+1;
    
    subplot(211);
    dB = 20*log10(abs(B));
    inds = find(F<=max_frequency);
    imagesc(T,F(inds),dB(inds,:));
    axis xy
    title('dB spectrogram');
    %colorbar;
    xlabel('Time (s)');
    ylabel('Frequency (Hz)');
    
    subplot(212);
    plot(T,truth, 'b');
    hold on;
    plot(T,0.9*calculated,'g');
    hold off;
    axis([0,max(t),-.1, 1.1]);
    title('Classification');
    xlabel('Time (s)');
    ylabel('Classification (0 = non-solo, non-zero = solo');
    legend('Truth data', 'Calculated');
end

% check what we got
[total_solo, total_solo_correct,...
        total_non_solo, total_non_solo_correct] = ...
    check_classification(truth,calculated);

time_block = T(2)-T(1);

if do_output
    disp(sprintf('total seconds solo: %f',...
        total_solo*(T(2)-T(1))));
    disp(sprintf('percent solo correct: %f',...
        100*total_solo_correct/total_solo));
    disp(sprintf('total seconds non-solo: %f',...
        total_non_solo*(T(2)-T(1))));
    disp(sprintf('percent non-solo correct: %f',...
        100*total_non_solo_correct/total_non_solo));
end

% check the calculated classification against the truth data
function [total_solo, total_solo_correct,...
        total_non_solo, total_non_solo_correct] =...
    check_classification(truth,calculated)

truth_solo_inds = logical(truth);
total_solo = length(find(truth_solo_inds));
total_solo_correct = length(find(calculated(truth_solo_inds) == 1));

truth_non_solo_inds = ~truth_solo_inds;
total_non_solo = length(find(truth_non_solo_inds));
total_non_solo_correct = ...
    length(find(calculated(truth_non_solo_inds)==0));







function [wav_file, info_file] = get_files(num,is_training)
if is_training
    wav_file = sprintf('final cuts\\training\\%d.wav',num);
    info_file = sprintf('labels\\training\\%d.txt',num);
else
    wav_file = sprintf('final cuts\\test\\%d.wav',num);
    info_file = sprintf('labels\\test\\%d.txt',num);
    
end