Compare commits

..

10 Commits

Author SHA1 Message Date
yaxu 9e92aba3de Update AGENTS.md 2026-12-03 10:56:29 +01:00
Alex McLean 789376d151 Merge pull request 'Add Api Documentation for Hap' (#2218) from Orasund/strudel:document-hap into main
Reviewed-on: https://codeberg.org/uzu/strudel/pulls/2218
2026-12-03 08:13:05 +01:00
Alex McLean 0fc0996d5c Merge branch 'main' into document-hap 2026-12-03 08:09:28 +01:00
Orasund e000020a9f add snapshot for example 2026-12-03 05:45:43 +01:00
Orasund 926cd6b772 fix up typos 2026-12-03 05:40:33 +01:00
Orasund 2271593c06 add State to the list of known types 2026-12-01 13:13:36 +01:00
Orasund 7dd9cacc37 codeformat 2026-12-01 13:06:30 +01:00
Orasund 446f8468c4 document missing fields 2026-12-01 13:04:21 +01:00
Orasund 28a6c304cd Link to Haps in references 2026-12-01 12:52:47 +01:00
Orasund d3d311e105 Add documentation for Hap 2026-11-30 22:39:10 +01:00
6 changed files with 76 additions and 88 deletions
+5
View File
@@ -18,6 +18,11 @@ Strudel has purposefully moved away from github.com, as a closed
source platform with ethical issues. Please do not fork strudel back
to that platform, or similar corporate platforms.
Strudel's git repo is hosted by codeberg, which attempts to block bots
including LLMs from accessing it, to avoid being overloaded. Agents
should not try to circumvent this, instead encouraging developers
to engage with the strudel community directly.
Projects using strudel must not use "strudel" in their name, unless
they are 'official' projects, or have the blessing of the strudel
community.
+1 -1
View File
@@ -99,7 +99,7 @@ export default [
rules: {
'jsdoc/no-undefined-types': [
'error',
{ definedTypes: ['Pattern', 'Pattern.prototype', 'Hap', 'KabelsalatExpression'] },
{ definedTypes: ['Pattern', 'Pattern.prototype', 'Hap', 'KabelsalatExpression', 'TimeSpan', 'State'] },
],
'jsdoc/check-tag-names': ['error', { definedTags: ['tags', 'synonyms', 'noAutocomplete', 'superdirtOnly'] }],
'jsdoc/no-restricted-syntax': [
+34 -15
View File
@@ -6,22 +6,41 @@ This program is free software: you can redistribute it and/or modify it under th
import Fraction from './fraction.mjs';
import { stringifyValues } from './util.mjs';
/**
* @tags internal
* @class Event class, representing a discrete or samples event (or "happening").
* 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 word "Event" is more or less a reserved word in javascript, hence this
* class is named called `Hap`.
*/
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 metadata, including a list of source code locations causing the event.
The word 'Event' is more or less a reserved word in javascript, hence this
class is named called 'Hap'.
*/
/**
* Create a hap. Create a hap. Haps and patterns are considered immutable, but you can create a new pattern with modified haps with functions like {@link Pattern#withHaps} and {@link Pattern#withValue}.
*
* @param {TimeSpan | undefined} whole timespan. 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 the start of the `part` timespan.
* @param {TimeSpan} part of the timespan. May never extend outside of the `whole`.
* @param {function(State):* | *} value active during the timespan `part`. Must be a function if `stateful` is set to `true`.
* @param {object} [context={}] - The context is to store metadata. When two patterns are combined, their context objects are merged.
* @param {Array<{ start: number, end: number }>} [context.locations] - list of source code locations causing the event
* @param {string[]} [context.tags] Metadata about the Hap
* @param {boolean} [stateful=false] - (experimental) if true the value depends on the state.
*
* @example
* // Keep only haps whose full `whole` timespan is at least half a cycle long
* s("bd*8")
* .slow(2)
* .filterHaps((hap) => hap.whole !== undefined && hap.whole.span.duration() >= 0.5);
*
* @example
* // Keep only haps that begin within the first half of a cycle
* note("c3 d3 e3 f3")
* .filterHaps((hap) => hap.part.begin < 0.5);
*/
constructor(whole, part, value, context = {}, stateful = false) {
this.whole = whole;
this.part = part;
+27 -29
View File
@@ -91,7 +91,7 @@ export class Pattern {
/**
* Returns a new pattern, with the function applied to the value of
* each hap. It has the alias `fmap`.
* each {@link Hap}. It has the alias `fmap`.
* @tags functional
* @synonyms fmap
* @param {Function} func to to apply to the value
@@ -407,7 +407,7 @@ export class Pattern {
// Utility methods mainly for internal use
/**
* Query haps inside the given time span.
* Query {@link Hap}s inside the given time span.
*
* @tags internals
* @param {Fraction | number} begin from time
@@ -450,7 +450,7 @@ export class Pattern {
* Returns a new pattern, where the given function is applied to the query
* timespan before passing it to the original pattern.
* @tags internals
* @param {Function} func the function to apply
* @param {function(TimeSpan):TimeSpan} func the function to apply
* @returns {Pattern}
* @noAutocomplete
*/
@@ -482,11 +482,11 @@ export class Pattern {
}
/**
* Similar to `withQuerySpan`, but the function is applied to the timespans
* of all haps returned by pattern queries (both `part` timespans, and where
* Similar to {@link Pattern#withQuerySpan}, but the function is applied to the timespans
* of all {@link Hap}s returned by pattern queries (both `part` timespans, and where
* present, `whole` timespans).
* @tags internals
* @param {Function} func
* @param {function(TimeSpan):TimeSpan} func
* @returns {Pattern}
* @noAutocomplete
*/
@@ -495,10 +495,10 @@ export class Pattern {
}
/**
* As with `withHapSpan`, but the function is applied to both the
* begin and end time of the hap timespans.
* As with {@link Pattern#withHapSpan}, but the function is applied to both the
* begin and end time of the {@link Hap} timespans.
* @tags internals
* @param {Function} func the function to apply
* @param {function(TimeSpan):TimeSpan} func the function to apply
* @returns {Pattern}
* @noAutocomplete
*/
@@ -507,9 +507,9 @@ export class Pattern {
}
/**
* Returns a new pattern with the given function applied to the list of haps returned by every query.
* Returns a new pattern with the given function applied to the list of {@link Hap}s returned by every query.
*
* As an end user, you will most likely not need to use this; look at `withContext`, `withHapSpan`, etc. instead.
* As an end user, you will most likely not need to use this; look at {@link Pattern#withContext}, {@link Pattern#withHapSpan}, etc. instead.
* @tags internals
* @param {Function} func
* @returns {Pattern}
@@ -535,9 +535,9 @@ export class Pattern {
}
/**
* As with `withHaps`, but applies the function to every hap, rather than every list of haps.
* As with {@link Pattern#withHaps}, but applies the function to every {@link Hap}, rather than every list of haps.
*
* As an end user, you will most likely not need to use this; look at `withContext`, `withHapSpan`, etc. instead.
* As an end user, you will most likely not need to use this; look at {@link Pattern#withContext}, {@link Pattern#withHapSpan}, etc. instead.
* @tags internals
* @param {Function} func
* @returns {Pattern}
@@ -559,7 +559,7 @@ export class Pattern {
}
/**
* Returns a new pattern with the context field set to every hap set to the given value.
* Returns a new pattern with the context field set to every {@link Hap} set to the given value.
* @tags internals
* @param {*} context
* @returns {Pattern}
@@ -570,7 +570,7 @@ export class Pattern {
}
/**
* Returns a new pattern with the given function applied to the context field of every hap.
* Returns a new pattern with the given function applied to the context field of every {@link Hap}.
* @tags internals
* @param {Function} func
* @returns {Pattern}
@@ -586,7 +586,7 @@ export class Pattern {
}
/**
* Returns a new pattern with the context field of every hap set to an empty object.
* Returns a new pattern with the `context` field of every {@link Hap} set to an empty object.
* @tags internals
* @returns {Pattern}
* @noAutocomplete
@@ -597,7 +597,7 @@ export class Pattern {
/**
* Returns a new pattern with the given location information added to the
* context of every hap.
* context of every {@link Hap}.
* @tags internals
* @param {number} start start offset
* @param {number} end end offset
@@ -621,9 +621,9 @@ export class Pattern {
}
/**
* Returns a new Pattern, which only returns haps that meet the given test.
* Returns a new Pattern, which only returns {@link Hap}s that meet the given test.
* @tags internals
* @param {Function} hap_test - a function which returns false for haps to be removed from the pattern
* @param {function(Hap):boolean} hap_test - a function which returns false for {@link Hap}s to be removed from the pattern
* @returns {Pattern}
* @example
* s("bd*8").velocity(rand).filterHaps((h) => (h.whole.begin % 1) < h.value.velocity)
@@ -633,7 +633,7 @@ export class Pattern {
}
/**
* As with `filterHaps`, but the function is applied to values
* As with {@link Pattern#filterHaps}, but the function is applied to values
* inside haps.
* @tags internals
* @param {Function} value_test
@@ -660,22 +660,20 @@ export class Pattern {
}
/**
* Returns a new pattern, with all haps without onsets filtered out. A hap
* with an onset is one with a `whole` timespan that begins at the same time
* Returns a new pattern, with all {@link Hap}s without onsets filtered out.
*
* A hap with an onset is one with a `whole` timespan that begins at the same time
* as its `part` timespan.
* @tags internals
* @returns {Pattern}
* @noAutocomplete
*/
onsetsOnly() {
// Returns a new pattern that will only return haps where the start
// of the 'whole' timespan matches the start of the 'part'
// timespan, i.e. the haps that include their 'onset'.
return this.filterHaps((hap) => hap.hasOnset());
}
/**
* Returns a new pattern, with 'continuous' haps (those without 'whole'
* Returns a new pattern, with 'continuous' {@link Hap}s (those without 'whole'
* timespans) removed from query results.
* @tags internals
* @returns {Pattern}
@@ -687,7 +685,7 @@ export class Pattern {
}
/**
* Combines adjacent haps with the same value and whole. Only
* Combines adjacent {@link Hap}s with the same value and whole. Only
* intended for use in tests.
* @tags internals
* @noAutocomplete
@@ -741,7 +739,7 @@ export class Pattern {
}
/**
* Queries the pattern for the first cycle, returning Haps. Mainly of use when
* Queries the pattern for the first cycle, returning {@link Hap}s. Mainly of use when
* debugging a pattern.
* @tags internals
* @param {boolean} with_context - set to true, otherwise the context field
@@ -780,7 +778,7 @@ export class Pattern {
}
/**
* Returns a new pattern, which returns haps sorted in temporal order. Mainly
* Returns a new pattern, which returns {@link Hap}s sorted in temporal order. Mainly
* of use when comparing two patterns for equality, in tests.
* @tags internals
* @returns {Pattern}
-18
View File
@@ -1129,24 +1129,6 @@ export const cyclesPer = new Pattern(function (state) {
return [new Hap(undefined, state.span, state.span.duration)];
});
/**
* A pattern measuring the duration of events, in seconds per event.
* `secondsPer` doesn't have structure itself, but takes structue, and therefore
* event durations, from the pattern that it is combined with.
* For example `secondsPer.struct("1 1 [1 1] 1")` with `cps = 1` would give the same as `"0.25 0.25 [0.125 0.125] 0.25"`.
*
* @tags temporal
* @synonyms sper
* @example
* note("f a [c d] e")
* .dec(secondsPer.mul(1))
*/
export const secondsPer = new Pattern(function (state) {
return [new Hap(undefined, state.span, state.span.duration.div(state.controls?._cps ?? 0.5))];
});
export const sper = secondsPer;
/**
* A pattern measuring the 'shortness' of events, or in other words, the duration of pattern events,
* in events per cycle. `per` doesn't have structure itself, but takes structure, and therefore
+9 -25
View File
@@ -611,6 +611,15 @@ exports[`runs examples > example "FX" example index 1 1`] = `
]
`;
exports[`runs examples > example "Hap" example index 0 1`] = `[]`;
exports[`runs examples > example "Hap" example index 1 1`] = `
[
"[ 0/1 → 1/4 | note:c3 ]",
"[ 1/4 → 1/2 | note:d3 ]",
]
`;
exports[`runs examples > example "K" example index 0 1`] = `
[
"[ 0/1 → 1/12 | note:Bb4 s:saw workletSrc:saw(saw(sFreq / pat[0]).mul(8).add(sFreq).lag(pat[1])).mul(0.3).mul(sGate.adsr(0 0.15 0.5 pat[2])).lpf(sGate.adsr(0 0.2 0.3 0.2).mul(1).add(0)).add(x => x.delay(pat[3]).mul(0.7)).add(x => x.delay(pat[4]).mul(0.77)).mul(0.7).add(x => x.delay(0.13).mul(0.7)).out() workletInputs:[2 0 0.1 0.3 0.03] ]",
@@ -11792,31 +11801,6 @@ exports[`runs examples > example "scut" example index 0 1`] = `
]
`;
exports[`runs examples > example "secondsPer" example index 0 1`] = `
[
"[ 0/1 → 1/4 | note:f decay:0.5 ]",
"[ 1/4 → 1/2 | note:a decay:0.5 ]",
"[ 1/2 → 5/8 | note:c decay:0.25 ]",
"[ 5/8 → 3/4 | note:d decay:0.25 ]",
"[ 3/4 → 1/1 | note:e decay:0.5 ]",
"[ 1/1 → 5/4 | note:f decay:0.5 ]",
"[ 5/4 → 3/2 | note:a decay:0.5 ]",
"[ 3/2 → 13/8 | note:c decay:0.25 ]",
"[ 13/8 → 7/4 | note:d decay:0.25 ]",
"[ 7/4 → 2/1 | note:e decay:0.5 ]",
"[ 2/1 → 9/4 | note:f decay:0.5 ]",
"[ 9/4 → 5/2 | note:a decay:0.5 ]",
"[ 5/2 → 21/8 | note:c decay:0.25 ]",
"[ 21/8 → 11/4 | note:d decay:0.25 ]",
"[ 11/4 → 3/1 | note:e decay:0.5 ]",
"[ 3/1 → 13/4 | note:f decay:0.5 ]",
"[ 13/4 → 7/2 | note:a decay:0.5 ]",
"[ 7/2 → 29/8 | note:c decay:0.25 ]",
"[ 29/8 → 15/4 | note:d decay:0.25 ]",
"[ 15/4 → 4/1 | note:e decay:0.5 ]",
]
`;
exports[`runs examples > example "seed" example index 0 1`] = `
[
"[ 0/1 → 1/4 | s:bd ]",