News | Projects | Articles | Photo | About

Quartet Music Generator

Quartet Music Generator

Introduction

You can download Quartet Generator and see examples of Quartet Generator music at the bottom of the page in the Examples section.

Quartet Generator generates music in steps, where step is at least one note changing. Generator uses voices, which limit the number of notes that can sound at the same time and also the number of notes that can change at a time. There are 6 voices in the figure.

Quartet Generator produces music based on the random numbers with uniform distribution. During the generation of each step several rules are checked and the quality of resulting music is analysed. If the quality is poor, the generation for particular step is repeated.

Generator outputs music in midi format and also shows graphical representation of the generation process, where many variables are shown for each step (see Examples section for details):

Algorithm is highly customizable. It needs more than 300 starting constants to be set to run. But you can always use default values instead.

Generation process

Here is the flow of the generation process. The "Generate one step" process is repeated for each step.

All the stages of generation are described in separate sections below.

Note limits generation

High note limit is changed during the generation to decrease the possibility and repeating of high notes.

Note limit moves up and down in a linear fashion between the special limits (note_max_max and note_max_min) with a customizable step. When high note limit comes close to a high voice, it stops until the voice moves down. Here the high note limit is black and when it stops it is shown gray. You can see more detailed information and listen to music in the Examples section below.

Step length generation

To generate step length a finite-state machine is used. Each note length is represented as the length of time a note sounds (note sounding time). The following note lengths are allowed from the box:

Note length Note sounding time Minimum repeat count
60 1
30 2
15 2
20 (each) 3
24 (each) 5

Minimum repeat count shows the number of steps the length cannot be changed if it was selected. This prevents complex syncopation and rhythm shift.

Each note length is a state. Each step the probability vector to change to all the other states is calculated. The vector looks like this:

Next state Probability to change to this state
15 33%
20 30%
24 15%
30 7%
60 15%

Then random number is generated with uniform distribution between 1 and 100. Numbers from 1 to 33 mean changing to state 15, numbers from 34 to 63 mean changing to state 20 and so on. This guarantees that changing the particular state has the probability from the vector.

The result of the generation can be like this. In the time row you see the length passed from the begining to the start of the step. Green cells represent length, that are multiple of 120 (full measure). You can see more detailed information and listen to music in the Examples section below.

Step mode generation

Mode generation is pretty simple. The starting mode in my models is usually C major, but you can set it anything. Each time a number of measure is passed (i use step of 2 measures), the probability to change mode is checked against random generator. If it wins, the mode is changed forward or backward the circle of fifths. How far the mode goes by the circle is determined by the mode step, which must always be between the starting constants (mode_mut_min and mode_mut_max). I usually use only 1 step for less alterations that added during the mode change.

The result of the mode generation with step of 2 measures, probability of 60% and maximum step of 1 looks like this:

Step notes generation

Notes generation is the most sophisticated procedure of the algorithm.

First the number of moving voices is determined (usually from 1 to 4). It depends on the following probabilities:

  1. Base probability of each number of moving voices (e.g. 63% for 1 voice, 21% for 2 voices, 6% for 3 voices, 10% for 4 voices)
  2. Probability multiplicator for the beginning of each measure (I usually use 50 times increased probability for 4 voices moves at the beginning of the measures) or other positions in measure
  3. Probability multiplicator based on the length of the step. For example, I usually increase probability for 3 and 4-voice movements in long steps and decrease in short steps.

Of course, if there are not enough voices, that can move, the number of moving voices is decreased.

After the number of moving voices is determined, we start to choose, which particular voices will move. I usually use 6 voices, which helps guarantee that there will be enough movable voices if for example algorithm will try to move 4 of them. The problem can be that the voice is smothered between other voices or is pressed to the higher or lower limit and cannot move until the surrounding voices make it more loose.

Determining which voices will move is also a complex random process and it takes the following probabilities into account:

  1. Base probability of each voice to move. The voices do not cross, which means that upper voices are always above the other. So you can increase the base probability of each of your voices to move. I usually slightly increase the probability for the voices 4 and 5 (where voice 0 is the lowest and 6 is the highest).
  2. If on the previous step the voice participated in the discord, algorithm increases the probability for this voice to move in the current step to resolve the discord.
  3. If the voice is smothered between the other voices or between the voice and the note limit, it's probability to move is lowered, usually down to zero.
  4. If the voice moved to a low note in the previous step, it is locked until some particular time is passed (the lower the note, the longer the lock). This ensures that low voices will not move fast and create artificial effect.
  5. If the voice is not moving for a long time, its probability to move is gradually increased each step, which ensures that music will not be formed only with part of the voices.
  6. There is also an algorithm, which can increase the probability of voices, which are close to the voices that moved in the previous step. This can be used to create trills and more smooth melodies.

