File: segment.py

package info (click to toggle)
pocketsphinx 5.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 51,236 kB
  • sloc: ansic: 54,519; python: 2,438; sh: 566; cpp: 410; perl: 342; yacc: 93; lex: 50; makefile: 30
file content (27 lines) | stat: -rw-r--r-- 946 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3
"""
Segment speech endlessly from the default audio device.
"""

# MIT license (c) 2022, see LICENSE for more information.
# Author: David Huggins-Daines <dhdaines@gmail.com>

from pocketsphinx import Segmenter
import subprocess

seg = Segmenter()
incmd = f"sox -q -r {seg.sample_rate} -c 1 -b 16 -e signed-integer -d -t raw -".split()
outcmd = f"sox -q -t raw -r {seg.sample_rate} -c 1 -b 16 -e signed-integer -".split()
with subprocess.Popen(incmd, stdout=subprocess.PIPE) as sox:
    try:
        for idx, speech in enumerate(seg.segment(sox.stdout)):
            outfile = "%03d_%.2f-%.2f.wav" % (
                idx,
                speech.start_time,
                speech.end_time,
            )
            with subprocess.Popen(outcmd + [outfile], stdin=subprocess.PIPE) as soxout:
                soxout.stdin.write(speech.pcm)
            print("Wrote %s" % outfile)
    except KeyboardInterrupt:
        pass