# Algorithmic Composition

"Formerly, when one worked alone, at a given point a decision was made, and one
went in one direction rather than another; whereas, in the case of working with
another person and with computer facilities, the need to work as though
decisions were scarce—as though you had to limit yourself to one idea—is no
longer pressing.  It’s a change from the influences of scarcity or economy to
the influences of abundance and—I’d be willing to say—waste."  (John Cage)

----

## Why algorithms?

- As your old maths teacher said about numbers, algorithms are nothing to be
scared of.  

- Following Descarte's principle of solving difficult problems by
dividing them into smaller, more manageable ones, an algorithm is merely a
step-by-step procedure aimed at solving a particular problem.  

- The gap between imagining a new piece of music and realising it in score or
any other format is often bridged by composers in an algorithmic manner,
whether explicitly or not.

- Algorithmic composition formalises this process and makes it explicit.  

- It usually involves the use of a finite set of step-by-step procedures, most
often encapsulated in software routines, to create music.

----

## Formalisation

- The formalisation of musical composition is nothing new.  

- Bach's fugues can be analysed in terms of their algorithmic properties.

- Going further back, to around 1026, Guido d'Arezzo, the inventor of modern
staff notation, developed a formal technique to set a text to music.
      - A pitch was assigned to each vowel so that the melody varied according
      to the vowels in the text.   

- But the beginnings of algorithmic composition can be traced even further
back, to the ancient Greeks and the Jewish Kabbalah.

----

## What would be the motivation to make music algorithmically?  

- The explanation lies at least partly in the cognitive processes involved in
experiencing music.

- As we listen, we are not merely enjoying the sensual sonic experience of the
moment but setting up expectations of what is to come based on what has already
occurred, not only in the music we are listening to but in other pieces we know
that are similar to it, as well as all the music we have heard thus far
in our lives.  

----

 ``Retention in short-term memory permits the experience of coherent
  musical entities, comparison with other events in the musical
  flow, conscious or subconscious comparison with previous musical
  experience stored in long-term memory, and the continuous
  formation of expecations of coming musical events.'' 

(Erik Christensen,  ``The Musical Timespace, a Theory of Music Listening'',
1996) 

----

## Musical architecture

- Essentially, using our memory and predictive faculties, we
listeners create musical architecture in our brains, and the more actively we
listen, the more elaborate that architecture becomes.  

- For centuries then, composers have taken advantage of this ability to build
sonic-structural relationships in time in order to formalise the compositional
process.

----

``Because we spontaneously compare any new feature appearing in consciousness
with the features already experienced, and from this comparison draw
conclusions about coming features, we pass through the musical edifice as if
its construction were present in its totality. The interaction of association,
abstraction, memory and prediction is the prerequisite for the formation of the
web of relations that renders the conception of musical form possible.''

  (Ligeti, *Über Form in der neuen Musik*, 1966)

----

## The benefits of algorithmic composition

- Formal planning cannot be conflated with algorithmic techniques of course, but
that the former should lead to the latter was a historical inevitability.  

- The potential for software algorithms to enrich our musical culture has been
established, in the 50+ years since such techniques were first introduced, by
personalities as diverse as Hiller, Xenakis, Cage, and Eno.

- There are many riches to be mined in algorithmic composition as, amongst
other benefits, the expression of compositional ideas in software often leads
to unexpected and exciting results, and these can seldom be achieved via
traditional means.

- Algorithmic composition techniques can thus play an energising role in the
development of modern music across all genres and styles.

----

## Bridging technologies

- *slippery chicken* can be seen as a bridging technology.  

- Traditional composition training in the West does not usually include
algorithmic or computer music techniques, despite the fact that many composers
use the computer regularly in their work.

- If the use of algorithmic techniques is to
increase, as I believe is inevitable, it is essential that bridging
mechanisms are found.  

- Systems based around or including programming interfaces to traditional
notation are one such solution.

----

## Why programme?

