pysox PTT数字录音机

时间:2015-09-06 20:07:22

标签: python sox

我需要帮助。 我花了至少一天时间进行实验并且没有生产 这个非常简单的声音应用程序。

PTT(按键通话)表示任何键开始录音,任何键都会停止录音。 我想组建一个录制的口语句子库。 从:

开始
  • 选择格式(.wav,.ogg,.mp3,...)
  • 文件转到的目录
  • 生成文件名的句子列表
  • 发短信的文字提示。

首选行为:

  • 文本句子出现在终端窗口
  • 用户点击PTT按钮
  • 用户说出提示句
  • 用户点击PTT按钮
  • 文件出现在目录中。

我预计LLOC应该只有十几行或两行。 这是一个失败的实验框架代码:

#!/usr/bin/env python

# This class is here just to support keyboard input.
from sys import (stdin, stdout)
from atexit import (register)
from select import (select)
from termios import (tcgetattr, tcsetattr, ICANON, ECHO, TCSAFLUSH)

class Raw(object):
    def __init__(self):
        self.fd = stdin.fileno()
        self.org = tcgetattr(self.fd)
        self.raw = tcgetattr(self.fd)
        self.raw[3] = (self.raw[3] & ~ICANON & ~ECHO)
        register(self.set_normal_term)
    def set_normal_term(self): tcsetattr(self.fd, TCSAFLUSH, self.org)
    def set_curses_term(self): tcsetattr(self.fd, TCSAFLUSH, self.raw)
    def putch(self, ch): stdout.write(ch)
    def getch(self): return stdin.read(1)
    def getche(self): ch = getch(); putch(ch); return ch
    def kbhit(self): dr,dw,de = select([stdin], [], [], 0); return dr <> []
    def __enter__(self): self.set_curses_term(); return self
    def __exit__(self, type, value, traceback): self.set_normal_term()

# This is the PTT recorder code.
from pysox import (CSoxStream, CSignalInfo)

def prompt(sentence, filename):
    print 'Hit any key to start recording.  Hit any key to stop recording.'
    with Raw() as raw:
        print 'Say: "%s".' % (sentence)
        while not raw.kbhit(): pass
        raw.getch()
        print 'Start'
        outfile = CSoxStream(filename,'w', CSignalInfo(48000,2,32))
        # Signal is to be collected until kbhit()
        while not raw.kbhit(): pass
        raw.getch()
        print 'Stop'
    outfile.close()

# This is the CLI to drive the PTT recorder code.
if __name__ == "__main__":
    prefix, ext = ['sentence', 'wav']
    sentences = ["hello world", "klatu barada nikto", "fubar" ]
    for sentence in sentences:
        filename = prefix + '.' + sentence.replace(' ', '.') + "." + ext
        prompt(sentence, filename)

1 个答案:

答案 0 :(得分:1)

诀窍是管道然后在完成时杀死管道pid。

#!/usr/bin/env python

# This class is here just to support keyboard input.
from atexit import (register)
from select import (select)
from termios import (tcgetattr, tcsetattr, ICANON, ECHO, TCSAFLUSH)

class Raw(object):
    def __init__(self):
        self.fd = stdin.fileno()
        self.org, self.raw = (tcgetattr(self.fd), tcgetattr(self.fd))
        self.raw[3] = (self.raw[3] & ~ICANON & ~ECHO)
        register(self.set_normal_term)
    def set_normal_term(self): tcsetattr(self.fd, TCSAFLUSH, self.org)
    def set_curses_term(self): tcsetattr(self.fd, TCSAFLUSH, self.raw)
    def getch(self): return stdin.read(1)
    def kbhit(self): dr,dw,de = select([stdin], [], [], 0); return dr <> []
    def __enter__(self): self.set_curses_term(); return self
    def __exit__(self, type, value, traceback): self.set_normal_term()

# This function handles prompts and keywaits
from sys import (stdin, stdout)

def prompt(pre, s, post):
    print '%s: "%s".  Press any key to %s.   \r' % (pre, s, post),
    stdout.flush()
    with Raw() as raw:
        while not raw.kbhit(): pass
        raw.getch()
    print ' '*79+'\r',
    stdout.flush()

# This is the PTT recorder code.
from subprocess import (Popen, PIPE)
from signal import (SIGTERM)
from os import (kill)

def collect(sentence):
    filename = prefix + '.' + sentence.replace(' ', '.') + "." + ext
    command = 'rec -q -r 16000 -b 16 -c 1 %s trim 0 2:00' % (filename)
    try:
        prompt('Preparing', sentence, 'start')
        pid = Popen(command.split(), stderr=PIPE).pid
        prompt('Recording', sentence, 'stop')
        kill(pid, SIGTERM)
    except:
        pass

# This is the CLI to drive the PTT recorder code.
if __name__ == "__main__":
    prefix, ext = ['sentence', 'wav']
    sentences = ["hello world", "klatu barada nikto", "fubar" ]
    print 'The following recordings must take less than 2 minutes of speech.'
    for sentence in sentences:
        collect(sentence)