Dawn@4: function [F0, err] = func_PraatPitch(wavfile, frameshift, frameprecision, minF0, maxF0, ... Dawn@4: silthres, voicethres, octavecost, ... Dawn@4: octavejumpcost, voiunvoicost, ... Dawn@4: killoctavejumps, smooth, smoothbw, ... Dawn@4: interpolate, method, datalen) Dawn@4: % [F0, err = func_PraatPitch(wavfile, frameshift, maxF0, minF0, Dawn@4: % voicethres, octavecost, octavejumpcost, voiunvoicost, datalen) Dawn@4: % Input: wavfile - input wav file Dawn@4: % frameshift - in seconds Dawn@4: % maxF0 - maximum F0 Dawn@4: % minF0 - minimum F0 Dawn@4: % silthres - Silence threshold (Praat specific) Dawn@4: % voicethres - Voice threshold (Praat specific) Dawn@4: % octavecost - Octave Cost (Praat specific) Dawn@4: % octavejumpcost - Octave Jump Cost (Praat specific) Dawn@4: % voiunvoicost - Voiced/unvoiced Cost (Praat specific) Dawn@4: % killoctavejumps - Kill octave jumps? (Praat specific) Dawn@4: % smooth - Smooth? (Praat specific) Dawn@4: % smoothbw - Smoothing bandwidth (Hz) (Praat specific) Dawn@4: % interpolate - Interpolate? (Praat specific) Dawn@4: % method - autocorrelation (ac) or cross-correlation (cc) Dawn@4: % datalen - output data length Dawn@4: % Output: F0 - F0 values Dawn@4: % err - error flag Dawn@4: % Notes: This currently only works on PC and Mac. Dawn@4: % Dawn@4: % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA Dawn@4: % Modified by Kristine Yu 2010-10-16 Dawn@4: % Copyright UCLA SPAPL 2010 Dawn@4: Dawn@4: % settings Dawn@4: iwantfilecleanup = 1; %delete files when done Dawn@4: Dawn@4: % check if we need to put double quotes around wavfile Dawn@4: if (wavfile(1) ~= '"') Dawn@4: pwavfile = ['"' wavfile '"']; Dawn@4: else Dawn@4: pwavfile = wavfile; Dawn@4: end Dawn@4: Dawn@4: if (ispc) % pc can run praatcon.exe Dawn@4: cmd = sprintf(['Windows\\praatcon.exe Windows\\praatF0.praat %s %.3f ' ... Dawn@4: '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %d %d %.3f %d %s'], pwavfile, frameshift, minF0, maxF0, silthres, ... Dawn@4: voicethres, octavecost, octavejumpcost, voiunvoicost, ... Dawn@4: killoctavejumps, smooth, smoothbw, interpolate, method); Dawn@4: elseif (ismac) % mac osx can run Praat using terminal, call Praat from Dawn@4: % Nix/ folder Dawn@4: curr_dir = pwd; Dawn@4: curr_wav = [curr_dir wavfile(2:end)]; Dawn@4: Dawn@4: cmd = sprintf(['MacOS/Praat Windows/praatF0.praat %s %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %d %d %.3f %d %s'], curr_wav, frameshift, minF0, maxF0, silthres, voicethres, octavecost, octavejumpcost, voiunvoicost, killoctavejumps, smooth, smoothbw, interpolate, method); Dawn@4: Dawn@4: else % otherwise Dawn@4: F0 = NaN; Dawn@4: err = -1; Dawn@4: return; Dawn@4: end Dawn@4: Dawn@4: %call up praat for pc Dawn@4: if (ispc) Dawn@4: err = system(cmd); Dawn@4: Dawn@4: if (err ~= 0) % oops, error, exit now Dawn@4: F0 = NaN; Dawn@4: if (iwantfilecleanup) Dawn@4: if (exist(f0file, 'file') ~= 0) Dawn@4: delete(f0file); Dawn@4: end Dawn@4: end Dawn@4: return; Dawn@4: end Dawn@4: end Dawn@4: Dawn@4: %call up praat for Mac OSX Dawn@4: if (ismac) Dawn@4: err = unix(cmd); Dawn@4: Dawn@4: if (err ~= 0) % oops, error, exit now Dawn@4: F0 = NaN; Dawn@4: if (iwantfilecleanup) Dawn@4: if (exist(f0file, 'file') ~= 0) Dawn@4: delete(f0file); Dawn@4: end Dawn@4: end Dawn@4: return; Dawn@4: end Dawn@4: end Dawn@4: Dawn@4: Dawn@4: % Get f0 file Dawn@4: if strcmp(method, 'ac') %if autocorrelation, get .praatac file Dawn@4: f0file = [wavfile '.praatac']; Dawn@4: else Dawn@4: f0file = [wavfile '.praatcc']; % else, cross-correlation, get .praatcc file Dawn@4: end Dawn@4: Dawn@4: Dawn@4: % praat call was successful, return F0 values Dawn@4: F0 = zeros(datalen, 1) * NaN; Dawn@4: Dawn@4: fid = fopen(f0file, 'rt'); Dawn@4: Dawn@4: % read the rest Dawn@4: C = textscan(fid, '%f %f', 'delimiter', '\n', 'TreatAsEmpty', '--undefined--'); Dawn@4: fclose(fid); Dawn@4: Dawn@4: t = round(C{1} * 1000); % time locations from praat pitch track Dawn@4: Dawn@4: %KY Since Praat outputs no values in silence/if no f0 value returned, must pad with leading Dawn@4: % undefined values if needed, so we set start time to 0, rather than t(1) Dawn@4: Dawn@4: start = 0; Dawn@4: finish = t(end); Dawn@4: increment = frameshift * 1000; Dawn@4: Dawn@4: for k=start:increment:finish Dawn@4: [val, inx] = min(abs(t - k)); % try to find the closest value Dawn@4: if (abs(t(inx) - k) > frameprecision * frameshift * 1000) % no valid value found Dawn@4: continue; Dawn@4: end Dawn@4: Dawn@4: n = round(k / (frameshift * 1000)) + 1; % KY I added a one since Dawn@4: % Matlab indexing starts at 1 Dawn@4: % not 0 Dawn@4: if (n < 1 || n > datalen) Dawn@4: continue; Dawn@4: end Dawn@4: Dawn@4: F0(n) = C{2}(inx); Dawn@4: end Dawn@4: Dawn@4: if (iwantfilecleanup) Dawn@4: if (exist(f0file, 'file') ~= 0) Dawn@4: delete(f0file); Dawn@4: end Dawn@4: end