morseman/morseman/main.py

60 lines
1.5 KiB
Python

import sys
from time import sleep
from typing import Sequence, Optional
from pyo import Server, Sine, Adsr
from morseman import morse
WPM = 20
STD_WORD_LENGTH = 26 # PARIS, 26 dits
DIT = 60 / WPM / 26 # seconds
DAH = 3 * DIT
CHAR_PAUSE = 3 * DIT
WORD_PAUSE = 7 * DIT
def play_morse(morse: Sequence[Optional[morse.MorseChar]], note_controller):
for char in morse:
if char is None:
print("None encountered in morse to play")
continue
if char.dashdots == "/":
sleep(WORD_PAUSE - CHAR_PAUSE)
continue
for ditdah in char.dashdots:
if ditdah == ".":
note_controller.dur = DIT
note_controller.play()
sleep(DIT + DIT)
elif ditdah == "-":
note_controller.dur = DAH
note_controller.play()
sleep(DAH + DIT)
sleep(CHAR_PAUSE - DIT)
def main():
# Creates a Server object with default arguments.
# See the manual about how to change the sampling rate, the buffer
# size, the number of channels or one of the other global settings.
s = Server()
# Boots the server. This step initializes audio and midi streams.
# Audio and midi configurations (if any) must be done before that call.
s.boot()
envelope = Adsr(attack=0.01, sustain=1, release=0.01)
# setExp method can be used to create exponential or logarithmic envelope.
envelope.setExp(0.75)
tone = Sine(600, mul=envelope).out()
# Starts the server. This step activates the server processing loop.
s.start()
for line in sys.stdin:
play_morse(morse.encode(line), envelope)