Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Aubio/Alsaaudio的正确配置_Python_Alsa_Aubio - Fatal编程技术网

Python Aubio/Alsaaudio的正确配置

Python Aubio/Alsaaudio的正确配置,python,alsa,aubio,Python,Alsa,Aubio,我正在尝试将aubio和python用于一个学校项目,目标是:检测某人何时发出两个声音,每个声音的长度为2秒,间隔最大为3秒。第二个需要比第一个高。当满足这些条件时,程序将发送Lan唤醒包(当前代码中未实现) import-alsaaudio 将numpy作为np导入 进口奥比奥 导入时间 导入线程 类音频监视程序: #常数 采样率=44100 win_s=2048 hop_s=win_s//2 framesize=hop\u s nb_样品=20 音调持续时间=2.0 每采样=音调持续时间/n

我正在尝试将aubio和python用于一个学校项目,目标是:检测某人何时发出两个声音,每个声音的长度为2秒,间隔最大为3秒。第二个需要比第一个高。当满足这些条件时,程序将发送Lan唤醒包(当前代码中未实现)

import-alsaaudio
将numpy作为np导入
进口奥比奥
导入时间
导入线程
类音频监视程序:
#常数
采样率=44100
win_s=2048
hop_s=win_s//2
framesize=hop\u s
nb_样品=20
音调持续时间=2.0
每采样=音调持续时间/nb采样
音调最大间隔=3.0
音差比=2
定义初始化(自):
self.last_frequencies=np.zero(音频观察者.nb_样本)
self.last_energies=np.zero(音频观察者.nb_样本)
自检测音=0
#设置音频输入
记录器=alsaaudio.PCM(类型=alsaaudio.PCM\u捕获)
recorder.setperiodsize(音频监视程序.framesize)
记录器.设定速率(音频监视程序.采样器)
recorder.setformat(alsaaudio.PCM\u格式\u浮点值)
记录器。设置通道(1)
self.recorder=记录器
pitcher=aubio.pitch(“默认”,Audio\u watcher.win\u s,Audio\u watcher.hop\u s,Audio\u watcher.samplerate)
投手设置单位(“Hz”)
投手,请安静(-40)
self.pitcher=投手
#过滤器
f=aubio.数字滤波器(7)
f、 设置加权(音频观察者、采样器)
self.f=f
def get_音频(自):
#从音频输入读取和转换数据
_,data=self.recorder.read()
samples=np.fromstring(数据,dtype=aubio.float\u类型)
过滤的样本=self.f(样本)
打印(已过滤的样本)
#当前帧的基音和能量
freq=自投(过滤样本)[0]
打印(频率)
self.last_频率=np.roll(self.last_频率,1)
自上次_频率[0]=频率
self.last_energies=np.roll(self.last_energies,1)
自上次能量[0]=np.和(过滤样本**2)/len(过滤样本)
Timer(Audio\u watcher.per\u sampling,self.get\u Audio.start())
def reset_检测到_音调():
自检测音=0
def检测音(自):
标准上次=np.std(自上次频率)
如果std_最后为0:
平均频率=np.平均值(自上次频率)
如果self.detected_tone==0:
自检测音调=平均频率
threading.Timer(Audio\u watcher.tone\u max\u interval,self.reset\u detected\u tone).start()
elif mean_freq>Audio_watcher.tone_diff_ratio*自检测音调:
打印('wol')
Timer(Audio\u watcher.tone\u duration,self.detect\u tone.start())
aw=音频观察者()
aw.get_audio()
aw.detect_tone()
然而,通过这段代码,我在声音和它们的检测之间获得了很大的延迟,我认为这与录音机每0.1s只被调用一次有关,但我找不到如何为aubio提供正确的参数。
有人知道如何配置常量使其工作吗?

非常感谢

找到了导致此错误的原因,我需要将设置音频输入的代码放入get_audio函数中,以便每次更新

import alsaaudio
import numpy as np
import aubio
import time
import threading



class Audio_watcher:
    # constants
    samplerate = 44100
    win_s = 2048
    hop_s = win_s // 2
    framesize = hop_s
    nb_samples = 20
    tone_duration = 2.0
    per_sampling = tone_duration / nb_samples
    tone_max_interval = 3.0
    tone_diff_ratio = 2


    def __init__(self):
        self.last_frequencies = np.zeros(Audio_watcher.nb_samples)
        self.last_energies = np.zeros(Audio_watcher.nb_samples)
        self.detected_tone = 0

        # set up audio input
        recorder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
        recorder.setperiodsize(Audio_watcher.framesize)
        recorder.setrate(Audio_watcher.samplerate)
        recorder.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE)
        recorder.setchannels(1)
        self.recorder = recorder

        pitcher = aubio.pitch("default", Audio_watcher.win_s, Audio_watcher.hop_s, Audio_watcher.samplerate)
        pitcher.set_unit("Hz")
        pitcher.set_silence(-40)
        self.pitcher = pitcher
        # A filter
        f = aubio.digital_filter(7)
        f.set_a_weighting(Audio_watcher.samplerate)
        self.f = f


    def get_audio(self):
        # read and convert data from audio input
        _, data = self.recorder.read()
        samples = np.fromstring(data, dtype=aubio.float_type)
        filtered_samples = self.f(samples)
        print(filtered_samples)

        # pitch and energy of current frame
        freq = self.pitcher(filtered_samples)[0]
        print(freq)
        self.last_frequencies = np.roll(self.last_frequencies, 1)
        self.last_frequencies[0] = freq
        self.last_energies = np.roll(self.last_energies, 1)
        self.last_energies[0] = np.sum(filtered_samples**2)/len(filtered_samples)

        threading.Timer(Audio_watcher.per_sampling, self.get_audio).start()


    def reset_detected_tone():
        self.detected_tone = 0


    def detect_tone(self):
        std_last = np.std(self.last_frequencies)
        if std_last <= 200 and std_last > 0:
            mean_freq = np.mean(self.last_frequencies)
            if self.detected_tone == 0:
                self.detected_tone = mean_freq
                threading.Timer(Audio_watcher.tone_max_interval, self.reset_detected_tone).start()
            elif mean_freq > Audio_watcher.tone_diff_ratio * self.detected_tone:
                print('wol')

        threading.Timer(Audio_watcher.tone_duration, self.detect_tone).start()



aw = Audio_watcher()
aw.get_audio()
aw.detect_tone()