Wednesday, September 7, 2011

R is a cool sound editor!

Capabilities of R are definitely unless! After my previous posts about some easy image editing in R (they are here, and here), now is the time to explore if R is capable of sound editing!

Just for fun, here I created a function that receives a phone number (or another sequence of numbers), and returns the equivalent melody you can listen if you press that sequence on your house' phone... =D



It requires the sound library, and here's the code.

PlayTel <- function(x=character){
# load package
require(sound)
# control if the input is a character
if (!is.character(x)) stop("x must be a character")
# split the input
num <- strsplit(as.character(x), "")
len <- length(num[[1]])
# create the sequence of time
t <- seq(0, 0.2, length=44100 * 0.2)
# control if all characters are in the group of those supported
poss <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "*", "#", "A", "a", "B", "b", "C", "c", "D", "d")
if(prod(num[[1]] %in% poss) == 0) stop("Character not supported")
# create a silence of 0.1 sec
ts <- as.Sample(seq(0, 0.1, length=44100 * 0.1), 44100, 16)
# create an empty list
ll <- vector("list", length=len)
# here are the frequencies of the tones =)
t1 <- appendSample(as.Sample((sin(4379*t)+sin(7596*t))/2, 44100, 16), ts)
t2 <- appendSample(as.Sample((sin(4379*t)+sin(8394*t))/2, 44100, 16), ts)
t3 <- appendSample(as.Sample((sin(4379*t)+sin(9280*t))/2, 44100, 16), ts)
t4 <- appendSample(as.Sample((sin(4838*t)+sin(7596*t))/2, 44100, 16), ts)
t5 <- appendSample(as.Sample((sin(4838*t)+sin(8394*t))/2, 44100, 16), ts)
t6 <- appendSample(as.Sample((sin(4838*t)+sin(9280*t))/2, 44100, 16), ts)
t7 <- appendSample(as.Sample((sin(5353*t)+sin(7596*t))/2, 44100, 16), ts)
t8 <- appendSample(as.Sample((sin(5353*t)+sin(8394*t))/2, 44100, 16), ts)
t9 <- appendSample(as.Sample((sin(5353*t)+sin(9280*t))/2, 44100, 16), ts)
t0 <- appendSample(as.Sample((sin(5912*t)+sin(8394*t))/2, 44100, 16), ts)
tas <- appendSample(as.Sample((sin(5912*t)+sin(7596*t))/2, 44100, 16), ts)
tca <- appendSample(as.Sample((sin(5912*t)+sin(9280*t))/2, 44100, 16), ts)
ta <- appendSample(as.Sample((sin(4379*t)+sin(10260*t))/2, 44100, 16), ts)
tb <- appendSample(as.Sample((sin(4838*t)+sin(10260*t))/2, 44100, 16), ts)
tc <- appendSample(as.Sample((sin(5353*t)+sin(10260*t))/2, 44100, 16), ts)
td <- appendSample(as.Sample((sin(5912*t)+sin(10260*t))/2, 44100, 16), ts)
# full the list with the tone sequence
for(i in 1:len){
if(num[[1]][i] == "1") ll[[i]] = t1
if(num[[1]][i] == "2") ll[[i]] = t2
if(num[[1]][i] == "3") ll[[i]] = t3
if(num[[1]][i] == "4") ll[[i]] = t4
if(num[[1]][i] == "5") ll[[i]] = t5
if(num[[1]][i] == "6") ll[[i]] = t6
if(num[[1]][i] == "7") ll[[i]] = t7
if(num[[1]][i] == "8") ll[[i]] = t8
if(num[[1]][i] == "9") ll[[i]] = t9
if(num[[1]][i] == "0") ll[[i]] = t0
if(num[[1]][i] == "*") ll[[i]] = tas
if(num[[1]][i] == "#") ll[[i]] = tca
if(num[[1]][i] == "A" | num[[1]][i] == "a") ll[[i]] = ta
if(num[[1]][i] == "B" | num[[1]][i] == "b") ll[[i]] = tb
if(num[[1]][i] == "C" | num[[1]][i] == "c") ll[[i]] = tc
if(num[[1]][i] == "D" | num[[1]][i] == "d") ll[[i]] = td
}
# make sound
s <- as.Sample(unlist(ll))
# make stereo sound
sst <- stereo(s, s)
sst
}
view raw PlayTel.R hosted with ❤ by GitHub


Now you can simply create your phone melody =)

s2 <- PlayTel("556c885a4623#")

You can listen to it with the command:

play(s2)

(NOTE: in Windows 7 I was unable to find a wave player that works on batch mode - i.e. mplay32.exe. So this command doesn't work on Windows 7. It works on Windows XP)

You can save the output using the command:

saveSample(s2, "tel.wav")

(This command works on Windows 7)

Here is an example of the output:



Have fun!! =)

No comments:

Post a Comment