首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Aubio / Alsaaudio配置正确

Aubio / Alsaaudio配置正确
EN

Stack Overflow用户
提问于 2018-03-20 17:23:21
回答 1查看 116关注 0票数 2

我正在尝试使用aubio和python做一个学校项目,目标是:检测某人何时发出两个声音,每个声音的长度为2秒,并且它们之间的间隔最大为3秒。第二个需要比第一个更高。当满足这些条件时,程序会发送一个Wake-On-Lan包(在当前代码中没有实现)。

代码语言:javascript
复制
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()

然而,使用这个代码,我在声音和它们的检测之间得到了很大的延迟,我认为这与记录器每0.1s只被调用一次有关,但我找不到如何为aubio提供正确的参数。

有没有人知道如何配置常量才能正常工作?

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-03-25 01:40:27

找出导致这个错误的原因,我需要将设置音频输入的代码放在get_audio函数中,这样每次都会更新

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49380390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档