gpt4 book ai didi

javascript - 设置网络音频,多种声音,单独的gainNodes和全局LOW PASS滤波器?

转载 作者:行者123 更新时间:2023-12-03 00:50:25 25 4
gpt4 key购买 nike

我正在尝试设置一些网络音频以同时加载/播放多个声源。
目前正在加载声音,并通过按钮输入触发播放。

我的问题是,我希望所有声音都通过一个BiquadFilter运行(在这种情况下,类型为0; // LOWPASS过滤器)。
我相信我已经正确地创建了滤波器(在两个不同的地方,不确定在哪个地方看所附的代码),但是我无法获得用于控制频率的范围输入,某些通信无法很好地进行并且完全丢失。

另外,围绕同一主题,我希望每个声音都通过各自独立的gainNodes(音量控件)运行,这又将通过范围输入进行更改。
基本上会有6个音频文件,它们通过它们自己的gainNodes运行,然后聚集在一起经过LOWPASS滤波器,然后到达目的地(即扬声器)。

我希望可以遍历各个pannerNode,但目前面临着一起放弃该项目的机会。

下面是我的代码(就像我之前说过的那样,按钮触发了所有声音,但是过滤器是一个大问题):

HTML:

<body> 

<div id="startbtn">
<p><input type="button" onClick="tracks.toggle();">PLAY!</p>
</div> <!-- startbtn div -->

<div id="frequency">
<p><input type="range" id="freq1" min="0" max="1" step="0.01" value="1" onchange="sound.changeFrequency(this);" style="width:180px; background-color:#FFF;"> Frequency</p>
</p>
</div>

<script>
var tracks = new SongTracks();
var sound = new playSound();
</script>
</body>

JAVASCRIPT:
var context = new webkitAudioContext();
var myAudioAnalyser;

function init() {
if('webkitAudioContext' in window) {
myAudioContext = new webkitAudioContext();
// an analyser is used for the spectrum
myAudioAnalyser = myAudioContext.createAnalyser();
myAudioAnalyser.smoothingTimeConstant = 0.85;
myAudioAnalyser.connect(myAudioContext.destination);

fetchSounds();
};
};


// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();


function playSound(buffer, time) {
var source = context.createBufferSource();
source.buffer = buffer;

var filter = context.createBiquadFilter(); ///////////////// HERE
filter.type = filter.LOWPASS;
filter.frequency.value = 5000;

source.connect(filter);
filter.connect(context.destination);
source.start(time);

this.filter = filter;

};

function loadSounds(obj, soundMap, callback) {

var names = []
var paths = []
for (var name in soundMap) {
var path = soundMap[name];
names.push(name);
paths.push(path);
}
bufferLoader = new BufferLoader(context, paths, function(bufferList) {
for (var i = 0; i < bufferList.length; i++) {
var buffer = bufferList[i];
var name = names[i];
obj[name] = buffer;
}
if (callback) {
callback();
}
});
bufferLoader.load();
};

function BufferLoader(context, urlList, callback) {
this.context = context;
this.urlList = urlList;
this.onload = callback;
this.bufferList = new Array();
this.loadCount = 0;
}

BufferLoader.prototype.loadBuffer = function(url, index) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";

var loader = this;

request.onload = function() {
// Asynchronously decode the audio file data in request.response
loader.context.decodeAudioData(
request.response,
function(buffer) {
if (!buffer) {
alert('error decoding file data: ' + url);
return;
}
loader.bufferList[index] = buffer;
if (++loader.loadCount == loader.urlList.length)
loader.onload(loader.bufferList);
},
function(error) {
console.error('decodeAudioData error', error);
}
);
}

request.onerror = function() {
alert('BufferLoader: XHR error');
}

request.send();
};

BufferLoader.prototype.load = function() {
for (var i = 0; i < this.urlList.length; ++i)
this.loadBuffer(this.urlList[i], i);
};

var SongTracks = function() {
loadSounds(this, {
vocals: 'tracks/vocals.mp3',
guitar: 'tracks/guitar.mp3',
piano: 'tracks/piano.mp3'
});
};

var filter;

SongTracks.prototype.play = function() {
playSound(this.vocals, 0);
playSound(this.guitar, 0);
playSound(this.piano, 0);
///////////////////////////////////////////////////////////// OR HERE
var source1 = context.createBufferSource();
source1.buffer = this.buffer
source1 = bufferList[0];


var filter = context.createBiquadFilter();
filter.type = filter.LOWPASS;
filter.frequency.value = 5000;

source1.connect(filter);
filter.connect(context.destination);

this.filter = filter;
///////////////////////////////////////////////////////////////////// TO HERE?
};

SongTracks.prototype.stop = function() {
this.source.stop(0);
};

SongTracks.prototype.toggle = function() {
this.isPlaying ? this.stop() : this.play();
this.isPlaying = !this.isPlaying;
};

/* SongTracks.prototype.changeFrequency = function(element) {
var minValue = 40;
var maxValue = context.sampleRate / 2;

var numberOfOctaves = Math.log(maxValue / minValue) / Math.LN2;
var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0));
this.filter.frequency.value = maxValue * multiplier;
}; */

playSound.prototype.changeFrequency = function(element) {
var minValue = 40;
var maxValue = context.sampleRate / 2;

var numberOfOctaves = Math.log(maxValue / minValue) / Math.LN2;
var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0));
this.filter.frequency.value = maxValue * multiplier;
};


</script>

如您所见,通过我的笔记等,我非常困惑,有点撞墙了。
我看过区分音频文件的代码,类似;
   var source1 = context.createBufferSource();
var source2 = context.createBufferSource();
var source3 = context.createBufferSource();
var source4 = context.createBufferSource();

source1.buffer = bufferList[0];
source2.buffer = bufferList[1];
source3.buffer = bufferList[2];
source4.buffer = bufferList[3];

但是我不知道,祝你好运。

最佳答案

您可能应该简单地将要连接的节点传递到playSound中,然后将其传递给FilterNode。

在playSound内创建BiquadFilter的位置不正确-您最终将创建N个,每个播放声音一个,而只想要一个。

您想要类似的东西:

HTML文件相同,除了:

<input type="range" id="freq1" min="0" max="1" step="0.01" value="1" onchange="changeFilterFrequency(this);" style="width:180px; background-color:#FFF;">    Frequency</p>

JS:
function playSound(buffer, outputNode, time) {
var source = context.createBufferSource();
source.buffer = buffer;

source.connect(outputNode);
source.start(time);
}

var globalFilter = null; // one global filter

SongTracks.prototype.play = function() {
var globalFilter = context.createBiquadFilter();
globalFilter.type = globalFilter.LOWPASS;
globalFilter.frequency.value = 5000;
globalFilter.connect(context.destination);

playSound(this.vocals, globalFilter, 0);
playSound(this.guitar, globalFilter, 0);
playSound(this.piano, globalFilter, 0);
};

function changeFilterFrequency(element) {
var minValue = 40;
var maxValue = context.sampleRate / 2;

var numberOfOctaves = Math.log(maxValue / minValue) / Math.LN2;
var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0));
globalFilter.frequency.value = maxValue * multiplier;
}

关于javascript - 设置网络音频,多种声音,单独的gainNodes和全局LOW PASS滤波器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22026047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com