Skip to content

Commit

Permalink
Started implementation for lowshelf biquad filter & dynamic range com…
Browse files Browse the repository at this point in the history
…pression
  • Loading branch information
ChristianE00 committed May 16, 2024
1 parent 0b2f2f0 commit 26f3f49
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/js/offscreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// TODO: 5/15/24
// Get Audio-Enhancement inplemented

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
const highShelfBiquadFilters = new Map(); // A map to store all highshelf biquad filter (voice enhancement)

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

// NOTE: This isn't checking for the case of (gainNode) && !(biquadFilter)
if (msg.type === 'lowshelf-start'){
if (gainNodes.has(msg.tabId) && biquadFilters.has(msg.tabId)){
console.log('[OFFSCREEN] ERROR found gain node in lowshelf-start ');
Expand Down Expand Up @@ -112,4 +117,58 @@ chrome.runtime.onMessage.addListener(async (msg) => {
}
}

if (msg.type === 'highshelf-start'){

if (gainNodes.has(msg.tabId) && highShelfBiquadFilters.has(msg.tabId)){
console.log('[OFFSCREEN] ERROR found gain node in highshelf-start ');
}
else{

console.log('[OFFSCREEN] Creating new gain node in lowshelf-start');
console.log('[OFFSCREEN-highshelf] highshelf entered');
// Create a biquad filter for equalization
const eq = output.createBiquadFilter();
eq.type = "peaking";
eq.frequency.value = 3000; // Center frequency of 3000 Hz
eq.Q.value = 1; // Quality factor, determines the bandwidth of the frequencies affected
eq.gain.value = 6; // Boost the cener frequency by 6 dB

// Create a dyncamic compressor for compression
const compressor = output.createDynamicsCompressor();
compressor.threshold.value = -50; // dB level above which compressoin will start taking place
compressor.knee.value = 40; // Value representing the range above the threshold where the curve smoothly transitions to the 'ratio' portion
compressor.ratio.value = 12; // Amout of change (dB) need in the input for a 1 dB change in the output
compressor.attack.value = 0; // The ammout of time, in seconds, required to reduce the gain by 10 dB
compressor.release.value = 0.25; // The amout of time, in seconds, required to increase the gain by 10 dB

// Connect nodes
source.connect(eq);

// highShelfBiquadFilters
}
}
if (msg.type === 'highshelf'){
console.log('[OFFSCREEN-highshelf] highshelf entered');

if(gainNodes.has(msg.tabId) && highShelfBiquadFilters.has(msg.tabId)){
// Create a biquad filter for equalization
const eq = highShelfBiquadFilters.get(msg.tabId);
eq.type = "peaking";
eq.frequency.value = 3000; // Center frequency of 3000 Hz
eq.Q.value = 1; // Quality factor, determines the bandwidth of the frequencies affected
eq.gain.value = 6; // Boost the cener frequency by 6 dB

// Create a dyncamic compressor for compression
const compressor = output.createDynamicsCompressor();
compressor.threshold.value = -50; // dB level above which compressoin will start taking place
compressor.knee.value = 40; // Value representing the range above the threshold where the curve smoothly transitions to the 'ratio' portion
compressor.ratio.value = 12; // Amout of change (dB) need in the input for a 1 dB change in the output
compressor.attack.value = 0; // The ammout of time, in seconds, required to reduce the gain by 10 dB
compressor.release.value = 0.25; // The amout of time, in seconds, required to increase the gain by 10 dB
// Don't need to connect eq, already connected

}

}

});
7 changes: 6 additions & 1 deletion src/js/worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// TODO: 5/15/24
// Get Audio-Enhancement inplemented



function worker(a, b) {

return a + b;
Expand Down Expand Up @@ -31,7 +36,7 @@ chrome.runtime.onMessage.addListener(async (msg) => {
console.log("[SERVICE-WORKER] Popup loaded message received sending level: ", level);
chrome.runtime.sendMessage({ type: 'popup-level', level: level});
break;
case "adjust-level":
case "adjust-level":
console.log("[SERVICE-WORKER] Adjust level message received");
var currTab = await getCurrentTab();
await updateTabVolume(currTab.id, msg.level);
Expand Down

0 comments on commit 26f3f49

Please sign in to comment.