-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web-wasm] Add minimal audio support
- Loading branch information
Showing
23 changed files
with
548 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
ts/@live-compositor/web-wasm/src/mainContext/AudioMixer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { Api } from 'live-compositor'; | ||
import { assert } from '../utils'; | ||
|
||
type AudioInput = { | ||
source: MediaStreamAudioSourceNode; | ||
gain: GainNode; | ||
}; | ||
|
||
export class AudioMixer<OutputNode extends AudioNode = AudioNode> { | ||
private ctx: AudioContext; | ||
private inputs: Record<string, AudioInput> = {}; | ||
protected outputNode: OutputNode; | ||
|
||
constructor(ctx: AudioContext, outputNode: OutputNode) { | ||
this.ctx = ctx; | ||
this.outputNode = outputNode; | ||
} | ||
|
||
public addInput(inputId: string, track: MediaStreamTrack) { | ||
const stream = new MediaStream(); | ||
stream.addTrack(track); | ||
const source = this.ctx.createMediaStreamSource(stream); | ||
const gain = this.ctx.createGain(); | ||
source.connect(gain); | ||
gain.connect(this.outputNode ?? this.ctx.destination); | ||
this.inputs[inputId] = { | ||
source, | ||
gain, | ||
}; | ||
} | ||
|
||
public removeInput(inputId: string) { | ||
this.inputs[inputId]?.source.disconnect(); | ||
this.inputs[inputId]?.gain.disconnect(); | ||
delete this.inputs[inputId]; | ||
} | ||
|
||
public update(inputConfig: Api.InputAudio[]) { | ||
for (const [inputId, input] of Object.entries(this.inputs)) { | ||
const config = inputConfig.find(input => input.input_id === inputId); | ||
input.gain.gain.value = config?.volume || 0; | ||
} | ||
} | ||
|
||
public async close() { | ||
await this.ctx.close(); | ||
for (const inputId of Object.keys(this.inputs)) { | ||
this.removeInput(inputId); | ||
} | ||
} | ||
} | ||
|
||
export class MediaStreamAudioMixer extends AudioMixer<MediaStreamAudioDestinationNode> { | ||
constructor() { | ||
const ctx = new AudioContext(); | ||
const outputNode = ctx.createMediaStreamDestination(); | ||
const silence = ctx.createConstantSource(); | ||
silence.offset.value = 0; | ||
silence.connect(outputNode); | ||
silence.start(); | ||
super(ctx, outputNode); | ||
} | ||
|
||
public outputMediaStreamTrack(): MediaStreamTrack { | ||
const audioTrack = this.outputNode.stream.getAudioTracks()[0]; | ||
assert(audioTrack); | ||
return audioTrack; | ||
} | ||
} | ||
|
||
export class PlaybackAudioMixer extends AudioMixer<AudioDestinationNode> { | ||
constructor() { | ||
const ctx = new AudioContext(); | ||
super(ctx, ctx.destination); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
ts/@live-compositor/web-wasm/src/mainContext/input/stream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Input, RegisterInputResult } from '../input'; | ||
|
||
export class StreamInput implements Input { | ||
private mediaStream: MediaStream; | ||
|
||
constructor(mediaStream: MediaStream) { | ||
this.mediaStream = mediaStream; | ||
} | ||
|
||
public get audioTrack(): MediaStreamTrack | undefined { | ||
return this.mediaStream.getAudioTracks()[0]; | ||
} | ||
|
||
public async terminate(): Promise<void> { | ||
this.mediaStream.getTracks().forEach(track => track.stop()); | ||
} | ||
} | ||
|
||
export async function handleRegisterStreamInput( | ||
inputId: string, | ||
stream: MediaStream | ||
): Promise<RegisterInputResult> { | ||
const videoTrack = stream.getVideoTracks()[0]; | ||
const transferable = []; | ||
|
||
// @ts-ignore | ||
let videoTrackProcessor: MediaStreamTrackProcessor | undefined; | ||
if (videoTrack) { | ||
// @ts-ignore | ||
videoTrackProcessor = new MediaStreamTrackProcessor({ track: videoTrack }); | ||
transferable.push(videoTrackProcessor.readable); | ||
} | ||
|
||
return { | ||
input: new StreamInput(stream), | ||
workerMessage: [ | ||
{ | ||
type: 'registerInput', | ||
inputId, | ||
input: { | ||
type: 'stream', | ||
videoStream: videoTrackProcessor.readable, | ||
}, | ||
}, | ||
transferable, | ||
], | ||
}; | ||
} |
Oops, something went wrong.