14 recorder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
15 recorder.setperiodsize(framesize)
16 recorder.setrate(samplerate)
17 recorder.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE)
18 recorder.setchannels(1)
20 # create aubio pitch detection (first argument is method, "default" is
21 # "yinfft", can also be "yin", "mcomb", fcomb", "schmitt").
22 pitcher = aubio.pitch("default", win_s, hop_s, samplerate)
23 # set output unit (can be 'midi', 'cent', 'Hz', ...)
24 pitcher.set_unit("Hz")
25 # ignore frames under this level (dB)
26 pitcher.set_silence(-40)
28 print("Starting to listen, press Ctrl+C to stop")
33 # read data from audio input
34 _, data = recorder.read()
35 # convert data to aubio float samples
36 samples = np.fromstring(data, dtype=aubio.float_type)
37 # pitch of current frame
38 freq = pitcher(samples)[0]
39 # compute energy of current block
40 energy = np.sum(samples**2)/len(samples)
41 # do something with the results
42 print("{:10.4f} {:10.4f}".format(freq,energy))
43 except KeyboardInterrupt:
44 print("Ctrl+C pressed, exiting")