Skip to content

Instantly share code, notes, and snippets.

@crashingbooth
Last active March 5, 2018 01:11
Show Gist options
  • Save crashingbooth/ac71d945061135f7c4a33d05c57297d6 to your computer and use it in GitHub Desktop.
Save crashingbooth/ac71d945061135f7c4a33d05c57297d6 to your computer and use it in GitHub Desktop.
Euclidean Rhythm
func generateEuclidean(onsets: Int, pulses: Int) -> [Int] {
    let front:[[Int]] = Array(repeating: [1], count: onsets)
    let back:[[Int]] = Array(repeating: [0], count: pulses - onsets)
    return euclidRecursive(front: front, back: back).flatMap {$0}
}

private func euclidRecursive (front: [[Int]], back: [[Int]]) -> [[Int]] {
    var back = back
    var front = front
    
    guard back.count > 1 else { return front + back }
    
    var newFront = [[Int]]()
    while front.count > 0 && back.count > 0 {
        newFront.append(front.popLast()! + back.popLast()!)
    }
    return euclidRecursive(front: newFront, back: front + back)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment