Module pseudoduration
[frames] | no frames]

Source Code for Module pseudoduration

  1  #!/usr/bin/env python 
  2   
  3  """Duration estimator for speech. 
  4   
  5  Usage: s_slope [flags] 
  6  Flags: 
  7          - -f FFF opens file FFF for reading. 
  8          - -c XXX  selects column XXX (either a data name or an integer). 
  9          - -o FFF  sets the output file. 
 10          - -dt #.##   Sets the interval for calculating the output. 
 11   
 12  It takes a local spectrum, 
 13  bins it onto the Bark scale, 
 14  converts to perceptual loudness (via **E). 
 15  Then, it computes a measure of how far you can go 
 16  from each point before the spectrum changes too much. 
 17  """ 
 18   
 19  import sys 
 20  import math as M 
 21  import gpkimgclass 
 22  from gmisclib import die 
 23  from gmisclib import cache as CC 
 24  from gpk_voicing import pseudo_dur as PD 
 25   
 26  # These parameters are derived from /proj/aesop/QuasiDuration/adjust-med-v3.log, 
 27  # which derives from the code in /proj/aesop/QuasiDuration/bin/adjust-med-v3.py . 
 28  # This code optimized the match between the median quasiduration over a vowel and 
 29  # the duration, as determined by a manual segmentation. 
 30  C = 22.917 
 31  Lfac = M.exp(0.0753) 
 32  Nsv = 0.315 
 33  ROOT = '/tmp/pseudoduration_cache' 
 34   
 35   
 36   
 37  if __name__ == '__main__': 
 38          # try: 
 39                  # import psyco 
 40                  # psyco.full() 
 41          # except ImportError: 
 42                  # pass 
 43   
 44          DT = 0.01       # Seconds.  Default output sampling interval. 
 45          arglist = sys.argv[1:] 
 46          arglist0 = arglist 
 47          column = None 
 48          Cache = None 
 49          signalfile = None 
 50          outfile = None 
 51          extrahdr = {} 
 52          RTYPE = 'pseudoduration' 
 53          while arglist and arglist[0].startswith('-'): 
 54                  arg = arglist.pop(0) 
 55                  if arg == '--': 
 56                          break 
 57                  elif arg == '-DT' or arg == '-dt': 
 58                          DT = float(arglist.pop(0)) 
 59                  elif arg == '-f': 
 60                          signalfile = arglist.pop(0) 
 61                  elif arg == '-c' or arg == '-col': 
 62                          tmp = arglist.pop(0) 
 63                          try: 
 64                                  column = int( tmp ) 
 65                          except ValueError: 
 66                                  column = tmp 
 67                          die.note("signalfile", signalfile) 
 68                  elif arg == '-lfac': 
 69                          Lfac = M.exp(float(arglist.pop(0))) 
 70                  elif arg == '-Nsv': 
 71                          Nsv = float(arglist.pop(0)) 
 72                          assert Nsv > 0.0 
 73                  elif arg == '-C': 
 74                          C = float(arglist.pop(0)) 
 75                  elif arg == '-dur': 
 76                          RTYPE = 'pseudoduration' 
 77                  elif arg == '-logdur': 
 78                          RTYPE = 'log(pseudoduration)' 
 79                  elif arg == '-ctr': 
 80                          RTYPE = 'center_time' 
 81                  elif arg == '-o': 
 82                          outfile = arglist.pop(0) 
 83                  elif arg == '-cache': 
 84                          Cache = True 
 85                  elif arg == '-write': 
 86                          extrahdr = dict( [q.strip().split('=', 1) 
 87                                          for q in arglist.pop(0).split(';') ] 
 88                                          ) 
 89                  else: 
 90                          die.info("Unrecognized flag: %s" % arg) 
 91                          print __doc__ 
 92                          die.exit(1) 
 93          if arglist and signalfile is None: 
 94                  signalfile = arglist.pop(0) 
 95          if arglist and outfile is None: 
 96                  outfile = arglist.pop(0) 
 97          elif outfile is None: 
 98                  outfile = 'pdur.dat' 
 99          if column is None: 
100                  column = 0 
101          if signalfile is None: 
102                  die.info("No signal file specified.") 
103                  print __doc__ 
104                  die.exit(1) 
105          if arglist: 
106                  die.info('Extra arguments!') 
107                  print __doc__ 
108                  die.exit(1) 
109          try: 
110                  signal = gpkimgclass.read(signalfile) 
111          except gpkimgclass.CannotReadDataFile, x: 
112                  die.die(x) 
113          try: 
114                  signal.column(column) 
115          except KeyError: 
116                  die.info("File has %d columns" % signal.n[1]) 
117                  die.die("Bad column: %s" % str(column)) 
118          if Cache is not None: 
119                  Cache = CC.cache_info(root=ROOT, file=signalfile, info=(column,)) 
120   
121          try: 
122                  o = PD.pdur(signal.column(column), signal.dt(), DT, RTYPE, c=C, lfac=Lfac, Nsv=Nsv, cache_info=Cache) 
123          except: 
124                  die.catch("Exception in pdur calculation") 
125                  raise 
126   
127          hdr = signal.hdr.copy() 
128          hdr['Pseudoduration_Params'] = "C=%s; Nsv=%s; lfac=%s" % (C, Nsv, Lfac) 
129          hdr['program'] = sys.argv[0] 
130          hdr['ARGV'] = arglist0 
131          hdr['input_file'] = signalfile 
132          hdr['column'] = column 
133          hdr['CDELT2'] = DT 
134          hdr['CRPIX2'] = 1 
135          hdr['CRVAL2'] = signal.start() 
136          hdr['CDELT1'] = 1 
137          hdr['TTYPE1'] = RTYPE 
138          hdr['BITPIX'] = -32 
139          hdr['DataSamplingFreq'] = 1.0/signal.dt() 
140          hdr.update( extrahdr ) 
141          gpkimgclass.gpk_img(hdr, o).write(outfile) 
142