0

I could successfully extract phoneme data from an audio file using Pocketsphinx, but how can I also output the lasting time of each phoneme?

def phonemes(filename):
    audio_path = os.path.join(dir_path, filename)
    # Create a decoder with certain model
    config = Decoder.default_config()
    config.set_string('-hmm', os.path.join(MODELDIR, 'en-us'))
    config.set_string('-allphone', os.path.join(MODELDIR, 'en-us/en-us-phone.lm.dmp'))
    config.set_float('-lw', 2.0)
    config.set_float('-beam', 1e-10)
    config.set_float('-pbeam', 1e-10)

    # Decode streaming data.
    decoder = Decoder(config)

    decoder.start_utt()
    stream = open(audio_path, 'rb')
    while True:
      buf = stream.read(1024)
      if buf:
        decoder.process_raw(buf, False, False)
      else:
        break
    decoder.end_utt()


    pho = [seg.word for seg in decoder.seg()]
    print('Phoneme:', pho)

This is what the print looks like: ('Phonemes', ['SIL', 'HH', 'M', 'W', 'M', 'HH', 'HH', 'HH', 'HH', 'HH', 'HH', 'HH', 'HH', 'HH', 'L', 'HH', 'L', 'M', 'M', 'M', 'HH', 'HH', 'HH', 'HH', 'L', 'HH', 'HH', 'HH', 'HH', 'HH', 'HH', 'M', 'M', 'W', 'M', 'HH', 'HH', 'ER', 'ER', 'HH', 'HH', 'M', 'M', 'M', 'HH', 'M', 'M', 'G', 'M', 'M', 'M', 'SIL', 'M', 'HH', 'M', 'ER', 'SIL', 'W', 'M', 'M', 'NG', 'M', 'M', 'HH', 'L', 'M', 'M', 'SIL', 'W', 'HH', 'L', 'M', 'SIL', 'HH', 'AE', 'V', 'R', 'HH', '+SPN+', 'HH', 'HH', 'SIL', 'V', 'UW', 'L', 'V', 'N', 'HH', 'D', 'V', 'D', '+SPN+', 'D', 'B', 'AA', 'SIL', '+SPN+', 'HH', 'HH', 'AH', 'N', 'DH', 'UW', 'L', 'HH', 'UW', 'V', 'D', 'N', 'M', 'D', 'M', 'UW', 'P'])

Sam35
  • 11
  • 1

1 Answers1

0

Simply

print('Phoneme:', decoder.seg())

or you can use seg.start, seg.end in addition to seg.word.

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87