File size: 7,767 Bytes
0daf81d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Sound design, voice processing and mixing — all synthesised in the browser, no samples, no AI.
export const SR = 48000

const secs = (d: number) => Math.round(SR * d)

function onePoleLP(x: Float32Array, fc: (i: number) => number) {
  const out = new Float32Array(x.length)
  let y = 0
  let a = 0
  for (let i = 0; i < x.length; i++) {
    if (i % 64 === 0) a = Math.exp((-2 * Math.PI * fc(i)) / SR)
    y = (1 - a) * x[i] + a * y
    out[i] = y
  }
  return out
}

function rng(seed: number) {
  let s = seed >>> 0
  return () => {
    s = (s * 1664525 + 1013904223) >>> 0
    return s / 4294967296
  }
}
function gauss(r: () => number) {
  return Math.sqrt(-2 * Math.log(r() + 1e-12)) * Math.cos(2 * Math.PI * r())
}
function normalize(x: Float32Array, peak: number) {
  let m = 1e-9
  for (const v of x) m = Math.max(m, Math.abs(v))
  for (let i = 0; i < x.length; i++) x[i] = (x[i] / m) * peak
  return x
}

export function whoosh(d = 0.45) {
  const n = secs(d)
  const r = rng(3)
  const noise = new Float32Array(n).map(() => gauss(r))
  const hi = onePoleLP(noise, (i) => 500 + 6500 * Math.pow(i / n, 1.4))
  const lo = onePoleLP(noise, (i) => 150 + 1800 * Math.pow(i / n, 1.4))
  const out = new Float32Array(n)
  for (let i = 0; i < n; i++) {
    const e = Math.pow(Math.sin(Math.PI * Math.pow(i / n, 0.7)), 2)
    out[i] = (hi[i] - lo[i]) * e
  }
  return normalize(out, 0.42)
}

const SCALE = [0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24, 26, 28, 31, 33, 36]
export function tick(i: number) {
  const f = 587.33 * Math.pow(2, SCALE[Math.min(i, SCALE.length - 1)] / 12)
  const n = secs(0.18)
  const out = new Float32Array(n)
  for (let k = 0; k < n; k++) {
    const t = k / SR
    const s = Math.sin(2 * Math.PI * f * t) * 0.8 + Math.sin(2 * Math.PI * f * 4 * t) * 0.15 * Math.exp(-t * 70)
    out[k] = s * Math.exp(-t * 26) * (1 - Math.exp(-t * 2000)) * 0.3
  }
  return out
}

export function chime() {
  const n = secs(1.4)
  const out = new Float32Array(n)
  const notes: [number, number][] = [[1046.5, 0], [1318.5, 0.055], [1568, 0.11], [2093, 0.165]]
  notes.forEach(([f, dt], k) => {
    const s0 = secs(dt)
    for (let i = s0; i < n; i++) {
      const t = (i - s0) / SR
      const tone = Math.sin(2 * Math.PI * f * t) + 0.35 * Math.sin(2 * Math.PI * f * 2.76 * t) * Math.exp(-t * 9) +
        0.15 * Math.sin(2 * Math.PI * f * 5.4 * t) * Math.exp(-t * 16)
      out[i] += tone * Math.exp(-t * 4) * (1 - Math.exp(-t * 900)) * (0.9 - k * 0.12)
    }
  })
  return normalize(out, 0.26)
}

export function pop() {
  const n = secs(0.12)
  const out = new Float32Array(n)
  let ph = 0
  for (let i = 0; i < n; i++) {
    const t = i / SR
    ph += (2 * Math.PI * (1100 * Math.exp(-t * 22) + 260)) / SR
    out[i] = Math.sin(ph) * Math.exp(-t * 32) * (1 - Math.exp(-t * 3000)) * 0.32
  }
  return out
}

export function shimmer(d = 1.0) {
  const r = rng(11)
  const n = secs(d)
  const out = new Float32Array(n)
  for (let j = 0; j < 30; j++) {
    const st = secs(r() * d * 0.55)
    const f = 2800 + r() * 4000
    const amp = 0.2 + r() * 0.3
    const g = secs(0.2)
    for (let i = 0; i < g && st + i < n; i++) {
      const t = i / SR
      out[st + i] += Math.sin(2 * Math.PI * f * t) * Math.exp(-t * 28) * amp
    }
  }
  for (let i = 0; i < n; i++) out[i] *= Math.exp((-i / SR) * 2.4) * 0.16
  return out
}

// ---------- voice ----------
/** Decode any audio file the browser understands into mono 48 kHz samples. */
export async function decodeToMono(data: ArrayBuffer): Promise<Float32Array> {
  const Ctx = window.OfflineAudioContext || (window as unknown as { webkitOfflineAudioContext: typeof OfflineAudioContext }).webkitOfflineAudioContext
  const probe = new Ctx(1, 1, SR)
  const buf = await probe.decodeAudioData(data.slice(0))
  const off = new Ctx(1, Math.ceil(buf.duration * SR) + 1, SR)
  const src = off.createBufferSource()
  src.buffer = buf
  src.connect(off.destination)
  src.start()
  const out = await off.startRendering()
  return out.getChannelData(0).slice()
}

