make separate signal module
This commit is contained in:
parent
f75da0e2e3
commit
050296d0c2
3 changed files with 39 additions and 33 deletions
|
|
@ -4,6 +4,7 @@ import Fraction from './fraction.mjs';
|
||||||
export {Fraction};
|
export {Fraction};
|
||||||
export * from './hap.mjs';
|
export * from './hap.mjs';
|
||||||
export * from './pattern.mjs';
|
export * from './pattern.mjs';
|
||||||
|
export * from './signal.mjs';
|
||||||
export * from './state.mjs';
|
export * from './state.mjs';
|
||||||
export * from './timespan.mjs';
|
export * from './timespan.mjs';
|
||||||
export * from './util.mjs';
|
export * from './util.mjs';
|
||||||
|
|
|
||||||
|
|
@ -299,6 +299,13 @@ export class Pattern {
|
||||||
return this._asNumber().fmap((v) => Math.ceil(v));
|
return this._asNumber().fmap((v) => Math.ceil(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_toBipolar() {
|
||||||
|
return this.fmap((x) => x * 2 - 1);
|
||||||
|
}
|
||||||
|
_fromBipolar() {
|
||||||
|
return this.fmap((x) => (x + 1) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
// Assumes source pattern of numbers in range 0..1
|
// Assumes source pattern of numbers in range 0..1
|
||||||
range(min, max) {
|
range(min, max) {
|
||||||
return this.mul(max - min).add(min);
|
return this.mul(max - min).add(min);
|
||||||
|
|
@ -306,7 +313,7 @@ export class Pattern {
|
||||||
|
|
||||||
// Assumes source pattern of numbers in range -1..1
|
// Assumes source pattern of numbers in range -1..1
|
||||||
range2(min, max) {
|
range2(min, max) {
|
||||||
return _fromBipolar(this).range(min, max);
|
return this._fromBipolar().range(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
union(other) {
|
union(other) {
|
||||||
|
|
@ -737,37 +744,6 @@ export function pure(value) {
|
||||||
return new Pattern(query);
|
return new Pattern(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function steady(value) {
|
|
||||||
// A continuous value
|
|
||||||
return new Pattern((span) => Hap(undefined, span, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
export const signal = (func) => {
|
|
||||||
const query = (state) => [new Hap(undefined, state.span, func(state.span.midpoint()))];
|
|
||||||
return new Pattern(query);
|
|
||||||
};
|
|
||||||
|
|
||||||
const _toBipolar = (pat) => pat.fmap((x) => x * 2 - 1);
|
|
||||||
const _fromBipolar = (pat) => pat.fmap((x) => (x + 1) / 2);
|
|
||||||
|
|
||||||
export const sine2 = signal((t) => Math.sin(Math.PI * 2 * t));
|
|
||||||
export const sine = _fromBipolar(sine2);
|
|
||||||
|
|
||||||
export const cosine2 = sine2._early(Fraction(1).div(4));
|
|
||||||
export const cosine = sine._early(Fraction(1).div(4));
|
|
||||||
|
|
||||||
export const saw = signal((t) => t % 1);
|
|
||||||
export const saw2 = _toBipolar(saw);
|
|
||||||
|
|
||||||
export const isaw = signal((t) => 1 - (t % 1));
|
|
||||||
export const isaw2 = _toBipolar(isaw);
|
|
||||||
|
|
||||||
export const tri2 = fastcat(isaw2, saw2);
|
|
||||||
export const tri = fastcat(isaw, saw);
|
|
||||||
|
|
||||||
export const square = signal((t) => Math.floor((t * 2) % 2));
|
|
||||||
export const square2 = _toBipolar(square);
|
|
||||||
|
|
||||||
export function isPattern(thing) {
|
export function isPattern(thing) {
|
||||||
// thing?.constructor?.name !== 'Pattern' // <- this will fail when code is mangled
|
// thing?.constructor?.name !== 'Pattern' // <- this will fail when code is mangled
|
||||||
return thing instanceof Pattern;
|
return thing instanceof Pattern;
|
||||||
|
|
@ -1054,4 +1030,3 @@ Pattern.prototype.define = (name, func, options = {}) => {
|
||||||
// Pattern.prototype.define('early', (a, pat) => pat.early(a), { patternified: true, composable: true });
|
// Pattern.prototype.define('early', (a, pat) => pat.early(a), { patternified: true, composable: true });
|
||||||
Pattern.prototype.define('hush', (pat) => pat.hush(), { patternified: false, composable: true });
|
Pattern.prototype.define('hush', (pat) => pat.hush(), { patternified: false, composable: true });
|
||||||
Pattern.prototype.define('bypass', (pat) => pat.bypass(on), { patternified: true, composable: true });
|
Pattern.prototype.define('bypass', (pat) => pat.bypass(on), { patternified: true, composable: true });
|
||||||
|
|
||||||
|
|
|
||||||
30
packages/core/signal.mjs
Normal file
30
packages/core/signal.mjs
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { Hap } from './hap.mjs';
|
||||||
|
import { Pattern, fastcat } from './pattern.mjs';
|
||||||
|
import Fraction from './fraction.mjs';
|
||||||
|
|
||||||
|
export function steady(value) {
|
||||||
|
// A continuous value
|
||||||
|
return new Pattern((span) => Hap(undefined, span, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
export const signal = (func) => {
|
||||||
|
const query = (state) => [new Hap(undefined, state.span, func(state.span.midpoint()))];
|
||||||
|
return new Pattern(query);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isaw = signal((t) => 1 - (t % 1));
|
||||||
|
export const isaw2 = isaw._toBipolar();
|
||||||
|
|
||||||
|
export const saw = signal((t) => t % 1);
|
||||||
|
export const saw2 = saw._toBipolar();
|
||||||
|
|
||||||
|
export const sine2 = signal((t) => Math.sin(Math.PI * 2 * t));
|
||||||
|
export const sine = sine2._fromBipolar();
|
||||||
|
export const cosine = sine._early(Fraction(1).div(4));
|
||||||
|
export const cosine2 = sine2._early(Fraction(1).div(4));
|
||||||
|
|
||||||
|
export const square = signal((t) => Math.floor((t * 2) % 2));
|
||||||
|
export const square2 = square._toBipolar();
|
||||||
|
|
||||||
|
export const tri = fastcat(isaw, saw);
|
||||||
|
export const tri2 = fastcat(isaw2, saw2);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue