如何转移/使用Web API音频音频调制频率缓冲器 [英] How to shift/modulate audio buffer frequency using Web Audio API

查看:328
本文介绍了如何转移/使用Web API音频音频调制频率缓冲器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与网络音频API试验和我的目标是创建一个数字吉他,其中每个字符串有一个实际的吉他演奏串开放的初始声源,然后我想生成所有其他音品位置动态声音。一些研究的主题(这是所有pretty新的给我)后,它听起来像这可能通过改变源声音样本的频率来实现。

I'm experimenting with the Web Audio API and my goal is to create a digital guitar where each string has an initial sound source of an actual guitar playing the string open and then I would like to generate all other fret position sounds dynamically. After some research into the subject (this is all pretty new to me) it sounded like this might be achieved by altering the frequency of the source sound sample.

问题是我见过很多的算法改变合成罪波,但没有改变的音频采样频率。这里是我的code的样本给的如何我想实现这个更好的主意:

The problem is I've seen lots of algorithms for altering synthesized sin waves but nothing to alter the frequency of an audio sample. Here is a sample of my code to give a better idea of how i'm trying to implement this:

// Guitar chord buffer    
var chordBuffer = null;

// Create audio context
var context = new webkitAudioContext();

// Load sound sample
var request = new XMLHttpRequest();
request.open('GET', 'chord.mp3', true);
request.responseType = 'arraybuffer';
request.onload = loadChord;
request.send();

// Handle guitar string "pluck"
$('.string').mouseenter(function(e){
    e.preventDefault();

    var source = context.createBufferSource();
    source.buffer = chordBuffer;

    // Create javaScriptNode so we can get at raw audio buffer
    var jsnode = context.createJavaScriptNode(1024, 1, 1);
    jsnode.onaudioprocess = changeFrequency;

    // Connect nodes and play
    source.connect(jsnode);
    jsnode.connect(context.destination);
    source.noteOn(0);
});

function loadChord() {
    context.decodeAudioData(
        request.response,
        function(pBuffer) { chordBuffer = pBuffer; },
        function(pError) { console.error(pError); }
    );
}

function changeFrequency(e) {
    var ib = e.inputBuffer.getChannelData(0);
    var ob = e.outputBuffer.getChannelData(0);
    var n = ib.length;

    for (var i = 0; i < n; ++i) {
        // Code needed...
    }
}

所以你有它 - 我可以播放声音就好了,但我在一个有点失去时,谈到在 changeFrequency 功能,它会改变和弦创建code采样频率,使声音听起来像是另一个担心在字符串中的位置。与此code任何帮助将是什么,我试图做的是甚至有可能AP preciated或意见。

So there you have it - I can play the sound just fine but am at a bit of a lose when to comes to creating the code in the changeFrequency function which would change the chord samples frequency so it sounded like another fret position on the string. Any help with this code would be appreciated or opinions on whether what I'm attempting to do is even possible.

谢谢!

推荐答案

您可以通过设置 playbackRate ,但布拉德说,你要得到所需的行为必须使用多采样。另请参见这太问题:<一href=\"http://stackoverflow.com/questions/10239757/setting-playbackrate-on-audio-element-connected-to-web-audio-api\">Setting playbackRate连接到网络音频API 音频元素。

You can get the desired behavior by setting playbackRate, but as Brad says, you're going to have to use multi-sampling. Also see this SO question: Setting playbackRate on audio element connected to web audio api.

这篇关于如何转移/使用Web API音频音频调制频率缓冲器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