Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript音高随时间的变化_Javascript_Audio_Playback_Pitch Shifting - Fatal编程技术网

javascript音高随时间的变化

javascript音高随时间的变化,javascript,audio,playback,pitch-shifting,Javascript,Audio,Playback,Pitch Shifting,我是一个学习javascript的初学者。我的网站上有一个互动页面,上面有各种各样的项目,涉及微音频率和行星频率。 我需要能够在循环中播放我的音频样本.wav文件,但要让音频样本的时间与相应的音调变化同步 我试过myAudio.playbackRate=0.5播放速度慢0.5但音高保持不变。 我研究并发现了一些东西。但是如何将preservesPitch设置为false或true?我想这只适用于“谷歌浏览器”,所以我找到的另一个程序就在这里: 似乎无法让它工作,我不知道我应该如何修改它,我应该

我是一个学习javascript的初学者。我的网站上有一个互动页面,上面有各种各样的项目,涉及微音频率和行星频率。 我需要能够在循环中播放我的音频样本.wav文件,但要让音频样本的时间与相应的音调变化同步

我试过
myAudio.playbackRate=0.5播放速度慢0.5但音高保持不变。
我研究并发现了一些东西。但是如何将
preservesPitch
设置为false或true?我想这只适用于“谷歌浏览器”,所以我找到的另一个程序就在这里:

似乎无法让它工作,我不知道我应该如何修改它,我应该将我的音频.wav文件URL粘贴到程序中的何处? 任何其他与此相关的提示都将不胜感激。
提前感谢您的时间和帮助。

当您说
我尝试了myAudio.playbackRate=0.5我们正在谈论
网络音频API

如果是,请再听一遍,音高和样本的持续时间将受到影响

如果没有,可能是您使用的是原生html5功能,如果您想保持原始速度并更改音高,一种方法是在更改速度后应用某种类型的插值,尝试使用与拉伸音频相同的因子进行线性插值。如果您需要更改速度和音高,只需在不更改速度的情况下应用插值(在原始声音中),这就相当于以不同的采样率播放音频


代码
pitchshift.js
是代码从到
javascript
的一个端口(这会改变音高并保持速度不可触摸),您需要在每个音频块调用此函数,因此首先需要执行一个函数,以短int或float格式解码音频文件

嘿,您的web音频API现在似乎至少可以用于正弦波,您将在此处学习如何过早使用示例信息

您可以将
mozPreservesPitch
属性设置为false,以使用Firefox更改元素或声音文件的音高
webkitPreservesPitch
应该与webkit浏览器一起使用,但请注意“此API尚未标准化”

这对我很有用:

var soundPlayer = new Audio();

function playLowerNote() {
    soundPlayer.src = "piano.mp3";
    soundPlayer.mozPreservesPitch = false;
    soundPlayer.playbackRate = 0.5;
    soundPlayer.play();
}

playLowerNote();

我仍然在寻找一种更好的循环方式。

我也想改变音高,过了很久我才发现。
我还重新格式化了代码,使其更易于自己使用(您需要用自己的WAV或ogg文件名替换“decode audio data/viper.ogg”):


播放声音功能(文件,速度=1,pitchShift=1,循环=false,自动播放=true){
/*
使用play()方法启动音频。如果pitchShift为true
使用stop()方法停止音频并销毁对象。
如果pitchShift为false,请使用pause()方法暂停并设置
将属性currentTime设置为0以重置时间。
*/
if(俯仰移位){
/*
经过数周的搜索,我终于找到了一种改变音高的方法。
谢谢你,Mozilla。
2018/03/31:
https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/playbackRate
https://github.com/mdn/webaudio-examples/tree/master/decode-audio-data
https://www.w3schools.com/jsref/prop_audio_loop.asp
原始评论:
使用XHR加载音频曲目,然后
解码音频数据,将其解码并粘贴在缓冲区中。
然后我们将缓冲区放入源代码中
*/
audioCtx=new(window.AudioContext | | window.webkitadiocontext)();
source=audioCtx.createBufferSource();
请求=新的XMLHttpRequest();
打开('GET',file,true);
request.responseType='arraybuffer';
request.onload=函数(){
var audioData=请求。响应;
audioCtx.decodeAudioData(音频数据,功能(缓冲区){
myBuffer=缓冲区;
songLength=buffer.duration;
source.buffer=myBuffer;
source.playbackRate.value=速度;
source.connect(audioCtx.destination);
source.loop=循环;
},
函数(e){“解码音频数据时出错”+e.Error});
}
request.send();
source.play=source.start
}否则{
source=新音频(文件)
source.playbackRate=速度
source.loop=loop
}
如果(自动播放){
来源:play()
}
返回源
}
变量源
函数播放(){
源=播放声音('decode-audio-data/viper.ogg',音高=2);
}
函数停止(){
源。停止(0);
document.getElementById('play')。href=''
document.getElementById('play').innerHTML='Refresh to play'
}
| 

var Au=新音频(');Au.play();Au.回放率=0.5;这是我尝试过的代码,将播放速率更改为不同的值并不会改变音高,它只会改变持续时间。Web Audio API的文档说明,此更改音高和持续时间var source=new Audio('My URL Soundfile.wav');函数playNote(semitens){//假设从缓冲区创建了一个新源。var semitoneRatio=Math.pow(2,1/12);source.playbackRate.value=Math.pow(semitoneRatio,semitens);source.start(0);}playNote(7);这是我在您的链接中尝试的代码,但行“source.start(0)”出现错误。我尝试更改为“source.play(0)”,但不管我在“playNote(N)”中将N设置为什么,例如将7更改为2,都会听到相同的音调。那么为什么“Source.start(0)”会出现无法识别的错误呢?使用Chrome btwsource.start(0)小写btw
<script>
function playSound(file, speed=1, pitchShift=1, loop=false, autoplay=true) {
    /*
    Use the play() method to start the audio. if pitchShift is true
    use the stop() method to stop the audio and destroy the object.
    If pitchShift is false use the pause() method to pause and set
    the attribute currentTime to 0 to reset the time.
    */
    if(pitchShift) {
        /*
        After weeks of searching, I have finally found a way to pitch shift audio.
        Thank you Mozilla.
        2018/03/31:
            https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/playbackRate
            https://github.com/mdn/webaudio-examples/tree/master/decode-audio-data
            https://www.w3schools.com/jsref/prop_audio_loop.asp
        Original comments:
            use XHR to load an audio track, and
            decodeAudioData to decode it and stick it in a buffer.
            Then we put the buffer into the source
        */
        audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        source = audioCtx.createBufferSource();
        request = new XMLHttpRequest();

        request.open('GET', file, true);

        request.responseType = 'arraybuffer';


        request.onload = function() {
            var audioData = request.response;

        audioCtx.decodeAudioData(audioData, function(buffer) {
            myBuffer = buffer;
            songLength = buffer.duration;
            source.buffer = myBuffer;
            source.playbackRate.value = speed;
            source.connect(audioCtx.destination);
            source.loop = loop;
        },

        function(e){"Error with decoding audio data" + e.error});

        }

        request.send();
        source.play=source.start
    } else {
        source=new Audio(file)
        source.playbackRate=speed
        source.loop=loop
    }
    if(autoplay) {
        source.play()
    }
    return source
}
var source
function play() {
    source=playSound('decode-audio-data/viper.ogg', pitch=2);
}

function stop() {
    source.stop(0);
    document.getElementById('play').href=''
    document.getElementById('play').innerHTML='Refresh to play again'
}
</script>
<a id="play" href="#" onclick="play()">Play</a> | <a href="#" onclick="stop()">Stop</a>