简体   繁体   English

如何使用JavaScript应用基本音频过滤器

[英]How to apply basic audio filter using JavaScript

I'm trying to get basic audio filters to work. 我正在努力让基本的音频过滤器工作。 I want to evaluate at least 3-4 examples. 我想评估至少3-4个例子。

Here is a JS code that I'm evaluating and trying to get working: 这是一个我正在评估并尝试工作的JS代码:

var QUAL_MUL = 30;

function FilterSample() {
  this.isPlaying = false;
  loadSounds(this, {buffer: 'techno.wav'});
};

FilterSample.prototype.play = function() {
  // Create the source.
  var source = context.createBufferSource();
  source.buffer = this.buffer;
  // Create the filter.
  var filter = context.createBiquadFilter();
  filter.type = filter.LOWPASS;
  filter.frequency.value = 5000;
  // Connect source to filter, filter to destination.
  source.connect(filter);
  filter.connect(context.destination);
  // Play!
  source.start(0);
  source.loop = true;
  // Save source and filterNode for later access.
  this.source = source;
  this.filter = filter;
};

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

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

FilterSample.prototype.changeFrequency = function(element) {
  // Clamp the frequency between the minimum value (40 Hz) and half of the
  // sampling rate.
  var minValue = 40;
  var maxValue = context.sampleRate / 2;
  // Logarithm (base 2) to compute how many octaves fall in the range.
  var numberOfOctaves = Math.log(maxValue / minValue) / Math.LN2;
  // Compute a multiplier from 0 to 1 based on an exponential scale.
  var multiplier = Math.pow(2, numberOfOctaves * (element.value - 1.0));
  // Get back to the frequency value between min and max.
  this.filter.frequency.value = maxValue * multiplier;
};

FilterSample.prototype.changeQuality = function(element) {
  this.filter.Q.value = element.value * QUAL_MUL;
};

FilterSample.prototype.toggleFilter = function(element) {
  this.source.disconnect(0);
  this.filter.disconnect(0);
  // Check if we want to enable the filter.
  if (element.checked) {
    // Connect through the filter.
    this.source.connect(this.filter);
    this.filter.connect(context.destination);
  } else {
    // Otherwise, connect directly.
    this.source.connect(context.destination);
  }
};

It would be very nice, if someone could explain or better provide a simple working example of applying filter using JS. 如果有人可以解释或更好地提供使用JS应用过滤器的简单工作示例,那将是非常好的。

Thank you in advance. 先感谢您。

(Demo here ) 在这里演示)

Lets start with creating a source (this is HTML, pretty straightforward) 让我们从创建源开始(这是HTML,非常简单)

<audio controls="" preload="" src="add/src/here" autoplay=""></audio>

you should insert an audio file url at add/src/here . 你应该在add/src/here插入一个音频文件url。 For example put a file up on dropbox and add it there. 例如,将文件放在Dropbox上并将其添加到那里。

Now we want to create the audiocontext and hook up the <audio> tag to a webAudio node: 现在我们要创建audiocontext并将<audio>标记连接到webAudio节点:

context = new AudioContext();
source = context.createMediaElementSource(document.getElementsByTagName('audio')[0]);

That is some basic audioAPI programming. 这是一些基本的audioAPI编程。 Now we want to create a filter node, and connect the source to the filter, and then the filter to your speakers: 现在我们要创建一个过滤节点,并将源连接到过滤器,然后将过滤器连接到扬声器:

filter = context.createBiquadFilter();
source.connect(filter);
filter.connect(context.destination);

Now, you should chose what filter you want. 现在,您应该选择所需的过滤器。 You can find all filters here (basically all information about the biquadFilter) . 您可以在此处找到所有过滤器(基本上所有有关biquadFilter的信息) Notice there are 8 different filter types: 请注意,有8种不同的过滤器类型:

"lowpass", "highpass", "bandpass", "lowshelf", "highshelf", "peaking", "notch", "allpass" “低通”,“高通”,“带通”,“低架”,“高架”,“高峰”,“缺口”,“全通”

You can set these one of these types by getting its numeric value. 您可以通过获取其数值来设置这些类型中的这些类型。 According to the list above, Lowpass is 0, highpass is 1 and so on. 根据上面的列表,Lowpass为0,highpass为1,依此类推。 If you don't know it, simply call the function with the name in caps on the <#biquadFilter> object, like so: filter.LOWPASS . 如果您不知道,只需在<#biquadFilter>对象上调用名称为caps的函数,如下所示: filter.LOWPASS This returns 0. Now lets tell the filter that it should be a lowshelf. 这将返回0.现在让我们告诉过滤器它应该是一个lowshelf。 This is type 3: 这是类型3:

 filter.type = 3; 

Edit: the standards for defining the type have changed a couple times, apparently you can now just use the string directly. 编辑:定义类型的标准已经改变了几次,显然你现在可以直接使用字符串。 https://www.w3.org/TR/webaudio/#the-biquadfilternode-interface https://www.w3.org/TR/webaudio/#the-biquadfilternode-interface

