forked from seadeef/MorseAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.py
63 lines (53 loc) · 2.33 KB
/
encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from pydub import AudioSegment
from pydub import generators
ascii_dict = {"A": ".-", "B": "-...", "C": "-.-.",
"D": "-..", "E": ".", "F": "..-.",
"G": "--.", "H": "....", "I": "..",
"J": ".---", "K": "-.-", "L": ".-..",
"M": "--", "N": "-.", "O": "---",
"P": ".--.", "Q": "--.-", "R": ".-.",
"S": "...", "T": "-", "U": "..-",
"V": "...-", "W": ".--", "X": "-..-",
"Y": "-.--", "Z": "--..", "1": ".----",
"2": "..---", "3": "...--", "4": "....-",
"5": ".....", "6": "-....", "7": "--...",
"8": "---..", "9": "----.", "0": "-----",
", ": "--..--", ".": ".-.-.-", "?": "..--..",
"/": "-..-.", "-": "-....-", "(": "-.--.", ")": "-.--.-"}
class Encode:
def __init__(self, path, format, message, wpm):
self.tone = generators.Sine(freq=641) # Make a sine wave generator
self.dit = int(1000*(60/(int(wpm)*50))) # Calculate dit length
self.path = path
self.format = format
self.message = message.upper()
def ascii_to_morse(self):
morse = []
# Convert message to morse
for char in self.message:
if char == " ":
morse.append("/")
else:
morse.append(ascii_dict[char])
return morse
def morse_to_audio(self, morse):
audio = AudioSegment.empty()
# Add appropriate segments of "beep" and silence to audio
for letter in morse:
if letter != "/":
for char in letter:
if char == ".":
audio += self.tone.to_audio_segment(duration=self.dit)
elif char == "-":
audio += self.tone.to_audio_segment(duration=self.dit*3)
audio += AudioSegment.silent(duration=self.dit)
audio += AudioSegment.silent(duration=self.dit*3)
else:
audio += AudioSegment.silent(duration=self.dit*7)
return audio
def audio_to_file(self, audio):
audio.export(self.path, format=self.format) # Export audio to a sound file
def encode(self):
morse = self.ascii_to_morse()
audio = self.morse_to_audio(morse)
self.audio_to_file(audio)