Randomly Generating Music with R
Apr 19, 2021
Beyond single-cell analysis, coding in R
is a useful skill for lots of other fun things too :D
Here, I use the R
package gm
by Renfei Mao to generate a random song.
## install.packages("gm")
library(gm)
To generate my random song, I will select from a corpus of possible major chords.
## chords
notes <- list(
c('C4', 'E4', 'G4'), ## C major
c('D4', 'F#4', 'A4'), ## D major
c('E4', 'G#4', 'B4'), ## E major
c('F4', 'A4', 'C4'), ## F major
c('G4', 'B4', 'D4'), ## G major
c('A4', 'C#4', 'E4'), ## A major
c('B4', 'D#4', 'F#4') ## B major
)
I will randomly sample 3 chords and repeat the second chord to create a 4 chord melody. I will also repeat the 4 chords for the refrain. I do this twice to compose the general song.
## sample random notes
set.seed(100)
progn1 <- sample(notes, 3, replace=FALSE)
progn2 <- sample(notes, 3, replace=FALSE)
## song notes
songn <- c(progn1, progn1[2], ## repeat
progn1, progn1[2],
progn2, progn2[2], ## repeat
progn2, progn2[2])
Instead of just a progression of chords, I will have one line of half notes, and one line of eighth notes where I break up the chord into its 3 notes, repeating a note for 4 notes per chord.
## split into two lines
bgn <- songn
bgd <- as.list(rep("half", length(bgn)))
mainn <- as.list(unlist(lapply(songn, function(x) c(x[1], x[2], x[1], x[3]))))
maind <- as.list(rep("eighth", length(mainn)))
Now, let’s see what this song sounds like!
## make music
m <-
Music() +
Meter(4, 4) +
Line(pitches = mainn, durations = maind) +
Line(pitches = bgn, durations = bgd)
show(m + Tempo(120), to = c("score", "audio"))
It’d be even more fun to train a generative adversarial neural network or other deep learning model on say the top Billboard or jazz melodies to generate new songs, but perhaps that will be left to a more ambitious and motivated student :)
Try it out for yourself and see what you can come up with!
Additional resources
For more creative coding, check out of some my other fun products:
- aRt with code - generate custom art using R
- CuSTEMized - generate personalized STEM storybooks
- Older
- Newer
RECENT POSTS
- Aligning 10X Visium spatial transcriptomics datasets using STalign with Reticulate in R on 05 November 2023
- Aligning single-cell spatial transcriptomics datasets simulated with non-linear disortions on 20 August 2023
- 10x Visium spatial transcriptomics data analysis with STdeconvolve in R on 29 May 2023
- Impact of normalizing spatial transcriptomics data in dimensionality reduction and clustering versus deconvolution analysis with STdeconvolve on 04 May 2023
- Aligning Spatial Transcriptomics Data With Stalign on 16 April 2023
- 3D animation of the brain in R on 08 November 2022
- Ethical Challenges in Biomedical Engineering - Data Collection, Analysis, and Interpretation on 15 October 2022
- I use R to (try to) figure out the cost of medical procedures by analyzing insurance data from the Transparency in Coverage Final Rule on 12 September 2022
- Annotating STdeconvolve Cell-Types with ASCT+B Tables on 30 August 2022
- Deconvolution vs Clustering Analysis: An exploration via simulation on 11 July 2022
TAGS
RELATED POSTS
- Aligning 10X Visium spatial transcriptomics datasets using STalign with Reticulate in R
- 10x Visium spatial transcriptomics data analysis with STdeconvolve in R
- Impact of normalizing spatial transcriptomics data in dimensionality reduction and clustering versus deconvolution analysis with STdeconvolve
- Aligning Spatial Transcriptomics Data With Stalign