Skip to content

Commit

Permalink
Bass Boost implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianE00 committed May 12, 2024
1 parent c84665d commit 2c8e9d8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 62 additions & 1 deletion src/js/offscreen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
console.log("Offscreen is running");
let audioContext;
const gainNodes = new Map(); // Change the variable name to 'gainNodes'
const biquadFilters = new Map(); // Add a new map for biquad filters

chrome.runtime.onMessage.addListener(async (msg) => {
console.log("[OFFSCREEN] Message received from WORKER");
Expand Down Expand Up @@ -51,4 +52,64 @@ chrome.runtime.onMessage.addListener(async (msg) => {
}
}

});
if (msg.type === 'lowshelf-start'){
if (gainNodes.has(msg.tabId) && biquadFilters.has(msg.tabId)){
console.log('[OFFSCREEN] ERROR found gain node in lowshelf-start ');
}
else{
console.log('[OFFSCREEN] Creating new gain node in lowshelf-start');

// creating a new gainNode
const media = await navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: msg.data,
},
},
});

// Continue to play the captured audio to the user
const output = new AudioContext();
const source = output.createMediaStreamSource(media);

// Create gainNode
const gainNode = output.createGain();
//gainNode.gain.value = 0;

// Create a biquad filter
const biquadFilter = output.createBiquadFilter();
biquadFilter.type = "lowshelf";
biquadFilter.frequency.value = 200; // Cutoff frequency of 200 Hz
biquadFilter.gain.value = 6; // Boost frequencies below the cutoff by 6 dB

// Connect nodes
source.connect(gainNode);
gainNode.connect(biquadFilter);
biquadFilter.connect(output.destination);

// Save the gain node & biquadFilter for future changes
gainNodes.set(msg.tabId, gainNode);
biquadFilters.set(msg.tabId, biquadFilter);

}

}

if (msg.type === 'lowshelf'){

console.log('[OFFSCREEN-lowshelf] lowshelf entered');

if(gainNodes.has(msg.tabId) && biquadFilters.has(msg.tabId)){
console.log('[OFFSCREEN-lowshelf] Found gain node');
const biquadFilter = biquadFilters.get(msg.tabId);
biquadFilter.type = "lowshelf";
biquadFilter.frequency.value = 200;
biquadFilter.gain.value = 6;
}
else{
console.log('[OFFSCREEN] lowshelf entered');
}
}

});
2 changes: 1 addition & 1 deletion src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ document.addEventListener('DOMContentLoaded', async () => {
// Currently for testing purposes
bassBoost.addEventListener('click', async () => {
console.log("[POPUP] Bass Boost clicked");
await chrome.runtime.sendMessage({ type: 'testGet'});
await chrome.runtime.sendMessage({ type: 'lowshelf-worker'});
});

// Clear storage for testing purposes
Expand Down
36 changes: 27 additions & 9 deletions src/js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ chrome.runtime.onMessage.addListener(async (msg) => {

case "lowshelf-worker":
console.log("[SERVICE-WORKER] Lowshelf message received");
await lowshelf();

var currTab = await getCurrentTab();
await lowshelf(currTab.id);
break;
}
});

// Adjust bass frequencies with lowshelf filter
async function lowshelf(tabId){
console.log("[SERVICE-WORKER] Lowshelf function called");
}


async function saveTabLevel(tabId, level){
let items = await chrome.storage.local.get('levels');
Expand Down Expand Up @@ -130,24 +126,46 @@ async function createOffscreenDocument(){
}
}


// Adjust bass frequencies with lowshelf filter
async function lowshelf(tabId){
console.log("[SERVICE-WORKER] Lowshelf function called");
await createOffscreenDocument();
let tabIdS = tabId.toString();

if (await containsTab(tabIdS)){
chrome.runtime.sendMessage({ type: 'lowshelf', target: 'offscreen', tabId: tabId});
}
else{
console.log("[SERVICE-WORKER] tab not found in activeStreams W/ tabId: ", tabId);
const streamId = await chrome.tabCapture.getMediaStreamId({
targetTabId: tabId
});
// Send the stream ID to the offscreen document to start recording
chrome.runtime.sendMessage({ type: 'lowshelf-start', target: 'offscreen', data: streamId, tabId: tabId});
await saveTabLevel(tabIdS, 100);

}
}


async function updateTabVolume(tabId, volume){
await createOffscreenDocument();
let tabIdS = tabId.toString();
let volumeS = volume.toString();
// Tab already exits
if(await containsTab(tabIdS)){
console.log("[WORKER] tab found in activeStreams W/ tabId: ", tabId);
console.log("[WORKER] tab found in activeStreams W/ tabId: ", tabId, " volume: ", volume);
chrome.runtime.sendMessage({ type: 'adjust-level', target: 'offscreen', tabId: tabId, level: volume});
await saveTabLevel(tabIdS, volumeS);
}
// Tab doesn't exist
else{
console.log("[WORKER] tab not found in activeStreams W/ tabId: ", tabId);
console.log("[WORKER] tab not found in activeStreams W/ tabId: ", tabId, " volume: ", volume);
// Get a MediaStream for the Active Tab
const streamId = await chrome.tabCapture.getMediaStreamId({
targetTabId: tabId
});

// Send the stream ID to the offscreen document to start recording
chrome.runtime.sendMessage({ type: 'start-recording', target: 'offscreen', data: streamId, tabId: tabId, level: volume});
await saveTabLevel(tabIdS, volumeS);
Expand Down

0 comments on commit 2c8e9d8

Please sign in to comment.