45 lines · 1.3 KB · Python
RawDownload
#! /usr/bin/env python import alsaaudioimport numpy as npimport aubio # constantssamplerate = 44100win_s = 2048hop_s = win_s // 2framesize = hop_s # set up audio inputrecorder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)recorder.setperiodsize(framesize)recorder.setrate(samplerate)recorder.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE)recorder.setchannels(1) # create aubio pitch detection (first argument is method, "default" is# "yinfft", can also be "yin", "mcomb", fcomb", "schmitt").pitcher = aubio.pitch("default", win_s, hop_s, samplerate)# set output unit (can be 'midi', 'cent', 'Hz', ...)pitcher.set_unit("Hz")# ignore frames under this level (dB)pitcher.set_silence(-40) print("Starting to listen, press Ctrl+C to stop") # main loopwhile True:    try:        # read data from audio input        _, data = recorder.read()        # convert data to aubio float samples        samples = np.fromstring(data, dtype=aubio.float_type)        # pitch of current frame        freq = pitcher(samples)[0]        # compute energy of current block        energy = np.sum(samples**2)/len(samples)        # do something with the results        print("{:10.4f} {:10.4f}".format(freq,energy))    except KeyboardInterrupt:        print("Ctrl+C pressed, exiting")        break

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t