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
3import pyaudio
4import sys
5import numpy as np
6from aubio import pitch, sink
7
8# initialise pyaudio
9p = pyaudio.PyAudio()
10
11# open stream
12buffer_size = 1024
13pyaudio_format = pyaudio.paFloat32
14n_channels = 1
15samplerate = 44100
16stream = p.open(format=pyaudio_format,
17 channels=n_channels,
18 rate=samplerate,
19 input=True,
20 frames_per_buffer=buffer_size)
21
22if 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
28else:
29 # run forever
30 outputsink = None
31 record_duration = None
32
33# setup pitch
34tolerance = 0.8
35win_s = 4096 # fft size
36hop_s = buffer_size # hop size
37pitch_o = pitch("default", win_s, hop_s, samplerate)
38pitch_o.set_unit("midi")
39pitch_o.set_tolerance(tolerance)
40
41print("*** starting recording")
42while 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
63print("*** done recording")
64stream.stop_stream()
65stream.close()
66p.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