Skip to content

Instantly share code, notes, and snippets.

@patientzero
Created November 11, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patientzero/ccdc6d77fc64d34a1172e1edd3c7c856 to your computer and use it in GitHub Desktop.
Save patientzero/ccdc6d77fc64d34a1172e1edd3c7c856 to your computer and use it in GitHub Desktop.
morse code to wavefile implemented in python
def morse_code_to_wav(morse_str, sr=8000, short_burst_len=0.1, long_burst_len=0.2,
pause_len=0.05, frequency=500, output_file_name=None):
"""
@param morse_str: str containing . for short burst - for long burst and space for pause between words
@param sr: sampling frequency of the desired wav output
@param output_file_name: path to wavfile output
"""
amplitude = np.iinfo(np.int16).max * 0.7
t_pause = np.linspace(0, pause_len, int(sr * pause_len))
t_pause[:] = 0
sig = np.empty(0, dtype=np.int16)
for c in morse_str:
if c == '.':
t = np.linspace(0, short_burst, int(sr * short_burst_len))
morse_sig = amplitude * np.sin(2. * np.pi * frequency * t)
sig = np.append(sig, np.append(morse_sig, t_pause))
if c == '-':
t = np.linspace(0, long_burst, int(sr * long_burst_len))
morse_sig = amplitude * np.sin(2. * np.pi * frequency * t)
sig = np.append(sig, np.append(morse_sig, t_pause))
if c == ' ':
t = np.linspace(0, long_burst, int(sr * long_burst_len))
t[:] = 0
morse_sig = np.append(morse_sig, t)
if output_file_name is not None:
wavfile.write(output_file_name, sr, sig.astype(np.int16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment