Skip to content

Instantly share code, notes, and snippets.

@iani
Last active January 18, 2021 12:28
Show Gist options
  • Select an option

  • Save iani/f88b12bcf0d97f8a41f60ab3fe4f3d42 to your computer and use it in GitHub Desktop.

Select an option

Save iani/f88b12bcf0d97f8a41f60ab3fe4f3d42 to your computer and use it in GitHub Desktop.

Revisions

  1. iani revised this gist Jan 17, 2021. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions BresenhamEuclidean_SuperCollider.scd
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ https://medium.com/code-music-noise/euclidean-rhythms-391d879494df
    (0..12) do: { | i | postf("bresenham plain. i: %, rhythm: %\n", i, ~br.(i, 8)) };
    )
    /* // RESULT 1:
    bresenham plain. i: 0, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham plain. i: 0, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ] // error!
    bresenham plain. i: 1, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham plain. i: 2, rhythm: [ 1, 0, 0, 0, 1, 0, 0, 0 ]
    bresenham plain. i: 3, rhythm: [ 1, 0, 0, 1, 0, 0, 1, 0 ]
    @@ -22,9 +22,9 @@ bresenham plain. i: 6, rhythm: [ 1, 0, 1, 1, 1, 0, 1, 1 ]
    bresenham plain. i: 7, rhythm: [ 1, 0, 1, 1, 1, 1, 1, 1 ]
    bresenham plain. i: 8, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham plain. i: 9, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham plain. i: 10, rhythm: [ 1, 1, 1, 1, 2, 1, 1, 1 ]
    bresenham plain. i: 11, rhythm: [ 1, 1, 1, 2, 1, 1, 2, 1 ]
    bresenham plain. i: 12, rhythm: [ 1, 1, 2, 1, 2, 1, 2, 1 ]
    bresenham plain. i: 10, rhythm: [ 1, 1, 1, 1, 2, 1, 1, 1 ] // error!
    bresenham plain. i: 11, rhythm: [ 1, 1, 1, 2, 1, 1, 2, 1 ] // error!
    bresenham plain. i: 12, rhythm: [ 1, 1, 2, 1, 2, 1, 2, 1 ] // error!
    */
    //:Implementation 2 (allow o == 0, o > p)
    (
    @@ -37,7 +37,7 @@ bresenham plain. i: 12, rhythm: [ 1, 1, 2, 1, 2, 1, 2, 1 ]
    (0..12) do: { | i | postf("bresenham improved. i: %, rhythm: %\n", i, ~br1.(i, 8)) };
    )
    /* // RESULT 2:
    bresenham improved. i: 0, rhythm: [ 0, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham improved. i: 0, rhythm: [ 0, 0, 0, 0, 0, 0, 0, 0 ] // error fixed
    bresenham improved. i: 1, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham improved. i: 2, rhythm: [ 1, 0, 0, 0, 1, 0, 0, 0 ]
    bresenham improved. i: 3, rhythm: [ 1, 0, 0, 1, 0, 0, 1, 0 ]
    @@ -46,8 +46,8 @@ bresenham improved. i: 5, rhythm: [ 1, 0, 1, 0, 1, 1, 0, 1 ]
    bresenham improved. i: 6, rhythm: [ 1, 0, 1, 1, 1, 0, 1, 1 ]
    bresenham improved. i: 7, rhythm: [ 1, 0, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 8, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 9, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 10, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 11, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 12, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 9, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ] // error fixed
    bresenham improved. i: 10, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ] // error fixed
    bresenham improved. i: 11, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ] // error fixed
    bresenham improved. i: 12, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ] // error fixed
    */
  2. iani revised this gist Jan 17, 2021. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions BresenhamEuclidean_SuperCollider.scd
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,7 @@ https://medium.com/code-music-noise/euclidean-rhythms-391d879494df
    //:Implementation 1 (plain)
    (
    ~br = { | o = 1, p = 4 |
    //Note: in SC, the algorithm can be coded in a single line, as follows:
    (o / p * (0..p - 1)).floor.differentiate.asInteger.put(0, 1);
    // Note 2: the if (o <= 0) statement covers the case when we want 0 beats
    // in the pattern
    };
    // test:
    (0..12) do: { | i | postf("bresenham plain. i: %, rhythm: %\n", i, ~br.(i, 8)) };
    @@ -32,12 +29,11 @@ bresenham plain. i: 12, rhythm: [ 1, 1, 2, 1, 2, 1, 2, 1 ]
    //:Implementation 2 (allow o == 0, o > p)
    (
    ~br1 = { | o = 1, p = 4 |
    //Note: in SC, the algorithm can be coded in a single line, as follows:
    (o / p * (0..p - 1)).floor.differentiate.asInteger.min(1)[0] = if (o <= 0) { 0 } { 1 };
    // Note 2: the if (o <= 0) statement covers the case when we want 0 beats
    // Note: the if (o <= 0) statement covers the case when we want 0 beats
    // in the pattern
    };
    //
    // TEST
    (0..12) do: { | i | postf("bresenham improved. i: %, rhythm: %\n", i, ~br1.(i, 8)) };
    )
    /* // RESULT 2:
  3. iani created this gist Jan 17, 2021.
    57 changes: 57 additions & 0 deletions BresenhamEuclidean_SuperCollider.scd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    /* Sources:
    See: https://codepen.io/Dafuseder/pen/WEqOVw
    https://gist.github.com/crashingbooth/e9f0b7dbec8aecabf13db3663d28480f
    https://medium.com/code-music-noise/euclidean-rhythms-391d879494df
    */
    //:Implementation 1 (plain)
    (
    ~br = { | o = 1, p = 4 |
    //Note: in SC, the algorithm can be coded in a single line, as follows:
    (o / p * (0..p - 1)).floor.differentiate.asInteger.put(0, 1);
    // Note 2: the if (o <= 0) statement covers the case when we want 0 beats
    // in the pattern
    };
    // test:
    (0..12) do: { | i | postf("bresenham plain. i: %, rhythm: %\n", i, ~br.(i, 8)) };
    )
    /* // RESULT 1:
    bresenham plain. i: 0, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham plain. i: 1, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham plain. i: 2, rhythm: [ 1, 0, 0, 0, 1, 0, 0, 0 ]
    bresenham plain. i: 3, rhythm: [ 1, 0, 0, 1, 0, 0, 1, 0 ]
    bresenham plain. i: 4, rhythm: [ 1, 0, 1, 0, 1, 0, 1, 0 ]
    bresenham plain. i: 5, rhythm: [ 1, 0, 1, 0, 1, 1, 0, 1 ]
    bresenham plain. i: 6, rhythm: [ 1, 0, 1, 1, 1, 0, 1, 1 ]
    bresenham plain. i: 7, rhythm: [ 1, 0, 1, 1, 1, 1, 1, 1 ]
    bresenham plain. i: 8, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham plain. i: 9, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham plain. i: 10, rhythm: [ 1, 1, 1, 1, 2, 1, 1, 1 ]
    bresenham plain. i: 11, rhythm: [ 1, 1, 1, 2, 1, 1, 2, 1 ]
    bresenham plain. i: 12, rhythm: [ 1, 1, 2, 1, 2, 1, 2, 1 ]
    */
    //:Implementation 2 (allow o == 0, o > p)
    (
    ~br1 = { | o = 1, p = 4 |
    //Note: in SC, the algorithm can be coded in a single line, as follows:
    (o / p * (0..p - 1)).floor.differentiate.asInteger.min(1)[0] = if (o <= 0) { 0 } { 1 };
    // Note 2: the if (o <= 0) statement covers the case when we want 0 beats
    // in the pattern
    };
    //
    (0..12) do: { | i | postf("bresenham improved. i: %, rhythm: %\n", i, ~br1.(i, 8)) };
    )
    /* // RESULT 2:
    bresenham improved. i: 0, rhythm: [ 0, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham improved. i: 1, rhythm: [ 1, 0, 0, 0, 0, 0, 0, 0 ]
    bresenham improved. i: 2, rhythm: [ 1, 0, 0, 0, 1, 0, 0, 0 ]
    bresenham improved. i: 3, rhythm: [ 1, 0, 0, 1, 0, 0, 1, 0 ]
    bresenham improved. i: 4, rhythm: [ 1, 0, 1, 0, 1, 0, 1, 0 ]
    bresenham improved. i: 5, rhythm: [ 1, 0, 1, 0, 1, 1, 0, 1 ]
    bresenham improved. i: 6, rhythm: [ 1, 0, 1, 1, 1, 0, 1, 1 ]
    bresenham improved. i: 7, rhythm: [ 1, 0, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 8, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 9, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 10, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 11, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    bresenham improved. i: 12, rhythm: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
    */