After the voices that should move are chosen, algorithm starts to move the voices:

  1. First the window, where the voice can be moved is determined. It is usually the distance from the voice to the upper voice (or the the upper limit, if it is the highest voice) and to the lower voice (or to the lower limit, if it is the lowest voice).
  2. Then the window is cut to prevent long jumps, because the jumps will occur anyway due to voice change. I usually do not allow moves longer then a perfect fourth in a voice.
  3. Now the "gravitation" takes effect. It is a special instrument to push notes from the lower and higher limits so that they come to the limits less frequently and do not stay there for long. In Quartet Generator gravitation is maintained by cutting the movement window above the note (if close to upper limit) or below the note (if close to lower limit) to zero. This cutting leads to that the note can move only in the direction from the limit. The closer the note is to the limit, the more often this cutting occurs.
  4. After the limits are chosen, the voice is moved to any note within the limits (notes out of diatonic scales are not allowed at this stage).
  5. There is also a possibility of voice staying on the same note. This may be allowed or disallowed by user. Usually I allow this for 4-voice movements and disallow for 1-voice movements.
  6. Then the movement interval is checked. Moves by a tritone, minor and major 7th, 9th and diminished 9th are prohibited. If the occur, the move is recalculated from step 4. Frankly speaking, they cannot occur if you choose move limit at least to perfect fourth.

After the above algorithm has worked for all the voices that were chosen to move, program starts to analyse the resulting music. The result of analysis is the total cost of the step. The lower the cost, the higher the quality of music is. This is why the program then tries to optimize the cost by moving voices again and again and recalculating the cost. Each time the program sees the cost lower than any cost of the previous tries, it memorizes the move. If it was not possible to minimize the cost to zero this way, the program chooses the variant with the lowest cost, that was memorized.

The number of tries depends on the number of moving voices. For many moving voices the task is more complex and more tries are allowed.

The following is added to form the cost:

  1. The most important component of the cost is the number and rating of the intervals of dissonance (discords).
  2. Number of contradicting notes (when note not from the mode is in the previous step or one step before previous and in current step there is note that is chromatically close to that note or close the the note over an octave). These notes usually happen when mode changes.
  3. Number of detected parallel, opposite and hidden fifths and octaves.
  4. Orchestral density (prohibiting all voices to move in one direction) is usually not checked when using 6 voices. At least two voices stay on their places, which ensures orchestral density. Anyway, algorithm allows it to be checked.
  5. Long thickenings with 3th and 6th are not checked, because they usually not formed with the random voice movements.

After the note moves are fixed, algorithm starts to decide which notes to retrigger. Retriggering means that the voice does not move, but the note of the voice is repeated in the current step. Retriggering notes usually have lower velocities than normal notes.

When deciding if to retrigger each voice or not, the algorithm takes the following into account:

  1. The voices that moved in this step are not retriggered of course because it is impossible.
  2. Locked notes are not retriggered to prevent artificial sounding.
  3. Notes that were chosen to move, but did not move to decrease cost, have greater probability to retrigger.
  4. Higher and lower voices may have different probability to retrigger. I usually set lower probability to retrigger higher and lower voices.
  5. At the start of the measure retriggering may have different probabilities. I usually increase retriggering when 4 voices move in the beginning of the measure.
  6. I also usually set lower probability for short steps to retrigger, because when a man plays fast he usually does not play many voices at a time.

Retriggering of each note is independent and in some cases all notes except the moving one can retrigger.

An example of the resulting notes sequence is shown below (mcount is number of changing notes). Orange notes show changing notes, green are not changing, blue are retriggered. Yellow are affected by "too long note" rule. You can see more detailed information and listen to music in the Examples section below.

Step velocities generation

Velocities are generated for each note and are based on smooth model and increases for:

  1. Note length (usually the shorter the note, the higher the velocity)
  2. Note jump (usually the longer the note jump within the voice, the higher the velocity)
  3. Note pitch (usually the higher the note, the higher the velocity)

The smooth model usually has low range, which ensures that velocities do not change much randomly.

See description of smooth model in a separate section below.

Note analysis

During the analysis phase the final melody is analyzed for:

  1. Intervals of dissonance
  2. Parallel, opposite and hidden fifths and octaves
  3. Contradicting notes (see above)
  4. Locked and smothered notes are detected
  5. Chords are detected. Chords are used for pause generation. Also chords help to understand voice moves.

Find and filter culminations

Culminations are detected when there are no notes higher in range of n steps (n is customizable, I use 26 steps) and note makes a jump at least x semitones (x is customizable, usually 2).

Culminations receive rating based on their height. After the analysis all the culminations are sorted by their height and those which are lowest are thrown away. Filtering is based on percentage, which is customizable. For example, if you say to throw away 40% culminations, then if 5 culminations are detected during the whole piece, 2 of them will be thrown away and will not cause tempo changes.

Culminations, that are already stressed by the pause, are also thrown away.

Culminations, that suffer filtering process, receive tempo decrease before culmination and return of tempo to its previous value after culmination (tempo decreased due to culmination is shown yellow in the Temo row):

