Dawn@4: function [F1, F2, F3, F4, B1, B2, B3, B4, err] = func_PraatFormants(wavfile, ... Dawn@4: windowlength, frameshift, frameprecision, datalen) Dawn@4: % [F1, F2, F3, F4, B1, B2, B3, B4, err] = func_PraatFormants(wavfile, windowlength, frameshift, frameprecision, datalen) Dawn@4: % KY Time-stamp: <2010-10-16 21:47:24 amoebe> Dawn@4: % Input: wavfile - input wav file Dawn@4: % windowlength - windowlength in seconds Dawn@4: % frameshift - in seconds Dawn@4: % frameprecision - unitless Dawn@4: % datalen - output data length Dawn@4: % Output: Fx, Bx - formant and bandwidth vectors Dawn@4: % err - error flag Dawn@4: % Notes: This currently only works on a PC. Dawn@4: % Dawn@4: % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA Dawn@4: % Modified by Kristine Yu, 2010-10-16 to allow for variable precision in Dawn@4: % matching up time alignment between data vectors. Dawn@4: % Dawn@4: % Copyright UCLA SPAPL 2010 Dawn@4: Dawn@4: % settings Dawn@4: maxFormant = 6000; Dawn@4: iwantfilecleanup = 1; %delete pfmt 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\\praatformants.praat %s %.3f %.3f %d', pwavfile, frameshift, windowlength, maxFormant); Dawn@4: 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/praatformants.praat ' ... Dawn@4: '%s %.3f %.3f %d'], curr_wav, frameshift, windowlength, maxFormant); Dawn@4: Dawn@4: else Dawn@4: F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; Dawn@4: B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; Dawn@4: err = -1; Dawn@4: return; Dawn@4: end Dawn@4: Dawn@4: % Set name of formant track file Dawn@4: fmtfile = [wavfile '.pfmt']; Dawn@4: Dawn@4: %call up praat Dawn@4: Dawn@4: % for pc Dawn@4: if (ispc) Dawn@4: err = system(cmd); Dawn@4: Dawn@4: if (err ~= 0) % oops, error, exit now Dawn@4: F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; Dawn@4: B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; Dawn@4: if (iwantfilecleanup) Dawn@4: if (exist(fmtfile, 'file') ~= 0) Dawn@4: delete(fmtfile); Dawn@4: end Dawn@4: end Dawn@4: return; Dawn@4: end Dawn@4: end Dawn@4: Dawn@4: Dawn@4: % for mac Dawn@4: if (ismac) Dawn@4: err = unix(cmd); Dawn@4: Dawn@4: if (err ~= 0) % oops, error, exit now Dawn@4: F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; Dawn@4: B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; Dawn@4: if (iwantfilecleanup) Dawn@4: if (exist(fmtfile, 'file') ~= 0) Dawn@4: delete(fmtfile); Dawn@4: end Dawn@4: end Dawn@4: return; Dawn@4: end Dawn@4: end Dawn@4: Dawn@4: % praat call was successful, return fmt values Dawn@4: F1 = zeros(datalen, 1) * NaN; B1 = zeros(datalen, 1) * NaN; Dawn@4: F2 = zeros(datalen, 1) * NaN; B2 = zeros(datalen, 1) * NaN; Dawn@4: F3 = zeros(datalen, 1) * NaN; B3 = zeros(datalen, 1) * NaN; Dawn@4: F4 = zeros(datalen, 1) * NaN; B4 = zeros(datalen, 1) * NaN; Dawn@4: Dawn@4: % Get formant file Dawn@4: Dawn@4: fid = fopen(fmtfile, 'rt'); Dawn@4: Dawn@4: % read and discard the header Dawn@4: C = textscan(fid, '%s', 1, 'delimiter', '\n'); Dawn@4: Dawn@4: % read the rest Dawn@4: C = textscan(fid, '%f %f %f %f %f %f %f %f %f %f', 'delimiter', '\n', 'TreatAsEmpty', '--undefined--'); Dawn@4: fclose(fid); Dawn@4: t = round(C{1} * 1000); % time locations Dawn@4: Dawn@4: start = 0; % KY changed since Praat doesn't necessarily have data start Dawn@4: % at time = 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 1 since Matlab Dawn@4: % index starts at 1, not 0 Dawn@4: if (n < 1 || n > datalen) Dawn@4: continue; Dawn@4: end Dawn@4: Dawn@4: F1(n) = C{3}(inx); Dawn@4: B1(n) = C{4}(inx); Dawn@4: F2(n) = C{5}(inx); Dawn@4: B2(n) = C{6}(inx); Dawn@4: F3(n) = C{7}(inx); Dawn@4: B3(n) = C{8}(inx); Dawn@4: F4(n) = C{9}(inx); Dawn@4: B4(n) = C{10}(inx); Dawn@4: end Dawn@4: Dawn@4: if (iwantfilecleanup) Dawn@4: if (exist(fmtfile, 'file') ~= 0) Dawn@4: delete(fmtfile); Dawn@4: end Dawn@4: end