Skip to content

Commit

Permalink
Merge pull request #62 from tidalcycles/tidy-modules
Browse files Browse the repository at this point in the history
Separate out strudel.mjs, make index.mjs aggregate module
  • Loading branch information
yaxu committed Apr 13, 2022
2 parents 0396c3f + 050296d commit 2973223
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 361 deletions.
2 changes: 1 addition & 1 deletion packages/core/controls.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Pattern, sequence } from '@strudel.cycles/core/strudel.mjs';
import { Pattern, sequence } from './pattern.mjs';

const controls = {};
const generic_params = [
Expand Down
2 changes: 1 addition & 1 deletion packages/core/euclid.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Pattern, timeCat } from './strudel.mjs';
import { Pattern, timeCat } from './pattern.mjs';
import bjork from 'bjork';
import { rotate } from './util.mjs';
import Fraction from './fraction.mjs';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/fraction.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fraction from 'fraction.js';
import { TimeSpan } from './strudel.mjs';
import { TimeSpan } from './timespan.mjs';

// Returns the start of the cycle.
Fraction.prototype.sam = function () {
Expand Down
90 changes: 90 additions & 0 deletions packages/core/hap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

export class Hap {
/*
Event class, representing a value active during the timespan
'part'. This might be a fragment of an event, in which case the
timespan will be smaller than the 'whole' timespan, otherwise the
two timespans will be the same. The 'part' must never extend outside of the
'whole'. If the event represents a continuously changing value
then the whole will be returned as None, in which case the given
value will have been sampled from the point halfway between the
start and end of the 'part' timespan.
The context is to store a list of source code locations causing the event
*/

constructor(whole, part, value, context = {}, stateful = false) {
this.whole = whole;
this.part = part;
this.value = value;
this.context = context;
this.stateful = stateful;
if (stateful) {
console.assert(typeof this.value === 'function', 'Stateful values must be functions');
}
}

get duration() {
return this.whole.end.sub(this.whole.begin).valueOf();
}

wholeOrPart() {
return this.whole ? this.whole : this.part;
}

withSpan(func) {
// Returns a new event with the function f applies to the event timespan.
const whole = this.whole ? func(this.whole) : undefined;
return new Hap(whole, func(this.part), this.value, this.context);
}

withValue(func) {
// Returns a new event with the function f applies to the event value.
return new Hap(this.whole, this.part, func(this.value), this.context);
}

hasOnset() {
// Test whether the event contains the onset, i.e that
// the beginning of the part is the same as that of the whole timespan."""
return this.whole != undefined && this.whole.begin.equals(this.part.begin);
}

resolveState(state) {
if (this.stateful && this.hasOnset()) {
console.log('stateful');
const func = this.value;
const [newState, newValue] = func(state);
return [newState, new Hap(this.whole, this.part, newValue, this.context, false)];
}
return [state, this];
}

spanEquals(other) {
return (this.whole == undefined && other.whole == undefined) || this.whole.equals(other.whole);
}

equals(other) {
return (
this.spanEquals(other) &&
this.part.equals(other.part) &&
// TODO would == be better ??
this.value === other.value
);
}

show() {
return (
'(' + (this.whole == undefined ? '~' : this.whole.show()) + ', ' + this.part.show() + ', ' + this.value + ')'
);
}

combineContext(b) {
const a = this;
return { ...a.context, ...b.context, locations: (a.context.locations || []).concat(b.context.locations || []) };
}

setContext(context) {
return new Hap(this.whole, this.part, this.value, context);
}
}

export default Hap;
11 changes: 11 additions & 0 deletions packages/core/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from './controls.mjs';
export * from './euclid.mjs';
import Fraction from './fraction.mjs';
export {Fraction};
export * from './hap.mjs';
export * from './pattern.mjs';
export * from './signal.mjs';
export * from './state.mjs';
export * from './timespan.mjs';
export * from './util.mjs';
// export * from './value.mjs';
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@strudel.cycles/core",
"version": "0.0.3",
"description": "Port of Tidal Cycles to JavaScript",
"main": "strudel.mjs",
"main": "index.mjs",
"type": "module",
"scripts": {
"test": "mocha --colors"
Expand Down

0 comments on commit 2973223

Please sign in to comment.