Skip to content

Commit

Permalink
Merge branch 'tidalcycles:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
edcrub authored Jan 19, 2025
2 parents 15d8866 + 8d28317 commit 9d5fbde
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 9 deletions.
23 changes: 23 additions & 0 deletions packages/core/pattern.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ function _composeOp(a, b, func) {
div: [numeralArgs((a, b) => a / b)],
mod: [numeralArgs(_mod)],
pow: [numeralArgs(Math.pow)],
log2: [numeralArgs(Math.log2)],
band: [numeralArgs((a, b) => a & b)],
bor: [numeralArgs((a, b) => a | b)],
bxor: [numeralArgs((a, b) => a ^ b)],
Expand Down Expand Up @@ -3112,3 +3113,25 @@ export let xfade = (a, pos, b) => {
Pattern.prototype.xfade = function (pos, b) {
return xfade(this, pos, b);
};

/**
* creates a structure pattern from divisions of a cycle
* especially useful for creating rhythms
* @name beat
* @example
* s("bd").beat("0:7:10", 16)
* @example
* s("sd").beat("4:12", 16)
*/
const __beat = (join) => (t, div, pat) => {
t = Fraction(t).mod(div);
div = Fraction(div);
const b = t.div(div);
const e = t.add(1).div(div);
return join(pat.fmap((x) => pure(x)._compress(b, e)));
};

export const { beat } = register(
['beat'],
__beat((x) => x.innerJoin()),
);
35 changes: 33 additions & 2 deletions packages/core/signal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th
*/

import { Hap } from './hap.mjs';
import { Pattern, fastcat, reify, silence, stack, register } from './pattern.mjs';
import { Pattern, fastcat, pure, register, reify, silence, stack } from './pattern.mjs';
import Fraction from './fraction.mjs';

import { id, keyAlias, getCurrentKeyboardState } from './util.mjs';
Expand Down Expand Up @@ -160,6 +160,37 @@ const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n);
*/
export const run = (n) => saw.range(0, n).floor().segment(n);

/**
* Creates a pattern from a binary number.
*
* @name binary
* @param {number} n - input number to convert to binary
* @example
* "hh".s().struct(binary(5))
* // "hh".s().struct("1 0 1")
*/
export const binary = (n) => {
const nBits = reify(n).log2(0).floor().add(1);
return binaryN(n, nBits);
};

/**
* Creates a pattern from a binary number, padded to n bits long.
*
* @name binaryN
* @param {number} n - input number to convert to binary
* @param {number} nBits - pattern length, defaults to 16
* @example
* "hh".s().struct(binaryN(55532, 16))
* // "hh".s().struct("1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 0")
*/
export const binaryN = (n, nBits = 16) => {
nBits = reify(nBits);
// Shift and mask, putting msb on the right-side
const bitPos = run(nBits).mul(-1).add(nBits.sub(1));
return reify(n).segment(nBits).brshift(bitPos).band(pure(1));
};

export const randrun = (n) => {
return signal((t) => {
// Without adding 0.5, the first cycle is always 0,1,2,3,...
Expand Down Expand Up @@ -499,7 +530,7 @@ export const undegrade = register('undegrade', (pat) => pat._undegradeBy(0.5), t

export const sometimesBy = register('sometimesBy', function (patx, func, pat) {
return reify(patx)
.fmap((x) => stack(pat._degradeBy(x), func(pat._undegradeBy(1 - x))))
.fmap((x) => stack(pat._degradeBy(x), func(pat)._undegradeBy(1 - x)))
.innerJoin();
});

Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/pattern.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ import {
rev,
time,
run,
binaryN,
pick,
stackLeft,
stackRight,
stackCentre,
s_cat,
calculateTactus,
sometimes,
} from '../index.mjs';

import { steady } from '../signal.mjs';
Expand Down Expand Up @@ -958,6 +960,18 @@ describe('Pattern', () => {
expect(run(4).firstCycle()).toStrictEqual(sequence(0, 1, 2, 3).firstCycle());
});
});
describe('binaryN', () => {
it('Can make a binary pattern from a decimal', () => {
expect(binaryN(55532).firstCycle()).toStrictEqual(
sequence(1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0).firstCycle(),
);
});
it('Can make a binary pattern from patterned inputs', () => {
expect(binaryN(pure(0x1337), pure(14)).firstCycle()).toStrictEqual(
sequence(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1).firstCycle(),
);
});
});
describe('ribbon', () => {
it('Can ribbon', () => {
expect(cat(0, 1, 2, 3, 4, 5, 6, 7).ribbon(2, 4).fast(4).firstCycle()).toStrictEqual(
Expand Down Expand Up @@ -1253,4 +1267,13 @@ describe('Pattern', () => {
expect(s('bev').chop(8).loopAt(2).tactus).toStrictEqual(Fraction(4));
});
});
describe('sometimes', () => {
it('works with constant functions', () => {
expect(
pure('a')
.sometimes((x) => pure('b'))
.fast(16).firstCycleValues.length,
).toStrictEqual(16);
});
});
});
72 changes: 72 additions & 0 deletions test/__snapshots__/examples.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,24 @@ exports[`runs examples > example "bank" example index 0 1`] = `
]
`;

exports[`runs examples > example "beat" example index 0 1`] = `
[
"[ 0/1 → 1/16 | s:bd ]",
"[ 1/1 → 17/16 | s:bd ]",
"[ 2/1 → 33/16 | s:bd ]",
"[ 3/1 → 49/16 | s:bd ]",
]
`;

exports[`runs examples > example "beat" example index 1 1`] = `
[
"[ 1/48 → 1/12 | s:sd ]",
"[ 49/48 → 13/12 | s:sd ]",
"[ 97/48 → 25/12 | s:sd ]",
"[ 145/48 → 37/12 | s:sd ]",
]
`;

exports[`runs examples > example "begin" example index 0 1`] = `
[
"[ 0/1 → 1/2 | s:rave begin:0 ]",
Expand All @@ -967,6 +985,60 @@ exports[`runs examples > example "begin" example index 0 1`] = `
]
`;

exports[`runs examples > example "binary" example index 0 1`] = `
[
"[ 0/1 → 1/3 | s:hh ]",
"[ 2/3 → 1/1 | s:hh ]",
"[ 1/1 → 4/3 | s:hh ]",
"[ 5/3 → 2/1 | s:hh ]",
"[ 2/1 → 7/3 | s:hh ]",
"[ 8/3 → 3/1 | s:hh ]",
"[ 3/1 → 10/3 | s:hh ]",
"[ 11/3 → 4/1 | s:hh ]",
]
`;

exports[`runs examples > example "binaryN" example index 0 1`] = `
[
"[ 0/1 → 1/16 | s:hh ]",
"[ 1/16 → 1/8 | s:hh ]",
"[ 3/16 → 1/4 | s:hh ]",
"[ 1/4 → 5/16 | s:hh ]",
"[ 1/2 → 9/16 | s:hh ]",
"[ 9/16 → 5/8 | s:hh ]",
"[ 5/8 → 11/16 | s:hh ]",
"[ 3/4 → 13/16 | s:hh ]",
"[ 13/16 → 7/8 | s:hh ]",
"[ 1/1 → 17/16 | s:hh ]",
"[ 17/16 → 9/8 | s:hh ]",
"[ 19/16 → 5/4 | s:hh ]",
"[ 5/4 → 21/16 | s:hh ]",
"[ 3/2 → 25/16 | s:hh ]",
"[ 25/16 → 13/8 | s:hh ]",
"[ 13/8 → 27/16 | s:hh ]",
"[ 7/4 → 29/16 | s:hh ]",
"[ 29/16 → 15/8 | s:hh ]",
"[ 2/1 → 33/16 | s:hh ]",
"[ 33/16 → 17/8 | s:hh ]",
"[ 35/16 → 9/4 | s:hh ]",
"[ 9/4 → 37/16 | s:hh ]",
"[ 5/2 → 41/16 | s:hh ]",
"[ 41/16 → 21/8 | s:hh ]",
"[ 21/8 → 43/16 | s:hh ]",
"[ 11/4 → 45/16 | s:hh ]",
"[ 45/16 → 23/8 | s:hh ]",
"[ 3/1 → 49/16 | s:hh ]",
"[ 49/16 → 25/8 | s:hh ]",
"[ 51/16 → 13/4 | s:hh ]",
"[ 13/4 → 53/16 | s:hh ]",
"[ 7/2 → 57/16 | s:hh ]",
"[ 57/16 → 29/8 | s:hh ]",
"[ 29/8 → 59/16 | s:hh ]",
"[ 15/4 → 61/16 | s:hh ]",
"[ 61/16 → 31/8 | s:hh ]",
]
`;

exports[`runs examples > example "bite" example index 0 1`] = `
[
"[ 0/1 → 1/8 | note:Bb3 ]",
Expand Down
10 changes: 4 additions & 6 deletions test/__snapshots__/tunes.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports[`renders tunes > tune: amensister 1`] = `
"[ 0/1 → 1/4 | n:0 s:amencutup room:0.5 ]",
"[ 1/16 → 1/8 | s:breath room:1 shape:0.6 begin:0.875 end:0.9375 ]",
"[ 1/8 → 3/16 | s:breath room:1 shape:0.6 begin:0.8125 end:0.875 ]",
"[ 1/8 → 1/4 | n:0 s:amencutup room:0.5 ]",
"[ 1/8 → 1/4 | note:45 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:300.174310575404 ]",
"[ 1/8 → 1/4 | note:45 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:300.174310575404 ]",
"[ 3/16 → 1/4 | s:breath room:1 shape:0.6 begin:0.75 end:0.8125 ]",
Expand All @@ -17,22 +18,20 @@ exports[`renders tunes > tune: amensister 1`] = `
"[ 1/4 → 3/8 | note:A1 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:300.7878869297153 ]",
"[ 5/16 → 3/8 | s:breath room:1 shape:0.6 begin:0.625 end:0.6875 ]",
"[ 3/8 → 7/16 | s:breath room:1 shape:0.6 begin:0.5625 end:0.625 ]",
"[ 3/8 → 1/2 | n:1 s:amencutup room:0.5 ]",
"[ 3/8 → 1/2 | note:F1 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:302.11020572391345 ]",
"[ 3/8 → 1/2 | note:F1 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:302.11020572391345 ]",
"[ 7/16 → 1/2 | s:breath room:1 shape:0.6 begin:0.5 end:0.5625 ]",
"[ 1/2 → 9/16 | s:breath room:1 shape:0.6 begin:0.4375 end:0.5 ]",
"[ 1/2 → 5/8 | n:2 s:amencutup room:0.5 ]",
"[ 1/2 → 3/4 | n:2 s:amencutup room:0.5 ]",
"[ 9/16 → 5/8 | s:breath room:1 shape:0.6 begin:0.375 end:0.4375 ]",
"[ 5/8 → 11/16 | s:breath room:1 shape:0.6 begin:0.3125 end:0.375 ]",
"[ 11/16 → 3/4 | s:breath room:1 shape:0.6 begin:0.25 end:0.3125 ]",
"[ 3/4 → 13/16 | s:breath room:1 shape:0.6 begin:0.1875 end:0.25 ]",
"[ 3/4 → 7/8 | n:3 s:amencutup room:0.5 ]",
"[ 3/4 → 7/8 | note:Bb0 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:312.54769231985796 ]",
"[ 3/4 → 7/8 | note:Bb0 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:312.54769231985796 ]",
"[ 13/16 → 7/8 | s:breath room:1 shape:0.6 begin:0.125 end:0.1875 ]",
"[ 7/8 → 15/16 | s:breath room:1 shape:0.6 begin:0.0625 end:0.125 ]",
"[ 7/8 → 1/1 | n:3 s:amencutup room:0.5 ]",
"[ 7/8 → 1/1 | note:D1 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:318.7927796831686 ]",
"[ 7/8 → 1/1 | note:D1 s:sawtooth gain:0.4 decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 resonance:10 cutoff:318.7927796831686 ]",
"[ 15/16 → 1/1 | s:breath room:1 shape:0.6 begin:0 end:0.0625 ]",
Expand Down Expand Up @@ -6906,18 +6905,17 @@ exports[`renders tunes > tune: flatrave 1`] = `
"[ 1/8 → 1/4 | s:hh n:1 speed:0.5 delay:0.5 end:0.020001936784171157 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/8 → 1/4 | s:hh n:1 speed:0.5 delay:0.5 end:0.020001936784171157 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/8 → 1/4 | note:G1 s:sawtooth decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 cutoff:800 resonance:8 ]",
"[ 1/4 → 3/8 | s:hh n:1 end:0.02000875429921906 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/4 → 3/8 | s:hh n:1 end:0.02000875429921906 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/4 → 3/8 | note:G1 s:sawtooth decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 cutoff:800 resonance:8 ]",
"[ 3/8 → 1/2 | s:hh n:1 end:0.020023446730265706 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/2 → 5/8 | s:hh n:1 speed:0.5 delay:0.5 end:0.020048626493108724 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/2 → 5/8 | s:hh n:1 speed:0.5 delay:0.5 end:0.020048626493108724 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 1/2 → 5/8 | note:G1 s:sawtooth decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 cutoff:800 resonance:8 ]",
"[ 1/2 → 1/1 | s:bd bank:RolandTR909 ]",
"[ 1/2 → 1/1 | s:cp bank:RolandTR909 ]",
"[ 1/2 → 1/1 | s:sd bank:RolandTR909 ]",
"[ 5/8 → 3/4 | s:hh n:1 end:0.020086608138500644 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 5/8 → 3/4 | s:hh n:1 end:0.020086608138500644 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 5/8 → 3/4 | note:G1 s:sawtooth decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 cutoff:800 resonance:8 ]",
"[ 3/4 → 7/8 | s:hh n:1 end:0.02013941880355398 bank:RolandTR909 room:0.5 gain:0.4 ]",
"[ 7/8 → 1/1 | note:G1 s:sawtooth decay:0.1 sustain:0 lpattack:0.1 lpenv:-4 cutoff:800 resonance:8 ]",
]
`;
Expand Down
1 change: 1 addition & 0 deletions website/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const SIDEBAR: Sidebar = {
{ text: 'Coding syntax', link: 'learn/code' },
{ text: 'Pitch', link: 'understand/pitch' },
{ text: 'Cycles', link: 'understand/cycles' },
{ text: 'Voicings', link: 'understand/voicings' },
{ text: 'Pattern Alignment', link: 'technical-manual/alignment' },
{ text: 'Strudel vs Tidal', link: 'learn/strudel-vs-tidal' },
],
Expand Down
10 changes: 9 additions & 1 deletion website/src/pages/learn/factories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ These are the equivalents used by the Mini Notation:

## run

<JsDoc client:idle name="run" h={0} punchcard />
<JsDoc client:idle name="run" h={0} />

## binary

<JsDoc client:idle name="binary" h={0} />

## binaryN

<JsDoc client:idle name="binaryN" h={0} />

After Pattern Constructors, let's see what [Time Modifiers](/learn/time-modifiers) are available.
Loading

0 comments on commit 9d5fbde

Please sign in to comment.