Created
July 8, 2015 04:37
-
-
Save larryfox/516a6fd1e8a34742eafe to your computer and use it in GitHub Desktop.
Euclidean Rhythm Generator in Julia http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module EuclideanRhythm | |
export euclideanrhythm | |
typealias Bits BitArray{1} | |
one() = ~Bits(1) | |
zero() = Bits(1) | |
flatten(m::Array{Bits}) = reduce(vcat, m) | |
function euclideanrhythm(n::Int, k::Int) | |
ones = [ one() for _ = 1:k ] | |
zeros = [ zero() for _ = 1:n-k ] | |
return euclid(ones, zeros) |> flatten | |
end | |
function euclid(m::Array{Bits}, k::Array{Bits}) | |
num, den = minmax(length(m), length(k)) | |
rem = num == den ? den : num % den | |
(rem == 1 || length(k) == 0) && return [m, k] | |
ms = [ [m[i], pop!(k)] for i = 1:rem ] | |
ks = isempty(k) ? m[rem+1:end] : k | |
euclid(ms, ks) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment