Merge branch 'time-warp' of github.com:Bubobubobubobubo/Topos into time-warp

This commit is contained in:
2023-09-01 13:02:52 +02:00
8 changed files with 107 additions and 13 deletions

View File

@ -405,6 +405,11 @@ export class UserAPI {
player.updateLastCallTime();
if(id !== "") {
// Sync named patterns to z0 by default
player.sync("z0");
}
return player;
};
@ -1044,6 +1049,19 @@ export class UserAPI {
return final_pulses.some((p) => p == true);
};
oneuclid = (pulses: number, length: number, rotate: number=0): boolean => {
/**
* Returns true if the current beat is in the given euclid sequence.
* @param pulses - The number of pulses in the cycle
* @param length - The length of the cycle
* @param rotate - Rotation of the euclidian sequence
* @returns True if the current beat is in the given euclid sequence
*/
const cycle = this._euclidean_cycle(pulses, length, rotate);
const beats = cycle.reduce((acc:number[], x:boolean, i:number) => { if(x) acc.push(i+1); return acc; }, []);
return this.oncount(beats, length);
};
// ======================================================================
// Delay related functions
// ======================================================================

View File

@ -121,7 +121,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.div = function (divisor: number) {
const chunk_size = divisor; // Get the first argument (chunk size)
const timepos = api.epulse();
const timepos = api.app.clock.pulses_since_origin;
const slice_count = Math.floor(
timepos / Math.floor(chunk_size * api.ppqn())
);

View File

@ -74,8 +74,8 @@ export abstract class Event {
return this.modify(func);
};
duration = (value: number): Event => {
this.values["duration"] = value;
length = (value: number): Event => {
this.values["length"] = value;
return this;
};
}

View File

@ -2,20 +2,20 @@ import { type Editor } from "../main";
import { Event } from "./AbstractEvents";
export class RestEvent extends Event {
constructor(duration: number, app: Editor) {
constructor(length: number, app: Editor) {
super(app);
this.values["duration"] = duration;
this.values["length"] = length;
}
_fallbackMethod = (): Event => {
return RestEvent.createRestProxy(this.values["duration"], this.app);
return RestEvent.createRestProxy(this.values["length"], this.app);
};
public static createRestProxy = (
duration: number,
length: number,
app: Editor
): RestEvent => {
const instance = new RestEvent(duration, app);
const instance = new RestEvent(length, app);
return new Proxy(instance, {
// @ts-ignore
get(target, propKey, receiver) {

View File

@ -136,6 +136,20 @@ export class SoundEvent extends AudibleEvent {
public velocity = (value: number) => this.updateValue("velocity", value);
public vel = this.velocity;
// ================================================================================
// AbstactEvent overrides
// ================================================================================
modify = (func: Function): this => {
const funcResult = func(this);
if (funcResult instanceof Object) return funcResult;
else {
func(this.values);
this.update();
return this;
}
};
update = (): void => {
const [note, _] = noteFromPc(
this.values.key || "C4",

View File

@ -11,9 +11,9 @@ export type InputOptions = { [key: string]: string | number };
export class Player extends Event {
input: string;
ziffers: Ziffers;
initCallTime: number = 1;
startCallTime: number = 1;
lastCallTime: number = 1;
initCallTime: number = 0;
startCallTime: number = 0;
lastCallTime: number = 0;
waitTime = 0;
startBeat: number = 0;
played: boolean = false;
@ -98,6 +98,7 @@ export class Player extends Event {
this.startCallTime = 0;
this.index = 0;
this.waitTime = 0;
this.skipIndex = 0;
}
// Main logic
@ -207,6 +208,23 @@ export class Player extends Event {
return this;
}
sync(value: string|Function) {
if(this.atTheBeginning() && this.notStarted()) {
const origin = this.app.clock.pulses_since_origin;
const syncId = typeof value === "function" ? value.name : value;
if(origin>0) {
const syncPattern = this.app.api.patternCache.get(syncId) as Player;
if(syncPattern) {
const syncPatternDuration = syncPattern.ziffers.duration;
const syncPatternStart = syncPattern.startCallTime;
const syncInPulses = syncPatternDuration*4*this.app.clock.ppqn;
this.waitTime = syncPatternStart + syncInPulses;
}
}
}
return this;
}
out = (): void => {
// TODO?
};

View File

@ -152,6 +152,19 @@ mod(.25) && euclid($(4), 7, 9, 1) && snd('hh').out()
false
)}
Alternatively, you can <ic>oneuclid</ic> or <ic>rhythm</ic> without the _iterators_:
- <ic>oneuclid(pulses: number, length: number, rotate: number): boolean</ic>: generates <ic>true</ic> or <ic>false</ic> values from an euclidian rhythm sequence. This is another version of <ic>euclid</ic> that does not take an iterator.
${makeExample(
"Using oneuclid to create a rhythm without iterators",
`
// Change speed using bpm
// bpm(250)
oneuclid(5, 9) :: snd('kick').out()
oneuclid(7,16) :: snd('east').end(0.5).n(irand(3,5)).out()
`,
false
)}
- <ic>rhythm(divisor: number, pulses: number, length: number, rotate: number): boolean</ic>: generates <ic>true</ic> or <ic>false</ic> values from an euclidian rhythm sequence. This is another version of <ic>euclid</ic> that does not take an iterator.
${makeExample(

View File

@ -86,7 +86,7 @@ Ziffers provides shorthands for **many** numeric and algorithimic operations suc
* **List operations:** Cartesian operation (_e.g._ <ic>(3 2 1)+(2 5)</ic>) using the <ic>+</ic> operator. All the arithmetic operators are supported.
${makeExample(
"Cartesian operation for melodic generation",
"Element-wise operations for melodic generation",
`
z1("q 0 s (3 2 1)+(2 5) q 0 s (4 5 6)-(2 3)").sound('sine')
.scale('minor').fmi(2).fmh(2).room(0.5).size(0.5).sustain(0.1)
@ -192,7 +192,38 @@ mod(1, 1.75) :: snd(['kick', 'hat'].div(1)).out()
## Synchronization
Ziffers numbered methods **(z0-z16)** can be used to parse and play patterns. Each method is individually cached and can be used to play multiple patterns simultaneously. They can be synchronized together by using a **cue** system. By default, each Ziffers expression will have a different duration. This system is thus necessary to make everything fit together in a loop-based environment like Topos.
Ziffers numbered methods **(z0-z16)** can be used to parse and play patterns. Each method is individually cached and can be used to play multiple patterns simultaneously. By default, each Ziffers expression can have a different duration. This system is thus necessary to make everything fit together in a loop-based environment like Topos.
Numbered methods are synced automatically to **z0** method if it exsists. Syncing can also be done manually by using either the <ic>wait</ic> method, which will always wait for the current pattern to finish before starting the next cycle, or the <ic>sync</ic> method will only wait for the synced pattern to finish on the first time.
${makeExample(
"Automatic sync to z0",
`
z0('w 0 8').sound('peri').out()
z1('e 0 4 5 9').sound('bell').out()
`,
true
)}
${makeExample(
"Sync with wait",
`
z1('w 0 5').sound('pluck').release(0.1).sustain(0.25).out()
z2('q 6 3').wait(z1).sound('sine').release(0.16).sustain(0.55).out()
`,
true
)}
${makeExample(
"Sync on first run",
`
z1('w __ 0 5 9 3').sound('bin').out()
z2('q __ 4 2 e 6 3 q 6').sync(z1).sound('east').out()
`,
true
)}
## Examples