/** Trim leading/trailing silence, fade edges, normalise. */
export function cleanVoice(x: Float32Array): Float32Array {
  const win = 480
  const env = new Float32Array(x.length)
  let acc = 0
  for (let i = 0; i < x.length; i++) {
    acc += Math.abs(x[i])
    if (i >= win) acc -= Math.abs(x[i - win])
    env[i] = acc / win
  }
  let peakEnv = 0
  for (const v of env) peakEnv = Math.max(peakEnv, v)
  const thr = Math.max(0.004, peakEnv * 0.06)
  let a = 0
  let b = x.length - 1
  while (a < x.length && env[a] < thr) a++
  while (b > a && env[b] < thr) b--
  a = Math.max(0, a - 720 - win)
  b = Math.min(x.length, b + 2400)
  const y = x.slice(a, b)
  const f = Math.min(480, Math.floor(y.length / 4))
  for (let i = 0; i < f; i++) {
    y[i] *= i / f
    y[y.length - 1 - i] *= i / f
  }
  return normalize(y, 0.92)
}

/** Slow a clip down by resampling (pitch drops slightly, like a tape slowed down). */
export function slowDown(x: Float32Array, rate: number): Float32Array {
  const n = Math.floor(x.length / rate)
  const out = new Float32Array(n)
  for (let i = 0; i < n; i++) {
    const p = i * rate
    const k = Math.floor(p)
    const fr = p - k
    out[i] = (x[k] ?? 0) * (1 - fr) + (x[k + 1] ?? 0) * fr
  }
  return out
}

export function envelope(clip: Float32Array, fps: number): Float32Array {
  const hop = Math.floor(SR / fps)
  const n = Math.floor(clip.length / hop) + 1
  const e = new Float32Array(n)
  let m = 1e-9
  for (let i = 0; i < n; i++) {
    let s = 0
    let c = 0
    for (let k = i * hop; k < Math.min(clip.length, (i + 1) * hop); k++) {
      s += clip[k] * clip[k]
      c++
    }
    e[i] = c ? Math.sqrt(s / c) : 0
    m = Math.max(m, e[i])
  }
  return e.map((v) => Math.min(1, v / m))
}

export class Mixer {
  L: Float32Array
  R: Float32Array
  constructor(public dur: number) {
    this.L = new Float32Array(secs(dur))
    this.R = new Float32Array(secs(dur))
  }
  add(clip: Float32Array, at: number, gain = 1, pan = 0) {
    const s = Math.round(at * SR)
    const l = Math.cos(((pan + 1) * Math.PI) / 4) * Math.SQRT2 * gain
    const r = Math.sin(((pan + 1) * Math.PI) / 4) * Math.SQRT2 * gain
    for (let i = 0; i < clip.length; i++) {
      const p = s + i
      if (p < 0) continue
      if (p >= this.L.length) break
      this.L[p] += clip[i] * l
      this.R[p] += clip[i] * r
    }
  }
  finish() {
    const k = Math.tanh(1.1)
    const n = this.L.length
    const fade = secs(0.06)
    for (let i = 0; i < n; i++) {
      let g = 1
      if (i < 240) g = i / 240
      if (i > n - fade) g = (n - i) / fade
      this.L[i] = (Math.tanh(this.L[i] * 1.1) / k) * g
      this.R[i] = (Math.tanh(this.R[i] * 1.1) / k) * g
    }
    return this
  }
  wav(): Uint8Array {
    const n = this.L.length
    const buf = new ArrayBuffer(44 + n * 4)
    const v = new DataView(buf)
    const w = (o: number, s: string) => [...s].forEach((c, i) => v.setUint8(o + i, c.charCodeAt(0)))
    w(0, 'RIFF'); v.setUint32(4, 36 + n * 4, true); w(8, 'WAVE'); w(12, 'fmt ')
    v.setUint32(16, 16, true); v.setUint16(20, 1, true); v.setUint16(22, 2, true); v.setUint32(24, SR, true)
    v.setUint32(28, SR * 4, true); v.setUint16(32, 4, true); v.setUint16(34, 16, true); w(36, 'data'); v.setUint32(40, n * 4, true)
    let o = 44
    for (let i = 0; i < n; i++) {
      v.setInt16(o, Math.max(-1, Math.min(1, this.L[i])) * 32767, true)
      v.setInt16(o + 2, Math.max(-1, Math.min(1, this.R[i])) * 32767, true)
      o += 4
    }
    return new Uint8Array(buf)
  }
  toAudioBuffer(ctx: BaseAudioContext): AudioBuffer {
    const b = ctx.createBuffer(2, this.L.length, SR)
    b.copyToChannel(this.L as Float32Array<ArrayBuffer>, 0)
    b.copyToChannel(this.R as Float32Array<ArrayBuffer>, 1)
    return b
  }
}