Python 3.x についての質問です。
マイクに一定以上の入力が一定時間無いと入力を中断し、
それまでに入力があった音声をスピーカーから出力するプログラムを考えております。
ですが、入力は受け付けるのですが、出力することが出来ません。
どなたかお知恵を貸していただけないでしょうか。
import pyaudio import math import struct import time Threshold = 50 SHORT_NORMALIZE = (1.0/32768.0) CHUNK = 8192 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 swidth = 2 TIMEOUT_LENGTH = 1 p=pyaudio.PyAudio() def rms(frame): count = len(frame) / swidth format = "%dh" % (count) shorts = struct.unpack(format, frame) sum_squares = 0.0 for sample in shorts: n = sample * SHORT_NORMALIZE sum_squares += n * n rms = math.pow(sum_squares / count, 0.5) return rms * 1000 stream=p.open( format = pyaudio.paInt16, channels = 1, rate = RATE, frames_per_buffer = CHUNK, input = True, output = True) # inputとoutputを同時にTrueにする while stream.is_active(): current = time.time() end = time.time() + TIMEOUT_LENGTH while current <= end: data = stream.read(CHUNK) if rms(data) >= Threshold: end = time.time() + TIMEOUT_LENGTH print(rms(data)) current = time.time() # output = stream.write(data) # ここでならスピーカーから音声が出力される(ここには書きたくない) output = stream.write(data) # ここでスピーカーから音声を出力したい stream.stop_stream() stream.close() p.terminate() print("Stop Streaming")
https://ja.stackoverflow.com/questions/88928
以下ご対応ください。
https://teratail.com/help#posted-otherservice
回答2件
あなたの回答
tips
プレビュー