Skip to content

Instantly share code, notes, and snippets.

@1d10t
Created March 9, 2017 02:57
Show Gist options
  • Save 1d10t/ff706bcbcd447690b4cfc3146e6e6f20 to your computer and use it in GitHub Desktop.
Save 1d10t/ff706bcbcd447690b4cfc3146e6e6f20 to your computer and use it in GitHub Desktop.
htmlaudioelement (audio) random stereo balance example
HTMLAudioElement.prototype.balance = function(leftValue, rightValue){
if(!this.leftGain || !this.rightGain){
var audioCtx = new AudioContext();
if(audioCtx.destination.channelCount != 2){
console.log('Destination channel count is not two:', audioCtx.destination.channelCount);
return;
}
var audioSrc = audioCtx.createMediaElementSource(this);
var audioSplt = audioCtx.createChannelSplitter(2);
var audioMrgr = audioCtx.createChannelMerger(2);
this.leftGain = audioCtx.createGain();
this.rightGain = audioCtx.createGain();
audioSrc.connect(audioSplt);
audioSplt.connect(this.leftGain, 0);
audioSplt.connect(this.rightGain, 1);
this.leftGain.connect(audioMrgr, 0, 0);
this.rightGain.connect(audioMrgr, 0, 1);
audioMrgr.connect(audioCtx.destination);
}
this.leftGain.gain.value = leftValue;
this.rightGain.gain.value = rightValue;
}
var audio = document.createElement('audio');
audio.crossOrigin = "anonymous"
audio.controls = true;
audio.src = 'https://tts.voicetech.yandex.net/generate?speed=1.4&format=mp3&speaker=zahar&key=596e5b08-c082-49b3-adaf-d6aab5a20726&text=%D1%80%D0%B0%D0%BC%D0%BA%D0%B0%D1%85.%20%D0%9B%D0%BE%D0%B3%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B0%D1%8F%20%D1%80%D0%B5%D0%BF%D0%BB%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D1%8F%20%D1%82%D0%B0%D0%BA%D0%B6%D0%B5%20%D0%BF%D0%BE%D0%B7%D0%B2%D0%BE%D0%BB%D1%8F%D0%B5%D1%82%20%D0%B4%D0%BE%D0%B1%D0%B8%D1%82%D1%8C%D1%81%D1%8F%20%D1%82%D0%BE%D0%B3%D0%BE%2C%20%D1%87%D1%82%D0%BE%20%D0%B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5%20%D0%BE%D0%B4%D0%BD%D0%BE%D0%B9%20%D0%B8%D0%B7%20%D0%BD%D0%B8%D1%85%20%D0%B4%D0%BE%20%D0%BC%D0%B0%D1%81%D1%82%D0%B5%D1%80%D0%B0.&lang=ru-RU';
document.body.append(audio);
setInterval(function(){
audio.balance(Math.random(), Math.random());
}, 100);
audio.play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment