Morse Code Ringtone Generator

And now for something completely different — actual working code!

A long time ago, I decided I wanted a very distinctive ringtone for my cellphone, so I wrote a Morse Code ringtone generator in Rexx, which produced Nokia ringtones in a format suitable to cut-and-paste to AT&T Wireless’s text message sending page.

AT&T Wireless is gone now (although I wouldn’t be surprised to find the name revived once SBC…err, AT&T…buys BellSouth, and, along with it, the rest of Cingular), and so is my Nokia phone. And I find myself writing very little Rexx these days.

So I reworked the code, this time in Python, and now it creates an iMelody (.IMY) file, suitable for several brands of phone, including Motorola. Getting the file to the phone is up to the user (the easiest way, if your phone and PC support it, is Bluetooth file transfer).

One reason I haven’t posted this code before is that I thought it would be difficult to get IBM’s permission to do so — even though I wrote it on my own time, IBM has rights to it through it. But it turned out to be surprisingly simple; since it was clearly not competitive to IBM business, my manager was able to give me permission to post it, as long as I included a suitable license and made it clear that it was my personal work and not the company’s.

Cut and paste the following as morsemelody.py or download it here; share and enjoy.

#!/usr/bin/python

# MorseMelody.py
# Copyright (c) 2006, David Singer (http://readthisblog.net)

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import sys
if 1 >= len(sys.argv):
  print sys.argv[0], 'word1 [words]'
  print
  print "Creates an IMelody file which plays its argument in Morse code."
  print
  print "The output file is named 'word1'.imy"
  sys.exit(0)

code = {}

code['A']   = '.-'
code['B']   = '-...'
code['C']   = '-.-.'
code['D']   = '-..'
code['E']   = '.'
code['F']   = '..-.'
code['G']   = '--.'
code['H']   = '....'
code['I']   = '..'
code['J']   = '.---'
code['K']   = '-.-'
code['L']   = '.-..'
code['M']   = '--'
code['N']   = '-.'
code['O']   = '---'
code['P']   = '.--.'
code['Q']   = '--.-'
code['R']   = '.-.'
code['S']   = '...'
code['T']   = '-'
code['U']   = '..-'
code['V']   = '...-'
code['W']   = '.--'
code['X']   = '-..-'
code['Y']   = '-.--'
code['Z']   = '--..'
code['0']   = '-----'
code['1']   = '.----'
code['2']   = '..---'
code['3']   = '...--'
code['4']   = '....-'
code['5']   = '.....'
code['6']   = '-....'
code['7']   = '--...'
code['8']   = '---..'
code['9']   = '----.'

input = ' '.join(sys.argv[1:])
outfilename = sys.argv[1]+'.imy'
outfile = open(outfilename,'w')
outfile.write('BEGIN:IMELODY\n')
outfile.write('VERSION:1.2\n')
outfile.write('FORMAT:CLASS1.0\n')
outfile.write('NAME:%s \n' % (input))
outfile.write('BEAT:700\n')
outfile.write('STYLE:S1\n')
outfile.write('VOLUME:V15\n')
outfile.write('MELODY:\n')

note = '*5f'
song = []
for word in input.upper().split():
  for letter in word:
     if letter in code.keys():
       char = code[letter]
       for element in char:
         if element == '.':
           song.append(note+'3;')
           song.append('r3;')
         else:
           song.append(note+'1')
           song.append('r3;')
       song[-1] = 'r1'
       outfile.write('  ')
       outfile.write(''.join(song))
       outfile.write('\n')
       song = []
  song.append('r3;r1')
# We'll be left with an unwritten wordspace, but no harm done.
outfile.write('END:IMELODY\n')
outfile.close()
print outfilename, "created successfully."


This entry was posted in Computer Stuff. Bookmark the permalink.

One Response to Morse Code Ringtone Generator

  1. epc says:

    There were reports earlier this year that Cingular will indeed rebrand as “AT&T Wireless” some time in 2007 (whether or not they merge with Bellsouth).