Distribute pyjack with freewheel-related functionality
[jack_freewheel_button.git] / pyjack-0.5.2 / demos / just_capture.py
blob9f8b5d6fa736795bb97d6eaca0b79bb412f128a3
1 #!/usr/bin/python
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.
9 import numpy
10 import jack
11 import time
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)
22 jack.activate()
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
34 sec = 3.0
36 capture = numpy.zeros((2,int(Sr*sec)), 'f')
37 dummy = numpy.zeros((2,0), 'f')
39 #time.sleep(1)
41 print "Capturing audio..."
43 i = 0
44 while i < capture.shape[1] - N:
45 try:
46 jack.process(dummy, capture[:,i:i+N])
47 i += N
48 except jack.InputSyncError:
49 print "Input Sync"
50 pass
51 except jack.OutputSyncError:
52 print "Output Sync"
53 pass
54 print ".",
56 print "\nPlaying back..."
58 jack.unregister_port("in_1")
59 jack.unregister_port("in_2")
61 jack.deactivate()
63 jack.register_port("out_1", jack.IsOutput)
64 jack.register_port("out_2", jack.IsOutput)
66 jack.activate()
68 jack.connect(myname+":out_1", "system:playback_1")
69 jack.connect(myname+":out_2", "system:playback_2")
71 i = 0
72 iS = 0
73 oS = 0
74 while i < capture.shape[1] - N:
75 try:
76 jack.process(capture[:,i:i+N], dummy)
77 i += N
78 except jack.InputSyncError:
79 iS += 1
80 pass
81 except jack.OutputSyncError:
82 oS += 1
83 pass
84 print "Finished with", iS, "input-off-sync &", oS, "output-off-sync"
86 jack.deactivate()
87 jack.detach()
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)