Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amplify audio with Web Audio API

For an upcoming project using the Web Audio API I would like to be able to amplify the volume of some audio.

As I read in the documentation, a gain node multiplies the incoming signal by it gain value. The documentation states that this value is in the range of 0 to 1, hence amplification seems not to be doable with a gain node. But how else could I do it?

like image 308
causa prima Avatar asked Aug 19 '13 15:08

causa prima


2 Answers

To be more specific: as Kevin said, the "nominal" value of one represents unity gain - i.e., no change. That is NOT the same as a range. Implementations MUST support values > 1 - in fact, the entire range of the value. There are many scenarios that use large gain scaling values to do interesting things, and would be very broken if implementations didn't support this.

In short: use a gain > 1 to amplify things. If you're at all worried about clipping, put a dynamics processor node after it.

like image 138
cwilso Avatar answered Oct 21 '22 22:10

cwilso


I'm not aware of an implementation that doesn't increase the gain above 1. That's what I've been using in all of my projects, and haven't run into any issues.

If you're super concerned about it, I guess you could use a ScriptProcessorNode and basically just multiply all your samples by whatever scaling value you want, but the performance will be quite a bit worse than you'd get with a gain node. And, also, that would just be flat out kind of ridiculous.

The way I read the spec doesn't really give me any reason to believe values greater than 1 will be ignored for the GainNode's gain parameter. It's basically just saying 1 is the nominal value. In other words, if you want your audio to pass through unaffected, set the value to 1. Otherwise, you'll get attenuation or amplification.

like image 21
Kevin Ennis Avatar answered Oct 21 '22 21:10

Kevin Ennis