var audioSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3';

function bufferSound(ctx, url) {
  var p = new Promise(function(resolve, reject) {
    var req = new XMLHttpRequest();
    req.open('GET', url, true);
    req.responseType = 'arraybuffer';
    req.onload = function() {
      ctx.decodeAudioData(req.response, resolve, reject);
    }
    req.send();
  });
  return p;
}

var audioContext = new AudioContext();
bufferSound(audioContext, audioSrc).then(function (buffer) {
    var g = audioContext.createGain();
    g.gain.value = 5;
    g.connect(audioContext.destination);

    var bq = audioContext.createBiquadFilter();
    // found out about detune here: http://chimera.labs.oreilly.com/books/1234000001552/ch04.html
    bq.detune.value = 1200;
    bq.connect(g);

    var src = audioContext.createBufferSource();
    src.buffer = buffer;
    src.connect(bq);

    src.start();
});
<label for="pitchIn">Set pitch</label>
<input type="number" min="-1200" max="1200" step="200" value="0" id="pitchIn">

其他参考:https://stackoverflow.com/questions/53876757/how-to-change-the-pitch-with-javascript

posted on 2019-11-04 14:09  你不知道的浪漫  阅读(411)  评论(0编辑  收藏  举报