Distribute pyjack with freewheel-related functionality
[jack_freewheel_button.git] / pyjack-0.5.2 / demos / capture.py
blobf791d803860b4ff17920627b9ef5d85510f90611
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 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)
22 jack.activate()
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
36 sec = 3.0
38 capture = numpy.zeros((2,int(Sr*sec)), 'f')
39 input = numpy.zeros((2,N), 'f')
40 output = numpy.zeros((2,N), 'f')
42 #time.sleep(1)
44 print "Capturing audio..."
46 i = 0
47 while i < capture.shape[1] - N:
48 try:
49 jack.process(output, capture[:,i:i+N])
50 i += N
51 except jack.InputSyncError:
52 print "Input Sync"
53 pass
54 except jack.OutputSyncError:
55 print "Output Sync"
56 pass
57 print ".",
59 print "\nPlaying back..."
61 i = 0
62 iS = 0
63 oS = 0
64 while i < capture.shape[1] - N:
65 try:
66 jack.process(capture[:,i:i+N], input)
67 i += N
68 except jack.InputSyncError:
69 iS += 1
70 pass
71 except jack.OutputSyncError:
72 iO += 1
73 pass
74 print "Finished with", iS, "input-off-sync &", oS, "output-off-sync"
76 jack.deactivate()
77 jack.detach()
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)