use SoundCheck/ReplayGain for normalization

This commit is contained in:
vapormusic 2021-12-29 23:16:52 +07:00
parent f777f832e4
commit 610633b9e3
4 changed files with 51 additions and 25 deletions

View file

@ -25,6 +25,7 @@
"discord-rpc": "^4.0.1",
"ejs": "^3.1.6",
"electron-acrylic-window": "^0.5.11",
"electron-fetch": "^1.7.4",
"electron-log": "^4.4.3",
"electron-store": "^8.0.1",
"electron-updater": "^4.6.1",
@ -32,7 +33,9 @@
"express": "^4.17.2",
"get-port": "^5.1.1",
"lastfmapi": "^0.1.1",
"media-metadata": "^2.1.0",
"mpris-service": "^2.1.2",
"music-metadata": "^7.11.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"source-map-support": "^0.5.21",

View file

@ -9,6 +9,8 @@ const yt = require('youtube-search-without-api-key');
const discord = require('./discordrpc');
const lastfm = require('./lastfm');
const mpris = require('./mpris');
const mm = require('music-metadata');
const fetch = require('electron-fetch').default;
// Analytics for debugging.
const ElectronSentry = require("@sentry/electron");
@ -216,6 +218,7 @@ const CiderBase = {
lastfm.scrobbleSong(a)
lastfm.updateNowPlayingSong(a)
});
ipcMain.on('nowPlayingItemDidChange', (_event, a) => {
app.media = a;
discord.updateActivity(a)
@ -224,6 +227,21 @@ const CiderBase = {
lastfm.updateNowPlayingSong(a)
});
ipcMain.on("getPreviewURL", (_event, url) => {
fetch(url)
.then(res => res.buffer())
.then(async (buffer) => {
try {
const metadata = await mm.parseBuffer(buffer, 'audio/x-m4a');
SoundCheckTag = metadata.native.iTunes[1].value
console.log(SoundCheckTag)
win.webContents.send('SoundCheckTag',SoundCheckTag)
} catch (error) {
console.error(error.message);
}
})
});
return win
},

View file

@ -28,32 +28,8 @@ var CiderAudio = {
CiderAudio.normalizerOn()
}
},
normalizerOn: function (){
let tuna = Tuna(CiderAudio.context)
if (!CiderAudio.audioNodes.compressorNode){
CiderAudio.audioNodes.compressorNode = new tuna.Compressor({
threshold: -80, //-100 to 0
makeupGain: 5, //0 and up (in decibels)
attack: 2, //0 to 1000
release: 200, //0 to 3000
ratio: 8, //1 to 20
knee: 0, //0 to 40
automakeup: false, //true/false
bypass: 0
});
}
try{
CiderAudio.audioNodes.gainNode.disconnect(CiderAudio.context.destination);
} catch (e){}
CiderAudio.audioNodes.gainNode.connect(CiderAudio.audioNodes.compressorNode);
CiderAudio.audioNodes.compressorNode.connect(CiderAudio.context.destination);
},
normalizerOn: function (){},
normalizerOff: function (){
try{
CiderAudio.audioNodes.compressorNode.disconnect();
CiderAudio.audioNodes.compressorNode = null
} catch (e){}
CiderAudio.audioNodes.gainNode.connect(CiderAudio.context.destination);
}
}

View file

@ -463,6 +463,15 @@ const app = new Vue({
MusicKit.getInstance().videoContainerElement = document.getElementById("apple-music-video-player")
ipcRenderer.on('SoundCheckTag', (event, tag) => {
console.log(tag)
let replaygain = self.parseSCTagToRG(tag)
try {
CiderAudio.audioNodes.gainNode.gain.value = (1 - Math.min(Math.pow(10, (replaygain.gain / 20)), (1 / replaygain.peak)))
} catch (e){
}
})
this.mk.addEventListener(MusicKit.Events.playbackTimeDidChange, (a) => {
self.lyriccurrenttime = self.mk.currentPlaybackTime
@ -480,6 +489,10 @@ const app = new Vue({
} catch (_) {
}
if (app.cfg.audio.normalization){
try{ ipcRenderer.send('getPreviewURL', self.mk.nowPlayingItem.previewURL)} catch(e){}
}
let type = (self.mk.nowPlayingItem != null) ? self.mk.nowPlayingItem["type"] ?? '' : '';
if (type.includes("musicVideo") || type.includes("uploadedVideo") || type.includes("music-movie")) {
@ -2501,6 +2514,22 @@ const app = new Vue({
element.innerHTML = `Disconnect\n<p style="font-size: 8px"><i>(Authed: ${lfmAuthKey})</i></p>`;
element.onclick = app.LastFMDeauthorize;
});
},
parseSCTagToRG: function(tag){
let soundcheck = tag.split(" ")
let numbers = []
for (item of soundcheck){
numbers.push(parseInt(item, 16))
}
numbers.shift()
let gain = Math.log10((Math.max(numbers[0],numbers[1]) ?? 1000) / 1000.0) * -10
let peak = Math.max(numbers[6],numbers[7]) / 32768.0
console.log(gain,peak)
return {
gain: gain,
peak: peak
}
}
}