user1374310
user1374310

Reputation: 311

Praat scripting: creating a text file

just working with Praat at the moment, and I'm trying to write a script to do the following with a collection of 3 Sound (narrative) files. I've managed as far as c), the scripting part is relatively easy. What I don't get is how to write it to a text file with those columns. Any help would be great!

a) create a program that extracts all intervals on the phone tier of each of Narratives 1–3 which represent vowels whose label is a single letter, keeping times. I need each resulting Sound to have an appropriate label which identifies the vowel concerned

b) creates a Formant (burg) object corresponding to each of those intervals

c) calculates the midpoint of each Formant object

c) gets the values of formants 1, 2 and 3 at each of those midpoints

d) writes a text file with the following heading:

Narrative# Label Midpoint Time F1 F2 F3

and under that, the appropriate information for each vowel

Upvotes: 3

Views: 4909

Answers (1)

jja
jja

Reputation: 2098

The easy way

The easiest way to do this would be to write your output to a Table object and then use Praat's Save to comma-separated file command to save it to an external file. Examples below use the new (slightly more reasonable) new syntax, so make sure to update Praat before trying them out (or try the shorthand versions in this answer's edit history).

Here's an example:

# Create a Table with no rows
table = Create Table with column names:
..."table", 0, "Narrative Label Midpoint Time F1 F2 F3"

for i to number_of_intervals
  # Assuming you have your Formant objects in an array named "burg"
  selectObject(burg[i])
  # Run your analysis here
  # For this example, I'm assuming values for the columns are in
  # variables called narrative$, label$, midpoint, time, f1, f2 and f3

  selectObject(table)
  Append row
  current_row = Get number of rows
  # Insert your values
  Set string value:  current_row, "Narrative", narrative$
  Set string value:  current_row, "Label", label$
  Set numeric value: current_row, "Midpoint", midpoint 
  Set numeric value: current_row, "Time", time
  Set numeric value: current_row, "F1", f1 
  Set numeric value: current_row, "F2", f2
  Set numeric value: current_row, "F3", f3
endfor

# Save it!
# Remember to select it if the table is not the active selection at
# the end of the loop
Save to comma-separated file: /path/to/file
# And then you can get rid of it
removeObject(table)

Or you could use, if you prefer tabs

Save to tab-separated file: /path/to/file

Note that this method won't allow you to have "Narrative#" as a column name.

The 'l33t' way

Alternatively, you could use Praat's file directives write directly to the file as explained in the documentation:

sep$ = ","
# sep$ = tab$

# Create / overwrite file and write header
writeFileLine: "/path/to/file",
  ..."Narrative#" + sep$ +
  ..."Label"      + sep$ + 
  ..."Midpoint"   + sep$ +
  ..."Time"       + sep$ +
  ..."F1"         + sep$ +
  ..."F2"         + sep$ +
  ..."F3"

for i to number_of_intervals
  selectObject(burg[i])
  # Run your analysis here

  appendFileLine: "/path/to/file",
    ...narrative$        + sep$ +
    ...label$            + sep$ +
    ...string$(midpoint) + sep$ +
    ...string$(time)     + sep$ + 
    ...string$(f1)       + sep$ +
    ...string$(f2)       + sep$ +
    ...string$(f3)

endfor

Upvotes: 4

Related Questions