Skip to content

Instantly share code, notes, and snippets.

@mal1kc
Created November 5, 2024 20:10
Show Gist options
  • Save mal1kc/dde60aea43cbc3094043ca95f3e23288 to your computer and use it in GitHub Desktop.
Save mal1kc/dde60aea43cbc3094043ca95f3e23288 to your computer and use it in GitHub Desktop.
javascript code to convert current tabs audio stream to stereo
(function() {
// Check if there is an audio or video element on the page
const mediaElements = document.querySelectorAll('audio, video');
if (mediaElements.length === 0) {
console.log('No media elements found on this page.');
return;
}
// Create an AudioContext
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Function to remix mono audio to true stereo (duplicate mono to both channels)
function remixToStereo(mediaElement) {
// Create a MediaElementAudioSourceNode from the media element
const sourceNode = audioContext.createMediaElementSource(mediaElement);
// Create a ChannelMergerNode to merge mono channels into stereo
const mergerNode = audioContext.createChannelMerger(2); // 2 channels (stereo)
// Connect the source to the merger node
sourceNode.connect(mergerNode, 0, 0); // Left channel
sourceNode.connect(mergerNode, 0, 1); // Right channel
// Connect the merger node to the destination (output)
mergerNode.connect(audioContext.destination);
console.log('Audio remixed to stereo.');
// Start playing the audio (in case it's paused)
mediaElement.play();
}
// Apply the remixToStereo function to all media elements on the page
mediaElements.forEach(mediaElement => {
remixToStereo(mediaElement);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment