# Using Formal Grammars as Musical Genome
## 2 Melodies and Genomics
### Composing Process  
Melomics uses a combination of formal grammar to represent music concepts of varying degrees of abstraction and evolutionary techniques to evolve the set of production rules. The system can be used to compose both atonal and tonal music and, although using slightly different encoding methods and a much stronger set of constraints in the second case, they both share a similar structure and execution workflow (see Figure 6). From a bio-inspired perspective this can be thought of as an evolutionary process that operates over a developmental procedure, held by the formal grammar. The music \"grows\" from the initial seed, the axiom, through the production rules to an internal symbolic representation  
Figure 5: Resulting score from Iteration 4 (see Audio S5).  
(similar to a MIDI file), where finally the compositions are subject to test by the set of constraints provided. The execution workflow can be described as follows:  
* Filling the desired input parameters. These parameters represent musical specifications or directions at different levels of abstraction, such as instruments that can appear, amount of dissonance, duration of the composition, etc., with no need for creative input from the user.
* The initial gene pool is created randomly, using part of the input parameters as boundaries and filters.
* Each valid genome, based on deterministic grammar and stored as a plain text file, is read and developed into a resulting string of symbols.
* The string of symbols is rewritten after some adjustment processes that can be of different forms: cleaning of the string, for example by removing sequences of idempotent operators; adjustments due to physical constraints of the instruments, like the maximum number of simultaneous notes playing; or the suppression (or emergence) of particular musical effects.
* Each symbol in the final string has a musical meaning with a low level of abstraction, which is interpreted by the system through a sequential reading from left to right and stored in an internal representation.
* Once again the musical information will be adjusted and stabilized. For example, shift the pitches to satisfy constraints in the instruments' tessituras, discretization of note durations and so on.
* The input directions are used to assess the produced composition that might be discarded or pass the filter. In that case it is saved as a 'valid' composition.
* A discarded theme's genome is replaced by a new random genome in the gene pool. On the other hand, a \"valid\" genome is taken back to the gene pool, after being iteratively subject to random mutations and possible crossover with another genome in the gene pool (see Section 2.2.1), until it passes the filters defined at genome level (the same as with a random genome).
* If desired, any composition (usually the ones that pass the filter) can be translated, using the different implemented modules, to standard musical formats that can be symbolic (MIDI, MusicXML, PDF) or audio (WAV, MP3, etc.), after executing a synthesis procedure with virtual instruments, which is also led by the information encoded in the composition's genome.  
The average time to build a new valid genome varies very much depending on the style and constraints imposed at this level. Using single core on an Intel Xeon E645, for a very simple style it can be ready in less than one second, while for the most complex style, symphonic orchestra in the atonal system, the process takes 283 s on average. Given a built gene pool, the time to obtain a new valid composition, which includes executing the developmental process possibly repeatedly until passing the filters, varies a lot too. It can take 6 s for the simplest atonal style, while it takes 368 s on average for the most complex style in the tonal system.  
Figure 6: A graphical representation of the composition workflow.