%--------------------------------------------------------------------------
% Coordinating Script - Stretch, Analyze, and Graph
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------

% set up paths
%--------------------------------------------------------------------------
restoredefaultpath;

p = genpath( '../analyze' );
addpath( p );
p = genpath( '../audio' );
addpath( p );
p = genpath( '../graph' );
addpath( p );
p = genpath( '../grain' );
addpath( p );
p = genpath( '../pvoc' );
addpath( p );
p = genpath( '../tones' );
addpath( p );

% 0/1 = off/on
stretchAudio = 1;
makeFileDisp = 1;
makeGraphs   = 1;
compareError = 1;

tic

% create stretched audio files
%----------------------------------------------------------------------
if( stretchAudio )

    [ filePath, fileName ] = a_getFile( 'amen.wav' );
    fft_bank        ( filePath, fileName );
    fft_ifft_classic( filePath, fileName );
    fft_ifft_lock   ( filePath, fileName );
    fft_ifft_peak   ( filePath, fileName );
    ola             ( filePath, fileName );
    sola            ( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'autumn.wav' );
    fft_bank        ( filePath, fileName );
    fft_ifft_classic( filePath, fileName );
    fft_ifft_lock   ( filePath, fileName );
    fft_ifft_peak   ( filePath, fileName );
    ola             ( filePath, fileName );
    sola            ( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'peaches.wav' );
    fft_bank        ( filePath, fileName );
    fft_ifft_classic( filePath, fileName );
    fft_ifft_lock   ( filePath, fileName );
    fft_ifft_peak   ( filePath, fileName );
    ola             ( filePath, fileName );
    sola            ( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'sine.wav' );
    fft_bank        ( filePath, fileName );
    fft_ifft_classic( filePath, fileName );
    fft_ifft_lock   ( filePath, fileName );
    fft_ifft_peak   ( filePath, fileName );
    ola             ( filePath, fileName );
    sola            ( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'sweep.wav' );
    fft_bank        ( filePath, fileName );
    fft_ifft_classic( filePath, fileName );
    fft_ifft_lock   ( filePath, fileName );
    fft_ifft_peak   ( filePath, fileName );
    ola             ( filePath, fileName );
    sola            ( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'square.wav' );
    fft_bank        ( filePath, fileName );
    fft_ifft_classic( filePath, fileName );
    fft_ifft_lock   ( filePath, fileName );
    fft_ifft_peak   ( filePath, fileName );
    ola             ( filePath, fileName );
    sola            ( filePath, fileName );

end

% generate input file displays
%--------------------------------------------------------------------------
if( makeFileDisp )

    [ filePath, fileName ] = a_getFile( 'amen.wav' );
    file_disp( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'autumn.wav' );
    file_disp( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'peaches.wav' );
    file_disp( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'sine.wav' );
    file_disp( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'sweep.wav' );
    file_disp( filePath, fileName );

    [ filePath, fileName ] = a_getFile( 'square.wav' );
    file_disp( filePath, fileName );
end

% perform analysis on stretched files and generate graphs
%----------------------------------------------------------------------
if( makeGraphs )

    % amen.wav
    [ idlPath, idlName ] = a_getFile( 'amen.wav' );
    [ strPath, strName ] = a_getFile( 'amen.classic.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'amen.lock.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'amen.peak.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'amen.bank.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'amen.ola.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'amen.sola.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    % autumn.wav
    [ idlPath, idlName ] = a_getFile( 'autumn.wav' );
    [ strPath, strName ] = a_getFile( 'autumn.classic.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'autumn.lock.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'autumn.peak.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'autumn.bank.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'autumn.ola.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'autumn.sola.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    % peaches.wav
    [ idlPath, idlName ] = a_getFile( 'peaches.wav' );
    [ strPath, strName ] = a_getFile( 'peaches.classic.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'peaches.lock.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'peaches.peak.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'peaches.bank.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'peaches.ola.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'peaches.sola.wav' );
    avg_file( idlPath, idlName, strPath, strName );
    mov_file( idlPath, idlName, strPath, strName );
    dif_file( idlPath, idlName, strPath, strName );

    % sine_ideal.wav
    [ idlPath, idlName ] = a_getFile( 'sine_ideal.wav' );
    [ strPath, strName ] = a_getFile( 'sine.classic.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sine.lock.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sine.peak.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sine.bank.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sine.ola.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sine.sola.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    % square_ideal.wav
    [ idlPath, idlName ] = a_getFile( 'square_ideal.wav' );
    [ strPath, strName ] = a_getFile( 'square.classic.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'square.lock.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'square.peak.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'square.bank.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'square.ola.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'square.sola.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    % sweep_ideal.wav
    [ idlPath, idlName ] = a_getFile( 'sweep_ideal.wav' );
    [ strPath, strName ] = a_getFile( 'sweep.classic.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sweep.lock.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sweep.peak.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sweep.bank.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sweep.ola.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );

    [ strPath, strName ] = a_getFile( 'sweep.sola.wav' );
    avg_tone( idlPath, idlName, strPath, strName );
    mov_tone( idlPath, idlName, strPath, strName );
    dif_tone( idlPath, idlName, strPath, strName );
end

if( compareError )

    % compare average spectrum error
    avg_error( 'amen' );
    avg_error( 'autumn' );
    avg_error( 'peaches' );
    avg_error( 'sine' );
    avg_error( 'sweep' );
    avg_error( 'square' );

    % compare moving spectral average error
    mov_error( 'amen' );
    mov_error( 'autumn' );
    mov_error( 'peaches' );
    mov_error( 'sine' );
    mov_error( 'sweep' );
    mov_error( 'square' );

    % compare zoomed moving spectral average error
    mov_error_detail( 'sine' );
    mov_error_detail( 'square' );
    mov_error_detail( 'sweep' );

    % compare error spectrograms
    dif_error( 'amen' );
    dif_error( 'autumn' );
    dif_error( 'peaches' );
    dif_error( 'sine' );
    dif_error( 'sweep' );
    dif_error( 'square' );

    % compare zoomed error spectrograms
    dif_error_detail( 'sine' );
    dif_error_detail( 'sweep' );
    dif_error_detail( 'square' );

    % generate summary charts
    avg_summary();
    mov_summary();
end

toc

% EOF