Skip to content

Instantly share code, notes, and snippets.

@larryfox
Created July 8, 2015 04:37
Show Gist options
  • Save larryfox/516a6fd1e8a34742eafe to your computer and use it in GitHub Desktop.
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
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