# Featured Algorithms: \newline fibonacci functions and chop

## Fibonacci functions

- Fibonacci series
- Named after the 13th-century Italian mathematician Leonardo Fibonacci.
- Each number is the sum of the two preceding numbers.

        (0 1 1 2 3 5 8 13...)

----

### fibonacci-transition and fibonacci-transitions

#### What they do
- Return a list in which one repeating element gradually transitions to a second.
- Repetitions and replacements determined by a Fibonacci series.

#### How they do it
- Internally select a list of decreasing Fibonacci numbers, e.g. `(13 8 5 3 2
  1)`.
- Create a list with groups whose lengths are equal to the Fibonacci numbers.
- Each group begins with repetitions of the first item.
- The last element of each group is one instance of the second item.
- Once the last group has been created, the second item is repeated.

        (0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 
         0 1 0 1 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1)

----

#### fibonacci-transition

- Transition between only two items.
- Can be called with 1 or 3 arguments.
- If one argument: transition from `0` to `1` in a list of length *n*.

        (fibonacci-transition 50)

        => (0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 
            0 1 0 1 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1)

- If three arguments: last two specify the items of the transition.

        (fibonacci-transition 50 's 'e)

        => (S S S S S S S S S S S S E S S S S S S S E S S S S E S S E 
            S E S E S E E S E E E E S E E E E E E E E)

----

#### fibonacci-transitions

- Transition through multiple items.
- Applies a simple `fibonacci-transition` between each two consecutive items.
- Can be called with two integers or one integer and a list.
- If two integers: 
    - First integer: length of resulting list.
    - Second integer: number of consecutive numbers from 0 that will be the
      items.
      
<!-- end list -->

        (fibonacci-transitions 76 4)

        => (0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 2
            1 1 2 1 2 1 2 2 1 2 1 2 2 2 2 2 2 2 3 2 2 3 2 3 2 3 3 2 3 2 3 3
            3 2 3 3 3 3 3 3 3 3 3 3)

----

- If one integer and a list:
    - Integer: length of resulting list.
    - List: the items through which to transition.

