class Morse2Au

Generate an .au file of Morse code

Initialize:

>> m = Morse2Au.new("/home/matt/output.au", 0.5)

Arguments:

file: (String)
rate: (Float)

Convert a string:

>> m.strToMorse("CQ CQ")

Arguments:

text: (String)

Write Out:

>> m.writeOut

Public Class Methods

new(ofile, rel_speed) click to toggle source

Arguments:

ofile: (String)
rel_speed: (Float)
# File lib/morse2au.rb, line 37
def initialize(ofile, rel_speed)
        @@io = File.open(ofile, "w")
        @@rel_speed = rel_speed
end

Public Instance Methods

addBeep(duration) click to toggle source

Arguments:

duration: (Float)
# File lib/morse2au.rb, line 47
def addBeep(duration)
        0.step(duration, 1.0/@@sample_rate) do |t|
                x = Math.sin(t * @@freq) * 50 + 127
                @@wave << x.to_i.chr
        end
end
addDah() click to toggle source

Add a “-”

# File lib/morse2au.rb, line 76
def addDah
        self.addBeep(@@rel_speed * 4)
        self.addSilence(@@rel_speed)
end
addDit() click to toggle source

Add a “.”

# File lib/morse2au.rb, line 68
def addDit
        self.addBeep(@@rel_speed)
        self.addSilence(@@rel_speed)
end
addLSpace() click to toggle source

Add a letter / short trailing space

# File lib/morse2au.rb, line 84
def addLSpace
        self.addSilence(@@rel_speed * 3)
end
addSilence(duration) click to toggle source

Arguments:

duration: (Float)
# File lib/morse2au.rb, line 59
def addSilence(duration)
        0.step(duration, 1.0/@@sample_rate) do |i|
                @@wave << 0
        end
end
addWSpace() click to toggle source

Add a word / long trailing space

# File lib/morse2au.rb, line 91
def addWSpace
        self.addSilence(@@rel_speed * 7)
end
strToMorse(text) click to toggle source

Convert a string to morse code

Arguments:

text: (String)
# File lib/morse2au.rb, line 101
def strToMorse(text)
        text.downcase!
        text.split('').each do |c|
                if /^[a-z0-9]$/.match(c)
                        $morse_chars[c].each do |c2|
                                if c2 == 0
                                        self.addDit
                                elsif c2 == 1
                                        self.addDah
                                end
                        end
                        self.addLSpace
                else
                        self.addWSpace      
                end
        end   
end
writeOut() click to toggle source

Write out using pre-determined file name

# File lib/morse2au.rb, line 122
def writeOut
        # No idea about next line
        @@au.data = @@wave #@@wave.slice(0..@@wave.length/2)
        @@au.ds = @@wave.bytesize
        @@au.write(@@io)
end