如何使用哨兵手动停止pyaudio的录制

时间:2019-07-13 06:52:15

标签: python audio pyaudio voice-recording

当前,在编译时,该程序仅输出以下内容:

Please enter 1 at any time to stop the recording process: * recording Enter your decision now:

,然后程序停止。如果用户输入“ 1”,则我认为该程序应停止for循环并打印“完成记录”。但是,相反,该程序只是继续运行for循环并重复打印:“立即输入您的决定”。为什么会这样呢?以及如何手动停止python中的记录?

import pyaudio
import wave
import pprint
import argparse
import datetime
import io
import json
import os
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from google.cloud import storage
import sys
from oauth2client.service_account import ServiceAccountCredentials

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 7200
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("Please enter 1 at any time to stop the recording process: ")

print("* recording")

frames = []

userDecision = 0

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)
    #What I am currently trying so far, you see below...this doesn't work...
    userDecision = input("Enter your decision now: ")
    if userDecision == 1:
        break
    elif userDecision == 0:
        continue

print("* done recording")



stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

0 个答案:

没有答案