Python 在检测到的声音上录制音频 [英] Python record audio on detected sound
问题描述
我希望在后台运行一个 python 脚本,并在麦克风的阈值达到某个点时使用 pyaudio 录制声音文件.这是用于双向无线电网络上的监视器.因此我们只想记录传输的音频.
I am looking to have a python script run in the background and use pyaudio to record sound files when the threshold of the microphone has reached a certain point. This is for a monitor on a two way radio network. So hence we only want to record transmitted audio.
考虑的任务:
在 n% 门限阈值上记录音频输入
Record audio input on a n% gate threshold
静默几秒后停止录音
在音频结束后继续录音几秒钟
keep recording for so many seconds after audio
阶段 2:将数据输入 MySQL 数据库以搜索录音
Phase 2: input data into MySQL database to search the recordings
我正在查看类似的文件结构
I am looking at a file structure of the similar
/home/Recodings/2013/8/23/12-33.wav 将是 23/08/2013 @ 12:33.wav 传输的录音
/home/Recodings/2013/8/23/12-33.wav would be a recording of the transmision on 23/08/2013 @ 12:33.wav
我使用了来自
我现在有点不知所措,不胜感激
I am at a bit of a loss where to go from here now and a little guidance would be greatly appreciated
谢谢
推荐答案
当前的最佳答案有点过时,仅适用于 python 2.这是为 python 3 更新的版本.它将函数包装到类中并打包所有内容成一个简单易用的版本.请注意,最佳答案与我的脚本之间存在一个关键区别:
The current top answer is a bit outdated and only works for python 2. Here is a version updated for python 3. It wraps the functions into classes and packages everything into one simple easy-to-use version. Note that there is one key difference between the top answer and my script:
顶部的脚本记录一个文件然后停止,而我的脚本会在检测到噪音时继续记录,并将记录转储到一个目录中.
The script at the top records for one file and then stops, while my script keeps recording whenever noise is detected and dumps the recordings into a directory as it goes.
两个脚本的主要思想非常相似:
The main idea for both scripts are pretty similar:
第 1 步:监听"直到 rms 大于阈值
Step 1: 'Listen' until rms becomes greater than the threshold
第 2 步:开始录制,设置何时停止录制的计时器,== TIMEOUT_LENGTH
Step 2: Start recording, set a timer for when to stop recording, == TIMEOUT_LENGTH
第 3 步:如果 rms 在计时器超时之前再次突破阈值,则重置计时器
Step 3: If the rms breaks threshold again before the timer times out reset the timer
第 4 步:现在计时器已过期,将录音写入目录并返回第 1 步
Step 4: Now that the timer is expired, write the recording to a directory and go back to step 1
import pyaudio
import math
import struct
import wave
import time
import os
Threshold = 10
SHORT_NORMALIZE = (1.0/32768.0)
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
swidth = 2
TIMEOUT_LENGTH = 5
f_name_directory = r'C:UsersJasonPyCharmProjectsAutoRecorder
ecords'
class Recorder:
@staticmethod
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
def __init__(self):
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=chunk)
def record(self):
print('Noise detected, recording beginning')
rec = []
current = time.time()
end = time.time() + TIMEOUT_LENGTH
while current <= end:
data = self.stream.read(chunk)
if self.rms(data) >= Threshold: end = time.time() + TIMEOUT_LENGTH
current = time.time()
rec.append(data)
self.write(b''.join(rec))
def write(self, recording):
n_files = len(os.listdir(f_name_directory))
filename = os.path.join(f_name_directory, '{}.wav'.format(n_files))
wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(self.p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(recording)
wf.close()
print('Written to file: {}'.format(filename))
print('Returning to listening')
def listen(self):
print('Listening beginning')
while True:
input = self.stream.read(chunk)
rms_val = self.rms(input)
if rms_val > Threshold:
self.record()
a = Recorder()
a.listen()
这篇关于Python 在检测到的声音上录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!