After my previous post about getting more volume with the Web Audio API , I decided to look into controlling the loudness of audio tags and video tags in a more straight-forward (prototypal) way. That’s when I came up with the following JS prototype:
Prototypal Functions
The following are all of the methods associated with each instance:
-
MediaElementAmplifier#getContext()
Returns a reference to the associatedAudioContext
. -
MediaElementAmplifier#getSource()
Returns a reference to the associated source node. -
MediaElementAmplifier#getGain()
Returns a reference to the associated gain node. -
MediaElementAmplifier#getElement()
Returns a reference to the media element that was passed in to instantiate theMediaElementAmplifier
. -
MediaElementAmplifier#getVolume(opt_getPerceived)
Returns the volume. Ifopt_getPerceived
istrue
-ish, the returned value will be the volume multiplied by the loudness. -
MediaElementAmplifier#setVolume(value, opt_setPerceived)
Sets the volume to the specified value. Ifvalue
is greater than1
, the loudness will be modified accordingly and the real volume will be set to1
. -
MediaElementAmplifier#getGainValue()
Returns how much gain is used for the audio of the instance’s media element. -
MediaElementAmplifier#setGainValue(value)
Sets the gain used for the audio of the instance’s media element. -
MediaElementAmplifier#getDecibels()
Returns how many decibels the audio of the instance’s media element will be playing at. -
MediaElementAmplifier#setDecibels()
Sets how many decibels the audio of the instance’s media element will be playing at. -
MediaElementAmplifier#getLoudness()
Returns a number indicating how loud the audio of the instance’s media element is playing compared to normal.1
is normal. -
MediaElementAmplifier#setLoudness()
Sets a value indicating how loud the audio of the instance’s media element will be playing.
How To Use It
Here is an example of how this can be used in the wild:
In addition to what is done in this simple example you can use the other instance functions to get and set the different values associated with the gain value and the volume.
Reference Material
I was able to come up with this solution thanks to the following page:
Happy coding! š