admin管理员组文章数量:1414604
I would like to implement an AudioWorkletProcessor that is aware of time. For example: how to reimplement the DelayNode as a Processor?
The MDN docs says:
By specification, each block of audio your process() function receives contains 128 frames (that is, 128 samples for each channel), but it is planned that this value will change in the future, and may in fact vary depending on circumstances, so you should always check the array's length rather than assuming a particular size.
I can get the number of frames with the inputs' length, but how to get the sample rate used? So that I can tell how long (in seconds) is this input.
My end goal is to be able to pute the average energy of a signal over a certain time window.
class EnergyProcessor extends AudioWorkletProcessor {
process(inputs, outputs, parameters) {
if (inputs.length !== 1) {
throw 'invalid inputs'
}
// how much time is covered by inputs?
inputs[0].forEach((channel, channelID) => {
let sum = 0
let count = 0
channel.forEach((value, i) => {
sum += value * value
count += 1
for (let o = 0; o < outputs.length; o++) {
// skip when writing x channels to x - 1
if (channelID >= outputs[o].length) {
continue
}
outputs[o][channelID][i] = sum / count
}
})
})
return true
}
}
registerProcessor('EnergyProcessor', EnergyProcessor)
I would like to implement an AudioWorkletProcessor that is aware of time. For example: how to reimplement the DelayNode as a Processor?
The MDN docs says:
By specification, each block of audio your process() function receives contains 128 frames (that is, 128 samples for each channel), but it is planned that this value will change in the future, and may in fact vary depending on circumstances, so you should always check the array's length rather than assuming a particular size.
I can get the number of frames with the inputs' length, but how to get the sample rate used? So that I can tell how long (in seconds) is this input.
My end goal is to be able to pute the average energy of a signal over a certain time window.
class EnergyProcessor extends AudioWorkletProcessor {
process(inputs, outputs, parameters) {
if (inputs.length !== 1) {
throw 'invalid inputs'
}
// how much time is covered by inputs?
inputs[0].forEach((channel, channelID) => {
let sum = 0
let count = 0
channel.forEach((value, i) => {
sum += value * value
count += 1
for (let o = 0; o < outputs.length; o++) {
// skip when writing x channels to x - 1
if (channelID >= outputs[o].length) {
continue
}
outputs[o][channelID][i] = sum / count
}
})
})
return true
}
}
registerProcessor('EnergyProcessor', EnergyProcessor)
Share
Improve this question
asked Jun 30, 2020 at 6:37
LaurentLaurent
1,7182 gold badges15 silver badges21 bronze badges
1 Answer
Reset to default 10The MDN says that
[...] lives in the AudioWorkletGlobalScope and runs on the Web Audio rendering thread.
and AudioWorkletGlobalScope
is said to have a reference to its context's
sampleRate
: Read only
Returns a float that represents the sample rate of the associated BaseAudioContext.
so you probably can simply, magically
console.log(sampleRate)
or whatever you need to do.
本文标签: javascriptWebAudio API How to access timesample rate in a AudioWorkletProcessorStack Overflow
版权声明:本文标题:javascript - WebAudio API: How to access timesample rate in a AudioWorkletProcessor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745170400a2645940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论