Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,46 @@ ipcMain.handle('set-hardware-acceleration', async (event, enabled) => {
}
});

ipcMain.handle('get-volume-boost', () => {
if (!store) return 1.0;

const raw = Number(store.get('volumeBoost', 1.0));
if (!Number.isFinite(raw) || raw <= 0) return 1.0;

const clamped = Math.min(Math.max(raw, 1.0), 10.0);
return clamped;
});

ipcMain.handle('set-volume-boost', async (event, value) => {
try {
if (!store) {
return { success: false, error: 'Settings store not available' };
}

let numeric = Number(value);
if (!Number.isFinite(numeric) || numeric <= 0) {
numeric = 1.0;
}

numeric = Math.min(Math.max(numeric, 1.0), 10.0);

store.set('volumeBoost', numeric);

if (mainBrowserView && mainBrowserView.webContents) {
mainBrowserView.webContents.send('volume-boost-changed', numeric);
}

return { success: true, value: numeric };
} catch (error) {
console.error('Failed to set volume boost:', error);
return {
success: false,
error: 'Failed to update volume boost. Please try again.',
code: 'SET_VOLUME_BOOST_FAILED',
};
}
});

app.whenReady().then(async () => {
// Set the app name
app.setName('P-Stream');
Expand All @@ -1032,6 +1072,7 @@ app.whenReady().then(async () => {
streamUrl: 'pstream.mov',
hardwareAcceleration: true,
warpLaunchEnabled: false,
volumeBoost: 1.0,
},
});

Expand Down
16 changes: 12 additions & 4 deletions src/main/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SimpleStore {
this.defaults = options.defaults || {};
this.filePath = path.join(app.getPath('userData'), 'settings.json');
this.data = this.load();
this._saveTimer = null;
}

load() {
Expand All @@ -22,11 +23,18 @@ class SimpleStore {
}

save() {
try {
fs.writeFileSync(this.filePath, JSON.stringify(this.data, null, 2), 'utf8');
} catch (error) {
console.error('Error saving settings:', error);
if (this._saveTimer) {
return;
}

this._saveTimer = setTimeout(() => {
this._saveTimer = null;
fs.writeFile(this.filePath, JSON.stringify(this.data, null, 2), 'utf8', (error) => {
if (error) {
console.error('Error saving settings:', error);
}
});
}, 100);
}

get(key, defaultValue) {
Expand Down
3 changes: 3 additions & 0 deletions src/preload/preload-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ contextBridge.exposeInMainWorld('settings', {
getHardwareAcceleration: () => ipcRenderer.invoke('get-hardware-acceleration'),
setHardwareAcceleration: (enabled) => ipcRenderer.invoke('set-hardware-acceleration', enabled),
restartApp: () => ipcRenderer.invoke('restartApp'),
// Volume boost
getVolumeBoost: () => ipcRenderer.invoke('get-volume-boost'),
setVolumeBoost: (value) => ipcRenderer.invoke('set-volume-boost', value),
});
118 changes: 118 additions & 0 deletions src/preload/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,122 @@ const observeThemeChanges = () => {
}
};

let pstreamAudioContext = null;
let pstreamMasterGain = null;
let pstreamCurrentBoost = 1.0;

const applyBoostValue = (rawValue) => {
let value = Number(rawValue);
if (!Number.isFinite(value) || value <= 0) value = 1.0;
value = Math.min(Math.max(value, 1.0), 10.0);
pstreamCurrentBoost = value;
if (pstreamMasterGain) {
pstreamMasterGain.gain.value = pstreamCurrentBoost;
}
};

const ensureAudioGraph = () => {
if (pstreamAudioContext && pstreamMasterGain) return;

const AudioContextImpl = window.AudioContext || window.webkitAudioContext;
if (!AudioContextImpl) {
return;
}

try {
pstreamAudioContext = new AudioContextImpl();
pstreamMasterGain = pstreamAudioContext.createGain();
pstreamMasterGain.gain.value = pstreamCurrentBoost;
pstreamMasterGain.connect(pstreamAudioContext.destination);
} catch (error) {
console.warn('[P-Stream] Failed to initialise audio context for volume boost:', error);
pstreamAudioContext = null;
pstreamMasterGain = null;
}
};

const hookMediaElementForBoost = (el) => {
if (!el || el.__pstreamBoosted) return;

ensureAudioGraph();
if (!pstreamAudioContext || !pstreamMasterGain) return;

try {
const sourceNode = pstreamAudioContext.createMediaElementSource(el);
const elementGain = pstreamAudioContext.createGain();
const initialVolume = typeof el.volume === 'number' ? el.volume : 1.0;
elementGain.gain.value = initialVolume;

sourceNode.connect(elementGain).connect(pstreamMasterGain);

const resumeContext = () => {
if (pstreamAudioContext && pstreamAudioContext.state === 'suspended') {
pstreamAudioContext.resume().catch(() => {});
}
};

el.addEventListener('play', resumeContext);

const onVolumeChange = () => {
const vol = typeof el.volume === 'number' ? el.volume : 1.0;
elementGain.gain.value = vol;
};
el.addEventListener('volumechange', onVolumeChange);

el.__pstreamBoosted = true;
} catch (error) {
console.warn('[P-Stream] Failed to hook media element for volume boost:', error);
}
};

const scanAndHookMediaElements = () => {
try {
const mediaEls = document.querySelectorAll('audio, video');
mediaEls.forEach((el) => hookMediaElementForBoost(el));
} catch (error) {
console.warn('[P-Stream] Failed to scan media elements for volume boost:', error);
}
};

ipcRenderer.on('volume-boost-changed', (_event, value) => {
applyBoostValue(value);
});

const setupVolumeBoost = async () => {
try {
const initialBoost = await ipcRenderer.invoke('get-volume-boost');
applyBoostValue(initialBoost);
} catch (error) {
console.warn('[P-Stream] Failed to load initial volume boost:', error);
}

scanAndHookMediaElements();

try {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (!(node instanceof Element)) continue;
if (node.matches('audio, video')) {
hookMediaElementForBoost(node);
}
const nested = node.querySelectorAll && node.querySelectorAll('audio, video');
if (nested && nested.length) {
nested.forEach((el) => hookMediaElementForBoost(el));
}
}
}
});

observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
} catch (error) {
console.warn('[P-Stream] Failed to observe media elements for volume boost:', error);
}
};

