R is a cool sound editor!

[This article was first published on Statistic on aiR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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!! =)

To leave a comment for the author, please follow the link and comment on their blog: Statistic on aiR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)