mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented new audio engine and deck engine
This commit is contained in:
1 parent
4fdb2b7ce1
commit
f5e7202f3f
19 files changed
+1794
-577
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// The app's ONE AudioContext. Every deck plays into it, so the decks can be
|
||||
// mixed (and limited) before the speakers; two contexts cannot be connected.
|
||||
// Never closed: a deck that goes away disconnects its own nodes.
|
||||
|
||||
import deckWorkletUrl from './deckProcessor.worklet.ts?worker&url'
|
||||
import rubberbandUrl from './rubberband.wasm?url'
|
||||
|
||||
let ctx: AudioContext | null = null
|
||||
let master: GainNode | null = null
|
||||
let worklet: Promise<ArrayBuffer | null> | null = null
|
||||
|
||||
export function audioContext(): AudioContext {
|
||||
if (!ctx) {
|
||||
const AC: typeof AudioContext =
|
||||
window.AudioContext ?? (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext
|
||||
ctx = new AC({ latencyHint: 'interactive' })
|
||||
master = ctx.createGain()
|
||||
master.connect(ctx.destination)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
/** Resume on a user gesture (autoplay policy). */
|
||||
export function resumeAudio(): void {
|
||||
void audioContext().resume()
|
||||
}
|
||||
|
||||
/** Where decks connect: the master bus. */
|
||||
export function masterInput(): AudioNode {
|
||||
audioContext()
|
||||
return master!
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the deck processor (once per context) and fetch Rubber Band's
|
||||
* module bytes for keylock. Resolves to null bytes when the module cannot be
|
||||
* fetched — the decks then play without keylock rather than not at all.
|
||||
*/
|
||||
export function loadDeckWorklet(): Promise<ArrayBuffer | null> {
|
||||
if (!worklet) {
|
||||
const c = audioContext()
|
||||
const bytes = fetch(rubberbandUrl)
|
||||
.then((r) => (r.ok ? r.arrayBuffer() : null))
|
||||
.catch(() => null)
|
||||
worklet = c.audioWorklet.addModule(deckWorkletUrl).then(() => bytes)
|
||||
worklet.catch(() => {
|
||||
worklet = null
|
||||
})
|
||||
}
|
||||
return worklet
|
||||
}
|
||||
Reference in new issue
Block a user