- The programming aspect is important, as this gives the freedom and
flexibility that stimulates innovation.

- Interfaces don't always help
    - E.g., is XCode really simpler than `gcc` on the command line?
    - 'Wizards' force order of execution; what if you want to do step B before
       step A? 

- *slippery chicken* is an open-source, specialised algorithmic composition
programme written in the general programming language Common Lisp and its
object-oriented extension, CLOS.

- It has been in development since 2000.  

----

## Specialised but agnostic

- By specialised as opposed to generalised, it is meant that the software was
originally tailor-made to encapsulate the author’s personal composition
techniques and to suit his own compositional needs and goals.

- As the software has developed however, many general-purpose algorithmic
composition tools have been programmed that should be useful to a range of
composers.  

- The system does not produce music of any particular aesthetic
strain; for example, although not programmed to generate tonal music the system
is quite capable of producing it.  

- Furthermore, the software's release as open-source, object-oriented Common
Lisp code encourages further development and extensions on the part of the
user.

----

\vspace*{8mm}
## Composition focussed

- The algorithmic system in "slippery chicken" has been used to create musical
structure for pieces since its inception and for several years now has been at
the stage where it can generate, in one pass, complete musical scores.  

- It can also, with the same data used to generate those scores, write sound
files using samples, or MIDI file realisations of the instrumental score.

- Techniques for the innovative combination of rhythm and pitch data--arguably
one of the most difficult aspects of making convincing musical algorithms--are
offered by *slippery chicken*.

- The project's main aim is to facilitate a melding of electronic and
instrumental sound worlds, not just at the sonic but also at the structural
level.

- Hence certain processes common in one medium (for instance sound file slicing
and looping) are transferred to another (the slicing up of notated musical
phrases and the instigation of sub-phrase loops, for example).

- Musical notation is not the exclusive focus, as
many forms of music do not involve the use of Western musical notation in their
practice.


----

## Hybrid solutions

- Perhaps the debate surrounding the relative merits of analogue versus digital
studio technology have most convincingly shown that combinations of the old and
the new offer the best solutions.  

- This applies just as well to music composition and performance.

- Formats that continue to include rather than bypass the talented and
highly-trained acoustic musicians which our musical infrastructure has produced
yield the most impact, particularly when viewed, for better or for worse, from
the audience’s perspective.

- To this end, hybrid
works combining digital and acoustic instrumental technologies are ideal.
*slippery chicken* is focussed on exactly such musical bridging solutions:
    - using the computer to combine and meld together electronic and acoustic
    resources at both the structural and formal level.


----

## The motivation for *slippery chicken*

- The integration of electronic and acoustic sound sources and/or instruments

- Using electronics as an independent self-sufficient contrapuntal voice
  instead of a colouring of basically instrumental music

- ``If you can formalise it, you can programme it''

- To further an individual musical and compositional development through
(usually deterministic)  computer-programming-enabled ``voyages of discovery''

----

## The path to *slippery chicken*

- Translating structural processes for computer music into instrumental
  music was by no means straightforward.

- It took me several years to understand how to do this: need more data for
  instrumental music than for pure computer music.

- *slippery when wet*: commission from the *Österreichisches Ensemble für Neue
  Musik* and the solo violinist Frank Stadler

- Whole piece based on the composition method that I first tested out in 
an earlier piece *pas de poule, pas de pot*

- This method then became the basis for *slippery chicken*

----

\vspace*{1cm}
\begin{figure}
\includegraphics
[width=.8\linewidth,keepaspectratio]
{resources/slippery-rsp.pdf}
\caption{slippery when wet: the first 8 of the 21 rhythmic sequences}
\end{figure}


----

## *slippery when wet*: rhythmic mapping structure

\begin{figure}
\includegraphics
[width=.9\linewidth,keepaspectratio]
{resources/slippery-xls.pdf}
\caption{rhythmic sequence mapping in slippery when wet}
\end{figure}

----

## Sound Example: the beginning of *slippery when wet*

