@yaks/stage 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +73 -2
- package/_dist/aim.d.ts +14 -0
- package/_dist/aim.d.ts.map +1 -0
- package/_dist/air.d.ts +23 -0
- package/_dist/air.d.ts.map +1 -0
- package/_dist/bus.d.ts +8 -0
- package/_dist/bus.d.ts.map +1 -0
- package/_dist/cam.d.ts +75 -0
- package/_dist/cam.d.ts.map +1 -0
- package/_dist/ears.d.ts +71 -0
- package/_dist/ears.d.ts.map +1 -0
- package/_dist/flames.d.ts +10 -0
- package/_dist/flames.d.ts.map +1 -0
- package/_dist/fx.d.ts +27 -0
- package/_dist/fx.d.ts.map +1 -0
- package/_dist/halo.d.ts +4 -0
- package/_dist/halo.d.ts.map +1 -0
- package/_dist/instances.d.ts +24 -0
- package/_dist/instances.d.ts.map +1 -0
- package/_dist/mod.d.ts +7 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/pace.d.ts +19 -0
- package/_dist/pace.d.ts.map +1 -0
- package/_dist/render.d.ts +7 -0
- package/_dist/render.d.ts.map +1 -0
- package/_dist/samples.d.ts +27 -0
- package/_dist/samples.d.ts.map +1 -0
- package/_dist/scene.d.ts +28 -0
- package/_dist/scene.d.ts.map +1 -0
- package/_dist/sound.d.ts +13 -0
- package/_dist/sound.d.ts.map +1 -0
- package/aim.js +63 -0
- package/aim.js.map +1 -0
- package/aim.ts +71 -0
- package/air.js +40 -0
- package/air.js.map +1 -0
- package/air.ts +66 -0
- package/bus.js +36 -0
- package/bus.js.map +1 -0
- package/bus.ts +51 -0
- package/cam.js +110 -0
- package/cam.js.map +1 -0
- package/cam.ts +181 -0
- package/deno.json +1 -0
- package/ears.js +123 -0
- package/ears.js.map +1 -0
- package/ears.ts +131 -0
- package/flames.js +61 -0
- package/flames.js.map +1 -0
- package/flames.ts +69 -0
- package/fx.js +235 -0
- package/fx.js.map +1 -0
- package/fx.ts +330 -0
- package/halo.js +22 -0
- package/halo.js.map +1 -0
- package/halo.ts +23 -0
- package/instances.js +131 -0
- package/instances.js.map +1 -0
- package/instances.ts +166 -0
- package/mod.js +5 -0
- package/mod.js.map +1 -0
- package/mod.ts +7 -0
- package/pace.js +63 -0
- package/pace.js.map +1 -0
- package/pace.ts +86 -0
- package/package.json +73 -4
- package/render.js +50 -0
- package/render.js.map +1 -0
- package/render.ts +68 -0
- package/samples.js +162 -0
- package/samples.js.map +1 -0
- package/samples.ts +204 -0
- package/scene.js +80 -0
- package/scene.js.map +1 -0
- package/scene.ts +112 -0
- package/sound.js +117 -0
- package/sound.js.map +1 -0
- package/sound.ts +175 -0
package/sound.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sound.js","sources":["./sound.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,kFAAkF;AAClF,SAEE,OAAO,EAEP,IAAI,EACJ,KAAK,oBAEW;AAClB,SAAS,QAAQ,uBAAkC;AAmCnD,mEAAmE,GACnE,OAAO,IAAI,UAAU,CACnB,KACA,aACA;EAEA,IAAI,OAAO;EACX,IAAI,QAAmC,IAAI;EAC3C,IAAI,OAAmB;EACvB,IAAI,UAAU,IAAI;EAClB,IAAI,SAAS,SAAS,WAAW,CAAC,QAAQ;EAC1C,IAAI,SAAS;EACb,IAAI,YAAY,CAAC,IAAY,UAAqB,GAAG,QAAQ,CAAC,EAAE,IAAI;EACpE,yEAAyE;EACzE,sEAAsE;EACtE,kEAAkE;EAClE,IAAI,MAAM,CAAC,IAAkB,KAC3B,GAAG,OAAO,CAAC,CAAC,GAAG,IAAM,EAAE,KAAK,GAAG,EAAE,CAAC,EAAE;EACtC,IAAI,OAAO,CAAC,KAAiB,KAC3B,IAAI;MAAC,IAAI,SAAS;MAAE,IAAI,SAAS;MAAE,IAAI,SAAS;KAAC,EAAE;EACrD,IAAI,WAAW,CAAC,GAAkB;IAChC,IAAI,CA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AQ,MAAM,CAAC;QACjB;MACF;IACF;EACF;AACF,EAAC"}
|
package/sound.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Positional sources follow caller-supplied locations, independently of the
|
|
2
|
+
// world or graph that supplied them. Fixed points and direct sounds share voices.
|
|
3
|
+
import {
|
|
4
|
+
type Ear,
|
|
5
|
+
FALLOFF,
|
|
6
|
+
type Falloff,
|
|
7
|
+
hear,
|
|
8
|
+
QUIET,
|
|
9
|
+
type Vec3,
|
|
10
|
+
} from './ears.js'
|
|
11
|
+
import { oneShots, type Voice } from './samples.js'
|
|
12
|
+
|
|
13
|
+
/** Where a sound is made: at a moving source identified by a caller-supplied key, at a point, or nowhere, straight to the ears. */
|
|
14
|
+
export type From = string | Vec3 | null
|
|
15
|
+
|
|
16
|
+
/** A sound that keeps going into `into` once made, until it is stopped by
|
|
17
|
+
* what this returns. */
|
|
18
|
+
export type Keep = (ctx: AudioContext, into: AudioNode) => () => void
|
|
19
|
+
|
|
20
|
+
// A panner where something is, while it sounds, and `into` it, the loudness
|
|
21
|
+
// its falloff has up close: `until` it has sounded its last; `fixed` at a
|
|
22
|
+
// point, or following what its id names; `end` stops the sound kept there.
|
|
23
|
+
type Source = {
|
|
24
|
+
id: string
|
|
25
|
+
pan: PannerNode
|
|
26
|
+
into: AudioNode
|
|
27
|
+
until: number
|
|
28
|
+
fixed: boolean
|
|
29
|
+
shots: ReturnType<typeof oneShots>
|
|
30
|
+
end?: () => void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Sources owned by one audio context, updated with locations each frame. */
|
|
34
|
+
export interface Sources<Channel extends string> {
|
|
35
|
+
make(from: From, voice: Voice, key?: string): void
|
|
36
|
+
keep(
|
|
37
|
+
id: string,
|
|
38
|
+
sound: Keep,
|
|
39
|
+
falloff?: Falloff,
|
|
40
|
+
channel?: Channel,
|
|
41
|
+
): () => void
|
|
42
|
+
playing(id: string, channel?: Channel): boolean
|
|
43
|
+
listen(ears: Ear, locations: ReadonlyMap<string, Vec3>): void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A source bank for one context and a set of named output buses. */
|
|
47
|
+
export let sources = <Channel extends string>(
|
|
48
|
+
ctx: AudioContext,
|
|
49
|
+
destination: Record<Channel, AudioNode>,
|
|
50
|
+
effects: Channel,
|
|
51
|
+
): Sources<Channel> => {
|
|
52
|
+
let TAIL = 0.5
|
|
53
|
+
let spots: ReadonlyMap<string, Vec3> = new Map()
|
|
54
|
+
let ears: Ear | null = null
|
|
55
|
+
let sources = new Map<string, Source>()
|
|
56
|
+
let direct = oneShots(destination[effects])
|
|
57
|
+
let points = 0
|
|
58
|
+
let sourceKey = (id: string, channel: Channel) => `${channel}/${id}`
|
|
59
|
+
// A source or the ears, put where they are this frame. Set outright, not
|
|
60
|
+
// automated: a frame moves them a few centimetres, and a panner whose
|
|
61
|
+
// position is automated works its sound out anew at every sample.
|
|
62
|
+
let put = (ps: AudioParam[], vs: number[]) =>
|
|
63
|
+
ps.forEach((p, i) => p.value = vs[i])
|
|
64
|
+
let move = (pan: PannerNode, at: Vec3) =>
|
|
65
|
+
put([pan.positionX, pan.positionY, pan.positionZ], at)
|
|
66
|
+
let listenAt = (l: AudioListener, e: Ear) => {
|
|
67
|
+
if (!l.positionX) {
|
|
68
|
+
// Firefox turns its listener only the old way.
|
|
69
|
+
l.setPosition(...e.at)
|
|
70
|
+
l.setOrientation(...e.forward, ...e.up)
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
put([
|
|
74
|
+
l.positionX,
|
|
75
|
+
l.positionY,
|
|
76
|
+
l.positionZ,
|
|
77
|
+
l.forwardX,
|
|
78
|
+
l.forwardY,
|
|
79
|
+
l.forwardZ,
|
|
80
|
+
l.upX,
|
|
81
|
+
l.upY,
|
|
82
|
+
l.upZ,
|
|
83
|
+
], [...e.at, ...e.forward, ...e.up])
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let panner = (
|
|
87
|
+
id: string,
|
|
88
|
+
at: Vec3,
|
|
89
|
+
fixed: boolean,
|
|
90
|
+
falloff: Falloff = FALLOFF,
|
|
91
|
+
channel: Channel = effects,
|
|
92
|
+
) => {
|
|
93
|
+
let pan = new PannerNode(ctx, falloff.pan)
|
|
94
|
+
let into = new GainNode(ctx, { gain: falloff.near })
|
|
95
|
+
move(pan, at)
|
|
96
|
+
into.connect(pan).connect(destination[channel])
|
|
97
|
+
let s: Source = {
|
|
98
|
+
id,
|
|
99
|
+
pan,
|
|
100
|
+
into,
|
|
101
|
+
until: 0,
|
|
102
|
+
fixed,
|
|
103
|
+
shots: oneShots(into),
|
|
104
|
+
}
|
|
105
|
+
sources.set(sourceKey(id, channel), s)
|
|
106
|
+
return s
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Make `v` at `from`: not at all while muted or asleep, or when it would be
|
|
110
|
+
* too faint to hear. */
|
|
111
|
+
let make = (from: From, v: Voice, key?: string) => {
|
|
112
|
+
if (from == null) return direct.play(v, key)
|
|
113
|
+
let id = typeof from == 'string' ? from : `@${points++}`
|
|
114
|
+
let at = typeof from == 'string' ? spots.get(from) : from
|
|
115
|
+
if (!at || (ears && hear(ears, at).gain * v.loud < QUIET)) return
|
|
116
|
+
let s = sources.get(sourceKey(id, effects)) ??
|
|
117
|
+
panner(id, at, typeof from != 'string')
|
|
118
|
+
s.until = Math.max(s.until, ctx.currentTime + v.dur + TAIL)
|
|
119
|
+
s.shots.play(v, key)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// A kept sound ends: it fades, and its source goes once it has.
|
|
123
|
+
let end = (s: Source) => {
|
|
124
|
+
s.end?.()
|
|
125
|
+
s.end = undefined
|
|
126
|
+
s.until = ctx.currentTime + 1 + TAIL
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Keep a sound going at `id`, where the frames put it, falling away as
|
|
130
|
+
* `falloff` says, until it is let go by what this returns, or what it is at
|
|
131
|
+
* is gone. */
|
|
132
|
+
let keep = (
|
|
133
|
+
id: string,
|
|
134
|
+
sound: Keep,
|
|
135
|
+
falloff: Falloff = FALLOFF,
|
|
136
|
+
channel: Channel = effects,
|
|
137
|
+
) => {
|
|
138
|
+
let at = spots.get(id)
|
|
139
|
+
if (!at) return () => {}
|
|
140
|
+
let s = sources.get(sourceKey(id, channel)) ??
|
|
141
|
+
panner(id, at, false, falloff, channel)
|
|
142
|
+
if (s.end) end(s)
|
|
143
|
+
let stop = sound(ctx, s.into)
|
|
144
|
+
s.end = stop
|
|
145
|
+
s.until = Infinity
|
|
146
|
+
return () => {
|
|
147
|
+
if (s.end == stop) end(s)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
make,
|
|
153
|
+
keep,
|
|
154
|
+
playing: (id: string, channel: Channel = effects) =>
|
|
155
|
+
!!sources.get(sourceKey(id, channel))?.end,
|
|
156
|
+
listen: (e: Ear, locations: ReadonlyMap<string, Vec3>) => {
|
|
157
|
+
ears = e
|
|
158
|
+
spots = locations
|
|
159
|
+
listenAt(ctx.listener, e)
|
|
160
|
+
direct.prune()
|
|
161
|
+
let t = ctx.currentTime
|
|
162
|
+
for (let [key, s] of sources) {
|
|
163
|
+
s.shots.prune()
|
|
164
|
+
let at = s.fixed ? undefined : spots.get(s.id)
|
|
165
|
+
if (at) move(s.pan, at)
|
|
166
|
+
else if (s.end) end(s)
|
|
167
|
+
if (t > s.until) {
|
|
168
|
+
s.into.disconnect()
|
|
169
|
+
s.pan.disconnect()
|
|
170
|
+
sources.delete(key)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
}
|