2 # -*- coding: utf-8 -*-
3 # Capture 3 seconds of stereo audio from alsa_pcm:capture_1/2; then play it back.
5 # Copyright 2003, Andrew W. Schmeder
6 # This source code is released under the terms of the GNU Public License.
7 # See LICENSE for the full text of these terms.
13 jack
.attach("captest")
15 myname
= jack
.get_client_name()
16 print "Client:", myname
17 print "Jack ports (before):", jack
.get_ports()
19 jack
.register_port("in_1", jack
.IsInput
)
20 jack
.register_port("in_2", jack
.IsInput
)
24 print "Jack ports (after):", jack
.get_ports()
26 jack
.connect("system:capture_1", myname
+":in_1")
27 jack
.connect("system:capture_2", myname
+":in_2")
29 print jack
.get_connections(myname
+":in_1")
31 N
= jack
.get_buffer_size()
32 Sr
= float(jack
.get_sample_rate())
33 print "Buffer Size:", N
, "Sample Rate:", Sr
36 capture
= numpy
.zeros((2,int(Sr
*sec
)), 'f')
37 dummy
= numpy
.zeros((2,0), 'f')
41 print "Capturing audio..."
44 while i
< capture
.shape
[1] - N
:
46 jack
.process(dummy
, capture
[:,i
:i
+N
])
48 except jack
.InputSyncError
:
51 except jack
.OutputSyncError
:
56 print "\nPlaying back..."
58 jack
.unregister_port("in_1")
59 jack
.unregister_port("in_2")
63 jack
.register_port("out_1", jack
.IsOutput
)
64 jack
.register_port("out_2", jack
.IsOutput
)
68 jack
.connect(myname
+":out_1", "system:playback_1")
69 jack
.connect(myname
+":out_2", "system:playback_2")
74 while i
< capture
.shape
[1] - N
:
76 jack
.process(capture
[:,i
:i
+N
], dummy
)
78 except jack
.InputSyncError
:
81 except jack
.OutputSyncError
:
84 print "Finished with", iS
, "input-off-sync &", oS
, "output-off-sync"
89 import numpy
, scipy
.io
.wavfile
90 toSave
= numpy
.array((2**15-1)*capture
.transpose(),dtype
="int16")
91 scipy
.io
.wavfile
.write("output.wav", int(Sr
), toSave
)