Hi I have a piece of code for recording audio using python and alsa audio module based on the example described here to test out some pitch detection algorithms. I was hoping if there's a way I could somehow record the audio from browser or media player into my program.
Here's my code :-
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)
Right now I am using a cable to direct audio out to input but the quality as expected is not very good.