Skip to content

Commit

Permalink
feat: audioWorkletNode
Browse files Browse the repository at this point in the history
  • Loading branch information
QinZhen001 committed May 10, 2024
1 parent 0dc68c9 commit 37e9105
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
15 changes: 12 additions & 3 deletions public/pcm-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@ class PCMProcessor extends AudioWorkletProcessor {
process(inputs, outputs, parameters) {
const input = inputs[0] // 获取输入音频数据
const output = outputs[0] // 获取输出音频数据
console

console.log("inputs:", inputs)
console.log("outputs:", outputs)
// console.log("input:", input.length)

console.log("inputs1:", input[0])
// console.log("inputs2:", input[0].length)
// console.log("inputs3:", input[0].length)

// console.log("inputs:", inputs)
// console.log("outputs:", outputs)
// for (let channel = 0; channel < input.length; ++channel) {
// const inputBuffer = input[channel] // 获取输入音频的每个通道
// const outputBuffer = output[channel] // 获取输出音频的每个通道

// for (let i = 0; i < inputBuffer.length; ++i) {
// // 处理PCM数据(此处示例为将PCM数据降低一半音量)
// outputBuffer[i] = inputBuffer[i] * 0.5
// }
// }

// this.port.postMessage({
// inputs,
// })

return true
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/pages/web-rtc/audio-pcm/components/planAudioWorkletNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const PlanAudioWorkletNode = (props: PlanAudioWorkletNodeProps) => {
const audioContext = new AudioContext()

await audioContext.audioWorklet.addModule("pcm-processor.js")
const audioMediaStreamTrack = audioTrack.getMediaStreamTrack()
// const audioMediaStreamTrack = audioTrack.getMediaStreamTrack()
// 创建MediaStreamDestination节点
const audioDestination = audioContext.createMediaStreamDestination()
// const audioDestination = audioContext.createMediaStreamDestination()
// 创建AudioWorkletNode节点
const audioWorkletNode = new AudioWorkletNode(audioContext, "pcm-processor")
// 连接AudioWorkletNode到MediaStreamDestination
Expand All @@ -29,19 +29,18 @@ const PlanAudioWorkletNode = (props: PlanAudioWorkletNodeProps) => {
// 将MediaStreamDestination作为媒体输入
// const mediaStream = audioDestination.stream

audioWorkletNode.port.onmessage = (event) => {
// 获取 PCM 数据
const pcmData = event.data
// 处理 PCM 数据
// ...你的代码...
const audioMediaStreamTrack = audioTrack.getMediaStreamTrack()
const mNode = audioContext.createMediaStreamSource(new MediaStream([audioMediaStreamTrack]))
mNode.connect(audioWorkletNode)

console.log("pcmData", pcmData)
audioWorkletNode.port.onmessage = (event) => {
console.log("event onmessage", event.data)
}

// const options = {}
// const source = new MediaElementAudioSourceNode(audioContext, options)

// const source = audioContext.createMediaStreamSource(audioTrack)
// const source = audioContext.createMediaStreamSource(audioMediaStreamTrack)
// const processor = audioContext.createScriptProcessor(1024, 1, 1)

// processor.onaudioprocess = (event) => {
Expand Down

0 comments on commit 37e9105

Please sign in to comment.