您好我有一段代码用于使用python和alsa音频模块录制音频 基于here描述的示例来测试一些音调检测算法。我希望有一种方法可以将浏览器或媒体播放器的音频记录到我的程序中。
这是我的代码: -
import alsaaudio
import struct
from aubio.task import *
# Constants
CHANNELS = 1
INFORMAT = alsaaudio.PCM_FORMAT_FLOAT_LE
RATE = 44100
FRAMESIZE = 1024
PITCHALG = aubio_pitch_yin
PITCHOUT = aubio_pitchm_freq
# set up the audio input
recorder = alsaaudio.PCM(type = alsaaudio.PCM_CAPTURE)
recorder.setchannels(CHANNELS)
recorder.setrate(RATE)
recorder.setformat(INFORMAT)
recorder.setperiodsize(FRAMESIZE)
# set up pitch detect
detect = new_aubio_pitchdetection(FRAMESIZE, FRAMESIZE / 2, CHANNELS,
RATE, PITCHALG, PITCHOUT)
buf = new_fvec(FRAMESIZE, CHANNELS)
def pitches():
runflag = 1
while runflag :
# read data from audio input
[length, data] = recorder.read()
# convert to an array of floats
floats = struct.unpack('f' * FRAMESIZE,data)
# copy floats into structure
for i in range(len(floats)):
fvec_write_sample(buf, floats[i], 0, i)
# find pitch of audio frame
freq = aubio_pitchdetection(detect, buf)
现在我正在使用电缆将音频输出到输入,但是预期的质量不是很好。