From e6791b872d26d8f9aed4de8e446afcd9873a089f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 8 May 2023 09:20:51 -0600 Subject: [PATCH] Finish merging audio with video. --- player/src/audio/index.ts | 77 -------------------------------------- player/src/media/index.ts | 25 ++++++------- player/src/player/index.ts | 2 +- 3 files changed, 13 insertions(+), 91 deletions(-) delete mode 100644 player/src/audio/index.ts diff --git a/player/src/audio/index.ts b/player/src/audio/index.ts deleted file mode 100644 index 7076cd3..0000000 --- a/player/src/audio/index.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as Message from "./message" -import Renderer from "../media/audio" -import Decoder from "./decoder" - -import { RingInit } from "../media/ring" - -// Abstracts the Worker and Worklet into a simpler API -// This class must be created on the main thread due to AudioContext. -export default class Audio { - context: AudioContext; - worker: Worker; - worklet: Promise; - - constructor() { - // Assume 44.1kHz and two audio channels - const config = { - sampleRate: 44100, - ring: new RingInit(2, 4410), // 100ms at 44.1khz - } - - this.context = new AudioContext({ - latencyHint: "interactive", - sampleRate: config.sampleRate, - }) - - this.worker = this.setupWorker(config) - this.worklet = this.setupWorklet(config) - } - - private setupWorker(config: Message.Config): Worker { - const url = new URL('worker.ts', import.meta.url) - const worker = new Worker(url, { - name: "audio", - type: "module", - }) - - worker.postMessage({ config }) - - return worker - } - - private async setupWorklet(config: Message.Config): Promise { - // Load the worklet source code. - const url = new URL('worklet.ts', import.meta.url) - await this.context.audioWorklet.addModule(url) - - const volume = this.context.createGain() - volume.gain.value = 2.0; - - // Create a worklet - const worklet = new AudioWorkletNode(this.context, 'renderer'); - worklet.onprocessorerror = (e: Event) => { - console.error("Audio worklet error:", e) - }; - - worklet.port.postMessage({ config }) - - // Connect the worklet to the volume node and then to the speakers - worklet.connect(volume) - volume.connect(this.context.destination) - - return worklet - } - - init(init: Message.Init) { - this.worker.postMessage({ init }) - } - - segment(segment: Message.Segment) { - this.worker.postMessage({ segment }, [ segment.buffer.buffer, segment.reader ]) - } - - play(play: Message.Play) { - this.context.resume() - //this.worker.postMessage({ play }) - } -} \ No newline at end of file diff --git a/player/src/media/index.ts b/player/src/media/index.ts index fd42ed9..376052a 100644 --- a/player/src/media/index.ts +++ b/player/src/media/index.ts @@ -30,19 +30,6 @@ export default class Media { this.worklet = this.setupWorklet(config) } - init(init: Message.Init) { - this.worker.postMessage({ init }, [ init.buffer.buffer, init.reader ]) - } - - segment(segment: Message.Segment) { - this.worker.postMessage({ segment }, [ segment.buffer.buffer, segment.reader ]) - } - - play(play: Message.Play) { - this.context.resume() - //this.worker.postMessage({ play }) - } - private setupWorker(config: Message.Config): Worker { const url = new URL('worker.ts', import.meta.url) @@ -79,4 +66,16 @@ export default class Media { return worklet } + init(init: Message.Init) { + this.worker.postMessage({ init }, [ init.buffer.buffer, init.reader ]) + } + + segment(segment: Message.Segment) { + this.worker.postMessage({ segment }, [ segment.buffer.buffer, segment.reader ]) + } + + play(play: Message.Play) { + this.context.resume() + //this.worker.postMessage({ play }) + } } \ No newline at end of file diff --git a/player/src/player/index.ts b/player/src/player/index.ts index 7fafccb..9e9c2a6 100644 --- a/player/src/player/index.ts +++ b/player/src/player/index.ts @@ -28,6 +28,6 @@ export default class Player { } play() { - //this.media.play() + this.media.play({}) } } \ No newline at end of file