Last active
March 19, 2021 17:24
-
-
Save mgurov/c345a92fbd7516b8049b870507b0e938 to your computer and use it in GitHub Desktop.
quick'n'dirty morse player for sonic-pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit = 0.1 # as in time unit | |
note = 80 | |
message = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.' | |
define :play_morse do |str| | |
words = string_to_words str | |
for word in words do | |
letters = word_to_letters word | |
for l in letters do | |
play_letter l | |
sleep unit * 2 # the space between letters 3 units - 1 unit already paused at the end of the letter | |
end | |
sleep unit * 4 # the space between words is 7 units - 3 units after the letter | |
end | |
end | |
define :play_letter do |letter| | |
# dot - 1 unit; dash - 3 units; | |
for char in letter.split("") | |
if char == '-' | |
play_tick 3 | |
else | |
play_tick 1 | |
end | |
sleep unit #the space betweeen the parts of the same letter is 1 unit | |
end | |
end | |
define :play_tick do |d| | |
play note, attack:0, sustain: unit * d, release: 0.01 | |
sleep unit * d + 0.01 | |
end | |
define :string_to_words do |str| | |
str.split(" ") | |
end | |
morse_code = { | |
'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' => '--..', | |
'1' => '.----', | |
'2' => '..---', | |
'3' => '...--', | |
'4' => '....-', | |
'5' => '.....', | |
'6' => '-....', | |
'7' => '--...', | |
'8' => '---..', | |
'9' => '----.', | |
'0' => '-----', | |
} | |
morse_code.default = '' | |
define :word_to_letters do |str| | |
str.split("").map {|c| | |
morse_code[c] | |
} | |
end | |
play_morse message | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://en.wikipedia.org/wiki/Morse_code