import { type Editor } from "../../main";
import { makeExampleFactory } from "../../Documentation";
export const patterns = (application: Editor): string => {
const makeExample = makeExampleFactory(application);
return `
# Array patterns
**Topos** is using arrays as a way to make dynamic patterns of data (rhythms, melodies, etc).
It means that the following:
${makeExample(
"Boring kick",
`
beat(1)::sound('kick').out()
`,
true,
)}
can be turned into something more interesting like this easily:
${makeExample(
"Less boring kick",
`
let c = [1,2].dur(3, 1)
beat([1, 0.5, 0.25].dur(0.75, 0.25, 1) / c)::sound(['kick', 'fsoftsnare'].beat(0.75))
.ad(0, .25).shape(usine(1/2)*0.5).speed([1, 2, 4].beat(0.5)).out()
`,
true,
)}
**Topos** comes with a lot of array methods to deal with musical patterns of increasing complexity. Some knowledge of patterns and how to use them will help you to break out of simple loops and repeating structures. The most basic JavaScript data structure is the [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). Topos is extending it with custom methods to describe patterns that evolve over time. These methods can often be chained to compose more complex expressions: [1, 2, 3].repeatOdd(5).palindrome().beat().
## Temporal iteration
- beat(division: number): this method will return the next value in the list every _n_ pulses. By default, 1 equals to one beat but integer and floating point number values are supported as well. This method is extremely powerful and can be used for many different purposes. Check out the examples.
${makeExample(
"Light drumming",
`
// Every bar, use a different rhythm
beat([1, 0.75].beat(4)) :: sound('cp').out()
beat([0.5, 1].beat(4)) :: sound('kick').out()
beat(2)::snd('snare').shape(.5).out()
`,
true,
)}
${makeExample(
"Using beat to create arpeggios",
`
// Arpeggio using pulse divisions
beat([.5, .25].beat(0.5)) :: sound('sine')
.lpf(100+usine(1/4)*400).lpad(2, 0, .25)
.fmi([1,2].beat(8)).fmh([1, 2].beat(0.5))
.note([0,2,4,5].scale('minor', 40).beat(0.25)
+ [0, 7].bar()
+ [12,24].beat(0.5))
.sustain([0.25, 0.5].beat(8))
.room(0.9).size(0.5)
.delay(0.5).delayt([0.5,0.25].beat(16))
.delayfb(0.5)
.out()
`,
false,
)}
${makeExample(
"Cool ambiance",
`
beat(.5) :: snd(['kick', 'hat'].beat(0.5)).out()
beat([2,4].beat(2)) :: snd('shaker').delay(.5).delayfb(.75).delayt(0.125).out()
flip(2)::beat(1)::snd('froomy').out()
flip(4)::beat(2)::snd('pad').n(2).shape(.5)
.orbit(2).room(0.9).size(0.9).release(0.5).out()
`,
false,
)}
- bar(value: number = 1): returns the next value every bar (if value = 1). Using a larger value will return the next value every n bars.
${makeExample(
"A simple drumbeat in no time!",
`
beat(1)::sound(['kick', 'hat', 'snare', 'hat'].beat()).out()
beat([1/4, 1/2].dur(1.5, 0.5))::sound(['jvbass', 'fikea'].bar())
.ad(0, .25).room(0.5).size(2).resonance(0.15).lpf(
[200,400,800,1200,2000].beat(2)
* [1, 2].bar())
.out()
`,
true,
)}
${makeExample(
"Using beat and bar in the same example",
`
beat(2)::snd('snare').out()
beat([1, 0.5].beat()) :: sound(['bass3'].bar())
.freq(100).n([12, 14].bar())
.room(0.5).size(4).orbit(2)
.pan(r(0, 1))
.speed([1,2,3].beat())
.out()
`,
)}
- dur(...list: numbers[]) : keeps the same value for a duration of n beats corresponding to the nth number of the list you provide.
${makeExample(
"Holding a value for n beats",
`
// The second note is kept for twice as long
beat(0.5)::sound('notes').n([1,2].dur(1, 2))
.room(0.5).size(8).delay(0.125).delayt(1/8)
.speed(0.5).ad(0, .125).out()
// Kick (3 beats), Snare (1bpm beat)
beat(1)::sound(['kick', 'fsnare'].dur(3, 1))
.n([0,3].dur(3, 1)).out()
`,
true,
)}
${makeExample(
"Patterning with ternary statements",
`
const dada = flipbar(2) ? [0,[3,5,-1].bar(3),2,3] : [9,8,9,6]
beat(0.5) :: sound('wt_hvoice:3')
.pitch(dada.beat(0.5))
.scale("88.0")
.adsr(0.05, 0.05, 0, 0)
.cutoff(500 + usine(1/8) * 5000)
.room(1.5)
.resonance(0.25)
.out()
beat(1) :: sound('kick').n(4).out()
onbeat([0.5,0.8].beat(1),2) :: sound('snare').out()
onbeat(0.5,0.8,1,1.5,2,2.5,3,4) :: sound('hh').out()
`,
true,
)}
## Iteration using a counter
- counter(name,limit?,step?): return the next value on the list based on counter value. The limit is optional and defaults to the length of the list. The step is optional and defaults to 1. Setting / changing limit will reset the counter.
- $(name,limit?,step?): shorter alias for the counter.
${makeExample(
"Using counter to iterate over a list",
`
beat(0.5) :: sound("bd").gain(line(0,1,0.01).$("ramp")).out()
`,
true,
)}
## Manipulating notes and scales
- pitch(): convert a list of integers to pitch classes
${makeExample(
"Converting a list of integers to pitch classes using key and scale",
`
beat(0.25) :: snd('sine')
.pitch([0,1,2,3,4,6,7,8].beat(0.125))
.key(["F4","F3"].beat(2.0))
.scale("minor").ad(0, .25).out()
`,
true,
)}
- semitones(number[], ...args?): Create scale from semitone intervals.
${makeExample(
"Play pitches from scale created from semitone intervals",
`
beat(1) :: sound('gtr').pitch([0, 4, 3, 2].beat()).key(64)
.semitones(1, 1, 3, 1, 1, 2, 3).out()
`,
true,
)}
- cents(number[], ...args?): Create scale from cent intervals.
${makeExample(
"Play pitches from scale created from cent intervals",
`
rhythm([0.5,0.25].beat(1),14,16) :: sound('pluck')
.stretch(iR(1,5)).pitch(iR(0,6)).key(57)
.cents(120,270,540,670,785,950,1215).out()
`,
true,
)}
- ratios(number[], ...args?): Create scale from ratios.
${makeExample(
"Play pitches from scale created from ratios",
`
rhythm([0.5,0.25].beat(0.25),5,7) :: sound('east:3')
.pitch([0,1,2,3,4,5,6,7,8,9,10,11].beat(0.25)).key(67)
.ratios(2/11,4/11,6/11,8/11,10/11,11/11).out()
`,
true,
)}
- edo(number, scale?: string|number[]): Create scale from equal divisions of the octave. Creates chromatic scale by default.
${makeExample(
"Play pitches from scale created from equal divisions of the octave",
`
z0("e bd bd ").sound().out()
flipbar(1) :: rhythm(.25,14,16) :: sound("ST10:30").stretch(3).gain(0.5)
.pitch([0,10,r(20,40),r(100,200),r(-200,200),r(200,300),200,r(3,666)].beat([1.0,0.5,0.25].bar(6)))
.octave(r(-6,6))
.edo(666,"rocritonic")
.out()
rhythm(2.0,26,32) :: sound("ST20").n([22,5,24,34,31,5,11,19].pick()).stretch(rI(1,6))
.pitch(rI(127,300))
.edo(666)
.out()
`,
true,
)}
- scale(scale: string, base note: number): Map each element of the list to the closest note of the slected scale. [0, 2, 3, 5 ].scale("major", 50) returns [50, 52, 54, 55]. You can use western scale names like (Major, Minor, Minor pentatonic ...) or [zeitler](https://ianring.com/musictheory/scales/traditions/zeitler) scale names. Alternatively you can also use the integers as used by Ian Ring in his [study of scales](https://ianring.com/musictheory/scales/).
${makeExample(
"Mapping the note array to the E3 major scale",
`
beat(1) :: snd('gtr')
.note([0, 5, 2, 1, 7].scale("Major", 52).beat())
.out()
`,
true,
)}
- scaleArp(scale: string, mask: number): extrapolate a custom-masked scale from each list elements. [0].scale("major", 3) returns [0,2,4]. scaleArp supports the same scales as scale.
${makeExample(
"Extrapolate a 3-elements Mixolydian scale from 2 notes",
`
beat(1) :: snd('gtr')
.note([0, 5].scaleArp("mixolydian", 3).beat() + 50)
.out()
`,
true,
)}
## Iteration using the mouse
- mouseX() / mouseY(): divides the screen in n zones and returns the value corresponding to the mouse position on screen.
${makeExample(
"Controlling an arpeggio (octave and note) with mouse",
`
beat(0.25)::sound('wt_piano')
.note([0,2,3,4,5,7,8,9,11,12].scale(
'minor', 30 + [0,12,24].mouseY()).mouseX())
.room(0.5).size(4).lpad(-2, .2).lpf(500, 0.3)
.ad(0, .2).out()
`,
true,
)}
## Simple data operations
- palindrome(): Concatenates a list with the same list in reverse.
${makeExample(
"Palindrome filter sweep",
`
beat([1,.5,.25].beat()) :: snd('wt_stereo')
.speed([1, 0.5, 0.25])
.pan(r(0, 1)).freq([100,200,300].beat(0.25))
.fmi([1,2,3].palindrome().beat(0.5))
.fmh([0.5, 1].palindrome().beat())
.lpf([500,1000,2000,4000].palindrome().beat())
.lpad(4, 0, .25).sustain(0.125).out()
`,
true,
)}
- random(index: number): pick a random element in the given list.
- rand(index: number): shorter alias for the same method.
${makeExample(
"Sipping some gasoline at the robot bar",
`
// rand, random and pick are doing the same thing!
beat(1)::snd('fhardkick').shape(0.5)
.ad(0, .1).lpf(500).db(-12).out()
beat([.5, 1].rand() / 2) :: snd(
['amencutup', 'synth'].random())
.clip(1).n(irand(4,10)).room(0.5)
.size(3).freq(200)
.lpf([5000,3000,2000].pick())
.end(0.5).out()
`,
true,
)}
- pick(): pick a random element in the list.
${makeExample(
"Picking values in lists",
`
beat(0.25)::sound(['ftabla', 'fwood'].pick())
.speed([1,2,3,4].pick()).ad(0, .125).n(ir(1,10))
.room(0.5).size(1).out()
`,
true,
)}
- degrade(amount: number): removes _n_% of the list elements. Lists can be degraded as long as one element remains. The amount of degradation is given as a percentage.
${makeExample(
"Amen break suffering from data loss",
`
// Tweak the value to degrade this amen break even more!
beat(.25)::snd('amencutup').n([1,2,3,4,5,6,7,8,9].degrade(20).loop($(1))).out()
`,
true,
)}
- repeat(amount: number): repeat every list elements _n_ times.
- repeatEven(amount: number): repeat every pair element of the list _n_ times.
- repeatOdd(amount: number): repeat every odd element of the list _n_ times.
${makeExample(
"Repeating samples a given number of times",
`
beat(.25)::sound('amencutup').n([1,2,3,4,5,6,7,8].repeat(4).beat(.25)).out()
`,
true,
)}
- loop(index: number): loop takes one argument, the _index_. It allows you to iterate over a list using an iterator such as a counter. This is super useful to control how you are accessing values in a list without relying on a temporal method such as .beat() or .bar().
${makeExample(
"Don't you know how to count up to 5?",
`
beat(1) :: sound('numbers').n([1,2,3,4,5].loop($(3, 10, 2))).out()
`,
true,
)}
- shuffle(): this: shuffles a list! Simple enough!
${makeExample(
"Shuffling a list for extra randomness",
`
beat(1) :: sound('numbers').n([1,2,3,4,5].shuffle().loop($(1)).out()
`,
true,
)}
- rotate(steps: number): rotate a list to the right _n_ times. The last value become the first, rinse and repeat.
${makeExample(
"To make things more complex... here you go",
`
beat(.25) :: snd('sine').fmi([1.99, 2])
.ad(0, .125).lpf(500+r(1,400))
.lpad(usine()*8, 0, .125)
.fmenv(2).fmdecay(0.125).fmsustain(0)
.delay(0.5).fmh(parseInt(usine(1/12)*3))
.note(["C3", "E3", "G3", "Bb3", "D4"]
.rotate([0, 1, 3, 5].beat(4)) // Rotation over notes
.beat(.25)) // while the index changes
.out()
`,
true,
)}
## Filtering
- unique(): filter a list to remove repeated values.
${makeExample(
"Demonstrative filtering. Final list is [100, 200]",
`
// Remove unique and 100 will repeat four times!
beat(1)::snd('sine').sustain(0.1).freq([100,100,100,100,200].unique().beat()).out()
`,
true,
)}
## Simple math operations
- add(): add a given amount to every list element.
- sub(): add a given amount to every list element.
- mult(): add a given amount to every list element.
- div(): add a given amount to every list element.
${makeExample("Simple addition", `[1, 2 ,3].add(2).beat()`, true)}
`;
};