Skip to content

Instantly share code, notes, and snippets.

@andreasjansson
Created December 9, 2011 00:04
Show Gist options
  • Save andreasjansson/1449352 to your computer and use it in GitHub Desktop.
Save andreasjansson/1449352 to your computer and use it in GitHub Desktop.
library(rmidi)
midi.set.tempo(105)
bs <- 0
as <- c(1, 5, 9, 13, 17)
us <- c(1, 9)
r <- function(x) {
x + rep((0:41) * 21, each = length(x))
}
bbs <- r(bs)
aas <- r(as)
aas <- aas[aas >= 2 * 21]
uus <- r(us)
uus <- uus[uus >= 10 * 21]
lls <- 0:floor((max(aas) + 1) / 2) * 2
lls <- lls[lls >= 18 * 21]
kls <- 0:floor((max(aas) + 1) / 2) * 2
kls <- kls[kls >= 26 * 21]
oos <- 0:floor((max(aas) + 1) / 2) * 2
oos <- oos[oos >= 34 * 21]
bd <- 20
ad <- 2
ud <- c(8, 10)
ld <- 1
bn1 <- 0
bn2 <- 5
an <- rep(c(1, 1, 3, 3), each = 5)
un <- c(8, 6, 8, 6, 8, 6, 8, 6, 7, 5, 7, 5, 11, 9, 11, 9)
ln <- c(rep(0, 21), rep(5, 10), rep(3, 11))
uud <- rep(ud, length.out = length(uus))
uun <- rep(un, length.out = length(uus))
nuus <- c()
nuud <- c()
nuun <- c()
for(i in 1:length(uus)) {
if(runif(1) > cos(1:length(uus) * 32 / length(uus)) && i < length(uus) - 1) {
lng <- sample(c(32, 64, 128, 256), 1)
for(j in seq(uus[i], uus[i + 1], length.out = lng)) {
nuus <- c(nuus, j)
nuud <- c(nuud, (uus[i + 1] - uus[i]) / lng)
if(nuun[i] %in% c(9, 11))
scale <- c(3, 5, 9, 11)
else if(nuun[i] %in% c(5, 7))
scale <- c(3, 5, 7, 10)
else
scale <- c(0, 5, 6, 8)
unn <- midi.scale(sample(60:120, 1), scale) - 12 * 8
nuun <- c(nuun, unn)
}
}
else {
nuus <- c(nuus, uus[i])
nuud <- c(nuud, uud[i])
nuun <- c(nuun, uun[i])
}
}
ppq <- midi.get.ppq() / 2
b <- rbind(midi.note(bbs, bd, bn1 + 12 * 5), midi.note(bbs, bd, bn2 + 12 * 7))
a <- midi.note(aas, ad, an + 12 * 7, channel = 1)
u <- midi.note(nuus, nuud, nuun + 12 * 8, channel = 2)
l <- rbind(midi.note(lls * ppq, ld * ppq, ln + 12 * 3, channel = 3),
midi.note(lls * ppq, ld * ppq, ln + 12 * 4, channel = 4))
k <- midi.note(kls * ppq, ld * ppq, 36, channel = 5)
o1 <- midi.note(oos * ppq, 1 * ppq, 12 * 5 + 5, channel = 6)
o2 <- midi.note(oos * ppq, 1 * ppq, 12 * 5 + 5, channel = 7)
m <- rbind(b, a, u)
m[,1] <- m[,1] * ppq
float <- sin(m[,1] / (ppq * 10)) * ppq
m[,1] <- floor(m[,1] + float)
m <- rbind(m, l, k, o1, o2)
play.b <- function() {
midi.play(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment