I'm trying to use R to make music. I've found:
Package musicmakeR, which is broken.
Package audio, which is more for manipulating/playing existing audio files.
Package tuneR.
I THINK tuneR can do everything I want to do, and I found this post (Is it possible to code music in R and play it back? (Mac OS X)). The first response in that post (from 'rakshith1124') seems to answer my question, but either I'm coding something incorrectly or there's an issue with my OS or .wav player. Here is the code I'm using:
library(tuneR)
sr <- 8000
bits <- 16
secs <- 1
amp <- 1
t <- seq(0, secs, 1/sr)
C0 <- 16.35
G3 <- 196
A5 <- 880
C0 <- floor(2^(bits-2)*(amp*sin(2*pi*C0*t)))
G3 <- floor(2^(bits-2)*(amp*sin(2*pi*G3*t)))
A5 <- floor(2^(bits-2)*(amp*sin(2*pi*A5*t)))
u <- Wave(c(C0,G3,A5), samp.rate=sr, bit=bits)
play(u)
This should play three notes (C0, G3, A5) for one second each. I got the frequencies of the notes from https://pages.mtu.edu/~suits/notefreqs.html. The .wav played by the script [play (you)] seems to be truncating the first note or something else I don't understand. Does anyone know what's going on, and relatedly, is there a better package to be using for music creation?
The R package "gm" is designed for creating music. A "Hello! World" example:
library(gm)
m <-
# initialize a Music object
Music() +
# add a 4/4 time signature
Meter(4, 4) +
# add a musical line of four quarter notes
Line(list("C5", "D5", "E5", "F5"), list(1, 1, 1, 1))
show(m)
You can check its complete guide for more examples. It generate musical scores and audio files in R Markdown documents, Jupyter Notebooks, RStudio.
I am the author of the package (I am asked to disclose my affiliation with the suggested solution).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With