Given the nature of my position, people are always asking me, “Can you do {insert request}?”. By and large, I can usually tell them that I can do it and am always happy to help with someone’s project. This same question gets asked of R. Although R is becoming more and more popular, there are still people who will question its ability to do something. Whenever I start down this line with someone, I will tell them that R can do essentially anything and I use the ‘tuneR’ package as a case in point. As with everything in R, it has its serious uses. But, more than most packages (in my opinion), ‘tuneR’ can be used to leisurely burn through an afternoon without feeling too bad about it (“Yes, I am coding…to make music”).

Playing with ‘tuneR’ was part of a large line of “research” into Chuck. To date, I am still working on Chuck, but have mostly used it to produce sounds that would find themselves in Sega Genesis games of the early 1990’s.

tuneR

As noted earlier, tuneR has functions for serious audio research. It has melody plots, computes powerspectrums, and does things of which I have little to no undestanding. The fun really comes in its ability to create music.

Mashups

In general, I do not find mashups to be entertaining (I fully accept that I am in the minority here). However, tuneR makes mashup creation easy.

The first step is to load an MP3 into R. You will need to use files that you have on your own machine, but I chose Kraftwerk’s “Pocket Calculator” and LazerHawk’s “Distress Signal”; given the endeavor, these seemed like great choices.

library(tuneR)
pocketCalc = readMP3(filename = "C://Users/sberry5/Documents/R/01-02- Pocket Calculator (2009 Remastered Version).mp3")
distress = readMP3(filename = "C://Users/sberry5/Documents/R/01-04- Distress Signal.mp3")

The next step is to take some snippets from the two files. For the sake of this example, we are just going to take the first 100 seconds from each.

distress2 = extractWave(distress, from=0, to=100, xunit="time")
pocket2 = extractWave(pocketCalc, from=0, to=100, xunit="time")

To get these mashed together, we need to put them onto separate channels and then combine them. It does not matter which song you put on which channel.

distressLeft = mono(distress2, "left")
pocketRight = mono(pocket2, "right")
distPocket = stereo(pocketRight, distressLeft)
play(distPocket)

You should now have an audiological festival!

Creating Music

In addition to doing things with files you might already have laying around, tuneR can help you produce your own music. It is particularly well suited for making music of the “electronic” variety (thus, the earlier decision to go with Kraftwerk and LazerHawk).

The following code chunk will produce a simple melody that may be familiar to you, but probably will not. If you have any musical training at all, you will likely recognize that the tones are produced by giving the pulse function hertz values (think A440).

sampleMel = bind(
  pulse(587.3295, duration=1, xunit="time", bit=32),
  pulse(659.2551, duration=1, xunit="time", bit=32),
  pulse(523.2511, duration=1, xunit="time", bit=32),
  pulse(261.63, duration=1, xunit="time", bit=32),
  pulse(392.00, duration=1, xunit="time", bit=32)
)

Give yourself a pat on the back if you know the melody.