History

python/demos/demo_alsa.py: add example using alsaaudio (closes #72)

Paul Brossier committed 10 years ago

Commit 9492ec81 parent 8489d7b1 file · +45 −0Browse files

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

Unified · Split

python/demos/demo_alsa.py+45−0
@@ -0,0 +1,45 @@
1#! /usr/bin/env python
2
3import alsaaudio
4import numpy as np
5import aubio
6
7# constants
8samplerate = 44100
9win_s = 2048
10hop_s = win_s // 2
11framesize = hop_s
12
13# set up audio input
14recorder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
15recorder.setperiodsize(framesize)
16recorder.setrate(samplerate)
17recorder.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE)
18recorder.setchannels(1)
19
20# create aubio pitch detection (first argument is method, "default" is
21# "yinfft", can also be "yin", "mcomb", fcomb", "schmitt").
22pitcher = aubio.pitch("default", win_s, hop_s, samplerate)
23# set output unit (can be 'midi', 'cent', 'Hz', ...)
24pitcher.set_unit("Hz")
25# ignore frames under this level (dB)
26pitcher.set_silence(-40)
27
28print("Starting to listen, press Ctrl+C to stop")
29
30# main loop
31while True:
32 try:
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")
45 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