- for Solo Violin, Alto Flute/Piccolo, Clarinet/Bass Clarinet, Horn,
Percussion, Violin, Viola, Cello, and Stereo Tape 

- 1999/2000 

- 13:00

- Recording of the first performance, Salzburg, April 2000

- *Österreichisches Ensemble für Neue Musik*

- Frank Stadler, Solo Violin

- Alberto Caprioli, Conductor

- At the beginning of the piece, the whole ensemble plays in equal weight with
the tape; at the end of the example only solo violin, vibraphone and tape are
playing but the presence of the rest of the ensemble is still felt due to the
presence of algorithmically processed instrumental samples.

----

## *slippery when wet* code presentation

- We'll now look at the code that would generate this piece with the full
  system that wasn't at my disposal back when this was composed.

- It's interesting to look at the 100% generated score, even though it clearly
  deviates from the actual score.

- The details will be covered in later sessions but this provides a good
  early taster. 

- But before we do that, just to get an overview, here's a very basic *slippery
  chicken* structure:

----

### Example of simplest call to `make-slippery-chicken`

~~~~
(let* ((mini
           (make-slippery-chicken
            '+mini+
            :ensemble '(((vn (violin :midi-channel 1))))
            :set-palette '((1 ((c4 e4 g4))))
            :set-map '((1 (1)))
            :rthm-seq-palette '((1 ((((2 4) q e e))
                                    :pitch-seq-palette ((1 2 3)))))
            :rthm-seq-map '((1 ((vn (1))))))))
      (midi-play mini)
      (cmn-display mini)
      (write-lp-data-for-all mini))
~~~~

----

\vspace*{8mm}
### Now a small extension to show the power of algorithms

