3 # Use pyaudio to open the microphone and run aubio.pitch on the stream of
4 # incoming samples. If a filename is given as the first argument, it will
5 # record 5 seconds of audio to this location. Otherwise, the script will
6 # run until Ctrl+C is pressed.
9 # $ ./python/demos/demo_pyaudio.py
10 # $ ./python/demos/demo_pyaudio.py /tmp/recording.wav
22 pyaudio_format = pyaudio.paFloat32
25 stream = p.open(format=pyaudio_format,
29 frames_per_buffer=buffer_size)
33 output_filename = sys.argv[1]
34 record_duration = 5 # exit 1
35 outputsink = aubio.sink(sys.argv[1], samplerate)
40 record_duration = None
44 win_s = 4096 # fft size
45 hop_s = buffer_size # hop size
46 pitch_o = aubio.pitch("default", win_s, hop_s, samplerate)
47 pitch_o.set_unit("midi")
48 pitch_o.set_tolerance(tolerance)
50 print("*** starting recording")
53 audiobuffer = stream.read(buffer_size)
54 signal = np.fromstring(audiobuffer, dtype=np.float32)
56 pitch = pitch_o(signal)[0]
57 confidence = pitch_o.get_confidence()
59 print("{} / {}".format(pitch,confidence))
62 outputsink(signal, len(signal))
65 total_frames += len(signal)
66 if record_duration * samplerate < total_frames:
68 except KeyboardInterrupt:
69 print("*** Ctrl+C pressed, exiting")
72 print("*** done recording")