Is it possible to code music in R and play it back? (Mac OS X)

You can create music in R programming using the tuneR library

First you need to create simple sine waves for each note and concatenate all the notes into a vector.

Now that the tune will be ready, you can edit your sound using different sound processing techniques like timbre, filtering etc.

Example to create a simple A4 note:

library(tuneR)        #import the tuneR library
setWavPlayer("audacious")
f=440                 #frequency of A4 note
sr=8000 
bits=16
secs=2                #length of the note set to 2
amp=1
t=seq(0, secs, 1/sr)
y= amp*sin(2*pi*f*t)  #make a sinewave with above attributes
s=floor(2^(bits-2)*y) #floor it to make it an integer value
u=Wave(s, samp.rate=sr, bit=bits)  #make a wave structure 
play(u)

To concatenate two notes x and y, we simply use vector notation:

z=c(x,y)
w= Wave(z, samp.rate=sr, bit-bits)

To play two notes simultaneously (play chords for instance)

z=x+y
 w= Wave(z, samp.rate=sr, bit-bits)

For the moment, this is the best that I've come up with:

library("audio")
library(tuneR)        #import the tuneR library
for (i in 1:10) {
yo=abs(round(rnorm(1,400,500)))
f=yo                 #frequency of A4 note
sr=1000000
bits=116
secs=5                #length of the note set to 2
amp=1
t=seq(0, secs, 1/sr)
y= amp*sin(2*pi*f*t)  #make a sinewave with above attributes
s=floor(2^(bits-2)*y) #floor it to make it an integer value
# u=Wave(s, samp.rate=sr, bit=bits)  #make a wave structure 
u=audioSample(x = s, rate = sr,bits = bits)
audio::play(u)
}

We could experiment with setWavPlayer('/usr/bin/afplay')

That's my modern random canon.

library(tuneR)
setWavPlayer('/usr/bin/afplay')
muss = NULL
nbnotes = 100
for (i in 1:nbnotes) {
  yo = abs(round(rnorm(nbnotes,400,200)))
  lengthhtime = abs(rnorm(nbnotes,0.5,0.1))
  f=yo[i]                 #frequency of A4 note
  titi = lengthhtime[i]
  sr=1000
  bits=16
  secs=titi                
  amp=1
  t=seq(from = 0, to = secs, by = 1/sr)
  y= amp*sin(2*pi*f*t) 
  s=floor(2^(bits-2)*y)
  muss = c(muss,s)
}
u=Wave(left = muss,right = rev(muss), samp.rate=sr, bit=bits)  #make a wave structure
tuneR::play(u)

Here is a function I created to generate a random melody:

melodymachine <- function(nbnotes, seed = NULL) {
  if(!is.null(seed)){set.seed(seed)}
muss = NULL
  for (i in 1:nbnotes) {
    yo = abs(round(rnorm(nbnotes,400,200)))
    lengthhtime = abs(rnorm(nbnotes,0.0,0.2))
    f=yo[i]                 #frequency of A4 note
    titi = lengthhtime[i]
    sr=1000
    bits=16
    secs=titi                #length of the note set to 2
    amp=0.99
    t=seq(from = 0, to = secs, by = 1/sr)
    y= amp*sin(2*pi*f*t)  #make a sinewave with above attributes
    s=floor(2^(bits-2)*y) #floor it to make it an integer value
    muss = c(muss,s)
  }
  return(muss)
}

Here is an example of it:

mel1 = melodymachine(6,seed = 1)
mel2 = melodymachine(6,seed = 1)
mel3 = melodymachine(6,seed = 1)
mel4 = melodymachine(6,seed = 1)
mel5 = melodymachine(6,seed = 1)
u=Wave(left =  c(mel1,mel2,mel3,mel4,mel5),
       right = rev(c(mel1,mel2,mel3,mel4,mel5)), 
       samp.rate=sr, bit=bits)  #make a wave structure
tuneR::play(u)

You can take a look at the wave here:

plot(u, 
     info = TRUE, 
     xunit = c("time"), 
     ylim = NULL, main = "My random melody", 
     sub = "made by me", 
     xlab = NULL, ylab = NULL, 
     simplify = FALSE, nr = 2500, 
     axes = TRUE, yaxt = par("yaxt"), 
     las = 1, 
     center = TRUE)

enter image description here

Tags:

R