Get dur

From Phonlab
Revision as of 11:58, 19 October 2015 by Ronald (talk | contribs)
Jump to navigationJump to search

The get_dur script illustrates basic usage of the audiolabel library. It reads a Praat textgrid and reports time values for labels that match a set of target labels.

<nowiki>
  1. !/usr/bin/env python
  1. Simple script to measure phone durations.

Usage = "meas_dur file.TextGrid"

import sys import audiolabel

  1. Get the textgrid name from the input arguments and create a LabelManager object.

try:

   tg = sys.argv[1]
   lm = audiolabel.LabelManager(from_file=tg, from_type="praat")

except IndexError:

   exit("Usage: " + Usage)
  1. List of labels for which we want duration measurements.

phones = ["P", "T", "K", "B", "D", "G"]

  1. Print the header line.

print "phone\tword\tt1\tt2\tduration"

  1. Loop over all the labels in the 'phone' tier.

for plab in lm.tier('phone'):

   # Check to see if the current label text is one of our target phones.
   if plab.text in phones:
       # Grab the corresponding label from the 'word' tier.
       wlab = lm.tier('word').label_at(plab.center)
       # Print values from the phone label and corresponding word label.
       print "{:s}\t{:s}\t{:1.4f}\t{:1.4f}\t{:1.4f}".format(
           plab.text, wlab.text, plab.t1, plab.t2, plab.duration
       )