window.addEventListener('DOMContentLoaded', () => {
observeThemeChanges();
const intervalId = setInterval(sendThemeColor, 10000);
Expand All @@ -142,4 +258,6 @@ window.addEventListener('DOMContentLoaded', () => {
},
{ once: true },
);

setupVolumeBoost();
});
21 changes: 21 additions & 0 deletions src/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ <h3 class="settings__setting-title">Discord Rich Presence</h3>
</label>
</div>
</div>
<div class="settings__section">
<div class="settings__setting settings__setting--input">
<div class="settings__setting-info">
<h3 class="settings__setting-title">Volume boost</h3>
<p class="settings__setting-description">
Make playback louder (up to 10×)
</p>
<p class="settings__setting-status" id="volume-boost-label">1.0×</p>
</div>
<div class="settings__input-group">
<input
type="range"
id="volume-boost-slider"
class="settings__input settings__input--range"
min="1"
max="10"
step="0.1"
/>
</div>
</div>
</div>
<div class="settings__section">
<div class="settings__setting">
<div class="settings__setting-info">
Expand Down
52 changes: 52 additions & 0 deletions src/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const saveUrlBtn = document.getElementById('save-url-btn');
const resetAppBtn = document.getElementById('reset-app-btn');
const uninstallAppBtn = document.getElementById('uninstall-app-btn');
const hardwareAccelToggle = document.getElementById('hardware-accel-toggle');
const volumeBoostSlider = document.getElementById('volume-boost-slider');
const volumeBoostLabel = document.getElementById('volume-boost-label');

// Load initial state
async function loadState() {
Expand Down Expand Up @@ -63,6 +65,22 @@ async function loadState() {
console.error('Failed to load hardware acceleration state:', error);
}

// Load volume boost
try {
const boost = await window.settings.getVolumeBoost();
let numeric = Number(boost);
if (!Number.isFinite(numeric) || numeric <= 0) numeric = 1.0;
numeric = Math.min(Math.max(numeric, 1.0), 10.0);
if (volumeBoostSlider) {
volumeBoostSlider.value = numeric.toString();
}
if (volumeBoostLabel) {
volumeBoostLabel.textContent = `${numeric.toFixed(1)}×`;
}
} catch (error) {
console.error('Failed to load volume boost:', error);
}

// Check if we're in development mode
try {
const updateCheck = await window.settings.checkForUpdates();
Expand Down Expand Up @@ -178,6 +196,40 @@ hardwareAccelToggle.addEventListener('change', async (event) => {
}
});

// Handle volume boost slider change
if (volumeBoostSlider) {
volumeBoostSlider.addEventListener('input', (event) => {
const raw = Number(event.target.value);
let numeric = Number.isFinite(raw) && raw > 0 ? raw : 1.0;
numeric = Math.min(Math.max(numeric, 1.0), 10.0);

if (volumeBoostLabel) {
volumeBoostLabel.textContent = `${numeric.toFixed(1)}×`;
}
});

volumeBoostSlider.addEventListener('change', async (event) => {
const raw = Number(event.target.value);
let numeric = Number.isFinite(raw) && raw > 0 ? raw : 1.0;
numeric = Math.min(Math.max(numeric, 1.0), 10.0);

try {
const result = await window.settings.setVolumeBoost(numeric);
if (result && result.success && typeof result.value === 'number') {
const applied = Math.min(Math.max(result.value, 1.0), 10.0);
if (volumeBoostSlider.value !== applied.toString()) {
volumeBoostSlider.value = applied.toString();
}
if (volumeBoostLabel) {
volumeBoostLabel.textContent = `${applied.toFixed(1)}×`;
}
}
} catch (error) {
console.error('Failed to update volume boost:', error);
}
});
}

// Handle update check button
checkUpdatesBtn.addEventListener('click', async () => {
const buttonText = checkUpdatesBtn.textContent;
Expand Down