对检测出的声音Python的录制音频 [英] Python record audio on detected sound

查看:233
本文介绍了对检测出的声音Python的录制音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在后台运行一个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

沉默这么多秒后停止录制

stop recording after so many seconds of silence

保持音频录音后,这么多秒。

keep recording for so many seconds after audio

第2阶段:输入数据到MySQL数据库搜索记录

Phase 2: input data into MySQL database to search the recordings

我在看类似的文件结构

/home/Recodings/2013/8/23/12-33.wav将是transmision的记录上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

我已经使用了code从

I have used the code from

检测和记录与Python 完善

我是有点茫然的从这里到现在,去和一点点指导将大大AP preciated

I am at a bit of a loss where to go from here now and a little guidance would be greatly appreciated

感谢您

推荐答案

前一段时间我写了一些步骤

Some time ago I wrote some of the steps


  • 在N%栅极阈值录制音频输入

  • Record audio input on a n% gate threshold

答:启动布尔变量类型沉默,你可以计算 RMS 来决定,如果沉默是真还是假假的,组一个RMS阈值

A: Start a Boolean variable type for "Silence" and you can calculate RMS to decide if Silence is true or False, Set one RMS Threshold


  • 停止沉默这么多秒后录制

  • stop recording after so many seconds of silence

A:你需要计算一个超时,因为它得到你想要的帧速率,块大小和多少秒呢,来计算你的超时时间,(帧率/块* Max_Seconds)

A: Do you need calculate one timeout, for it get the Frame Rate, Chunk Size and how many seconds do you want, to calculate your timeout make (FrameRate / chunk * Max_Seconds)


  • 保持音频录音后,这么多秒

  • keep recording for so many seconds after audio

答:如果沉默是假的==(RMS>阈值)获得音频数据的最后一块(LastBlock),并只保留记录:-)

A: If Silence is false == (RMS > Threshold) get the last chunk of data of audio (LastBlock) and just keep record :-)


  • 第2阶段:输入数据到MySQL数据库搜索记录

  • Phase 2: input data into MySQL database to search the recordings

答:这一步是给你

来源$ C ​​$ C:

Source code:

import pyaudio
import math
import struct
import wave



#Assuming Energy threshold upper than 30 dB
Threshold = 30

SHORT_NORMALIZE = (1.0/32768.0)
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
swidth = 2
Max_Seconds = 10
TimeoutSignal=((RATE / chunk * Max_Seconds) + 2)
silence = True
FileNameTmp = '/home/Recodings/2013/8/23/12-33.wav'
Time=0
all =[]

def GetStream(chunk):
    return stream.read(chunk)



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 WriteSpeech(WriteData):
    stream.stop_stream()
    stream.close()
    p.terminate()
    wf = wave.open(FileNameTmp, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(WriteData)
    wf.close()



def KeepRecord(TimeoutSignal, LastBlock):


    all.append(LastBlock)
    for i in range(0, TimeoutSignal):
        try:
            data = GetStream(chunk)
        except:
            continue
        #I chage here (new Ident)
        all.append(data)

    print "end record after timeout";
    data = ''.join(all)
    print "write to File";
    WriteSpeech(data)
    silence = True
    Time=0
    listen(silence,Time)     

def listen(silence,Time):
    print "waiting for Speech"
    while silence:

        try:

            input = GetStream(chunk)

        except:

            continue


        rms_value = rms(input)

        if (rms_value > Threshold):

            silence=False

            LastBlock=input

            print "hello ederwander I'm Recording...."
            KeepRecord(TimeoutSignal, LastBlock)

        Time = Time + 1

        if (Time > TimeoutSignal):
            print "Time Out No Speech Detected"
            sys.exit()






p = pyaudio.PyAudio()

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




listen(silence,Time) 

这篇关于对检测出的声音Python的录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