function [t,f0,pvx] = ptrack_sacc(d,sr,paramfile)
% [t,f0,pvx] = ptrack_sacc(d,sr)
%    Run the SAcC pitch tracker on the waveform d (at sampling
%    frequency sr).
%    Returns three 1D vectors.  
%      t is the sample times at which pitches are estimated 
%          (typically uniformly spaced every 0.010 sec).  
%      f0 is the corresponding pitch value in Hz (which can be 
%          zero for unvoiced frames). 
%      pvx is the corresponding probability of voicing for each
%          frame (between 0 and 1).  Generally, a threshold of 0.5
%          is the best guess for actual voiced-unvoiced threshold.
%    This is supposed to be a generic framework for pitch trackers,
%    but in this case we'll run SAcC
% 2013-02-02 Dan Ellis dpwe@ee.columbia.edu

if nargin < 3; paramfile = ''; end

SAcCdir = '../SAcC';

if ~isdeployed() && exist('SAcC_main','file') ~= 2
  addpath(SAcCdir)
end

% Read file if any
[P,D] = config_read_srs(paramfile);
% Fill in defaults
P = config_default(P);

% Hack to make default paths work
if length(paramfile) == 0
  P.pca_file = fullfile(SAcCdir, P.pca_file);
  P.wgt_file = fullfile(SAcCdir, P.wgt_file);
  P.norms_file = fullfile(SAcCdir, P.norms_file);
  P.pcf_file = fullfile(SAcCdir, P.pcf_file);
end

% make sure audio is sampled at right SR
targetsr = P.SBF_sr;
if sr ~= targetsr
  d = resample(d,targetsr,sr);
  sr = targetsr;
end

[f0, lobs, pvx] = SAcC_main(d,sr,P);

f0 = f0';
pvx = pvx';

% SAcC uses 10 ms frames
t = [0:0.01:(0.01*(length(f0)-1))];
