# Using Formal Grammars as Musical Genome
## 2 Melodies and Genomics
### Representational Models  
#### 2.1.1. Atonal Music  
The atonal system uses an encoding based on a deterministic L-system (genetic part) and some global parameters used during the development phase (epigenetic part). An L-system is defined as a triple \((V,S,P)\) where \(V\) is the alphabet (a non-empty, finite set of symbols), \(S\in V\) is the axiom or starting symbol and \(P\subseteq V\times V^{*}\) is the set of production rules in the form \(A\to x,A\in V,x\in V^{*}\). Each production rule rewrites every appearance of the symbol \(A\) in the current string into \(x\). Because it is deterministic, there is only one rule \(A\to x\) for each symbol \(A\in V\). In our model, we divide symbols of the alphabet \(V\) into two types:  
* Operators, which are reserved symbols represented by a sequence of characters with the form \(@i\), \(i\in\mathbb{Z}\) or \(\$j\), \(j\in\{1\dots m\}\). There are no rules explicitly written for them, the rule \(A\to A\) is assumed instead. They control the current state of the abstract machine, modulating the musical variables: pitch, duration, onset time, effects, volume, tempo, etc.
* Instruments, with the form \(\#k\), \(k\in\{0\dots p\}\). These symbols have an explicit production rule with them on the left side and can represent either a musical structural unit (composition, phrase, idea, etc.) or, if it appears in the final string, a note played by the instrument that has been associated to that symbol.  
Since the rewriting process could be potentially infinite, in order to stop it, we introduce the following mechanism: each of the production rules have an associated value \(r_{i}\), indicating the possibility of that rule to be applied in the next rewriting iteration. There is a global parameter \(T\) which serves as the initial value for all \(r_{i}\) and there is a weight \(I_{i}\) for each production rule, to compute the new value of \(r_{i}\) from the previous time step, \(r_{i_{k}}=f(r_{i_{k-1}},I_{i})\). All \(r_{i}\) will progressively get a lower value until reaching 0, when the associated rule will not be applied anymore. The formula is applied if that rule is used at least once in the current iteration. To illustrate the process, let us introduce some of the operators that we use:$1 increases pitch value one step in the scale. $2 decreases pitch value one step in the scale.  
$5 saves current pitch and duration values in the _pitch stack_. $6 returns to the last value of pitch and duration stored in the _pitch stack_.  
$7 saves current time position in the _time stack_. $8 returns to the last time position saved in the _time stack_.  
$96 applies dynamic mezzo-forte. @60$102 applies the tempo: quarter equals 60.  
Now, let us consider the following simple and handmade L-System  
\(Ga=(V,S,P)\)  
\(V=\{\#0,\#1,\#2,\#3,\#4,\$1,\$2,\$5,\$5,\$6,\$7,\$8,\$96,\#60$102\}\)  
\(S=\{\#0\}\)  
And \(P\) consisting of the rules:  
\(P=\{\)  
\(\#0\rightarrow\#60\$102\$96\#251\$2 decreases pitch value one step in the scale.  
$01\$2 decreases pitch value one step in the scale.  
$1151\#2\)  
\(\#1\rightarrow\#1\)  
\(\#2\rightarrow\$2 decreases pitch value one step in the scale.  
$215\#1\#3\)  
\(\#4\rightarrow\#4\#15\$2 decreases pitch value one step in the scale.  
$31\$2 decreases pitch value one step in the scale.  
$46\#4\)  
Below we show the whole developmental process to get the composition generated by \(Ga\), considering \(T=1\) and \(I_{i}=1\) for all the rules. And the following global parameters:  
Scale: \(C\)_major_  
Tempo: 80 bpm  
Default duration: _quarter note_  
Default dynamic: _mezzo-piano_  
Initial pitch: _middle_\(C\)  
Instruments: piano (0), rest (1), church organ (2 and 3), cello (4)  
The vector \(r\) will show the current values of \(r_{i}\) for all the production rules. Iteration 0  
With no iterations, the resulting string is the axiom, which is interpreted as a single note, played by its associated instrument, the piano, with all the musical parameters being on their default values still.