Page 1 of 1

Morse Code Tutor Script

Posted: Thu Dec 29, 2005 10:11 am
by ozurr
This script along with the two wav files is a simple morse code tutor.
It contains a section where you click on the character and hear it's morse equivalent and a section that tests what you know.
the zip contains a script and two wav files only, so you will have to have Ren'Py to run it.
I've only run it on linux but I assume it will run ok under windows.

Posted: Fri Dec 30, 2005 5:32 pm
by absinthe
The "Info" part cracked me up. :)

It works just fine under Windows, XP at least.

I can't say I passed the test, but I now know how to say my name in Morse code! :D

Posted: Sat Dec 31, 2005 12:13 am
by PyTom
Okay, figured out the cause of the crackling under Linux.

SDL includes some MMX accelleration code for the sound mixing operation. Unfortunately, this code assumes that the size of a sound buffer is a multiple of 16 bytes long... an assumption that is as incorrect as it is undocumented. The last couple of samples failed to be mixed and were left at 0, causing the popping.

Why didn't we notice this? Well, it can only occur when a sound file is not a multiple of 4 samples in length. (Sound files have to be a multiple of 2 samples in length.) Most compressed audio formats have a frame size that is a multiple of 4... so it isn't a problem for them.

This will be fixed in the next release of Ren'Py, but isn't much of a problem in practice. It only affects wave files (and perhaps some other minor formats), and only if they have a number of samples that is not a multiple of 4.

There is still one pop at the end of each dash or dot, but that's due to the waveform of the file, and hence not my problem.

Some code you may wish to adapt:

Code: Select all

init:
    python:
        def morse(s):
            for c in s:
                if c == ".":
                    renpy.sound.queue("dit.wav", clear_queue=False)
                elif c == "-":
                    renpy.sound.queue("dah.wav", clear_queue=False)
                else:
                    renpy.sound.queue("pause.wav", clear_queue=False)

$ morse(".-. . -. .--. -.--")

Posted: Sun Jan 01, 2006 1:13 am
by ozurr
Thanks PyTom.
I'll put the new function in when I work on the script in a day or two.
The pause wav is a nifty idea. Now I can do sentences. :)

Have a Happy and Prosperous New Year!

Posted: Sun Jan 01, 2006 1:30 am
by PyTom
Another function might be the following:

Code: Select all


init:
   python:

       morse_table = {
            't' : '-',
            'o': '---',
            'm': '--',
            # etc... This is all the morse code I remember. Well, this 
            # and SOS.
       }

       def str_to_morse(s):
            s = s.lower()
            rv = ""

            for i in rv:
                rv += morse_table.get(i, i)
            return rv

$ code = str_to_morse("I am the very model of a modern major general")
$ morse(code)

"%(code)"

Posted: Mon Jan 02, 2006 7:28 pm
by ozurr
ReWrote the code to get rid of the huge blocks but haven't added any significat features so, I don't see any reason to update. It took me twice as long to do it this time, but It's turned out to be a good learning method. Seems there's lots of different ways to code something and get the same results, though some are faster than others. Used normal lists because the dictionary list doesn't return sequential items. I'll try it next time since I found some info on sorting dictionary lists. I imagine I'll end up rewriting this code many times, learning more and more in the process.

.--. -.-- - .... --- -. .. ... -.-. --- --- .-..

Posted: Mon Jan 02, 2006 7:58 pm
by PyTom
What do you mean by a "dictionary list"? A dictionary isn't a list (this isn't lisp, after all), a dictionary is its own data type. Like sets, dictionaries do not have order.