From 44d2afc32338ed723733196f6ceae1ed022abd65 Mon Sep 17 00:00:00 2001 From: pandadev <70103896+0PandaDEV@users.noreply.github.com> Date: Sat, 13 Apr 2024 02:31:44 +0200 Subject: [PATCH] added eq and volume restoring from config --- components/Player.vue | 83 +++++++++++++++++-------- lib/{Settings.ts => Config.ts} | 26 +++++--- lib/Player.ts | 88 +++++++++++++++++---------- pages/index.vue | 31 +++++++--- src-tauri/src/config.rs | 108 ++++++++++++++++++++------------- 5 files changed, 222 insertions(+), 114 deletions(-) rename lib/{Settings.ts => Config.ts} (50%) diff --git a/components/Player.vue b/components/Player.vue index 3ad3990..b57c9a3 100644 --- a/components/Player.vue +++ b/components/Player.vue @@ -3,7 +3,7 @@

Player

- +
{{ title }}
{{ artist }}
@@ -22,34 +22,50 @@ - +
- +
-
- {{ time }} / {{ duration }} -
+
{{ time }} / {{ duration }}
\ No newline at end of file +@import "~/assets/styles/components/player.scss"; + diff --git a/lib/Settings.ts b/lib/Config.ts similarity index 50% rename from lib/Settings.ts rename to lib/Config.ts index a5220e2..e976080 100644 --- a/lib/Settings.ts +++ b/lib/Config.ts @@ -2,10 +2,10 @@ interface EQSettings { [key: string]: string; } -class Settings { +export class Config { static async readSettings(): Promise> { try { - const settings: { [key: string]: string } = await window.__TAURI__.invoke('read_settings'); + const settings: { [key: string]: string } = await window.__TAURI__.core.invoke('read_settings'); return new Map(Object.entries(settings)); } catch (error) { console.error('Error reading settings:', error); @@ -13,19 +13,27 @@ class Settings { } } - static async readEQSettings(): Promise> { + static async readEQSettings(): Promise> { try { - const eqSettings: { [key: string]: string } = await window.__TAURI__.invoke('read_eq_settings'); - return new Map(Object.entries(eqSettings)); + const eqSettings: { [key: string]: string } = await window.__TAURI__.core.invoke('read_eq_settings'); + const eqSettingsNum: { [key: string]: number } = {}; + for (const key in eqSettings) { + eqSettingsNum[key] = parseFloat(eqSettings[key]); + } + return new Map(Object.entries(eqSettingsNum)); } catch (error) { console.error('Error reading EQ settings:', error); throw error; } } - static async writeEQSettings(eqSettings: EQSettings): Promise { + static async writeEQSettings(eqSettings: Map): Promise { try { - await window.__TAURI__.invoke('write_eq_settings', { eqSettings }); + const eqSettingsObj: { [key: string]: string } = {}; + eqSettings.forEach((value, key) => { + eqSettingsObj[key] = value.toString(); + }); + await window.__TAURI__.core.invoke('write_eq_settings', { eqSettings: eqSettingsObj }); } catch (error) { console.error('Error writing EQ settings:', error); throw error; @@ -34,7 +42,7 @@ class Settings { static async updateUserSetting(key: string, value: string): Promise { try { - await window.__TAURI__.invoke('update_user_setting', { key, value }); + await window.__TAURI__.core.invoke('update_user_setting', { key, value }); } catch (error) { console.error('Error updating user setting:', error); throw error; @@ -42,4 +50,4 @@ class Settings { } } -export default Settings; \ No newline at end of file +export default Config; \ No newline at end of file diff --git a/lib/Player.ts b/lib/Player.ts index addcbe7..f8091ac 100644 --- a/lib/Player.ts +++ b/lib/Player.ts @@ -1,4 +1,5 @@ import { readSongs } from './Songs'; +import Config from './Config'; class Player { private static instance: Player; @@ -7,20 +8,64 @@ class Player { private audioContext: AudioContext; private sourceNode: MediaElementAudioSourceNode; private eqFilters: BiquadFilterNode[]; + private volumeInitialized: Promise; private constructor() { - if (!Player.instance) { - this.audio = new Audio(); - this.audio.volume = 1; - this.audio.preload = 'auto'; - this.currentSongId = ''; - this.audioContext = new AudioContext(); - this.sourceNode = this.audioContext.createMediaElementSource(this.audio); - this.eqFilters = this.createEqFilters(); - this.connectEqFilters(); - Player.instance = this; + this.audio = new Audio(); + this.audio.preload = 'auto'; + this.currentSongId = ''; + this.audioContext = new AudioContext(); + this.sourceNode = this.audioContext.createMediaElementSource(this.audio); + this.eqFilters = this.createEqFilters(); + this.connectEqFilters(); + this.volumeInitialized = this.initVolume(); + } + + private async initVolume() { + const settings = await Config.readSettings(); + const volume = settings.get('volume'); + this.setVolume(volume); + } + + public async ensureVolumeInitialized() { + await this.volumeInitialized; + } + + public setVolume(volume: number) { + const minVolume = 0; + const maxVolume = 100; + volume = Math.max(minVolume, Math.min(maxVolume, volume)); + + const minp = 0; + const maxp = 100; + const minv = Math.log(0.01); + const maxv = Math.log(1); + const scale = (maxv - minv) / (maxp - minp); + const volumeFraction = Math.exp(minv + scale * (volume - minp)); + + this.audio.volume = volumeFraction; + } + + public async updateVolumeSetting(volume: number) { + await Config.updateUserSetting('volume', volume.toString()); + } + + public getVolume(): number { + const minp = 0; + const maxp = 100; + const minv = Math.log(0.01); + const maxv = Math.log(1); + const scale = (maxv - minv) / (maxp - minp); + + const volumeFraction = this.audio.volume; + + if (volumeFraction === 0) { + return 0; } - return Player.instance; + + const volume = (Math.log(volumeFraction) - minv) / scale + minp; + + return Math.round(volume); } private createEqFilters(): BiquadFilterNode[] { @@ -108,27 +153,6 @@ class Player { await this.setSong(songIds[prevIndex]); } - public setVolume(volume: number) { - if (volume == 0) { - this.audio.volume = 0; - return; - } - - const minVolume = 1; - const maxVolume = 100; - volume = Math.max(minVolume, Math.min(maxVolume, volume)); - - const minp = 0; - const maxp = 100; - - const minv = Math.log(0.01); - const maxv = Math.log(1); - - const scale = (maxv - minv) / (maxp - minp); - - this.audio.volume = Math.exp(minv + scale * (volume - minp)); - } - public seek(percentage: number) { const seekTime = this.audio.duration * (percentage / 100); this.audio.currentTime = seekTime; diff --git a/pages/index.vue b/pages/index.vue index 25d111f..a3a34dc 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -6,12 +6,12 @@ type="range" min="-12" max="12" - step="1" - v-model="eqGains[index]" - @input="updateEqGain(index, $event.target.value)" + step="0.1" + v-model.number="eqGains[index]" + @input="updateEqGain(index, $event.target.valueAsNumber)" /> - {{ eqGains[index] }} + {{ eqGains[index].toFixed(1) }}
  • @@ -29,21 +29,38 @@