javascript 音频和音高的播放率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8997813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:16:36  来源:igfitidea点击:

playbackRate on AUDIO and pitch

javascripthtmlhtml5-audiopitch-shifting

提问by Niet the Dark Absol

Little bit of background:
People like games.
People use the internet.
The internet needs games.
Games use sound.
HTML5 has <audio>.

一点背景:
人们喜欢游戏。
人们使用互联网。
互联网需要游戏。
游戏使用声音。
HTML5 具有<audio>.

Okay, all good so far.

好的,到目前为止一切都很好。

Recently I discovered - to my surprise - that IE9 actually supports playbackRate. I eagerly gave it a try. Even more surprising, it actually worked. I tried the same in Chrome, and while it worked it was horribly grating when I set it to 0.5. I've already ditched Firefox because it doesn't support MP3.

最近我发现 - 令我惊讶的是 - IE9 实际上支持回放速率。我迫不及待地试了一下。更令人惊讶的是,它确实有效。我在 Chrome 中尝试了同样的方法,虽然它有效,但当我将它设置为0.5. 我已经放弃了 Firefox,因为它不支持 MP3。

Moving on, here's my question: both IE and Chrome apply pitch correction when changing the playbackRate. IE does a great job, Chrome does a horrible one. Either way, I don't want this, I wantthe sounds to change pitch. With that kind of power I can delete 650 files I had to procedurally generate to have an alternate pitch, and will have far more freedom with my projects. Heck, I could even make a MOD track player in HTML5 (minus the Effects channel) if I really wanted to.

继续,这是我的问题:IE 和 Chrome 在更改播放速率时都应用音调校正。IE 做得很好,Chrome 做得很糟糕。无论哪种方式,我都不想这样,我希望声音改变音调。有了这种能力,我可以删除 650 个我必须以程序方式生成的文件,以便有一个备用音高,并且我的项目将有更多的自由。哎呀,如果我真的想的话,我什至可以在 HTML5(减去效果通道)中制作一个 MOD 轨道播放器。

So, is there anything in the HTML5 specification that allows me to turn pitch correction off, and just have the sound be played as if the samples had literally been stretched apart of squeezed together?

那么,HTML5 规范中是否有任何内容允许我关闭音高校正,并且只是播放声音,就好像样本实际上是被拉开或挤压在一起一样?

采纳答案by Ian Devlin

No, there's currenly nothing in the HTML5 specification that allows you such fine tuning with audio.

不,目前 HTML5 规范中没有任何内容允许您对音频进行如此微调。

But.

但。

Why do you care about "power" and "freedom with projects" when you're already limiting yourself by deciding to ditch Firefox? Incidentally Opera also doesn't support MP3.

当您已经决定放弃 Firefox 来限制自己时,为什么还要关心“权力”和“项目自由”?顺便说一下,Opera 也不支持 MP3。

Unless of course it's a personal project where no-one but yourself will be using it and therefore it's a moot point. In which case if you want to target Chrome for example, you could check out the Web Audio APIwhich might have something you want.

当然,除非这是一个个人项目,除了你自己之外没有人会使用它,因此这是一个有争议的问题。在这种情况下,如果你想以 Chrome 为目标,你可以查看Web Audio API,它可能有你想要的东西。

回答by bcoughlan

From the Mozilla bug tracker issue on implementing playbackRate

来自Mozilla 错误跟踪器问题关于实现回放速率

WebKit solves this by exporting an additional (prefixed) attribute "preservesPitch" (proposed to the WhatWG here: http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-July/021100.html)

WebKit 通过导出额外的(带前缀的)属性“preservesPitch”来解决这个问题(在此处向 WhatWG 提议:http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-July/021100.html )

Presumably you can set preservesPitch (webkitPreservesPitch for webkit) to false to turn off this feature in Webkit at least. I'm not familiar with other browser support for this property.

据推测,您可以将preservesPitch(webkitPreservesPitch for webkit)设置为false 以至少在Webkit 中关闭此功能。我不熟悉其他浏览器对此属性的支持。

回答by joeLepper

Chrome currently supports the Web Audio API ( http://www.w3.org/TR/webaudio/) which has a playbackRate audioParam that you can set. It's not as simple as the <audio>tag, but allows for all sorts of cool stuff. I'm currently using it to play with pitch-shifting / time-stretching distortion.

Chrome 当前支持 Web Audio API ( http://www.w3.org/TR/webaudio/),它有一个可以设置的 playbackRate audioParam。它不像<audio>标签那么简单,但允许各种很酷的东西。我目前正在使用它来处理音高移位/时间拉伸失真。

Here's an example of what you could do:

以下是您可以执行的操作的示例:

    //build a request and fire it off
    speedChanger.loader = (function(){

      var _request       = new XMLHttpRequest(),

          _handleRequest = function(url){
            _request.open('GET',url,true);
            _request.responseType = 'arraybuffer';
            _request.onload = function(){
              SpeedChanger.audioGraph.context.decodeAudioData(_request.response, function(buffer){
                _loadedBuffer = buffer;
                SpeedChanger.audioGraph.setBuffer(buffer);
                SpeedChanger.audioGraph.setBufferCache(buffer);

              },function(){//error stuff});
            };
            _request.send();
          };

      _handleRequest("audio/file.mp3");

  }());//loader

  grainTable.audioGraph = (function(){
    var _context = new webkitAudioContext(),         //this is the container for your entire audio graph
        _source = _context.createBufferSource(),     //your buffer will sit here
        _bufferCache,                                //buffer needs to be re-initialized before every play, so we'll cache what we've loaded here

        //for chaching / retrieving the buffer
        _getBufferCache = function(){
          return _bufferCache;  
        },
        _setBufferCache = function(_sound){
          _bufferCache = _sound;
        },

        //for setting the current instance of the buffer 
        _setBuffer = function(_sound){
          _source.buffer = _sound;
        },

        _setPlaybackRate = function(rate){
          _source.playbackRate.value = rate;
        },

        _setRate = function(myRate){
            _rate = myRate;
        }

        //play it
        _playSound = function(){

          _source.noteOff(0);                       //call noteOff to stop any instance already playing before we play ours

          _source = _context.createBufferSource();  //init the source
          _setBuffer(_bufferCache);                 //re-set the buffer

          _setPlaybackRate(_rate);                  //here's your playBackRate check

          _source.connect(_context.destination);    //connect to the speakers 
          _source.noteOn(0);                        //pass in 0 to play immediately
        },

}

    return{

      context        :_context,
      setBuffer      :_setBuffer,
      setBufferCache :_setBufferCache,
      playSound      :_playSound,
      setRate        :_setRate

    }

  }());//audioGraph