audio
element. <audio controls id="audio" src="path/to/file"></audio>
var audio = document.getElementById('audio'); equalize(audio); // - ,
so that I don’t have to think and it wouldn’t affect the work of the player. window.AudioContext = window.AudioContext || window.webkitAudioContext; var context = new AudioContext();
Uncaught NotSupportedError: Failed to construct 'AudioContext': The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6)
. That is Chrome allows you to create up to six contexts at the same time. )HTMLMediaElement
, with which we will work: var source = context.createMediaElementSource(audio);
<video />
elementssource
object is the first link in the chain (literally) that we are building. In the simplest case, the circuit consists of only two links - the source is immediately connected to the output. source.connect(context.destination);
var createFilter = function (frequency) { var filter = context.createBiquadFilter(); filter.type = 'peaking'; // filter.frequency.value = frequency; // filter.Q.value = 1; // Q-factor filter.gain.value = 0; return filter; };
lowpass, highpass, bandpass, lowshelf, highshelf, peaking, notch, allpass.
We need only a peaking filter - it allows you to selectively emphasize or attenuate a limited band of the sound spectrum. Read more.60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000 16000
Hz (values are drawn from winamp). var createFilters = function () { var frequencies = [60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000], filters; // filters = frequencies.map(createFilter); // . // , , . // , reduce . filters.reduce(function (prev, curr) { prev.connect(curr); return curr; }); return filters; };
window.AudioContext = window.AudioContext || window.webkitAudioContext; var context = new AudioContext(), audio = document.getElementById('audio'); var createFilter = function (frequency) { var filter = context.createBiquadFilter(); filter.type = 'peaking'; // filter.frequency.value = frequency; // filter.Q.value = 1; // Q-factor filter.gain.value = 0; return filter; }; var createFilters = function () { var frequencies = [60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000], filters = frequencies.map(createFilter); filters.reduce(function (prev, curr) { prev.connect(curr); return curr; }); return filters; }; var equalize = function (audio) { var source = context.createMediaElementSource(audio), filters = createFilters(); // source.connect(filters[0]); // - filters[filters.length - 1].connect(context.destination); }; equalize(audio);
// var bindEvents = function (inputs) { inputs.forEach(function (item, i) { item.addEventListener('change', function (e) { filters[i].gain.value = e.target.value; }, false); }); };
If you haven’t been able to receive this information, it has been created. ( documentation )
Access-Control-Allow-Origin
header should be added to the stream itself.ObjectURL
: // fileInput.addEventListener('change', function (e) { var url = URL.createObjectURL(e.target.files[0]); audio.src = url; }, false);
Source: https://habr.com/ru/post/240819/
All Articles