Skip to content

Commit

Permalink
出力チャネル数の指定ミス
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Nov 25, 2024
1 parent 81523be commit c516352
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
14 changes: 4 additions & 10 deletions packages/mp4-media-stream/src/audio_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Mp4MediaStreamAudioWorkletProcessor extends AudioWorkletProcessor {
}

process(inputs, outputs, parameters) {
const outputChannel = outputs[0][0]
for (let sampleIdx = 0; sampleIdx < outputs[0][0].length; sampleIdx++) {
for (let channelIdx = 0; channelIdx < outputs[0].length; channelIdx++) {
const outputChannel = outputs[0][channelIdx]
Expand All @@ -20,17 +19,12 @@ class Mp4MediaStreamAudioWorkletProcessor extends AudioWorkletProcessor {
} else {
outputChannel[sampleIdx] = audioData.samples[this.offset]
this.offset++
if (this.offset === this.inputBuffer[0].samples.length) {
this.inputBuffer.shift()
this.offset = 0
}
}
}

if (this.inputBuffer[0] === undefined) {
continue
}

if (this.offset === this.inputBuffer[0].samples.length) {
this.inputBuffer.shift()
this.offset = 0
}
}
return true
}
Expand Down
25 changes: 13 additions & 12 deletions packages/mp4-media-stream/src/mp4_media_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ interface PlayOptions {
const AUDIO_DECODER_ID: number = 0
const VIDEO_DECODER_ID: number = 1

const OPUS_SAMPLE_RATE: number = 48000

/**
* MP4 を入力にとって、それを再生する MediaStream を生成するクラス
*/
Expand Down Expand Up @@ -152,7 +150,7 @@ class Mp4MediaStream {
const playerId = this.nextPlayerId
this.nextPlayerId += 1

const player = new Player(this.info.audioConfigs.length > 0, this.info.videoConfigs.length > 0)
const player = new Player(this.info.audioConfigs, this.info.videoConfigs.length > 0)
this.players.set(playerId, player)
;(this.wasm.exports.play as CallableFunction)(
this.engine,
Expand Down Expand Up @@ -298,13 +296,11 @@ class Mp4MediaStream {

try {
// TODO: add timestamp handling (in processor)
console.log(data.numberOfChannels)
const channels = data.numberOfChannels
const samples = new Float32Array(data.numberOfFrames * data.numberOfChannels)
data.copyTo(samples, { planeIndex: 0 })

const timestamp = data.timestamp
player.audioInputNode.port.postMessage({ timestamp, channels, samples }, [samples.buffer])
player.audioInputNode.port.postMessage({ timestamp, samples }, [samples.buffer])
} catch (e) {
// 書き込みエラーが発生した場合には再生を停止する
await this.stopPlayer(playerId)
Expand All @@ -318,7 +314,6 @@ class Mp4MediaStream {
},
}

player.numberOfChannels = config.numberOfChannels
player.audioDecoder = new AudioDecoder(init)
player.audioDecoder.configure(config)
;(this.wasm.exports.notifyDecoderId as CallableFunction)(
Expand Down Expand Up @@ -425,28 +420,34 @@ type Mp4Info = {
class Player {
private audio: boolean
private video: boolean
private numberOfChannels = 1
private sampleRate = 48000
audioDecoder?: AudioDecoder
videoDecoder?: VideoDecoder
canvas?: HTMLCanvasElement
canvasCtx?: CanvasRenderingContext2D
audioContext?: AudioContext
audioInputNode?: AudioWorkletNode
numberOfChannels = 1

constructor(audio: boolean, video: boolean) {
this.audio = audio
constructor(audioConfigs: AudioDecoderConfig[], video: boolean) {
this.audio = audioConfigs.length > 0
this.video = video

if (audioConfigs.length > 0) {
this.numberOfChannels = audioConfigs[0].numberOfChannels
this.sampleRate = audioConfigs[0].sampleRate
}
}

async createMediaStream(): Promise<MediaStream> {
const tracks = []
if (this.audio) {
const blob = new Blob([AUDIO_WORKLET_PROCESSOR_CODE], { type: 'application/javascript' })
this.audioContext = new AudioContext({ sampleRate: OPUS_SAMPLE_RATE })
this.audioContext = new AudioContext({ sampleRate: this.sampleRate })
await this.audioContext.audioWorklet.addModule(URL.createObjectURL(blob))

const workletOptions = {
numberOfOutputs: this.numberOfChannels,
outputChannelCount: [this.numberOfChannels],
}
this.audioInputNode = new AudioWorkletNode(
this.audioContext,
Expand Down

0 comments on commit c516352

Please sign in to comment.