~~~~
(let* ((random-1-2 (loop repeat 30 collect (1+ (random 2))))
       (mini
        (make-slippery-chicken
         '+mini+
         :ensemble '(((vn (violin :midi-channel 1))))
         :set-palette '((1 ((c4 e4 g4)))
                        (2 ((fs4 b4 ds5))))
         :tempo-map '((1 (q 180)))
         :rthm-seq-palette '((1 ((((2 4) q e e))
                                 :pitch-seq-palette ((1 2 3))))
                             (2 ((((2 4) s s s s e s s))
                                 :pitch-seq-palette ((1 2 3 2 1 3 2)))))
         :set-map (list (list 1 random-1-2))
         :rthm-seq-map (list (list 1 (list (list 'vn random-1-2)))))))
  (midi-play mini)
  (cmn-display mini)
  (write-lp-data-for-all mini))
~~~~

- And now onto `slippery.lsp`

----

## Cheat Sheet 

- for solo electric guitar, alto flute/piccolo, e-flat clarinet/b-flat bass
clarinet (with low c), marimba (cymbals \& gongs ad lib), piano, violin, viola,
cello, double bass, and live electronics

- 2007 

- 13-25 Minutes

- This commission from the Bregenz Festival and OENM profits from a
  (finally!) fully-functioning *slippery chicken* programme

- Algorithmically selected pitches are selected and combined with rhythmic
  sequences to prepare a finished score

----

## *slippery chicken*: procedure

- Define the instruments: ranges; chord selection function; microtones;
any missing notes;

-  Define instrument changes for individual players (e.g. flute to piccolo)

-  Define the set palette, possibly algorithmically as with all remaining steps

-  Define the rhythm sequence palette 

-  Define the rhythm sequence map: sequence onto instruments

-  Define the set map

-  Define the tempo maps

-  Define the set limits: for whole piece and/or instruments

----

## Combining rhythm and pitch

- Pitch selection: not trivial: each person matches pitches to rhythms
according to taste etc.

- In SC, each sequence has an arbitrary number of pitch curves

- These are numbers representing pitch height; they're selected in rotation

- Combined with the given set, instrument, and any set limits, we can map
the numbers onto the notes of the set

- The instruments successively choose unused notes from the set

- We can define a hierarchy to specify who gets the pick of the notes first

----

## *cheat sheet*: unification of DSP and score generation processes 

- Audio loops created in CLM use an arbitrary number of user-designated markers
in a sound file to create loops.

- The number of repeats of any segment, and its progression to the next segment
is determined by a `folding in' structure based on the fibonacci series:

----

\vspace*{1cm}
\begin{figure}
\includegraphics
[width=1\linewidth,keepaspectratio]
{resources/fib-trans-simple.pdf}
\caption{cheat sheet: fibonacci transitions for `rhythmic fold in'}
\end{figure}

- Sound Example: DSP loops of saxophone sample

----

\vspace*{1cm}
\begin{figure}
\includegraphics
[width=1.1\linewidth,keepaspectratio]
{resources/cheat-sheet-rsp-orig.pdf}
\caption{cheat sheet: rhythmic/contrapuntal loop material}
\end{figure}

----

- The 4-bar counterpoint shown represents all the rhythmic/contrapuntal material
for the piece (1167 bars, 9 instruments).

- In my efforts to unify the structures of processed sound and instrumental
music, I've applied this essentially DSP technique to conventionally
notated musical material by dividing 5 bars of 4-part counterpoint into 400
segments (100 per voice: 10 per crotchet, 10 crotchets in 5 2/4 bars).

- Each crotchet is divided into 10 loop points with the semiquaver as the
shortest unit.

----

\vspace*{1cm}
##cheat sheet: semiquaver loop points within a single crotchet


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

\begin{figure}
\includegraphics
[width=.3\linewidth,keepaspectratio]
{resources/four-semis.pdf}
\end{figure}


----

\vspace*{1cm}
##cheat sheet: rhythmic loop slice mapping for opening flute and clarinet parts

- Taking the flute part, and comparing the score and the original 4-part
counterpoint with the following structure, we can see how this develops.


- Fibonacci Transition with *remix-in* modification: 

~~~~

1 1 1 1 2 1 1 2 1 2 1 2 2 1 2 1 2 1 2 2 2 1 2 2 3 2 3 2 3 1 3 3 2 1 3 3 1 3
2 1 3 2 3 4 1 3 4 1 3 4 2 4 1 3 2 4 4 4 4 5 1 4 4 5 2 4 5 2 4 5 1 5 2 4 5 1
4 5 2 4 5 1 5 2 5 2 4 5 5 6 5 2 6 5 6 1 6 6 2 5 6 2 6 3 6 5 2 6 6 3 7 6 2 7
3 6 3 7 7 6 7 7 3 7 7 8 2 7 7 3 8 7 3 8 ...  96 96 45 96 46 95 45 96 96 97
96 97 46 96 97 97 45 96 97 46 97 97 46 97 45 98 97 46 97 98 46 97 98 46 97
46 98 47 98 97 98 97 98 46 97 98 98 46 98 97 47 98 98 46 99 47 98 99 46 98
99 47 99 99 47 98 46 99 47 99 99 98 99 99 46 100 99 100 47 99 100 46 100

~~~~

\begin{figure}
\includegraphics
[width=1.0\linewidth,keepaspectratio]
{resources/cheat-sheet-beg.pdf}
\end{figure}

----

## Magda

- It's perhaps easier to understand this process more intuitively by listening
to another piece.

- *for Magda Cordell, if she'll have it* (piano and computer) was written in
2007 for Sarah Nicolls and the Huddersfield Festival.

- It also uses 4-part note-loops which generate a driving rhythmic character.

- The interesting characteristic of note-loops as opposed to audio loops is
that you can disconnect rhythm from pitch: a basic repeating rhythmic cell can
loop through several harmonic fields.

- **Sound Example: for Magda Cordell, if she'll have it**
    - for piano and computer
    - 2007
    - Sarah Nicolls, piano
    - courtesy BBC

----

\vspace*{1cm}
\begin{figure}
\includegraphics
[width=0.95\linewidth,keepaspectratio]
{resources/cheat-sheet-p23.pdf}
\caption{CMN output from slippery chicken using the cheat sheet algorithms
(solo guitar and strings only)}
\end{figure}

----

##scei = slippery chicken egg 

- The engagement with *slippery chicken* has become a
  self-sufficient project: SCEI.

- Contrary to traditional studio work (which can be thought of as sound
  sculpting) my approach is to generate perhaps hundreds of sound files
  automatically---file selection becomes the main activity then.

- The best results of the algorithms (no editing!) are to be found in the
  internet as short but complete pieces (http://www.sumtone.com/).


- **Sound Example: scei V (snow shoes)**

- **Sound Example: scei XII (skin)**

- **Sound Example: scei XVI (charlie)**

----

\vspace*{1cm}
## *24/7: freedom fried* 

- for viola d'amore and computer, 2004-6, 14:30

\begin{figure}
\includegraphics
[width=.3\linewidth,keepaspectratio]
{resources/garth-knox560.pdf}
\caption{Garth Knox with the viola d'amore}
\end{figure}



----

- The viola d'amore is a 7-string 7-sympathetic string member of the viol
  family that had its heyday in the baroque period and subsequently fell out of
  mainstream use due to the limitations of its tuning system.

- Garth Knox is developing a new repertoire for the instrument, in
  particular in combination with electronics.

- Released 2010 on a Wergo DVD, in Surround Sound and with a video
  from Brian O'Reilly (San Francisco).

- In addition to the *slippery chicken* structuring methods as
  described, I use further algorithmic processes in the generation of this
  piece. 

- Permutations of the four fingers of the left hand are used as a
  background to the whole piece.

----

- 24 possible permutations of the four fingers

~~~~
    ((1 2 3 4) (2 1 3 4) (1 3 2 4) (3 1 2 4) (2 3 1 4) (3 2 1 4) (1 2 4 3) 
     (2 1 4 3) (1 4 2 3) (4 1 2 3) (2 4 1 3) (4 2 1 3) (1 3 4 2) (3 1 4 2) 
     (1 4 3 2) (4 1 3 2) (3 4 1 2) (4 3 1 2) (2 3 4 1) (3 2 4 1) (2 4 3 1) 
     (4 2 3 1) (3 4 2 1) (4 3 2 1))
~~~~

----

- These 24 permutations are to be played through in any of the many
billions (620448401733239439360000) of their possible permutations as fast as
possible (unless otherwise notated in the score).

- The notes used for the four fingers range over a perfect fourth,
reflecting both the natural stretch of fingers 1--4 and the tuning system of
the viol.  

- Though notes (fingers) 1 and 4 are fixed, notes 2 and 3 microtonally
interpolate between tetrachords of the phrygian, dorian, and ionian modes:

----

\begin{figure}
\includegraphics
[width=1\linewidth,keepaspectratio]
{resources/fingerings-progression.pdf}
\caption{24/7: freedom fried: tetrachordal interpolation}
\end{figure}

----

## *24/7: freedom fried*: tetrachordal meandering

- In choosing the note groups to permutate, the player should wander
forwards and backwards along this line.

- In the graphic, each of the seven four-note groups are given a number.

- The numbers in parentheses represent the groups that may follow the current
group, hence after group 1 only 2 can follow; after group 2 may come 3 or 1,
depending on whether we are reading forwards or backwards.

- The basic pattern is 1 2 3 4 6 4 3 2 1 2 3 4 etc.  

- Groups 5 and 7 are given in square brackets and represent alternative
progressions that should be used to vary the basic pattern.

- Thus a constantly varying but basically static microtonal meandering
through the various tetrachords is possible

----

## *24/7: freedom fried*: tetrachordal meandering rules

- Rules:

~~~~
       '((1 ((2)))
         (2 ((3 1)))
         (3 ((4 2)))
         (4 ((6 3 5)))
         (5 ((2 4)))
         (6 ((4 7)))
         (7 ((3))))
~~~~

- Result from simply rotating through progressions:

~~~~
1 2 3 4 6 4 3 2 1 2 3 4 5 2 1 2 3 2 1 2 3 4 6 7 3 2 1 2 3 4 3 2 1 2 3 4 5 4
6 4 3 2 1 2 3 4 5 2 1 2 3 2 1 2 3 4 6 7 3 2 1 2 3 ...
~~~~

----

- Instead of writing out complex fast passages in the score, a fast
  permutation is simply indicated by the lowest note(s) in parentheses, and the
  string(s) and position the note(s) represent.


- See, for example, page 9 of the score  


- By freeing up the notation of the fast permutations we can more easily
  impose external structures upon them, e.g. jeté bowing, tremolo,
  glissandi, and microtonal compression/stretches of the basic tetrachord.



----

\vspace*{1.5cm}
\begin{figure}
\includegraphics
[width=0.8\linewidth,keepaspectratio]
{resources/24-7-p9.pdf}
\caption{24/7: freedom fried: score page 9}
\end{figure}

- Sound Example: 24/7: freedom fried: c 5:42 (bar 176)

----

\vspace*{1.5cm}

## tramontana 

- Viola and Computer, 2002-4, 13:30-15:00

- scordatura: Strings I, II, and III are tuned to be in-tune with
  the 7th partial of the C string.

- With the following flageolets we get the frequency of this partial
(IV=7 III=5 II=3 I=2):

\begin{figure}
\includegraphics[width=1.05\linewidth,keepaspectratio]
{resources/tramontana-scordatura.pdf}
\caption{tramontana: scordatura}
\end{figure}

----

##tramontana: Lindenmayer Systems

- The score was generated with *slippery chicken* and CMN

- The last part of the the piece was generated with L-Systems (Lindenmayer)


\begin{center}
Rules:\\
1 $\rightarrow$ 2 3 \\
2 $\rightarrow$ 1 3 \\
3 $\rightarrow$ 2 1 \\

\vspace*{1cm}
Step-by-step generation:\\
Seed: 2 \\
1 3 \\
2 3 $|$ 2 1 \\
1 3 $|$ 2 1 $|$ 1 3 $|$ 2 3 \\
\end{center}

----

\vspace*{1.5cm}
\begin{figure}
\includegraphics
[width=1\linewidth,keepaspectratio]
{resources/cheat-sheet-chord-l-seq.pdf}
\caption{Self-similarity becomes clear when large result sets are produced}
\end{figure}

----

\vspace*{5mm}
## Transitioning L-Systems


- Unlike normal L-Systems however I use what I call *Transitioning L-Systems*
  (where the number returned by the L-Sys is used as lookup into a table whose
  result depends on a curve)

- There is a slow development from:
    
    - More and more fast flageolets on the C and G strings, in comparison to
  the tremolo chords of previously

    - When we arrive at the point where only normal and half flageolets are
  played, then there is a tendency to have more and more normal and less half
  flageolets.

    - There is also the tendency to have more and more flageolets on the D
  string.


- Then, more and more of the half flageolets become ``real
  notes'', but these also die out...

- The important point here is, that as an instrumental and
  computer music composer, I have always thought structurally in this way.

- Now, with *slippery chicken*, I can programme these ideas, let the
  music be generated, try things out, change them etc., instead of
  doing everything on paper and giving up halfway through because it
  takes too long.


----

## Tramontana computer part

- The computer part consists of real-time and non real-time
  techniques.


- The real-time techniqes are:
 
    - Granular-Synthesis (with my Max/MSP External Object)
    - Diffusion (with the Strobl-Stiftung's Halophone)
    - Live-Recording/Playback/loops
 
- The non real-time techniques are the triggering of pre-prepared
  sound files.

- **Sound Example**: Transition to the last part of *tramontana* 

    - Live recording of the first performance with Barabara Maurer, Darmstadt,
    August 2004 (from c. 7:50 c. bar 175)
