History

python/demos/demo_pyaudio.py: added simple demo for pyaudio, see #6, closes #78, thanks to @jhoelzl and @notalentgeek

Paul Brossier committed 9 years ago

Commit e7da8ba1 parent 1f43c291 file · +66 −0Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/demos/demo_pyaudio.py+66−0
@@ -0,0 +1,66 @@
1+#! /usr/bin/env python
2+
3+import pyaudio
4+import sys
5+import numpy as np
6+from aubio import pitch, sink
7+
8+# initialise pyaudio
9+p = pyaudio.PyAudio()
10+
11+# open stream
12+buffer_size = 1024
13+pyaudio_format = pyaudio.paFloat32
14+n_channels = 1
15+samplerate = 44100
16+stream = p.open(format=pyaudio_format,
17+ channels=n_channels,
18+ rate=samplerate,
19+ input=True,
20+ frames_per_buffer=buffer_size)
21+
22+if len(sys.argv) > 1:
23+ # record 5 seconds
24+ output_filename = sys.argv[1]
25+ record_duration = 5 # exit 1
26+ outputsink = sink(sys.argv[1], samplerate)
27+ total_frames = 0
28+else:
29+ # run forever
30+ outputsink = None
31+ record_duration = None
32+
33+# setup pitch
34+tolerance = 0.8
35+win_s = 4096 # fft size
36+hop_s = buffer_size # hop size
37+pitch_o = pitch("default", win_s, hop_s, samplerate)
38+pitch_o.set_unit("midi")
39+pitch_o.set_tolerance(tolerance)
40+
41+print("*** starting recording")
42+while True:
43+ try:
44+ audiobuffer = stream.read(buffer_size)
45+ signal = np.fromstring(audiobuffer, dtype=np.float32)
46+
47+ pitch = pitch_o(signal)[0]
48+ confidence = pitch_o.get_confidence()
49+
50+ print("{} / {}".format(pitch,confidence))
51+
52+ if outputsink:
53+ outputsink(signal, len(signal))
54+
55+ if record_duration:
56+ total_frames += len(signal)
57+ if record_duration * samplerate < total_frames:
58+ break
59+ except KeyboardInterrupt:
60+ print("*** Ctrl+C pressed, exiting")
61+ break
62+
63+print("*** done recording")
64+stream.stop_stream()
65+stream.close()
66+p.terminate()

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