Last active
September 5, 2021 16:34
-
-
Save thotypous/6b93fc9ee52390d816273b8337831ad6 to your computer and use it in GitHub Desktop.
morse CW keyer using jack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import jack | |
import curses | |
import queue | |
from math import sin, pi | |
f = 700 # Hz | |
#duration = 0.240 # seconds | |
duration = 0.080 # seconds | |
latency = 0.020 # seconds | |
stdscr = curses.initscr() | |
stdscr.nodelay(True) | |
client = jack.Client("synth-cw") | |
audioport = client.outports.register("audio_out") | |
fs = client.samplerate | |
client.blocksize = int(latency * fs) | |
q = queue.Queue() | |
cw_left = 0 | |
silence_left = 0 | |
@client.set_process_callback | |
def process(frames): | |
global cw_left, silence_left | |
if cw_left == 0 and silence_left == 0: | |
try: | |
item = q.get_nowait() | |
except queue.Empty: | |
return | |
if item == '.': | |
cw_left = int(duration * fs) | |
silence_left = int(duration * fs) | |
elif item == '-': | |
cw_left = int(3 * duration * fs) | |
silence_left = int(duration * fs) | |
buf = memoryview(audioport.get_buffer()).cast('f') | |
i = 0 | |
while cw_left > 0 and i < len(buf): | |
buf[i] = sin(2*pi*cw_left*f/fs) | |
cw_left -= 1 | |
i += 1 | |
while silence_left > 0 and i < len(buf): | |
buf[i] = 0 | |
silence_left -= 1 | |
i += 1 | |
with client: | |
# comment the following 3 lines if you don't want to autoconnect to system's output | |
target_ports = client.get_ports(is_physical=True, is_input=True, is_audio=True) | |
audioport.connect(target_ports[0]) | |
audioport.connect(target_ports[1]) | |
while True: | |
c = stdscr.getch() | |
if c == ord('.') or c == ord('-'): | |
q.put(chr(c)) | |
elif c == ord('\n'): | |
stdscr.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment