Praat scripting: creating a text file

后端 未结 2 1318
北海茫月
北海茫月 2020-12-12 03:11

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

2条回答
  •  醉梦人生
    2020-12-12 03:34

    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
    

提交回复
热议问题