@stacksjs/stx 0.2.476 → 0.2.479
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-06f66a6g.js +2 -0
- package/dist/{chunk-rh6k1daj.js → chunk-1khb6g2w.js} +2 -2
- package/dist/{chunk-txkgkyf4.js → chunk-20wvgf5w.js} +5 -5
- package/dist/{chunk-pgnqzjbv.js → chunk-2d71d7n1.js} +1 -1
- package/dist/{chunk-5fn2wczm.js → chunk-2ynmmd3z.js} +1 -1
- package/dist/{chunk-6kdjjv3h.js → chunk-343k64zr.js} +1 -1
- package/dist/{chunk-nqfb33rv.js → chunk-3mzmz1ht.js} +1 -1
- package/dist/{chunk-26zad6kf.js → chunk-3pgewsrr.js} +1 -1
- package/dist/{chunk-c31z877d.js → chunk-4pcqg0te.js} +1 -1
- package/dist/{chunk-s94n9tr6.js → chunk-7ph65fcr.js} +1 -1
- package/dist/{chunk-p6av17y4.js → chunk-8cr4tqwg.js} +1 -1
- package/dist/{chunk-z4w57s7r.js → chunk-8g265pwq.js} +1 -1
- package/dist/{chunk-v5pg1jgb.js → chunk-9v1ewa0f.js} +1 -1
- package/dist/{chunk-23bgy025.js → chunk-9yverx7k.js} +1 -1
- package/dist/{chunk-2pfevhmz.js → chunk-a3a5s9qr.js} +2 -2
- package/dist/{chunk-vt3f3hm2.js → chunk-afxpbw68.js} +1 -1
- package/dist/{chunk-y0m9zz0x.js → chunk-cp3d3zrt.js} +1 -1
- package/dist/{chunk-4y62shxz.js → chunk-eh23r8f2.js} +1 -1
- package/dist/{chunk-26mz7hyg.js → chunk-ennnv0n0.js} +1 -1
- package/dist/chunk-gef1av6b.js +12 -0
- package/dist/{chunk-rqy8p8e1.js → chunk-h2aetzf6.js} +1 -1
- package/dist/{chunk-6bf687m6.js → chunk-jcrcqtb3.js} +1 -1
- package/dist/{chunk-vxb1x36c.js → chunk-k3fxq005.js} +1 -1
- package/dist/{chunk-em70a2vz.js → chunk-kqqe0hzx.js} +1 -1
- package/dist/{chunk-mde796zf.js → chunk-mkeq5ec2.js} +1 -1
- package/dist/{chunk-qewjmma1.js → chunk-mre4931b.js} +1 -1
- package/dist/{chunk-5c6ta229.js → chunk-nj1mgbt8.js} +1 -1
- package/dist/{chunk-08pfhmk0.js → chunk-npsccfxz.js} +1 -1
- package/dist/{chunk-2z3fsgnm.js → chunk-ntnwwb9z.js} +1 -1
- package/dist/{chunk-7f658pm7.js → chunk-p2775azk.js} +1 -1
- package/dist/{chunk-494t857g.js → chunk-pegv1j5x.js} +1 -1
- package/dist/{chunk-tdv7h1tq.js → chunk-pj332gj9.js} +1 -1
- package/dist/{chunk-yqdt6sxq.js → chunk-qrbpfexg.js} +2 -2
- package/dist/{chunk-c5dcyb9y.js → chunk-qzxpqyny.js} +1 -1
- package/dist/{chunk-yhjn4kej.js → chunk-r345b5an.js} +1 -1
- package/dist/{chunk-3dtz8pws.js → chunk-r8x97zbs.js} +1 -1
- package/dist/chunk-sjmx8ek1.js +2 -0
- package/dist/{chunk-0vcqc3d4.js → chunk-syv9rh52.js} +1 -1
- package/dist/{chunk-9sxwhb26.js → chunk-sz5m446r.js} +132 -131
- package/dist/chunk-v769ppj2.js +33 -0
- package/dist/{chunk-vrsnkj84.js → chunk-w02x3nd4.js} +1 -1
- package/dist/chunk-w52pta5n.js +2 -0
- package/dist/{chunk-fq2vjk8t.js → chunk-ws0akhpt.js} +1 -1
- package/dist/{chunk-j7ae2ah3.js → chunk-xy0502b6.js} +1 -1
- package/dist/{chunk-fqzjt6n4.js → chunk-y19egvjj.js} +1 -1
- package/dist/{chunk-gmcj4av9.js → chunk-yj9thf3n.js} +1 -1
- package/dist/{chunk-ckb8935d.js → chunk-zakby6bx.js} +1 -1
- package/dist/{chunk-wg3ag5e3.js → chunk-zbtad4yz.js} +1 -1
- package/dist/cli.js +7 -7
- package/dist/client-script.js +40 -1
- package/dist/composables/index.d.ts +12 -0
- package/dist/composables/index.js +4 -0
- package/dist/composables/use-ambient-sound.d.ts +44 -0
- package/dist/composables/use-ambient-sound.js +434 -0
- package/dist/composables/use-breath-pacer.d.ts +93 -0
- package/dist/composables/use-breath-pacer.js +155 -0
- package/dist/craft-entry.js +1 -1
- package/dist/craft.js +1 -1
- package/dist/native/compiler/icons.d.ts +1 -0
- package/dist/native/compiler/icons.js +1 -0
- package/dist/pwa.js +1 -1
- package/dist/ssg.js +2 -2
- package/dist/unresolved-identifiers.js +2 -0
- package/package.json +5 -5
- package/dist/chunk-0bfkjrq9.js +0 -2
- package/dist/chunk-n16e5fmq.js +0 -2
- package/dist/chunk-q1hx1th8.js +0 -2
- package/dist/chunk-qy4qp8w1.js +0 -12
- package/dist/chunk-vmp6yf2v.js +0 -33
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
const CHORDS = [
|
|
2
|
+
[50, 57, 62, 66, 69],
|
|
3
|
+
[47, 54, 59, 62, 66],
|
|
4
|
+
[43, 50, 55, 59, 62],
|
|
5
|
+
[45, 52, 57, 61, 64]
|
|
6
|
+
];
|
|
7
|
+
function hz(midi) {
|
|
8
|
+
return 440 * 2 ** ((midi - 69) / 12);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class AmbientSound {
|
|
12
|
+
context = null;
|
|
13
|
+
music = null;
|
|
14
|
+
tones = null;
|
|
15
|
+
reverb = null;
|
|
16
|
+
timers = [];
|
|
17
|
+
voices = new Set;
|
|
18
|
+
noise = null;
|
|
19
|
+
white = null;
|
|
20
|
+
genre = null;
|
|
21
|
+
level = 0.5;
|
|
22
|
+
generation = 0;
|
|
23
|
+
get playing() {
|
|
24
|
+
return !!this.context && !!this.genre;
|
|
25
|
+
}
|
|
26
|
+
async start(genre, volume = this.level) {
|
|
27
|
+
const request = ++this.generation;
|
|
28
|
+
this.level = clamp(volume);
|
|
29
|
+
const context = await this.ensureContext();
|
|
30
|
+
if (request !== this.generation || !context)
|
|
31
|
+
return;
|
|
32
|
+
this.stopMusic(context.currentTime);
|
|
33
|
+
if (genre === "none") {
|
|
34
|
+
this.genre = null;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.genre = genre;
|
|
38
|
+
this.music.gain.cancelScheduledValues(context.currentTime);
|
|
39
|
+
this.music.gain.setValueAtTime(0, context.currentTime);
|
|
40
|
+
this.music.gain.linearRampToValueAtTime(this.level, context.currentTime + 1.5);
|
|
41
|
+
({
|
|
42
|
+
ambient: (c) => this.ambient(c),
|
|
43
|
+
nature: (c) => this.nature(c),
|
|
44
|
+
house: (c) => this.house(c),
|
|
45
|
+
electronic: (c) => this.electronic(c),
|
|
46
|
+
drone: (c) => this.drone(c)
|
|
47
|
+
})[genre](context);
|
|
48
|
+
}
|
|
49
|
+
async prepare() {
|
|
50
|
+
return !!await this.ensureContext();
|
|
51
|
+
}
|
|
52
|
+
volume(value) {
|
|
53
|
+
this.level = clamp(value);
|
|
54
|
+
if (this.context && this.music && this.genre)
|
|
55
|
+
this.music.gain.setTargetAtTime(this.level, this.context.currentTime, 0.2);
|
|
56
|
+
}
|
|
57
|
+
async suspend() {
|
|
58
|
+
if (this.context?.state === "running")
|
|
59
|
+
await this.context.suspend().catch(() => {});
|
|
60
|
+
}
|
|
61
|
+
async resume() {
|
|
62
|
+
if (this.context?.state === "suspended")
|
|
63
|
+
await this.context.resume().catch(() => {});
|
|
64
|
+
}
|
|
65
|
+
breathTone(kind, seconds) {
|
|
66
|
+
const context = this.context;
|
|
67
|
+
if (!context || !this.tones || context.state !== "running")
|
|
68
|
+
return;
|
|
69
|
+
const now = context.currentTime, length = Math.max(0.4, Math.min(20, seconds)), oscillator = context.createOscillator();
|
|
70
|
+
oscillator.type = "sine";
|
|
71
|
+
const low = 196, high = 294;
|
|
72
|
+
if (kind === "in") {
|
|
73
|
+
oscillator.frequency.setValueAtTime(low, now);
|
|
74
|
+
oscillator.frequency.exponentialRampToValueAtTime(high, now + length);
|
|
75
|
+
} else if (kind === "out") {
|
|
76
|
+
oscillator.frequency.setValueAtTime(high, now);
|
|
77
|
+
oscillator.frequency.exponentialRampToValueAtTime(low, now + length);
|
|
78
|
+
} else
|
|
79
|
+
oscillator.frequency.setValueAtTime(kind === "hold" ? 247 : low, now);
|
|
80
|
+
const gain = context.createGain(), peak = kind === "hold" ? 0.035 : 0.07;
|
|
81
|
+
gain.gain.setValueAtTime(0, now);
|
|
82
|
+
gain.gain.linearRampToValueAtTime(peak, now + Math.min(0.6, length / 3));
|
|
83
|
+
gain.gain.setValueAtTime(peak, now + length * 0.75);
|
|
84
|
+
gain.gain.linearRampToValueAtTime(0, now + length);
|
|
85
|
+
oscillator.connect(gain).connect(this.tones);
|
|
86
|
+
oscillator.start(now);
|
|
87
|
+
oscillator.stop(now + length + 0.05);
|
|
88
|
+
}
|
|
89
|
+
chime(strength = 1) {
|
|
90
|
+
const context = this.context;
|
|
91
|
+
if (!context || !this.tones || context.state !== "running")
|
|
92
|
+
return;
|
|
93
|
+
const now = context.currentTime;
|
|
94
|
+
for (const [ratio, level, decay] of [[1, 0.12, 2.6], [2.76, 0.05, 1.4], [5.4, 0.02, 0.8]]) {
|
|
95
|
+
const oscillator = context.createOscillator();
|
|
96
|
+
oscillator.type = "sine";
|
|
97
|
+
oscillator.frequency.value = 880 * ratio;
|
|
98
|
+
const gain = context.createGain();
|
|
99
|
+
gain.gain.setValueAtTime(0, now);
|
|
100
|
+
gain.gain.linearRampToValueAtTime(level * strength, now + 0.01);
|
|
101
|
+
gain.gain.exponentialRampToValueAtTime(0.0001, now + decay);
|
|
102
|
+
oscillator.connect(gain).connect(this.tones);
|
|
103
|
+
if (this.reverb)
|
|
104
|
+
gain.connect(this.reverb);
|
|
105
|
+
oscillator.start(now);
|
|
106
|
+
oscillator.stop(now + decay + 0.05);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async stop() {
|
|
110
|
+
this.generation++;
|
|
111
|
+
const context = this.context;
|
|
112
|
+
this.context = null;
|
|
113
|
+
this.genre = null;
|
|
114
|
+
for (const timer of this.timers)
|
|
115
|
+
clearInterval(timer);
|
|
116
|
+
this.timers = [];
|
|
117
|
+
this.voices.clear();
|
|
118
|
+
this.music = null;
|
|
119
|
+
this.tones = null;
|
|
120
|
+
this.reverb = null;
|
|
121
|
+
if (context && context.state !== "closed")
|
|
122
|
+
await context.close().catch(() => {});
|
|
123
|
+
}
|
|
124
|
+
async ensureContext() {
|
|
125
|
+
if (this.context) {
|
|
126
|
+
if (this.context.state === "suspended")
|
|
127
|
+
await this.context.resume().catch(() => {});
|
|
128
|
+
return this.context;
|
|
129
|
+
}
|
|
130
|
+
const Audio = globalThis.AudioContext || globalThis.webkitAudioContext;
|
|
131
|
+
if (!Audio)
|
|
132
|
+
return null;
|
|
133
|
+
const context = new Audio;
|
|
134
|
+
this.context = context;
|
|
135
|
+
await context.resume().catch(() => {});
|
|
136
|
+
const music = context.createGain();
|
|
137
|
+
music.gain.value = 0;
|
|
138
|
+
const tones = context.createGain();
|
|
139
|
+
tones.gain.value = 1;
|
|
140
|
+
const compressor = context.createDynamicsCompressor();
|
|
141
|
+
compressor.threshold.value = -18;
|
|
142
|
+
compressor.ratio.value = 3;
|
|
143
|
+
music.connect(compressor);
|
|
144
|
+
tones.connect(compressor);
|
|
145
|
+
compressor.connect(context.destination);
|
|
146
|
+
const reverb = context.createConvolver();
|
|
147
|
+
reverb.buffer = this.impulse(context, 3.2);
|
|
148
|
+
const wet = context.createGain();
|
|
149
|
+
wet.gain.value = 0.35;
|
|
150
|
+
reverb.connect(wet).connect(music);
|
|
151
|
+
this.music = music;
|
|
152
|
+
this.tones = tones;
|
|
153
|
+
this.reverb = reverb;
|
|
154
|
+
this.noise = this.noiseBuffer(context);
|
|
155
|
+
this.white = this.whiteBuffer(context, 1);
|
|
156
|
+
return context;
|
|
157
|
+
}
|
|
158
|
+
stopMusic(at) {
|
|
159
|
+
for (const timer of this.timers)
|
|
160
|
+
clearInterval(timer);
|
|
161
|
+
this.timers = [];
|
|
162
|
+
for (const voice of this.voices)
|
|
163
|
+
voice.stop(at + 1.2);
|
|
164
|
+
this.voices.clear();
|
|
165
|
+
}
|
|
166
|
+
every(ms, run) {
|
|
167
|
+
run();
|
|
168
|
+
this.timers.push(setInterval(run, ms));
|
|
169
|
+
}
|
|
170
|
+
impulse(context, seconds) {
|
|
171
|
+
const length = Math.floor(context.sampleRate * seconds), buffer = context.createBuffer(2, length, context.sampleRate);
|
|
172
|
+
for (let channel = 0;channel < 2; channel++) {
|
|
173
|
+
const data = buffer.getChannelData(channel);
|
|
174
|
+
for (let index = 0;index < length; index++)
|
|
175
|
+
data[index] = (Math.random() * 2 - 1) * (1 - index / length) ** 3;
|
|
176
|
+
}
|
|
177
|
+
return buffer;
|
|
178
|
+
}
|
|
179
|
+
noiseBuffer(context) {
|
|
180
|
+
const length = context.sampleRate * 4, buffer = context.createBuffer(1, length, context.sampleRate), data = buffer.getChannelData(0);
|
|
181
|
+
let brown = 0;
|
|
182
|
+
for (let index = 0;index < length; index++) {
|
|
183
|
+
brown = (brown + (Math.random() * 2 - 1) * 0.02) / 1.02;
|
|
184
|
+
data[index] = brown * 3.5;
|
|
185
|
+
}
|
|
186
|
+
return buffer;
|
|
187
|
+
}
|
|
188
|
+
whiteBuffer(context, seconds) {
|
|
189
|
+
const buffer = context.createBuffer(1, Math.floor(context.sampleRate * seconds), context.sampleRate), data = buffer.getChannelData(0);
|
|
190
|
+
for (let index = 0;index < data.length; index++)
|
|
191
|
+
data[index] = Math.random() * 2 - 1;
|
|
192
|
+
return buffer;
|
|
193
|
+
}
|
|
194
|
+
pad(context, midi, start, length, level, type = "sawtooth", cutoff = 900) {
|
|
195
|
+
const out = context.createGain();
|
|
196
|
+
out.gain.setValueAtTime(0, start);
|
|
197
|
+
out.gain.linearRampToValueAtTime(level, start + Math.min(3, length / 3));
|
|
198
|
+
out.gain.setValueAtTime(level, start + length - Math.min(3, length / 3));
|
|
199
|
+
out.gain.linearRampToValueAtTime(0, start + length);
|
|
200
|
+
const filter = context.createBiquadFilter();
|
|
201
|
+
filter.type = "lowpass";
|
|
202
|
+
filter.frequency.value = cutoff;
|
|
203
|
+
filter.Q.value = 0.6;
|
|
204
|
+
filter.connect(out);
|
|
205
|
+
out.connect(this.music);
|
|
206
|
+
out.connect(this.reverb);
|
|
207
|
+
const oscillators = [-7, 7].map((cents) => {
|
|
208
|
+
const oscillator = context.createOscillator();
|
|
209
|
+
oscillator.type = type;
|
|
210
|
+
oscillator.frequency.value = hz(midi);
|
|
211
|
+
oscillator.detune.value = cents;
|
|
212
|
+
oscillator.connect(filter);
|
|
213
|
+
oscillator.start(start);
|
|
214
|
+
oscillator.stop(start + length + 0.1);
|
|
215
|
+
return oscillator;
|
|
216
|
+
}), voice = {
|
|
217
|
+
stop: (at) => {
|
|
218
|
+
out.gain.cancelScheduledValues(context.currentTime);
|
|
219
|
+
out.gain.setTargetAtTime(0, context.currentTime, 0.3);
|
|
220
|
+
for (const oscillator of oscillators)
|
|
221
|
+
try {
|
|
222
|
+
oscillator.stop(at);
|
|
223
|
+
} catch {}
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
this.voices.add(voice);
|
|
227
|
+
oscillators[0].onended = () => this.voices.delete(voice);
|
|
228
|
+
}
|
|
229
|
+
noiseBed(context, type, frequency, level, swellSeconds, depth) {
|
|
230
|
+
const source = context.createBufferSource();
|
|
231
|
+
source.buffer = this.noise;
|
|
232
|
+
source.loop = !0;
|
|
233
|
+
const filter = context.createBiquadFilter();
|
|
234
|
+
filter.type = type;
|
|
235
|
+
filter.frequency.value = frequency;
|
|
236
|
+
const gain = context.createGain();
|
|
237
|
+
gain.gain.value = level;
|
|
238
|
+
const lfo = context.createOscillator();
|
|
239
|
+
lfo.frequency.value = 1 / swellSeconds;
|
|
240
|
+
const lfoGain = context.createGain();
|
|
241
|
+
lfoGain.gain.value = level * depth;
|
|
242
|
+
lfo.connect(lfoGain).connect(gain.gain);
|
|
243
|
+
source.connect(filter).connect(gain).connect(this.music);
|
|
244
|
+
const start = context.currentTime;
|
|
245
|
+
source.start(start, Math.random() * 3);
|
|
246
|
+
lfo.start(start);
|
|
247
|
+
this.voices.add({
|
|
248
|
+
stop: (at) => {
|
|
249
|
+
gain.gain.setTargetAtTime(0, context.currentTime, 0.4);
|
|
250
|
+
try {
|
|
251
|
+
source.stop(at);
|
|
252
|
+
lfo.stop(at);
|
|
253
|
+
} catch {}
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
kick(context, at, level = 0.5) {
|
|
258
|
+
const oscillator = context.createOscillator();
|
|
259
|
+
oscillator.frequency.setValueAtTime(140, at);
|
|
260
|
+
oscillator.frequency.exponentialRampToValueAtTime(45, at + 0.18);
|
|
261
|
+
const gain = context.createGain();
|
|
262
|
+
gain.gain.setValueAtTime(level, at);
|
|
263
|
+
gain.gain.exponentialRampToValueAtTime(0.001, at + 0.35);
|
|
264
|
+
oscillator.connect(gain).connect(this.music);
|
|
265
|
+
oscillator.start(at);
|
|
266
|
+
oscillator.stop(at + 0.4);
|
|
267
|
+
}
|
|
268
|
+
hat(context, at, level = 0.08, length = 0.04) {
|
|
269
|
+
const source = context.createBufferSource();
|
|
270
|
+
source.buffer = this.white;
|
|
271
|
+
const filter = context.createBiquadFilter();
|
|
272
|
+
filter.type = "highpass";
|
|
273
|
+
filter.frequency.value = 7000;
|
|
274
|
+
const gain = context.createGain();
|
|
275
|
+
gain.gain.setValueAtTime(level, at);
|
|
276
|
+
gain.gain.exponentialRampToValueAtTime(0.001, at + length);
|
|
277
|
+
source.connect(filter).connect(gain).connect(this.music);
|
|
278
|
+
source.start(at, Math.random() * 0.8, length + 0.02);
|
|
279
|
+
}
|
|
280
|
+
clap(context, at, level = 0.12) {
|
|
281
|
+
const source = context.createBufferSource();
|
|
282
|
+
source.buffer = this.white;
|
|
283
|
+
const filter = context.createBiquadFilter();
|
|
284
|
+
filter.type = "bandpass";
|
|
285
|
+
filter.frequency.value = 1500;
|
|
286
|
+
filter.Q.value = 0.8;
|
|
287
|
+
const gain = context.createGain();
|
|
288
|
+
gain.gain.setValueAtTime(level, at);
|
|
289
|
+
gain.gain.exponentialRampToValueAtTime(0.001, at + 0.2);
|
|
290
|
+
source.connect(filter).connect(gain).connect(this.music);
|
|
291
|
+
gain.connect(this.reverb);
|
|
292
|
+
source.start(at, Math.random() * 0.7, 0.25);
|
|
293
|
+
}
|
|
294
|
+
pluck(context, midi, at, length, level, type = "square", destination) {
|
|
295
|
+
const oscillator = context.createOscillator();
|
|
296
|
+
oscillator.type = type;
|
|
297
|
+
oscillator.frequency.value = hz(midi);
|
|
298
|
+
const filter = context.createBiquadFilter();
|
|
299
|
+
filter.type = "lowpass";
|
|
300
|
+
filter.frequency.setValueAtTime(2400, at);
|
|
301
|
+
filter.frequency.exponentialRampToValueAtTime(400, at + length);
|
|
302
|
+
const gain = context.createGain();
|
|
303
|
+
gain.gain.setValueAtTime(0, at);
|
|
304
|
+
gain.gain.linearRampToValueAtTime(level, at + 0.005);
|
|
305
|
+
gain.gain.exponentialRampToValueAtTime(0.001, at + length);
|
|
306
|
+
oscillator.connect(filter).connect(gain).connect(destination || this.music);
|
|
307
|
+
oscillator.start(at);
|
|
308
|
+
oscillator.stop(at + length + 0.05);
|
|
309
|
+
}
|
|
310
|
+
sequence(context, bpm, step) {
|
|
311
|
+
const sixteenth = 60 / bpm / 4;
|
|
312
|
+
let next = context.currentTime + 0.1, index = 0;
|
|
313
|
+
this.every(25, () => {
|
|
314
|
+
if (!this.context)
|
|
315
|
+
return;
|
|
316
|
+
while (next < context.currentTime + 0.12) {
|
|
317
|
+
step(index, next);
|
|
318
|
+
next += sixteenth;
|
|
319
|
+
index++;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
ambient(context) {
|
|
324
|
+
let chord = 0;
|
|
325
|
+
this.every(8000, () => {
|
|
326
|
+
const at = context.currentTime + 0.05;
|
|
327
|
+
for (const note of CHORDS[chord % CHORDS.length])
|
|
328
|
+
this.pad(context, note, at, 11, 0.035, "sawtooth", 800);
|
|
329
|
+
this.pad(context, CHORDS[chord % CHORDS.length][2] + 24, at + 1, 9, 0.012, "sine", 4000);
|
|
330
|
+
chord++;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
drone(context) {
|
|
334
|
+
this.noiseBed(context, "lowpass", 280, 0.12, 14, 0.5);
|
|
335
|
+
let chord = 0;
|
|
336
|
+
this.every(16000, () => {
|
|
337
|
+
const at = context.currentTime + 0.05, root = [38, 35, 31, 33][chord % 4];
|
|
338
|
+
this.pad(context, root, at, 20, 0.07, "triangle", 420);
|
|
339
|
+
this.pad(context, root + 7, at + 2, 18, 0.04, "sine", 600);
|
|
340
|
+
this.pad(context, root + 19, at + 4, 15, 0.012, "sine", 1200);
|
|
341
|
+
chord++;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
nature(context) {
|
|
345
|
+
this.noiseBed(context, "lowpass", 700, 0.22, 9, 0.85);
|
|
346
|
+
this.noiseBed(context, "bandpass", 1600, 0.035, 17, 0.7);
|
|
347
|
+
let chord = 0;
|
|
348
|
+
this.every(12000, () => {
|
|
349
|
+
const at = context.currentTime + 0.05, notes = CHORDS[chord % CHORDS.length];
|
|
350
|
+
this.pad(context, notes[1], at, 14, 0.018, "sine", 1200);
|
|
351
|
+
this.pad(context, notes[3], at + 2, 12, 0.012, "sine", 1600);
|
|
352
|
+
chord++;
|
|
353
|
+
});
|
|
354
|
+
this.every(1000, () => {
|
|
355
|
+
if (Math.random() > 0.12)
|
|
356
|
+
return;
|
|
357
|
+
const at = context.currentTime + Math.random() * 0.5, chirps = 2 + Math.floor(Math.random() * 3);
|
|
358
|
+
for (let chirp = 0;chirp < chirps; chirp++) {
|
|
359
|
+
const start = at + chirp * 0.13, oscillator = context.createOscillator();
|
|
360
|
+
oscillator.type = "sine";
|
|
361
|
+
const base = 2600 + Math.random() * 1400;
|
|
362
|
+
oscillator.frequency.setValueAtTime(base, start);
|
|
363
|
+
oscillator.frequency.exponentialRampToValueAtTime(base * 1.35, start + 0.07);
|
|
364
|
+
const gain = context.createGain();
|
|
365
|
+
gain.gain.setValueAtTime(0, start);
|
|
366
|
+
gain.gain.linearRampToValueAtTime(0.012, start + 0.01);
|
|
367
|
+
gain.gain.exponentialRampToValueAtTime(0.0001, start + 0.09);
|
|
368
|
+
oscillator.connect(gain).connect(this.music);
|
|
369
|
+
gain.connect(this.reverb);
|
|
370
|
+
oscillator.start(start);
|
|
371
|
+
oscillator.stop(start + 0.1);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
house(context) {
|
|
376
|
+
const bpm = 118, bar = 60 / bpm * 4;
|
|
377
|
+
let lastChord = -1;
|
|
378
|
+
this.sequence(context, bpm, (index, at) => {
|
|
379
|
+
const beat = index % 4 === 0, chord = Math.floor(index / 32) % CHORDS.length;
|
|
380
|
+
if (beat)
|
|
381
|
+
this.kick(context, at, 0.42);
|
|
382
|
+
if (index % 4 === 2)
|
|
383
|
+
this.hat(context, at, 0.06);
|
|
384
|
+
if (index % 16 === 4 || index % 16 === 12)
|
|
385
|
+
this.clap(context, at, 0.07);
|
|
386
|
+
if (index % 4 === 2)
|
|
387
|
+
this.pluck(context, CHORDS[chord][0] - 12, at, 0.22, 0.13, "triangle");
|
|
388
|
+
if (chord !== lastChord && index % 32 === 0) {
|
|
389
|
+
lastChord = chord;
|
|
390
|
+
for (const note of CHORDS[chord].slice(1, 4))
|
|
391
|
+
this.pad(context, note + 12, at, bar * 2, 0.02, "sawtooth", 1400);
|
|
392
|
+
}
|
|
393
|
+
if (index % 8 === 6 && Math.random() < 0.35)
|
|
394
|
+
this.pluck(context, CHORDS[chord][3] + 12, at, 0.3, 0.025, "sine");
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
electronic(context) {
|
|
398
|
+
const bpm = 100, delay = context.createDelay(1);
|
|
399
|
+
delay.delayTime.value = 60 / bpm * 0.75;
|
|
400
|
+
const feedback = context.createGain();
|
|
401
|
+
feedback.gain.value = 0.32;
|
|
402
|
+
const wet = context.createGain();
|
|
403
|
+
wet.gain.value = 0.5;
|
|
404
|
+
delay.connect(feedback).connect(delay);
|
|
405
|
+
delay.connect(wet).connect(this.music);
|
|
406
|
+
const arp = context.createGain();
|
|
407
|
+
arp.gain.value = 1;
|
|
408
|
+
arp.connect(this.music);
|
|
409
|
+
arp.connect(delay);
|
|
410
|
+
let lastChord = -1;
|
|
411
|
+
const bar = 60 / bpm * 4;
|
|
412
|
+
this.sequence(context, bpm, (index, at) => {
|
|
413
|
+
const chord = Math.floor(index / 32) % CHORDS.length, notes = CHORDS[chord];
|
|
414
|
+
if (index % 8 === 0)
|
|
415
|
+
this.kick(context, at, 0.32);
|
|
416
|
+
if (index % 16 === 8)
|
|
417
|
+
this.clap(context, at, 0.06);
|
|
418
|
+
if (index % 2 === 1)
|
|
419
|
+
this.hat(context, at, 0.03, 0.03);
|
|
420
|
+
const pattern = [1, 2, 3, 4, 3, 2];
|
|
421
|
+
this.pluck(context, notes[pattern[index % pattern.length]] + 12, at, 0.18, 0.035, "square", arp);
|
|
422
|
+
if (chord !== lastChord && index % 32 === 0) {
|
|
423
|
+
lastChord = chord;
|
|
424
|
+
this.pad(context, notes[0], at, bar * 2, 0.04, "triangle", 700);
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
function clamp(value) {
|
|
430
|
+
return Math.max(0, Math.min(1, Number.isFinite(value) ? value : 0.5));
|
|
431
|
+
}
|
|
432
|
+
export function useAmbientSound() {
|
|
433
|
+
return new AmbientSound;
|
|
434
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** Start offsets of each step, and the total. */
|
|
2
|
+
export declare function stepOffsets(steps: readonly BreathPacerStep[]): { starts: number[], total: number };
|
|
3
|
+
/** Where `elapsed` seconds into the steps falls. */
|
|
4
|
+
export declare function positionAt<S extends BreathPacerStep>(steps: readonly S[], elapsed: number): BreathPosition<S>;
|
|
5
|
+
/**
|
|
6
|
+
* End an open step (a hold) now: the step becomes as long as it was actually
|
|
7
|
+
* held, and everything after it moves up. Returns the new steps and how long
|
|
8
|
+
* the hold lasted (0 when the current step is not open).
|
|
9
|
+
*/
|
|
10
|
+
export declare function endStepEarly<S extends BreathPacerStep>(steps: readonly S[], elapsed: number): { steps: S[], held: number };
|
|
11
|
+
/** Jump `delta` seconds, never past either end. */
|
|
12
|
+
export declare function seekBreath(steps: readonly BreathPacerStep[], elapsed: number, delta: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* How full the pacer is at a position: 0.6 empty to 1 full, eased so a breath
|
|
15
|
+
* feels round. Natural breathing drifts gently around the middle.
|
|
16
|
+
*/
|
|
17
|
+
export declare function pacerScale(position: Pick<BreathPosition, 'step' | 'progress' | 'elapsed'>): number;
|
|
18
|
+
/**
|
|
19
|
+
* A breathing exercise that plays. Call `tick()` on an interval or each frame;
|
|
20
|
+
* it moves the position on and calls `onStep` as steps begin.
|
|
21
|
+
*/
|
|
22
|
+
export declare function useBreathPacer<S extends BreathPacerStep>(initial: S[], options?: BreathPacerOptions<S>): BreathPacer<S>;
|
|
23
|
+
export declare interface BreathPacerStep {
|
|
24
|
+
kind: BreathStepKind
|
|
25
|
+
seconds: number
|
|
26
|
+
open?: boolean
|
|
27
|
+
}
|
|
28
|
+
export declare interface BreathPosition<S extends BreathPacerStep = BreathPacerStep> {
|
|
29
|
+
index: number
|
|
30
|
+
step: S | null
|
|
31
|
+
into: number
|
|
32
|
+
progress: number
|
|
33
|
+
remaining: number
|
|
34
|
+
elapsed: number
|
|
35
|
+
total: number
|
|
36
|
+
done: boolean
|
|
37
|
+
}
|
|
38
|
+
export declare interface BreathPacerOptions<S extends BreathPacerStep> {
|
|
39
|
+
onStep?: (step: S, position: BreathPosition<S>, previous: S | null) => void
|
|
40
|
+
onDone?: (activeSeconds: number) => void
|
|
41
|
+
now?: () => number
|
|
42
|
+
}
|
|
43
|
+
export declare interface BreathPacer<S extends BreathPacerStep> {
|
|
44
|
+
readonly steps: S[]
|
|
45
|
+
readonly state: 'ready' | 'running' | 'paused' | 'done'
|
|
46
|
+
readonly activeSeconds: number
|
|
47
|
+
readonly longestHold: number
|
|
48
|
+
start: () => void
|
|
49
|
+
pause: () => void
|
|
50
|
+
resume: () => void
|
|
51
|
+
seek: (delta: number) => void
|
|
52
|
+
breatheNow: () => number
|
|
53
|
+
reset: (steps?: S[]) => void
|
|
54
|
+
tick: () => BreathPosition<S>
|
|
55
|
+
position: () => BreathPosition<S>
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* useBreathPacer - play a guided breathing pattern, step by step
|
|
59
|
+
*
|
|
60
|
+
* A breathing exercise is a list of timed steps: breathe in, hold, breathe
|
|
61
|
+
* out, hold empty, natural breathing between rounds, an open hold that ends
|
|
62
|
+
* when the person needs to breathe. This module answers the questions every
|
|
63
|
+
* breathing screen asks of such a list (where are we, how full is the pacer,
|
|
64
|
+
* how long is left) and plays one: start, pause, resume, ±seek, end an open
|
|
65
|
+
* hold now, and a callback as each step begins (for a spoken cue, a haptic
|
|
66
|
+
* tap or a tone).
|
|
67
|
+
*
|
|
68
|
+
* The timing is the page's own clock (`performance.now`), so a paused pacer
|
|
69
|
+
* never counts the pause, and a page that moves the clock in a test moves the
|
|
70
|
+
* pacer with it. Pure helpers (`positionAt`, `pacerScale`, …) work on the
|
|
71
|
+
* server too.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const pacer = useBreathPacer(steps, { onStep: (step) => speak(step.kind) })
|
|
76
|
+
* pacer.start()
|
|
77
|
+
* // each frame: const at = pacer.tick(); orb.style.transform = `scale(${pacerScale(at)})`
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
/**
|
|
81
|
+
* What one moment of an exercise asks for.
|
|
82
|
+
*
|
|
83
|
+
* in / out a paced breath, the pacer growing or shrinking
|
|
84
|
+
* hold lungs comfortably full
|
|
85
|
+
* pause lungs comfortably empty
|
|
86
|
+
* breathe natural, unpaced breathing
|
|
87
|
+
* sniff a short top-up inhale (the second half of a physiological sigh)
|
|
88
|
+
* hum a slow exhale, humming
|
|
89
|
+
* pulse a short, sharp exhale with a passive inhale
|
|
90
|
+
* retention an empty-lung hold after power breathing
|
|
91
|
+
* recover one full breath in, held
|
|
92
|
+
*/
|
|
93
|
+
export type BreathStepKind = 'in' | 'out' | 'hold' | 'pause' | 'breathe' | 'sniff' | 'hum' | 'pulse' | 'retention' | 'recover';
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
export function stepOffsets(steps) {
|
|
2
|
+
const starts = [];
|
|
3
|
+
let total = 0;
|
|
4
|
+
for (const step of steps) {
|
|
5
|
+
starts.push(total);
|
|
6
|
+
total += Math.max(0, step.seconds);
|
|
7
|
+
}
|
|
8
|
+
return { starts, total };
|
|
9
|
+
}
|
|
10
|
+
export function positionAt(steps, elapsed) {
|
|
11
|
+
const { starts, total } = stepOffsets(steps), at = Math.max(0, Math.min(total, elapsed));
|
|
12
|
+
if (!steps.length || at >= total)
|
|
13
|
+
return { index: steps.length, step: null, into: 0, progress: 1, remaining: 0, elapsed: total, total, done: !0 };
|
|
14
|
+
let low = 0, high = steps.length - 1;
|
|
15
|
+
while (low < high) {
|
|
16
|
+
const middle = low + high + 1 >> 1;
|
|
17
|
+
if (starts[middle] <= at)
|
|
18
|
+
low = middle;
|
|
19
|
+
else
|
|
20
|
+
high = middle - 1;
|
|
21
|
+
}
|
|
22
|
+
const step = steps[low], into = at - starts[low];
|
|
23
|
+
return { index: low, step, into, progress: step.seconds > 0 ? Math.min(1, into / step.seconds) : 1, remaining: Math.ceil(step.seconds - into), elapsed: at, total, done: !1 };
|
|
24
|
+
}
|
|
25
|
+
export function endStepEarly(steps, elapsed) {
|
|
26
|
+
const position = positionAt(steps, elapsed);
|
|
27
|
+
if (!position.step?.open)
|
|
28
|
+
return { steps: steps.slice(), held: 0 };
|
|
29
|
+
const held = Math.max(0.5, Math.round(position.into * 10) / 10), next = steps.slice();
|
|
30
|
+
next[position.index] = { ...position.step, seconds: held };
|
|
31
|
+
return { steps: next, held };
|
|
32
|
+
}
|
|
33
|
+
export function seekBreath(steps, elapsed, delta) {
|
|
34
|
+
const { total } = stepOffsets(steps);
|
|
35
|
+
return Math.max(0, Math.min(total, elapsed + delta));
|
|
36
|
+
}
|
|
37
|
+
export function pacerScale(position) {
|
|
38
|
+
const step = position.step;
|
|
39
|
+
if (!step)
|
|
40
|
+
return 0.6;
|
|
41
|
+
const ease = (value) => 0.5 - Math.cos(Math.PI * Math.min(1, Math.max(0, value))) / 2;
|
|
42
|
+
switch (step.kind) {
|
|
43
|
+
case "in":
|
|
44
|
+
return 0.6 + 0.4 * ease(position.progress);
|
|
45
|
+
case "sniff":
|
|
46
|
+
return 0.92 + 0.08 * ease(position.progress);
|
|
47
|
+
case "out":
|
|
48
|
+
case "hum":
|
|
49
|
+
return 1 - 0.4 * ease(position.progress);
|
|
50
|
+
case "hold":
|
|
51
|
+
case "recover":
|
|
52
|
+
return 1;
|
|
53
|
+
case "pause":
|
|
54
|
+
case "retention":
|
|
55
|
+
return 0.6;
|
|
56
|
+
case "pulse":
|
|
57
|
+
return position.progress < 0.3 ? 0.8 - 0.1 * (position.progress / 0.3) : 0.7 + 0.1 * ((position.progress - 0.3) / 0.7);
|
|
58
|
+
default:
|
|
59
|
+
return 0.78 + 0.04 * Math.sin(position.elapsed / 5 * Math.PI);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function useBreathPacer(initial, options = {}) {
|
|
63
|
+
const clock = options.now ?? (() => globalThis.performance ? globalThis.performance.now() : Date.now());
|
|
64
|
+
let steps = initial.slice(), state = "ready", base = 0, resumedAt = 0, active = 0, lastIndex = -1, longestHold = 0;
|
|
65
|
+
const elapsed = () => state === "running" ? base + (clock() - resumedAt) / 1000 : base, bank = () => {
|
|
66
|
+
if (state === "running")
|
|
67
|
+
active += (clock() - resumedAt) / 1000;
|
|
68
|
+
};
|
|
69
|
+
function noteHold(step) {
|
|
70
|
+
if (step?.open)
|
|
71
|
+
longestHold = Math.max(longestHold, Math.round(step.seconds));
|
|
72
|
+
}
|
|
73
|
+
const pacer = {
|
|
74
|
+
get steps() {
|
|
75
|
+
return steps;
|
|
76
|
+
},
|
|
77
|
+
get state() {
|
|
78
|
+
return state;
|
|
79
|
+
},
|
|
80
|
+
get activeSeconds() {
|
|
81
|
+
return active + (state === "running" ? (clock() - resumedAt) / 1000 : 0);
|
|
82
|
+
},
|
|
83
|
+
get longestHold() {
|
|
84
|
+
return longestHold;
|
|
85
|
+
},
|
|
86
|
+
start() {
|
|
87
|
+
base = 0;
|
|
88
|
+
active = 0;
|
|
89
|
+
lastIndex = -1;
|
|
90
|
+
longestHold = 0;
|
|
91
|
+
resumedAt = clock();
|
|
92
|
+
state = "running";
|
|
93
|
+
pacer.tick();
|
|
94
|
+
},
|
|
95
|
+
pause() {
|
|
96
|
+
if (state !== "running")
|
|
97
|
+
return;
|
|
98
|
+
bank();
|
|
99
|
+
base = elapsed();
|
|
100
|
+
state = "paused";
|
|
101
|
+
},
|
|
102
|
+
resume() {
|
|
103
|
+
if (state !== "paused")
|
|
104
|
+
return;
|
|
105
|
+
resumedAt = clock();
|
|
106
|
+
state = "running";
|
|
107
|
+
},
|
|
108
|
+
seek(delta) {
|
|
109
|
+
const at = seekBreath(steps, elapsed(), delta);
|
|
110
|
+
bank();
|
|
111
|
+
base = at;
|
|
112
|
+
resumedAt = clock();
|
|
113
|
+
lastIndex = positionAt(steps, at).index;
|
|
114
|
+
},
|
|
115
|
+
breatheNow() {
|
|
116
|
+
const ended = endStepEarly(steps, elapsed());
|
|
117
|
+
if (!ended.held)
|
|
118
|
+
return 0;
|
|
119
|
+
steps = ended.steps;
|
|
120
|
+
longestHold = Math.max(longestHold, Math.round(ended.held));
|
|
121
|
+
return ended.held;
|
|
122
|
+
},
|
|
123
|
+
reset(next) {
|
|
124
|
+
if (next)
|
|
125
|
+
steps = next.slice();
|
|
126
|
+
state = "ready";
|
|
127
|
+
base = 0;
|
|
128
|
+
active = 0;
|
|
129
|
+
lastIndex = -1;
|
|
130
|
+
},
|
|
131
|
+
tick() {
|
|
132
|
+
const position = positionAt(steps, elapsed());
|
|
133
|
+
if (state !== "running")
|
|
134
|
+
return position;
|
|
135
|
+
if (position.done) {
|
|
136
|
+
bank();
|
|
137
|
+
base = position.total;
|
|
138
|
+
state = "done";
|
|
139
|
+
noteHold(steps[steps.length - 1]);
|
|
140
|
+
options.onDone?.(active);
|
|
141
|
+
return position;
|
|
142
|
+
}
|
|
143
|
+
if (position.index !== lastIndex) {
|
|
144
|
+
const previous = position.index > 0 ? steps[position.index - 1] : null;
|
|
145
|
+
if (lastIndex >= 0)
|
|
146
|
+
noteHold(previous);
|
|
147
|
+
lastIndex = position.index;
|
|
148
|
+
options.onStep?.(position.step, position, previous);
|
|
149
|
+
}
|
|
150
|
+
return position;
|
|
151
|
+
},
|
|
152
|
+
position: () => positionAt(steps, elapsed())
|
|
153
|
+
};
|
|
154
|
+
return pacer;
|
|
155
|
+
}
|
package/dist/craft-entry.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import{Fr,Eo,_o}from"./chunk-t7s0pdtw.js";import"./chunk-ab7vh24b.js";import"./chunk-wkc34drs.js";import"./chunk-9ztdvnkk.js";import"./chunk-01nx6zcw.js";import"./chunk-pf0d8y9n.js";import"./chunk-
|
|
2
|
+
import{Fr,Eo,_o}from"./chunk-t7s0pdtw.js";import"./chunk-ab7vh24b.js";import"./chunk-wkc34drs.js";import"./chunk-9ztdvnkk.js";import"./chunk-01nx6zcw.js";import"./chunk-pf0d8y9n.js";import"./chunk-sz5m446r.js";import"./chunk-tbef3yyk.js";import"./chunk-s10r6hxp.js";import"./chunk-99kt56fn.js";import"./chunk-pj332gj9.js";import"./chunk-pcq6vphz.js";import"./chunk-j4afmryj.js";import{uu,wo,du,pu,vo,Br,xo,fu,So,gu,mu,ko,Co,$o}from"./chunk-jcrcqtb3.js";import"./chunk-1khb6g2w.js";import"./chunk-8adstrk5.js";var m=new Map,d=new Map;function R(t,e){let n=e?.id||E(t),r=I(t),a=m.get(n);if(a&&a.hash===r)return a;let i=M(t),s=L(t),o=W(t),c=O(i),{staticParts:p,dynamicParts:l}=A(i),f=_(t),g={id:n,hash:r,tree:i,scripts:s,styles:o,staticParts:p,dynamicParts:l,dependencies:f,metadata:c};return m.set(n,g),g}async function Q(t,e={},n={}){let r=performance.now();if(n.cache){let o=`${t.id}:${JSON.stringify(e)}`,c=d.get(o);if(c&&Date.now()-c.timestamp<(n.cacheTTL||60000))return{html:c.html,styles:t.styles.join(`
|
|
3
3
|
`),scripts:t.scripts.join(`
|
|
4
4
|
`),metadata:{renderTime:0,cacheHit:!0,totalSize:c.html.length,componentCount:t.metadata.componentCount}}}let a;if(n.streaming)a=await H(t.tree,e);else a=h(t.tree,e);let i;if(n.hydrate){let o=j(a,t,e);a=o.html,i=o.data}if(n.minify)a=D(a);if(n.cache){let o=`${t.id}:${JSON.stringify(e)}`;d.set(o,{html:a,timestamp:Date.now()})}let s=performance.now()-r;return{html:a,styles:t.styles.join(`
|
|
5
5
|
`),scripts:t.scripts.join(`
|