Now a lowshelf means: apply the given gain boost/decrease to all frequencies containing and below the given frequency. 现在,lowshelf意味着:将给定的增益增强/减小应用于包含和低于给定频率的所有频率。 So if I give the filter a frequency of 95: 所以,如果我给过滤器的频率为95:

filter.frequency.value = 95;

The the filter adds the gain to all frequencies below 95. Also notice the .value . 滤波器将增益添加到95以下的所有频率。还要注意.value This is because there are more functions like filter.frequency.linearRampToValueAtTime() . 这是因为有更多函数,如filter.frequency.linearRampToValueAtTime() These functions are handy if you want the frequency to ramp to a certain value, or set the value at a time. 如果您希望频率斜坡到某个值,或者一次设置该值,这些功能很方便。 If you want more info on this, feel free to ask. 如果您想了解更多信息,请随时提出。 I'd like to tell you about it as I know it cost's much time to research how it works. 我想告诉你它,因为我知道它需要花费很多时间来研究它是如何工作的。 There is like no info on this, but at least I got it working. 没有关于此的信息,但至少我得到了它的工作。

Now we want to actually set the gain, this is also with a .value : 现在我们想要实际设置增益,这也是一个.value

filter.gain.value = 30;

Not all filters require a gain. 并非所有过滤器都需要增益。 For example a lowpass means let all frequencies below the given frequency pass, and do not do anything special. 例如,低通意味着让低于给定频率的所有频率通过,并且不做任何特殊的事情。 The lowshelf does boost/attenuate, so you need to specify how much. lowshelf会增强/衰减,因此您需要指定多少。

Some filters also require a Q value. 某些过滤器还需要Q值。 This determines the peaking at the cutoff (where your frequency band ends). 这决定了截止峰值(频段结束的地方)。 As I am not good in explaining this, I got this straight from w3.org: 由于我不善于解释这一点,我直接从w3.org得到了这个:

Controls how peaked the response will be at the cutoff frequency. 控制响应在截止频率处达到峰值的程度。 A large value makes the response more peaked. 较大的值会使响应更加尖锐。 Please note that for this filter type, this value is not a traditional Q, but is a resonance value in decibels. 请注意,对于此滤波器类型,此值不是传统的Q值,而是以分贝为单位的谐振值。

Now let's say we actually want to do something with the ramps. 现在让我们说我们实际上想要用斜坡做点什么。 I want the frequency increase from 0 to 120 in 10 seconds. 我想在10秒内将频率从0增加到120。 As this is still experimental, it doesn't quite work like you would expect. 由于这仍然是实验性的,它并不像你期望的那样工作。 Lets first initialize setting the value using the time. 让我们首先使用时间初始化设置值。 We just set the frequency value to 0 at the current time: 我们只是在当前时间将频率值设置为0:

filter.frequency.setValueAtTime(0.0, context.currentTime);

Now we want it to be at 120 after 10 seconds: 现在我们希望它在10秒后达到120:

filter.frequency.linearRampToValueAtTime(120.0, context.currentTime+10);

It is pretty clear to hear the frequency increase. 听到频率增加非常清楚。 (But it doesn't really sound clear...). (但它听起来并不清楚......)。 You can find a small demo of it here 你可以在这里找到一个小的演示

It is also possible to do all the editing yourself using mathematical functions inside scriptprocessors, but that is just a pain to do as support for it is so experimental the chrome versions scriptprocessors work on are scarce. 也可以使用脚本处理器中的数学函数自己进行所有编辑,但这只是一件痛苦的事情,因为支持它是如此实验性的铬版本脚本处理器工作是稀缺的。

I hope this helps you how filters work. 我希望这可以帮助您过滤器的工作原理。 Be adviced, using much filters makes your output sound weird. 请注意,使用大量过滤器会使您的输出听起来很奇怪。 Adding a compressor on the end normalizes everything. 最后添加一个压缩器可以规范化所有事情。

If you need to know anything else, feel free to ask. 如果您还需要了解其他信息,请随时提出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 JavaScript 过滤器中应用条件 - How to apply a condition in JavaScript filter 如何使用javascript将过滤器应用于嵌套数组中的所有对象 - How to apply filter to all objects in nested array using javascript 在 JavaScript 中为 ArcGis 使用对象时如何为多个图层应用过滤器 - How to apply filter for multiple layers when using object in JavaScript for ArcGis 如何使用JavaScript有条件地将过滤器应用于Firestore查询 - How to conditionally apply a filter to a Firestore query with JavaScript 如何使用AngularJS对多个对象应用过滤器? - How to apply a filter on multiple objects using AngularJS? 如何使用反应在列表中应用过滤器 - how to apply filter in list using react 基本 Javascript,如何按数值过滤数组? - Basic Javascript, How can i filter an array by numeric value? 如何使用javascript随机播放音频标签中的多个音频文件? - How to randomly play multiple audio files in audio tag using javascript? 如何使用javascript录制音频元素的音频 - How to record audio from Audio Element using javascript 如何使用 Javascript 将 append 多个音频文件转换为当前正在播放的音频? - How to append multiple audio files to a currently playing audio using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM