Post by microfarad on Feb 20, 2011 20:47:53 GMT
Morse code generator written in Python... Run Morse.py, and type in a message to hear it as Morse code. Requires Python 2.7, I plan to port this to 3.x eventually. Only works on Windows, but there are ways to get it to work in other operating systems, this was just convenient for me.
We should invent our own kind of code. Maybe with 2 or 3 different tones. Just something to think about...
Morse.py
MicroPy_Music.py
We should invent our own kind of code. Maybe with 2 or 3 different tones. Just something to think about...
Morse.py
from MicroPy_Music import *
import string
import winsound
table = {'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 Morse():
def __init__(self):
self.Settings(.1,.4,.05,.1,.2,440,.5)
def Settings(self,dit,dah,pause,gap,space,freq,amplitude):
tone = PWWave(PWWaves['sine'])
self.dit = Snippet()
self.dit.Cycle(tone,freq,0,amplitude,samplerate)
self.dit.Loop(freq*dit)
self.dah = Snippet()
self.dah.Cycle(tone,freq,0,amplitude,samplerate)
self.dah.Loop(freq*dah)
self.pause = Snippet()
self.pause.Add(0)
self.pause.Loop(samplerate*pause)
self.gap = Snippet()
self.gap.Add(0)
self.gap.Loop(samplerate*gap)
self.space = Snippet()
self.space.Add(0)
self.space.Loop(samplerate*space)
self.sounds = {'-':self.dah,'.':self.dit,'g':self.gap,'s':self.space}
def Signal(self,text):
temp = ''
for char in text:
if (char == ' '):
temp += 's'
else:
temp += table[string.upper(char)]
temp += 'g'
return temp
def Sound(self,sig):
temp = Snippet()
for char in sig:
temp.Extend(self.sounds[char])
temp.Extend(self.pause)
return temp
def Play(self,text):
Save(self.Sound(self.Signal(text)).Format(),samplerate,'morse.wav')
winsound.PlaySound('morse.wav',winsound.SND_FILENAME)
while True:
Morse().Play(raw_input('> '))
MicroPy_Music.py
import wave
import math
import struct
import random
toneclasses = {'A':0,'B':2,'C':3,'D':5,'E':7,'F':8,'G':10}
samplerate = 44100
maximum = 32767
PWWaves = {'sine':[['0<=x<1','math.sin(2*math.pi*x)']],
'triangle':[['0<=x<.5','4*(x-.25)'],['.5<=x<1','4*(.75-x)']],
'ramp':[['0<=x<1','2*(x-.5)']],
'saw':[['0<=x<1','2*(.5-x)']],
'square':[['0<=x<.5','1'],['.5<=x<1','-1']]}
class Snippet:
def __init__(self):
self.data = []
def Add(self,value):
self.data.append(struct.pack('h',int(maximum*value)))
def Loop(self,repetitions):
self.data = self.data[:]
self.data *= math.trunc(repetitions)
self.data.extend(self.data[:int(len(self.data)*(repetitions-math.trunc(repetitions)))])
def Cycle(self,wave,frequency,offset,amplitude,framerate):
samples = int(samplerate/frequency)
for n in range(samples):
self.Add(wave.Evaluate(frequency,offset,(n/float(samples*frequency)),amplitude))
def Extend(self,clip):
self.data.extend(clip.data)
def Format(self):
return ''.join(self.data)
class PWWave:
def __init__(self,function=PWWaves['sine']):
self.function = function
def Evaluate(self,frequency,offset,time,amplitude):
T = 1/float(frequency)
f = frequency
x = f*((offset+time)%T)
for piece in self.function:
if eval(piece[0]):
return amplitude*eval(piece[1])
def Save(signal,samplerate,location):
wavfile = wave.open(location,'wb')
wavfile.setparams((1,2,samplerate,len(signal)/2,'NONE','noncompressed'))
wavfile.writeframes(signal)
wavfile.close()
def Note(notation):
if (notation[1] == '#'):
marker = 1
octave = int(notation[2:])
elif (notation[1] == 'b'):
marker = -1
octave = int(notation[2:])
else:
marker = 0
octave = int(notation[1:])
return 440*(2**((toneclasses[notation[0]]+marker+(12*(octave-4)))/float(12)))