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 print jack
.get_ports()
17 jack
.register_port("in_1", jack
.IsInput
)
18 jack
.register_port("in_2", jack
.IsInput
)
19 jack
.register_port("out_1", jack
.IsOutput
)
20 jack
.register_port("out_2", jack
.IsOutput
)
24 print jack
.get_ports()
26 jack
.connect("system:capture_1", "captest:in_1")
27 jack
.connect("system:capture_2", "captest:in_2")
28 jack
.connect("captest:out_1", "system:playback_1")
29 jack
.connect("captest:out_2", "system:playback_2")
31 print jack
.get_connections("captest:in_1")
33 N
= jack
.get_buffer_size()
34 Sr
= float(jack
.get_sample_rate())
35 print "Buffer Size:", N
, "Sample Rate:", Sr
38 capture
= numpy
.zeros((2,int(Sr
*sec
)), 'f')
39 input = numpy
.zeros((2,N
), 'f')
40 output
= numpy
.zeros((2,N
), 'f')
44 print "Capturing audio..."
47 while i
< capture
.shape
[1] - N
:
49 jack
.process(output
, capture
[:,i
:i
+N
])
51 except jack
.InputSyncError
:
54 except jack
.OutputSyncError
:
59 print "\nPlaying back..."
64 while i
< capture
.shape
[1] - N
:
66 jack
.process(capture
[:,i
:i
+N
], input)
68 except jack
.InputSyncError
:
71 except jack
.OutputSyncError
:
74 print "Finished with", iS
, "input-off-sync &", oS
, "output-off-sync"
79 import numpy
, scipy
.io
.wavfile
80 toSave
= numpy
.array((2**15-1)*capture
.transpose(),dtype
="int16")
81 scipy
.io
.wavfile
.write("output.wav", int(Sr
), toSave
)