comparison Code/Descriptors/Matlab/MPEG7/FromWeb/VoiceSauce/func_PraatPitch.m @ 4:92ca03a8fa99 tip

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