javascript - WebAudio API: How to access timesample rate in a AudioWorkletProcessor? - Stack Overflow

I would like to implement an AudioWorkletProcessor that is aware of time. For example: how to reimpleme

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
Add a ment  | 

1 Answer 1

Reset to default 10

The 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170401a4614891.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信