Dawn@4
|
1 function [F0, err] = func_PraatPitch(wavfile, frameshift, frameprecision, minF0, maxF0, ...
|
Dawn@4
|
2 silthres, voicethres, octavecost, ...
|
Dawn@4
|
3 octavejumpcost, voiunvoicost, ...
|
Dawn@4
|
4 killoctavejumps, smooth, smoothbw, ...
|
Dawn@4
|
5 interpolate, method, datalen)
|
Dawn@4
|
6 % [F0, err = func_PraatPitch(wavfile, frameshift, maxF0, minF0,
|
Dawn@4
|
7 % voicethres, octavecost, octavejumpcost, voiunvoicost, datalen)
|
Dawn@4
|
8 % Input: wavfile - input wav file
|
Dawn@4
|
9 % frameshift - in seconds
|
Dawn@4
|
10 % maxF0 - maximum F0
|
Dawn@4
|
11 % minF0 - minimum F0
|
Dawn@4
|
12 % silthres - Silence threshold (Praat specific)
|
Dawn@4
|
13 % voicethres - Voice threshold (Praat specific)
|
Dawn@4
|
14 % octavecost - Octave Cost (Praat specific)
|
Dawn@4
|
15 % octavejumpcost - Octave Jump Cost (Praat specific)
|
Dawn@4
|
16 % voiunvoicost - Voiced/unvoiced Cost (Praat specific)
|
Dawn@4
|
17 % killoctavejumps - Kill octave jumps? (Praat specific)
|
Dawn@4
|
18 % smooth - Smooth? (Praat specific)
|
Dawn@4
|
19 % smoothbw - Smoothing bandwidth (Hz) (Praat specific)
|
Dawn@4
|
20 % interpolate - Interpolate? (Praat specific)
|
Dawn@4
|
21 % method - autocorrelation (ac) or cross-correlation (cc)
|
Dawn@4
|
22 % datalen - output data length
|
Dawn@4
|
23 % Output: F0 - F0 values
|
Dawn@4
|
24 % err - error flag
|
Dawn@4
|
25 % Notes: This currently only works on PC and Mac.
|
Dawn@4
|
26 %
|
Dawn@4
|
27 % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA
|
Dawn@4
|
28 % Modified by Kristine Yu 2010-10-16
|
Dawn@4
|
29 % Copyright UCLA SPAPL 2010
|
Dawn@4
|
30
|
Dawn@4
|
31 % settings
|
Dawn@4
|
32 iwantfilecleanup = 1; %delete files when done
|
Dawn@4
|
33
|
Dawn@4
|
34 % check if we need to put double quotes around wavfile
|
Dawn@4
|
35 if (wavfile(1) ~= '"')
|
Dawn@4
|
36 pwavfile = ['"' wavfile '"'];
|
Dawn@4
|
37 else
|
Dawn@4
|
38 pwavfile = wavfile;
|
Dawn@4
|
39 end
|
Dawn@4
|
40
|
Dawn@4
|
41 if (ispc) % pc can run praatcon.exe
|
Dawn@4
|
42 cmd = sprintf(['Windows\\praatcon.exe Windows\\praatF0.praat %s %.3f ' ...
|
Dawn@4
|
43 '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %d %d %.3f %d %s'], pwavfile, frameshift, minF0, maxF0, silthres, ...
|
Dawn@4
|
44 voicethres, octavecost, octavejumpcost, voiunvoicost, ...
|
Dawn@4
|
45 killoctavejumps, smooth, smoothbw, interpolate, method);
|
Dawn@4
|
46 elseif (ismac) % mac osx can run Praat using terminal, call Praat from
|
Dawn@4
|
47 % Nix/ folder
|
Dawn@4
|
48 curr_dir = pwd;
|
Dawn@4
|
49 curr_wav = [curr_dir wavfile(2:end)];
|
Dawn@4
|
50
|
Dawn@4
|
51 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
|
52
|
Dawn@4
|
53 else % otherwise
|
Dawn@4
|
54 F0 = NaN;
|
Dawn@4
|
55 err = -1;
|
Dawn@4
|
56 return;
|
Dawn@4
|
57 end
|
Dawn@4
|
58
|
Dawn@4
|
59 %call up praat for pc
|
Dawn@4
|
60 if (ispc)
|
Dawn@4
|
61 err = system(cmd);
|
Dawn@4
|
62
|
Dawn@4
|
63 if (err ~= 0) % oops, error, exit now
|
Dawn@4
|
64 F0 = NaN;
|
Dawn@4
|
65 if (iwantfilecleanup)
|
Dawn@4
|
66 if (exist(f0file, 'file') ~= 0)
|
Dawn@4
|
67 delete(f0file);
|
Dawn@4
|
68 end
|
Dawn@4
|
69 end
|
Dawn@4
|
70 return;
|
Dawn@4
|
71 end
|
Dawn@4
|
72 end
|
Dawn@4
|
73
|
Dawn@4
|
74 %call up praat for Mac OSX
|
Dawn@4
|
75 if (ismac)
|
Dawn@4
|
76 err = unix(cmd);
|
Dawn@4
|
77
|
Dawn@4
|
78 if (err ~= 0) % oops, error, exit now
|
Dawn@4
|
79 F0 = NaN;
|
Dawn@4
|
80 if (iwantfilecleanup)
|
Dawn@4
|
81 if (exist(f0file, 'file') ~= 0)
|
Dawn@4
|
82 delete(f0file);
|
Dawn@4
|
83 end
|
Dawn@4
|
84 end
|
Dawn@4
|
85 return;
|
Dawn@4
|
86 end
|
Dawn@4
|
87 end
|
Dawn@4
|
88
|
Dawn@4
|
89
|
Dawn@4
|
90 % Get f0 file
|
Dawn@4
|
91 if strcmp(method, 'ac') %if autocorrelation, get .praatac file
|
Dawn@4
|
92 f0file = [wavfile '.praatac'];
|
Dawn@4
|
93 else
|
Dawn@4
|
94 f0file = [wavfile '.praatcc']; % else, cross-correlation, get .praatcc file
|
Dawn@4
|
95 end
|
Dawn@4
|
96
|
Dawn@4
|
97
|
Dawn@4
|
98 % praat call was successful, return F0 values
|
Dawn@4
|
99 F0 = zeros(datalen, 1) * NaN;
|
Dawn@4
|
100
|
Dawn@4
|
101 fid = fopen(f0file, 'rt');
|
Dawn@4
|
102
|
Dawn@4
|
103 % read the rest
|
Dawn@4
|
104 C = textscan(fid, '%f %f', 'delimiter', '\n', 'TreatAsEmpty', '--undefined--');
|
Dawn@4
|
105 fclose(fid);
|
Dawn@4
|
106
|
Dawn@4
|
107 t = round(C{1} * 1000); % time locations from praat pitch track
|
Dawn@4
|
108
|
Dawn@4
|
109 %KY Since Praat outputs no values in silence/if no f0 value returned, must pad with leading
|
Dawn@4
|
110 % undefined values if needed, so we set start time to 0, rather than t(1)
|
Dawn@4
|
111
|
Dawn@4
|
112 start = 0;
|
Dawn@4
|
113 finish = t(end);
|
Dawn@4
|
114 increment = frameshift * 1000;
|
Dawn@4
|
115
|
Dawn@4
|
116 for k=start:increment:finish
|
Dawn@4
|
117 [val, inx] = min(abs(t - k)); % try to find the closest value
|
Dawn@4
|
118 if (abs(t(inx) - k) > frameprecision * frameshift * 1000) % no valid value found
|
Dawn@4
|
119 continue;
|
Dawn@4
|
120 end
|
Dawn@4
|
121
|
Dawn@4
|
122 n = round(k / (frameshift * 1000)) + 1; % KY I added a one since
|
Dawn@4
|
123 % Matlab indexing starts at 1
|
Dawn@4
|
124 % not 0
|
Dawn@4
|
125 if (n < 1 || n > datalen)
|
Dawn@4
|
126 continue;
|
Dawn@4
|
127 end
|
Dawn@4
|
128
|
Dawn@4
|
129 F0(n) = C{2}(inx);
|
Dawn@4
|
130 end
|
Dawn@4
|
131
|
Dawn@4
|
132 if (iwantfilecleanup)
|
Dawn@4
|
133 if (exist(f0file, 'file') ~= 0)
|
Dawn@4
|
134 delete(f0file);
|
Dawn@4
|
135 end
|
Dawn@4
|
136 end
|