• No results found

The random Pattern

In document Algorithmic Composition (Page 179-183)

Algorithmic Composition

Chapter 13 Algorithmic Composition Using Probabilistic

13.2 The random Pattern

Chapter 13 Algorithmic Composition Using Probabilistic

Methods

Chapter 5 introduced the random item stream pattern type. Using the random item stream pattern type, we were able to select randomly among sets of values. In music, we often want some choices to be more likely than others, or a choice might depend upon the previous choice. In this chapter, we will explore probability theory and ways to make biased (and hopefully more musical) choices.

13.1 Introduction to Probability

Probability is a branch of mathematics that studies the chance or likelihood that an event will occur. A probability is expressed as a ratio – the number of times a given event is expected to occur di-vided by the total number of outcomes. If all outcomes are equally likely, the probability of some event is simply the number of out-comes in which the event is present divided by the total number of outcomes. For example, consider a six-sided die where each side is uniquely identified 1, 2, 3, 4, 5, and 6. The probability that a 1 will be rolled is 1:6. The ratio 1:6 may also be expressed as a real number between 0 and 1. Considering the example of the six-sided die, the sum of all possible outcomes is 6/6 or 1.0 (100%).

13.2 The

random

Pattern

A random pattern is created by calling make-random. The first in-put is a list of items to choose among. In the simplest case, a call to make-random might look like

make-random({2, 3, 5, 7, 11, 13, 17})

which generates a stream of small prime numbers chosen randomly from the list. In this case, the probability of choosing any particular number is 1/7.

To make some elements more likely than others, we can specify a weight. To associate a weight with an item, the item is replaced by a list containing the item, the keyword weight:, and the weight, a

13.2 The random Pattern 165

numerical value that defaults to 1. Example 13.2.1 illustrates a ran-dom pattern where the first element is assigned a weight.

Example 13.2.1: Random pattern with weights

set note = make-random({{c4 weight: 0.5} d4 e4}) Notice that while weight: has the appearance of a keyword in a pa-rameter list, here it is just a symbol in a list, and since the list is con-structed with braces, there are no commas separating any of the list elements. Because the weight for c4 is 0.5, c4 is 1/2 as likely to be selected as d4 or e4, which have default weights of 1. Given that there are 3 notes and the sum of their probabilities must equal 1 (or 100%), and c4 is 1/2 as likely to be selected as d4 or e4, Table 13.2.1 shows the probabilities of the note events.

Table 13.2.1: Note probabilities for Example 13.2.1 Note Name C4 D4 E4

Weight 0.2 0.4 0.4

To compute these probabilities, divide the weight of an element (e.g. 0.5 for c4) by the sum of all weights (e.g. 0.5 + 1 + 1, or 2.5).

Thus the weight for c4 is 0.5/2.5 = 0.2, and the weight for d4 is 1/2.5

= 0.4.

If no weights are specified, the probabilities of all events are equal. The term “white” is often used informally to describe equal probability. This terminology comes from natural white light, which is light in which all visible frequencies (or colors) are present in equal strength. By analogy, we can describe audio as “white noise”

when all audible frequencies are equally intense. It turns out that each individual audio sample value of digitized white noise is equally likely and unrelated to other samples, as if sample values were chosen by rolling a die. (Admittedly, it would be hard to find a die with 65,536 sides to represent all the different sample values in 16-bit digital audio!) Thus, a sequence of random outcomes from a die or any other random process with equally probable outcomes can be called “white noise.” In Example 13.2.2, the range of events is the rhythms for a quarter, sixteenth, and eighth note.

Example 13.2.2: Random rhythm pattern

set rhythm-pattern = make-random(list(q, s, i)) Table 13.2.2 shows the selection of rhythms based on equal prob-ability.

Table 13.2.2: Rhythm probability table

Rhythm Quarter Sixteenth eIghth Weight .333 .333 .333

With random events such as flipping coins, rolling dice, or getting the next item from rhythm-pattern, the next outcome is completely independent of all previous outcomes. This can be counterintuitive:

most people feel that after a coin toss yields “heads” four times in a row, the next toss is more likely to be “tails.” This is known as the gambler’s fallacy. The probability of the next toss coming up heads is always 0.5, regardless of what happened in the past.

Perhaps because we feel that an uninterrupted run of heads or tails is particularly unlikely, these runs stand out. Similarly, a melody may not sound “random” if pitches are repeated, and these repeti-tions may sound wrong or out-of-place. The random pattern is nor-mally like a coin toss. To generate each item, the next function se-lects an item at random according to the weights. This may result in runs where the same item is chosen several times in a row.

To avoid this behavior, the option max: sets a maximum value an element may be repeated before a different element must be selected.

Alternatively, the min: keyword forces a number of direct repetitions before a different element is selected. If min: or max: is used with make-random, it is possible that the generated sequence of items will not be in proportion to the specified weights. You can think of starting with a random sequence that is constructed according to the weights and then removing items that violate the max: constraints and inserting items to satisfy the min: constraints.

Example 13.2.3 indicates that 0.1 and 0.2 can repeat at most one time before a new (and different) event must be selected. 0.3 and 0.4 must repeat at least 2 times before a different event is selected.

Example 13.2.3: Using max: and min: in random pattern set amplitude-pattern =

make-random({{0.1 max: 1} {0.2 max: 1}

{0.3 min: 2} {0.4 min: 2}}) Example 13.2.4 illustrates a simple score with random pitch, duration, and velocity. The patterns are constructed first and bound to variables in the with expression. The body of the begin-end is a call to score-gen, which uses these patterns to construct successive notes. Patterns are constructed with make-random, which takes a list of items.

13.2 The random Pattern 167

score-print is used to print the result. In this case there are 1 C4 (60), 5 D4s (62), and 6 E4s (64). There are 5 quarter notes (1), 6 eighth notes (0.5), and 1 sixteenth note (0.25). The vel: parameter does not have successive values of 70 or 80 (where max: is 1), and ((0 0 (SCORE-BEGIN-END 0 8.25)) (0 0.25 (NOTE vel: 70 pitch: 62))

In Example 13.2.4, notice that pitch-pattern and rhythm-pattern are specified using calls to list, but velocity-pattern is specified using braces {}. Most of the previous examples use braces because the syntax is simpler and easier to read. However, braces implicitly quote all of the list elements. This is fine when elements are numbers and/or symbols, but not good when we want to use the values of variables.

In Example 13.2.5, similar lists are formed using list and braces.

Notice in the first two cases, using list, that the variable c4 is evaluated to obtain 60, but in the third case, c4 in braces is an unevaluated symbol, and the printout shows that the list contains the symbol c4. Use list to form lists when you want the values of variables such as symbolic pitches (c4) or durations (q).

This example also illustrates two ways to introduce a keyword into a list. The first uses keyword(name) to construct a symbol. The second uses weight: without a subsequent comma. This only works when the keyword is followed by another expression. (This notation is also used in Example 13.2.4).

Example 13.2.5: Comparing the list function to brace notation SAL> print list(c4, keyword(weight), 0.5), list(c4, weight: 0.5),

{c4 weight: 0.5}

{60 :WEIGHT 0.5} {60 :WEIGHT 0.5} {C4 :WEIGHT 0.5}

In document Algorithmic Composition (Page 179-183)