Kivy / Audiostream麦克风输入数据格式

时间:2016-02-16 22:00:29

标签: audio kivy

我正在为Kivy玩一些Audiostream包的基础知识。

我想制作一个简单的在线输入滤波器输出系统,例如,接收麦克风数据,强制使用带通滤波器,发送给扬声器。

但是,我似乎无法弄清楚麦克风输入的数据格式或操作方式。在下面的代码中,buf是类型字符串,但是如何从中获取数据以便以这种方式操作它[即function(buf)]做一个类似带通滤波器的事情?

该代码目前仅用于将麦克风输入直接发送到扬声器。

感谢。

from time import sleep
from audiostream import get_input
from audiostream import get_output, AudioSample

#get speakers, create sample and bind to speakers
stream = get_output(channels=2, rate=22050, buffersize=1024)
sample = AudioSample()
stream.add_sample(sample)


#define what happens on mic input with arg as buffer
def mic_callback(buf):
    print 'got', len(buf)
    #HERE: How do I manipulate buf?
    #modified_buf = function(buf)
    #sample.write(modified_buf)
    sample.write(buf)


# get the default audio input (mic on most cases)
mic = get_input(callback=mic_callback)
mic.start()
sample.play()
sleep(3)  #record for 3 seconds
mic.stop()
sample.stop()

1 个答案:

答案 0 :(得分:1)

缓冲区由需要解释为signed short的字节组成。您可以使用struct或array模块来获取值。在您的示例中,您有2个通道(L / R)。让我们假设您希望将正确的频道音量降低20%(仅为正确频道的原始声音的80%)

from array import array

def mic_callback(buf):
    # convert our byte buffer into signed short array
    values = array("h", buf)

    # get right values only
    r_values = values[1::2]

    # reduce by 20%
    r_values = map(lambda x: x * 0.8, r_values)

    # you can assign only array for slice, not list
    # so we need to convert back list to array
    values[1::2] = array("h", r_values)

    # convert back the array to a byte buffer for speaker
    sample.write(values.tostring())
相关问题