Click here to watch this tutorial on YouTube™
It is useful for code to behave differently given different scenarios. In programming, the conditional statement, or if-then-else statement, evaluates an expression (the condition), performing one action if true, or an alternative action if false (the else clause). In Manhattan, such functionality is expressed as follows:
(condition) ? (consequence) : (alternative)
For example, choosing a volume based on pitch:
Here, volume is set depending on a notes pitch. High pitches (above C-5) are played more softly (48), than other (lower) pitches, which play at full volume (64).
The expression uses the greater-than operator (>), one of several conditional operators available:
| x = = y | is true if x and y are equal |
| x > y | is true if x is greater than y |
| x < y | is true if x is less than y |
| x >= y | is true if x is greater than or equal to y |
| x <= y | is true if x is less than or equal to y |
| x != y | is true if x does not equal y |
| x && y | is true if both x and y are true |
| x || y | is true if either x or y are true |
| !x | is true if x is not true (or false if it is) |
Where x and y come from is up to you - any number or property can be compared with any other - their value might come from a random number, MIDI input, or a property representing the state of the music. However, one very powerful use is to do different things at different times...
- Continue to part 2, where we'll introduce the concept of loops (repeats) and use them with conditional statements to recreate the 80's synth pop classic "Blue Monday" by New Order.
Practically all music uses repeating patterns that change over time - as listeners, we unconsciously recognise the patterns and predict what's going to happen. As composers, we play with this mechanism and make changes to gratify or surprise these expectations - from which music gets its emotional impact.
We'll use loops to create patterns and conditional statements to make changes in the recreation of New Order's classic 80's synth pop track, "Blue Monday". The template for the piece consists of three looped sections of the pattern - Intro, Chorus, and Fill.
Use loops and if-then-else to recreate the drum part of New Order's "Blue Monday":
A distinctive feature of Blue Monday is its mechanical bass drum 'roll'. This can be seen in the Chorus section (Channel 02, Rows 16-24), but is currently missing from the Intro.
To add it, we will use a conditional statement that detects which repeat we are on and inserts the notes accordingly:
- Sections repeat via the SB0/SBx (row repeat) effect. Within a channel, SB0 marks the start of a repeat and SBx marks the last row to be repeated, where x is the number of repeats. All channels will be repeated - and repeats can also be repeated (nested). For example...
- The Intro section covers the first eight rows of the pattern and features a repeating bass drum. The section will play eight times - four times due to SB3 (= 3 repeats), which is then itself repeated in Channel 3 (SB1), for a total of 16 drum hits (two per loop; 2 x 4 x 2 = 16).
- We want to retrigger the drum on the first repeat of Channel 2:
To do this, add the following code for the cell in the second row of Channel 2 (labelled "Ex 5.2"):0 1 2 3 ¤···¤··· ¤¤¤¤¤¤¤¤ ¤···¤··· ¤···¤··· @Intro.repeat == 1 ? [0] : noneThe .repeat property tells us the current repeat of the channel containing the "Intro" label. On the first repeat (2nd play), the initial drum (in Row 0) will be copied; otherwise (for the 1st, 3rd and 4th play) the cell is cleared (= none).
Subsequent cells simply copy whatever's placed in this row, so this is the only code we need.
- Audition the Intro to ensure the drums sound the like the Chorus, before moving to step 3.
The rest of the song is made up of two further repeated sections, where we'll again use the .repeat property to vary the Chorus and Fill respectively.
(a) Vary the bass to complete the chorus:
The Chorus section is the core of the song, featuring a rich drum pattern and bass part, repeated four times.
A classic example of the use of repetition in music, the final repeat should subtly change, starting with a G instead of an F...
- Add an if-then-else statement for the first note in the bass part (Row 4) that sets pitch as G-2 instead of F-2 on the third and final repeat.
Vary the bassUse the following code for [05:008].pitch:@Chorus.repeat == 3 ? G-2 : F-2
- Ask Chorus for its current .repeat property.
- If it equals 3 (third repeat, fourth time), set pitch to G-2, else default to F-2.
- Using cell addresses and simple arithmetic, add a formula for the subsequent note, copying from the previous note but raising it an octave (+12 semitones).
Follow the leadUse the following code for cell [05:010]:.pitch[-2].pitch + 12
- Reference the cell two rows back.
- Read its .pitch property.
- Add 12 semitones (one octave).
When you listen to the chorus now, the bass should go F-C-D three times, then G-C-D. The change is made that much more effective (and affective) because of the previous repetition.
(b) Finish the fill with an offbeat snare:
The piece's similarly distinctive Fill punctuates the Chorus and features a series of open hi-hats and snare roll. The phrase is repeated twice, but should gain a snare hit on the final offbeat (Row 52) on the second-time repeat.
- Use what you have learnt to add a suitable conditional statement for the pitch formula in cell [03:52] that triggers a snare (E-2) on the second iteration (first repeat).
And the beat goes off...Use the following code for cell [03:052]:.pitch@Fill.repeat ? E-2 : none
- Ask Fill for its current .repeat property.
- If it's the repeat, set pitch to E-2, else default to blank (none).
- Notice how the Cxx (Pattern Break) effect causes playback to jump back (or 'cut') to Row 8, rather than the start of the pattern. This effect mirrors the programming concept of branching (also known as a jump, or goto statement), which can be similarly controlled with formulas using conditions (as in conditional branching).
- Finally, audition the completed pattern, and check it sounds as expected. Refer to online recordings of the original track, for comparison.
(c) Explore the extended version:
A completed and significantly extended version (with synth lead, choir, and bass guitar) of the exercise can be found in the Help > Examples > Popular menu. Have a look at the added channels to see how the synth lead, choir and bass guitar are added and varied - and note how adding extra layers of nested loops can build towards longer, more complex works.
Finally, think about how you might capture other variations in pieces, using what you have learnt about formulas - which now includes all the fundamental concepts in programming: variables, expressions, functions, arrays, loops, and conditional statements.
There are many other features of the software and aspects to programming, but these provide the basic building blocks required for most coding tasks. Often, more advanced applications are simply a matter of finding innovative ways to combine simple ideas - so be creative!