<!-- end list -->

        (fibonacci-transitions 304 '(s e q h))

        => (S S S S S S S S S S S S S S S S S S S S S S S S S S S S S E S S
            S S S S S S S S S S E S S S S S S S E S S S S E S S S S E S S E
            S S E S S E S E S E S E E S E S E S E E S E E S E E E E S E E E
            E S E E E E E E E E E E E E E E E E E E E E Q E E E E E E E Q E
            E E E Q E E E E Q E E Q E E Q E E Q E Q E Q E Q Q E Q E Q E Q Q
            E Q Q E Q Q Q Q E Q Q Q Q E Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q
            Q Q H Q Q Q Q Q Q Q H Q Q Q Q H Q Q Q Q H Q Q H Q Q H Q Q H Q H
            Q H Q H H Q H Q H Q H H Q H H Q H H H H Q H H H H Q H H H H H H
            H H Q H H H H H H H H H H H H H H H H H H H H H H H H H H H H H
            H H H H H H H H H H H H H H H H)

----

### remix-in

#### What it does
- Creates a new list by lengthening an existing list.
- Inserts consecutive elements of the original list back into itself.

#### How it does it

- Mixes earlier items of the list back into the original after the 3rd
  unique item.
- Items mixed back in sequentially starting at the beginning of the original list.
- Insert-points determined by Fibonacci series.

        (remix-in '(1 2 3 4 5 6 7 8 9 10 11))

        => (1 2 3 4 5 1 6 7 2 8 9 3 10 4 11 5)

----

- "Seed" argument determines how often an earlier item is
  inserted.
- Lower values: earlier items mixed back in more frequently.
- Value of 1 or 2: earlier items inserted after every
  original element 
    - (after the third original item has been reached).

<!-- end list -->

        (remix-in '(1 2 3 4 5 6 7 8 9 10 11) :remix-in-fib-seed 1)

        => (1 2 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9)

- `:mirror` argument: Pass backwards through original list once
  end is reached.

----

## chop

### What it does

- Divides rhythmic content into multiple fragments for further use.
    - DSP technique applied to conventionally notated musical material.
- Used in technique of *intra-phrasal looping*:
    - Resulting fragments assembled into sequences using fibonacci-transitions.
- Source material fragmented based on specified *chopping unit* and *chop
  points*.

----

### Chopping unit
- Must be duplet rhythm (e.g. `32`, `'s`, `'e` etc.)
- Must be an even subdivision of the beat basis.

### Chop points

- Chop points define the start and end points of segments within one beat.
- With a unit of `'s`, a quarter can be segmented into:
    - 4 sixteenths
    - 3 sixteenths
    - 2 sixteenths 
    - 1 sixteenth

----

![Chopping points with unit of `'s` applied to a quarter, represented as below.](../doc/manual/resources/chop-points.png)

    '((1 4) (1 3) (2 4) (1 2) (2 3) (3 4) (1 1) (2 2) (3 3) (4 4))

----

### Process

- Applies the chopping pattern to each beat of the original material.
    - Can only be applied to material with beat-basis defined by the chop
      points.
- Creates one new rthm-seq from each successive segment.
- Each new rthm-seq contains only one bar, with corresponding time signature.
- Only creates sounding notes in new bars from attacked notes in the
  original.
- Sustained notes with attacks in a previous segment become rests.

----

### Example using above chopping points

- Original material:

\includegraphics[scale=0.6]{../doc/manual/resources/chop-orig.png}

- Resulting fragments:

\includegraphics[scale=0.6]{../doc/manual/resources/chop-extraction.png}

----

### Three levels of chop

- There is a chop method for the classes of:
    - rthm-seq-bar
    - rthm-seq
    - rthm-seq-palette
- Generally only need to use the rthm-seq-palette method.

----

### The rthm-seq-palette chop method

- Generates a new rthm-seq-palette object from an existing one.
    - This can be inserted directly into a slippery-chicken object.
- New rthm-seq-palette has same top-level structure as original.
- Further level of nesting:
    - Each rthm-seq of the original replaced by a rthm-seq-palette.
- Each nested rthm-seq-palette consists of rthm-seqs with fragment bars
  produced by `chop`, as above.

----

### Example of the call with rthm-seq-palette

- *This code produces the fragments in the musical example above*

        (let* ((orig (make-rsp 'orig 
                               '((1 
                                  ((((1 4) - s e s -)))))))
               (chopped (chop orig 
                              '((1 4) 
                                (1 3) (2 4) 
                                (1 2) (2 3) (3 4) 
                                (1 1) (2 2) (3 3) (4 4))
                               's)))
         [...])

----

### Accessing rthm-seqs in a chopped rthm-seq-palette

- Additional level of nesting requires additional ID to reference its
  rthm-seqs.
- Method automatically sets consecutive numerical IDs for each new rthm-seq.
- New rthm-seqs accessed by combination of:
    - original rthm-seq's ID 
    - new rthm-seq's ID

<!-- end list -->

- That means: IDs in the rthm-seq-map must be lists. 
- `(1 1)`: first new rthm-seq from an original rthm-seq with ID `1`.
- `(1 2)`: second new rthm-seq from an original rthm-seq with ID `1`, etc.
- `(2 1)`: first new rthm-seq from an original rthm-seq with ID `2`.
- `(seq3 2)`: second new rthm-seq from an original rthm-seq with ID `seq3`, etc.

----

#### Practical example

    (let* ((orig-palette (make-rsp 'orig ; original rthm-seq-palette
                                   '((1 ((((1 4) - s e s - ))
                                         :pitch-seq-palette ((1 2 3))))
                                     (2 ((((1 4) - e. s - ))
                                         :pitch-seq-palette ((1 2))))
                                     (3 ((((1 4) - (e.) s - ))
                                         :pitch-seq-palette ((1)))))))
           (chopped-palette (chop orig-palette ; chopped rthm-seq-palette
                                  '((1 4) 
                                    (1 3) (2 4) 
                                    (1 2) (2 3) (3 4) 
                                    (1 1) (2 2) (3 3) (4 4)) ; chop points  
                                  's)) ; chopping unit
           (chop-examp
            (make-slippery-chicken
             '+chop-examp+
             :ensemble '(((vn (violin :midi-channel 1))))
             :set-palette '((1 ((c4 d4 e4))))
             :set-map '((1 (1 1 1 1 1)))
             :rthm-seq-palette chopped-palette
             :rthm-seq-map '((1 ((vn ((1 1) (1 2) (1 3) (2 1) (3 2))))))))) 
      (cmn-display chop-examp))

----

![Resulting score from above code](../doc/manual/resources/chop-nested-refs.png)

----

### re-bar

- Results of `chop`: short bars with multiple time signatures.
- Use `slippery-chicken` class's `re-bar` method.
- Only combines short bars into longer ones.
    - Won't split up longer bars and recombine them.
    - Won't split up rhythms into tied rhythms over bar lines.
- `:min-time-sig` is target only.
    - Specified time signature not always possible.

<!-- end list -->

        [...]
        (re-bar chop-examp :min-time-sig '(1 4))
        (cmn-display chop-examp))

----

![After re-bar](./resources/re-barred-chop.png)

----

### Exercise: FIBONACCI TRANSITIONS AND CHOP

See http://michael-edwards.org/sc/workshop-exercises.lsp

