How to do panning in HTML5-Audio/Mozilla Audio Data API

浪子不回头ぞ 提交于 2019-12-13 02:12:38

问题


[EDIT: this question is about Mozilla Audio Data API which is no longer considered for HTML5 Audio API and not supported]

I am trying to change the padding of an audio file using Mozilla audio data API. I haven't found any way to do so. I have even tried to separate channels and pass them through different filters with different dbGain with no luck.


回答1:


The key to change the balance of your audio file, is to grab the data (for example of a stereo musicfile) and change the value (amplitude) of the right and the left channel data. Accessing the right and the left channel value would be done with [i*2] and [i*2+1], because they are in a row.

Take an example at the one below (which I copied out of the HTML5 Games Book from Wiley):

function generateTone(freq, balance,sampleRate) {
  var samples = Math.round(sampleRate / freq),
  data = new Float32Array(samples *2), 
  var sample, i;

  for (i = 0; i < samples; i++) {
    sample = Math.sin(Math.PI * 2 * i / samples);
    data[i * 2] = sample * (0.5 - balance);
    data[i * 2 + 1] = sample * (0.5 + balance);
  }

  return data;
}

The sinus generation you won't need in your case. Further questions? Best regards, Lukas



来源:https://stackoverflow.com/questions/9746841/how-to-do-panning-in-html5-audio-mozilla-audio-data-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!