Apr 13 2009
As part of the process of working on the new iPhone app, HamMorse, I’ve been delving into the gory details of making digital sound. At one level things are going really well but as is often the way, there’s some weirdness cropping up that is getting in the way. I thought I share this just in case someone can help fix the problem!
A quick recap – to make sound you need to make a sound wave. For morse code this is usually a simple sine wave with a frequency somewhere around 600-800 Hz. To make morse code out of this you need to modulate it to make the dits and dahs, typically by turning the sound on and off as appropriate. If you are making this digitally (vs modulating a RF carrier frequency) you write your code so that there is silence when you want no sound and then when you need to have a dit or a dah you start the sine wave at the beginning of the dit/dah and then stop it after the appropriate length of time.
How long is an appropriate length of time? Well, the wikipedia page on morse code explains this all rather well but basically the timing works out to be one dit (in ms) = 1200/(desired code speed in words per minute). So, at 15 wpm, this works out to be 1200/15 = 80ms per dit. A dah is three times the length of a dit so that makes it 240ms long. There’s a one dit-length silence between each part of a letter (eg. between the dit and the dah of the character A), a three dit-length silence between characters in the same word and a seven dit-length silence between words. Throw all that together along with a translation table between the letters in your piece of text and the morse code needed for those letters and you are off and running.
Skipping over a bunch of bits for now (see a great explanation by Walt Fair Jr., W5ALT if you want more info), I’ve written code that gets this done on the iPhone and tested it on the iPhone Simulator that Apple provides for you to test your application. It sounds great, the audio waveform looks like this when I record the sound coming out of the computer’s speaker into Audacity.
Once things are working on the simulator you can connect your iPhone and run the same code on the actual device. When I do this and listen to the audio that comes out of the phone’s speakers, its not quite so nice, when you record the audio into audacity here’s what it looks like:
Not so pretty as before and while the audio still sounds passable, its clearly not as it should be. My challenge at the moment is to figure out why this is going on! The wave looks a bit like another sine wave is being added to the main one, causing the regular distortion observed but I cant for the life of me see where that is creeping in. I get the same distortion via headphones so I dont think its the speakers, I’ve made the audio mono to see if one of the stero channels was out of phase and causing the problem, this had no effect. When I look at the numerical values being created in the code, it should be making a nice smooth sine wave but…
This is the entertainment that is software development! I am currently waiting for the “Duh!” moment to occur where upon I will add back the missing semi-colon and everything will work as planned. Until that happens, if anyone has any suggestions I’d be very interested to hear them – I’m hoping that my fellow hams who are used to looking at wave forms and harmonics and things can get me pointed in the right direction.