admin管理员组文章数量:1417684
I'm working with the AudioContext() on a hearing-test and I was wondering how to raise/lower the volume by x dB. Is it even possible?
At the moment, I have a gainNode connected to my AudioContext, which looks (in short) like this:
var context = new AudioContext(), gainNode;
context.decodeAudioData(req.target.response, function(buffer) {
gainNode = context.createGain();
...
}
To change the volume I do this:
gainNode.gain.value = {-1 to 1}
Here, I don't have a chance to exactly define a dB value. Are there other ways?
I think the problem is, the browser never knows the exact volume of the sound ing out of the speakers, therefore there is no base to calculate a new dB-volume.
An approach to determine the current dB-value is via the difference of 2 sounds, such as a test sound (white noise) and spoken numbers. To calculate the difference I found the formula:
20 * Math.log10(gainNoise / gainSpeech);
Then I have a basis of e.g. -6 dB, when speech is -0.6 and noise is -0.3. But how do I raise this value by a certain dB value?
Example: I raise -6 dB by 5 dB to -1 dB. How do I recalculate speech / noise?
I'm working with the AudioContext() on a hearing-test and I was wondering how to raise/lower the volume by x dB. Is it even possible?
At the moment, I have a gainNode connected to my AudioContext, which looks (in short) like this:
var context = new AudioContext(), gainNode;
context.decodeAudioData(req.target.response, function(buffer) {
gainNode = context.createGain();
...
}
To change the volume I do this:
gainNode.gain.value = {-1 to 1}
Here, I don't have a chance to exactly define a dB value. Are there other ways?
I think the problem is, the browser never knows the exact volume of the sound ing out of the speakers, therefore there is no base to calculate a new dB-volume.
An approach to determine the current dB-value is via the difference of 2 sounds, such as a test sound (white noise) and spoken numbers. To calculate the difference I found the formula:
20 * Math.log10(gainNoise / gainSpeech);
Then I have a basis of e.g. -6 dB, when speech is -0.6 and noise is -0.3. But how do I raise this value by a certain dB value?
Example: I raise -6 dB by 5 dB to -1 dB. How do I recalculate speech / noise?
Share Improve this question asked Dec 3, 2014 at 11:21 misantronicmisantronic 1,2112 gold badges14 silver badges24 bronze badges 1- you do not need to know the sound amplitude ... just multiply the samples by change ratio (ie change from 0dB to your desired dB). if you can control the gain directly then multiply the gain instead of samples ... the problem is with the sample value bounds. if you are increasing volume you will soon hit the boundary. so you should remove the constant offset from your sound (like serial capacitor removes direct current/voltage) that will give you more space, and also you have to cut the peaks above sound sample limits to avoid sound glitches – Spektre Commented Dec 3, 2014 at 12:41
2 Answers
Reset to default 6Gain generally refers to a linear increase or decrease in the amplitude of a signal. For example, you can double the amplitude of a signal by multiplying by 2 without regard to the original signal level. Likewise, you can halve the signal by multiplying by 0.5. In digital, the gain is applied by multiplying each sample of the input signal by the desired ratio. Gain is applied equally to the signal and the noise.
Your question implies that the gain has a range of -1 to 1. I've read the documentation and can't find any evidence that is the case. I'd suspect that it is more like 0 to N. A gain of -1 would be nonsense as it would just have the effect of inverting the signal. A gain range of 0 to 1 would only allow you to reduce the gain.
It is mon practice to talk about gain in terms of dB. There is a simple conversion between gain in ratio and gain in dB.
dB = 20 * log10(ratio)
ratio = 10^(dB/20)
To set the gain from a value in dB you simply have to apply the conversion to ratio.
dBgain = 20;
gainNode.gain.value = 10^(dBgain/20);
I am not allowed to ment so I have to post this as an answer. To try to clarify the behavior of the gainnode: a gain value of -1 is not nonsense, nor plete silence. -1 means that the amplitude of the input / phase of the wave is inverted. So jaket is right about it inverting the signal but not that it is not useful. This can be used for e.g. making an inverted sawtooth wave (rampdown instead of rampup).
So a gainnode with gain value -1 is an Inverter. A gainnode with values between 0 — 1 is an Attenuator. A gainnode with values between -1 — 0 is an Attenuverter. A gainnode with values above 1 can be used as an Amplifier, Distorter, etc. A gainnode with value either 0 or 1 can be used as an on/off switch.
Complete silence is a value of 0.
本文标签: javascriptAudioContext Increase volume by x decibelStack Overflow
版权声明:本文标题:javascript - AudioContext: Increase volume by x decibel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745270114a2650837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论