function [ pitch ] = detect_pitch( sampleWavFileName, OVERWRITE )

% open original pitch calculation
sampleFileName = sampleWavFileName( 1 : length( sampleWavFileName ) - 4 );
fileName = [ sampleFileName '_YIN_pitch.txt'];
% read pitch metrics from file
fileID = fopen( fileName );

if( (fileID <= 0) || (OVERWRITE) ) %does the file exist?
    % no
    disp('WARNING: MISSING PITCH FILE');
    %calculate it
    [x, fs, frameLength, noOfFrames] = openFile(  [ sampleFileName '.wav' ] );       
    % perform pitch detection
    framePrd = calculate_pitchYIN( sampleFileName, x, fs, frameLength );
    
    fileID = fopen( fileName, 'w');
    for( i=1:length( framePrd ))
        fprintf(fileID, '%d %f \n',  framePrd(i,1),  framePrd(i,2) );
    end
    fclose( fileID );
    fileID = fopen( fileName );
end

pitch = fscanf( fileID, '%f', inf );
pitch = pitch(2:2:end);
fclose( fileID );
   


end

