|
9 | 9 | acoustical feedback! |
10 | 10 |
|
11 | 11 | """ |
12 | | -import jack |
13 | | -import threading |
14 | 12 | import sys |
15 | | -import os |
16 | 13 | import signal |
| 14 | +import os |
| 15 | +import jack |
| 16 | +import threading |
| 17 | + |
| 18 | +if sys.version_info < (3, 0): |
| 19 | + # In Python 2.x, event.wait() cannot be interrupted with Ctrl+C. |
| 20 | + # Therefore, we disable the whole KeyboardInterrupt mechanism. |
| 21 | + # This will not close the JACK client properly, but at least we can |
| 22 | + # use Ctrl+C. |
| 23 | + signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 24 | +else: |
| 25 | + # If you use Python 3.x, everything is fine. |
| 26 | + pass |
17 | 27 |
|
18 | 28 | argv = iter(sys.argv) |
19 | 29 | # By default, use script name without extension as client name: |
@@ -54,44 +64,37 @@ def shutdown(status, reason, userdata): |
54 | 64 | client.inports.register("input_{0}".format(number)) |
55 | 65 | client.outports.register("output_{0}".format(number)) |
56 | 66 |
|
57 | | -# Tell the JACK server that we are ready to roll. |
58 | | -# Our process() callback will start running now. |
59 | | -client.activate() |
| 67 | +with client: |
| 68 | + # When entering this with-statement, client.activate() is called. |
| 69 | + # This tells the JACK server that we are ready to roll. |
| 70 | + # Our process() callback will start running now. |
60 | 71 |
|
61 | | -# Connect the ports. You can't do this before the client is activated, |
62 | | -# because we can't make connections to clients that aren't running. |
63 | | -# Note the confusing (but necessary) orientation of the driver backend |
64 | | -# ports: playback ports are "input" to the backend, and capture ports |
65 | | -# are "output" from it. |
| 72 | + # Connect the ports. You can't do this before the client is activated, |
| 73 | + # because we can't make connections to clients that aren't running. |
| 74 | + # Note the confusing (but necessary) orientation of the driver backend |
| 75 | + # ports: playback ports are "input" to the backend, and capture ports |
| 76 | + # are "output" from it. |
66 | 77 |
|
67 | | -capture = client.get_ports(is_physical=True, is_output=True) |
68 | | -if not capture: |
69 | | - raise RuntimeError("No physical capture ports") |
| 78 | + capture = client.get_ports(is_physical=True, is_output=True) |
| 79 | + if not capture: |
| 80 | + raise RuntimeError("No physical capture ports") |
70 | 81 |
|
71 | | -for src, dest in zip(capture, client.inports): |
72 | | - client.connect(src, dest) |
| 82 | + for src, dest in zip(capture, client.inports): |
| 83 | + client.connect(src, dest) |
73 | 84 |
|
74 | | -playback = client.get_ports(is_physical=True, is_input=True) |
75 | | -if not playback: |
76 | | - raise RuntimeError("No physical playback ports") |
| 85 | + playback = client.get_ports(is_physical=True, is_input=True) |
| 86 | + if not playback: |
| 87 | + raise RuntimeError("No physical playback ports") |
77 | 88 |
|
78 | | -for src, dest in zip(client.outports, playback): |
79 | | - client.connect(src, dest) |
| 89 | + for src, dest in zip(client.outports, playback): |
| 90 | + client.connect(src, dest) |
80 | 91 |
|
81 | | -if sys.version_info < (3, 0): |
82 | | - # In Python 2.x, event.wait() cannot be interrupted with Ctrl+C. |
83 | | - # Therefore, we disable the whole KeyboardInterrupt mechanism. |
84 | | - # This will not close the JACK client properly, but at least we can |
85 | | - # use Ctrl+C. |
86 | | - signal.signal(signal.SIGINT, signal.SIG_DFL) |
87 | | -else: |
88 | | - # If you use Python 3.x, everything is fine. |
89 | | - pass |
90 | | - |
91 | | -try: |
92 | 92 | print("Press Ctrl+C to stop") |
93 | | - event.wait() |
94 | | -except KeyboardInterrupt: |
95 | | - print("\nInterrupted by user") |
96 | | - |
97 | | -client.close() |
| 93 | + try: |
| 94 | + event.wait() |
| 95 | + except KeyboardInterrupt: |
| 96 | + print("\nInterrupted by user") |
| 97 | + |
| 98 | +# When the above with-statement is left (either because the end of the |
| 99 | +# code block is reached, or because an exception was raised inside), |
| 100 | +# client.deactivate() and client.close() are called automatically. |
0 commit comments