-
Notifications
You must be signed in to change notification settings - Fork 24
/
collect_pitch_data_from_files.praat
114 lines (100 loc) · 3.89 KB
/
collect_pitch_data_from_files.praat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# This script goes through sound and TextGrid files in a directory,
# opens each pair of Sound and TextGrid, calculates the pitch maximum
# of each labeled interval, and saves results to a text file.
# To make some other or additional analyses, you can modify the script
# yourself... it should be reasonably well commented! ;)
#
# This script is distributed under the GNU General Public License.
# Copyright 4.7.2003 Mietta Lennes
form Analyze pitch maxima from labeled segments in files
comment Directory of sound files
text sound_directory D:\tmp\
sentence Sound_file_extension .wav
comment Directory of TextGrid files
text textGrid_directory D:\tmp\
sentence TextGrid_file_extension .TextGrid
comment Full path of the resulting text file:
text resultfile D:\tmp\pitchresults.txt
comment Which tier do you want to analyze?
sentence Tier segments
comment Pitch analysis parameters
positive Time_step 0.01
positive Minimum_pitch_(Hz) 75
positive Maximum_pitch_(Hz) 300
endform
# Here, you make a listing of all the sound files in a directory.
# The example gets file names ending with ".wav" from D:\tmp\
Create Strings as file list... list 'sound_directory$'*'sound_file_extension$'
numberOfFiles = Get number of strings
# Check if the result file exists:
if fileReadable (resultfile$)
pause The result file 'resultfile$' already exists! Do you want to overwrite it?
filedelete 'resultfile$'
endif
# Write a row with column titles to the result file:
# (remember to edit this if you add or change the analyses!)
titleline$ = "Filename Segment label Maximum pitch (Hz)'newline$'"
fileappend "'resultfile$'" 'titleline$'
# Go through all the sound files, one by one:
for ifile to numberOfFiles
filename$ = Get string... ifile
# A sound file is opened from the listing:
Read from file... 'sound_directory$''filename$'
# Starting from here, you can add everything that should be
# repeated for every sound file that was opened:
soundname$ = selected$ ("Sound", 1)
To Pitch... time_step minimum_pitch maximum_pitch
# Open a TextGrid by the same name:
gridfile$ = "'textGrid_directory$''soundname$''textGrid_file_extension$'"
if fileReadable (gridfile$)
Read from file... 'gridfile$'
# Find the tier number that has the label given in the form:
call GetTier 'tier$' tier
numberOfIntervals = Get number of intervals... tier
# Pass through all intervals in the selected tier:
for interval to numberOfIntervals
label$ = Get label of interval... tier interval
if label$ <> ""
# if the interval has an unempty label, get its start and end:
start = Get starting point... tier interval
end = Get end point... tier interval
# get the Pitch maximum at that interval
select Pitch 'soundname$'
pitchmax = Get maximum... start end Hertz Parabolic
printline 'pitchmax'
# Save result to text file:
resultline$ = "'soundname$' 'label$' 'pitchmax''newline$'"
fileappend "'resultfile$'" 'resultline$'
select TextGrid 'soundname$'
endif
endfor
# Remove the TextGrid object from the object list
select TextGrid 'soundname$'
Remove
endif
# Remove the temporary objects from the object list
select Sound 'soundname$'
plus Pitch 'soundname$'
Remove
select Strings list
# and go on with the next sound file!
endfor
Remove
#-------------
# This procedure finds the number of a tier that has a given label.
procedure GetTier name$ variable$
numberOfTiers = Get number of tiers
itier = 1
repeat
tier$ = Get tier name... itier
itier = itier + 1
until tier$ = name$ or itier > numberOfTiers
if tier$ <> name$
'variable$' = 0
else
'variable$' = itier - 1
endif
if 'variable$' = 0
exit The tier called 'name$' is missing from the file 'soundname$'!
endif
endproc