Add chromatic turns and introductions

Chromatic turns are introduced by moving the note down chromatically to decrease the pitch change when both notes before and after the changing note are lower. When both notes are higher, note is moved up chromatically. For this to be allowed, several requirements must be met:

  1. Jumps before and after note must be in allowed diapasone (I usually use 2 semitones strict)

In the figure G is altered down to Gb. C is altered up to C#.

Chromatic introductions are created by moving the note down to the next note when the previous note is higher and next note is lower then the current note. When previous note is lower and next note is higher then the current note, current note is moved up. For this to be allowed, several requirements must be met:

  1. Jump after note must be in allowed diapasone (I usually use 2 semitones strict)

Both chromatic turns and introductions are allowed only when the following conditions are met:

  1. There must be enough room for alteration between the altered voice and the voice in direction of alteration (I usually use 2 semitones minimum)
  2. Chromatic turns and introductions are usually allowed only when one voice is moved to prevent dissonating intervals.
  3. Chromatic turns and introductions are not allowed if length of any note in a customizable window around the changing notes is longer then some customizable length. I usually check one note before and one after to be shorter than 21.
  4. After chromatic turn and chromatic introduction all retriggered notes in the current step, that form dissonating intervals, may be deleted (this is customizable).
  5. Notes in the beginning and at the end of the piece cannot be altered (I usually set 25 starting and 25 ending notes). This prevents alterations when mode is not set up yet and when the resolving strictness is required.

Create arpeggios

Creating arpeggio is really distribution of the chord notes on the time scale. Arpeggios are created for the chords, that have at least L length (L is customizable, I use 100).

During the arpegio generation the time between arpegiated notes is chosen randomly between minimum and maximum values. Also a slowing down with random tempo can be added (a), if the length of the pause allows.

First the random time offset increase a is calculated, which falls within the available pause time.

All the voices of the current step are retriggered for longer arpeggio experience.

Generate tempo

Tempo generation has an algorithm similar to note velocities generation.

In the figure the colors are similar to the colors in the tempo histogram of the algorithm output. On the histogram you see how much tempo each algorithm subtracts:

The following is used to generate tempo:

  1. Smooth model is used with limited range to ensure that tempo does not stay constant but is not totally random.
  2. Near the long notes and chords (multivoice moves) the tempo is decreased. This is done to prepare the listener to the long notes and chords coming.
  3. When after the longer note goes a shorter note, the tempo is decreased for several steps to gradually decrease and prepare fast note passages.
  4. Culminations result in most powerful tempo decrease and you can hear it in the examples.

Generate pauses

During the pause generation stage algorithm counts the probability of creating a pause. The probability depends on:

  1. Intervals of dissonance and contradicting notes usually prohibit pauses to avoid stressing the step and to resolve faster.
  2. More long steps have greater probability of having a pause.
  3. Basic standard chords and intervals have greater probability of having a pause over complex chords like diminished, augmented, 7th, maj7 and other.
  4. Chords have more probability of having a pause then single voice melodies.
  5. Parallel, opposite and hidden fifths and octaves may decrease the possibility of having a pause.

The chart shows the probability of the pause with the intensity of the yellow color. If the probability is less than 100% there is always possibility that there will be no pause. Pause is added to step length.

Pauses are added recursively, each time checking the probability, until the maximum value is reached.

Apply velocity and tempo envelope

At the beginning of the piece tempo and note velocities gradually increase to their normal values.

At the end of the piece tempo and note velocities gradually decrease.

Tempo envelope for short melody of 30 notes is shown below:

Final time randomization

Before convertion to midi the start of each note is randomized by 1/15 of the eighth note length for less machine-gun sound.

This randomization is not included in MIDI files generated for music software to allow more exact recognition.

About Smooth modeling

In Quarter Generator smooth modeling is used for velocity and tempo modeling. I can tell you that much more profound use of smooth modeling can be found in my City Generator, where smooth models are also used for note pitches and note lengths.

Smooth model works in a step-by-step manner, modeling the second derivative of the target value, then using it to model the first derivative and then modeling the target value.

Here is an example of smooth modeling a value. Important properties of the model is that it produces cycling value without jumps, with changing period and amplitude peaks.

Examples

Here you can see several examples of the Quartet Generator in action. You can view music analysis only in Chrome. Other browsers are not supported.

  1. Short piece #1: Listen. See music analysis.
  2. Short piece #2: Listen. See music analysis.
  3. Longer pieces can be listened on the Rhaos project page (Train pieces).

Download Quartet Generator

Quartet Generator is a PHP project. To run it you need an HTTP server (Apache) with PHP installed.

You should understand that generation is very CPU intensive. For an example, generation of 100 step music (about 40 seconds of sounding) takes algorithm about 3-7 seconds to run, during which the CPU is used 100%.

Download Quartet Generator source code (180 Kb)

If you want only to change settings, do not modify any files except CQSettings.php