Permalink
Browse files

Add base64 dit/dah sound generation script

  • Loading branch information...
Elucidation committed Sep 4, 2016
1 parent ccbc7e3 commit 1c2cf0a95d32c0a450f8c1d10babf860221180e5
Showing with 92 additions and 2 deletions.
  1. +21 −2 README.md
  2. +71 −0 morse_sound.py
View
@@ -1,8 +1,12 @@
# MorsePy
Simple set of python functions to translate alphanumeric characters to morse code and back
`morse.py` contains a simple set of python functions to translate alphanumeric characters to morse code and back
## Example output
`morse_sound.py` generates base 64 encoded strings of the dit and dah sounds for use in the HTML5 page.
The wave sounds are a 800Hz tone, 100ms and 300ms for dit and dah respectively, at a framerate of 4.410 Khz.
## Example output for morse.py
```
--------------------------------------------------------------
@@ -23,4 +27,19 @@ Original: WIERD CHARS # IS !@#$%^&*()<> BLOOP
and Back: WIERD CHARS IS !@$&() BLOOP
Valid? No
--------------------------------------------------------------
```
## Dit/Dah output
### Dit
```
UklGRpYDAABXQVZFZm10IBAAAAABAAEAOhEAAHQiAAACABAAZGF0YXIDAAAAACU6lTBz7sHAtdzCIYA/TBOg0B7FLv5cOcAxNvAPwTPbMiCzPwcV39FuxFv8hzjgMvzxacG52Zse2T++FifTycOK+qY39jPF89HBR9j+HPI/cBh51DHDufi6NgE1kPVGwt3WWxv+Px0a1NWmwur2wjUBNl73yMJ81bIZ/D/EGzfXKMId9b809jYt+VbDJNQEGO0/Zh2j2LbBUvOxM983/vrxw9TSURbRPwEfF9pSwYrxmTK9OND8mcSO0ZkUqD+WIJPb+sDF73Yxjzmi/kzFUtDdEnE/JSIX3a/AA+5IMFU6dAAMxiDPHREtP6wjot5ywEXsES8PO0cC2Mb3zVkP3D4sJTPgQsCL6tAtvTsZBLDH2sySDX4+pCbM4R/A1eiFLF486wWUyMbLyQsTPhQoauMJwCXnMSvzPLsHg8m+yv0Jmz18KQ/lAcB55dQpez2JCX7KwckvCBY92yq55gbA0+NvKPY9VguDy8/IXwaEPDEsaegYwDPiASdkPiANlMzox44E5jt+LR3qOMCZ4Islxj7oDq/NDce8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DJNQEGO0/Zh2j2LbBUvOxM983/vrxw9TSURbRPwEfF9pSwYrxmTK9OND8mcSO0ZkUqD+WIJPb+sDF73Yxjzmi/kzFUtDdEnE/JSIX3a/AA+5IMFU6dAAMxiDPHREtP6wjot5ywEXsES8PO0cC2Mb3zVkP3D4sJTPgQsCL6tAtvTsZBLDH2sySDX4+pCbM4R/A1eiFLF486wWUyMbLyQsTPhQoauMJwCXnMSvzPLsHg8m+yv0Jmz18KQ/lAcB55dQpez2JCX7KwckvCBY92yq55gbA0+NvKPY9VguDy8/IXwaEPDEsaegYwDPiASdkPiANlMzox44E5jt+LR3qOMCZ4Islxj7oDq/NDce8Ajw7wi7W62XABd8NJBo/rBDVzj7G6QCFOvwvk+2fwHndhyJhP20SBNB7xRf/wjkrMVTv5sDz2/sgmz8qFD7RxMRE/fM4UTIY8TrBddpnH8g/4xWC0hrEcvsYOGwz4PKcwf/YzR3oP5cXz9N8w6H5MTd9NKr0CsKR1y0c+j9HGSXV6sLR9z82gjV39oXCLNaHGv8/8RqE1mXCA/ZCNX02RfgNw8/U2xj3P5Yc7NftwTf0OjRsNxX6osN70ysX4T80HlzZgsFu8iYzUDjn+0PEMNJ1Fb4/zR/U2iTBp/AJMig5uf3xxO/QuxOOP14hVNzTwOPu4DD0OYz/q8W4z/0RUT/pItvdj8Aj7a4vtDpeAXHGis47EAY/bSRq31jAZ+tyLmc7MANDx2fNdg6uPukl/+AvwK/pLC0PPAIFIchPzK4MSj5dJ5riE8D859wrqjzTBgrJQcvjCtg9ySg85ATATuaEKjg9ogj/yT7KFglaPSwq4+UCwKXkIym6PXAK/8pGyUcHzzyHK5DnDsAC47knLz47DArMWsh2BTc82SxC6SfAZeFHJpc+BA4gzXnHpQOSOyEu+epNwM7fzSTxPsoPQM6kxtIB4jpgL7TsgMA+3ksjPz+NEWvP28UAAA==
```
View
@@ -0,0 +1,71 @@
# This file generates the morse code dits and dahs sounds
# as base 64 encoded strings for use in the HTML5/Javsacript page
import base64
from io import BytesIO
import numpy as np
import struct
import wave
# Code based off of this answer http://stackoverflow.com/a/33246354/2574639
def sine_samples(freq, duration, framerate=44100, amplitude=0.5):
# Get (sample rate * duration) samples on X axis
# (between freq oscillations of 2pi)
X = (2*np.pi*freq/framerate) * np.arange(framerate*duration)
# Get sine values for these X axis samples (as integers)
S = (amplitude*32767*np.sin(X)).astype(int)
# Pack integers as signed "short" integers (-32767 to 32767)
as_packed_bytes = (map(lambda v:struct.pack('h',v), S))
return as_packed_bytes
def output_wave(path, frames, framerate=44100):
with wave.open(path,'w') as output:
# Set parameters for output WAV file
# (1 channel, 2 bytes per sample, framerate, num frames, no compression, no compression)
output.setparams((1,2,framerate,0,'NONE','not compressed'))
output.writeframes(frames)
def output_sound(path, freq, dur, framerate=44100):
# join the packed bytes into a single bytes frame
frames = b''.join(sine_samples(freq,dur, framerate))
# output frames to file
output_wave(path, frames, framerate)
def output_base64(freq, dur, framerate=44100):
"""Return Base 64 encoded string of wave file"""
data_buffer = BytesIO()
output_sound(data_buffer, freq, dur, framerate)
return base64.b64encode(data_buffer.getvalue())
# We'll use a low 4.41khz framerate for our audio to reduce the string length
framerate = 4410
beep_freq = 800 # Hz
beep_duration = 0.1 # seconds
# # We could just write a wav file directly
# output_sound('dit.wav', 800, beep_duration, framerate)
# output_sound('dah.wav', 800, beep_duration*3, framerate)
# Instead of writing to file, and then reading from file to convert to base64
# We'll keep it in memory using ByteIO
dit_b64 = output_base64(beep_freq, beep_duration, framerate)
dah_b64 = output_base64(beep_freq, beep_duration*3, framerate)
# Write to text file for usage with html5 web audio
with open('dit_base64.txt','w') as f:
f.write(dit_b64.decode('utf8'))
with open('dah_base64.txt','w') as f:
f.write(dah_b64.decode('utf8'))
# Alternatively print to console
print("Dit (%d):\n%s" % (len(dit_b64), dit_b64.decode('utf8')))
print("Dah (%d):\n%s" % (len(dah_b64), dah_b64.decode('utf8')))
# These base64 strings can be directly used to make sound
# in an HTML5 page via javascript using:
#
# var snd = new Audio("data:audio/wav;base64,<base64_encoded_wav_string>")
# snd.play()

0 comments on commit 1c2cf0a

Please sign in to comment.