Skip to content

Instantly share code, notes, and snippets.

@Arcolye
Created April 28, 2015 09:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arcolye/407371a24d3c27a33045 to your computer and use it in GitHub Desktop.
Save Arcolye/407371a24d3c27a33045 to your computer and use it in GitHub Desktop.
Morse Code Generator for Sonic Pi
#MORSE CODE GENERATOR
#ENTER A MESSAGE AND PRESS RUN
message = 'Morse code is old'
#CUSTOMIZE
words_per_minute = 18 # slow:12, standard: 18, fast: 24
@u = 1.2 / words_per_minute #One time unit (length of 1 dot)
@looped = true #comment out to play message only once
@pitch = 83
use_synth :beep
def play_message(message)
loop do
message.downcase.each_char do |character|
play_one_character(character)
end
exit unless @looped
sleep 36*@u
end
end
def play_one_character(character)
blip_sequence = ALPHABET[character]
blip_sequence.chars.each_with_index do |blip,i|
case blip
when '.' then play_dot
when '-' then play_dash
when ' ' then play_blip_space
end
play_blip_space unless i == blip_sequence.length - 1
end
play_character_ending_space
end
def play_dot #1 time unit in length
play @pitch, sustain: @u, release: 0.001; sleep @u
end
def play_dash #3 time units in length
play @pitch, sustain: 3*@u, release: 0.001; sleep 3*@u
end
def play_blip_space #The time between blips: 1 time unit
sleep @u
end
def play_character_ending_space #The time between characters: 3 time units
sleep 3*@u
end
ALPHABET = {
'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' => '--..',
' ' => ' '
}
play_message(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment