Musical Steganography
The following code takes the plaintext message “Mary had a little lamb”,
and converts it to notes in musical scale and then plays the notes.
A character in the message is converted to a number by the Ruby function ord
a="M"
b=a.ord
b=77
# b is in the range of 0 to 255
# and is expressed as an octal number
c=b.to_s.to_i(8) # =63
So “M” will be played as the 3ird note of the scale followed by the 6th note of the scale followed by the 0th note of the scale (scale numbered from 0 thru 7 as in an array)
#Steganography.rb
# 17 Oct 2017
# Hiding a message in music
# https://en.wikipedia.org/wiki/Steganography
##############
#Define tempo and note lengths
#####
tempo=0.750 ### try changing tempo
#define note timings
whole=1.0
half=whole/2.0
dothalf=half*1.5
quart=half/2.0
dotquart=quart*1.5
eighth=quart/2.0
doteighth=eighth*1.5
sixteenth=eighth/2
#########
### try different keys
key= note(:c4)
puts midi2note(key)
puts " "
### try minor
mode=:major
####
keyscale=scale(key,mode)
puts keyscale
## Put the message to hide in plaintext string
plaintext="Mary had a little lamb"
## converting character to a number
puts plaintext[0],plaintext[0].ord,plaintext[0].ord.chr
###
tune1=[]
i=0
while i<plaintext.length
tune1=tune1.push(plaintext[i].ord)
i+=1
end
# take character expressed as a number in tune1
# convert the number to 3 octal digits values 0..7
# lookup octal value in the scale to push the note onto tune2
tune2=[]
i=0
while i<tune1.length
j=0
t=tune1[i]
while j<3
tune2=tune2.push(keyscale[(t%7)])
t=t/8
j+=1
end #next j
i+=1
end #next i
### Now play tune 2
use_synth :fm
with_fx :level, amp: 0.3 do
i=0
while i<tune2.length
play tune2[i]
sleep quart*tempo
i+=1
end
end
a="M"
b=a.ord
c=b.to_s.to_i(8)
puts a,b,c