koel/resources/assets/js/stores/equalizerStore.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-02 19:25:22 +00:00
import { preferenceStore as preferences } from '@/stores'
import { equalizerPresets as presets } from '@/config'
2022-04-15 14:24:30 +00:00
export const equalizerStore = {
2024-01-24 22:39:47 +00:00
getPresetByName (name: string) {
return presets.find(preset => preset.name === name)
2022-04-15 14:24:30 +00:00
},
/**
* Get the current equalizer config.
*/
2022-11-02 19:25:22 +00:00
getConfig () {
let config: EqualizerPreset|undefined
if (this.isCustom(preferences.equalizer)) return preferences.equalizer
if (preferences.equalizer.name !== null) {
config = this.getPresetByName(preferences.equalizer.name)
2022-04-15 14:24:30 +00:00
}
return config || presets[0]
},
isCustom(preset: any) {
return typeof preset === 'object'
&& preset !== null
&& preset.name === null
&& typeof preset.preamp === 'number'
&& Array.isArray(preset.gains)
&& preset.gains.length === 10
&& preset.gains.every((gain: any) => typeof gain === 'number')
2022-04-15 14:24:30 +00:00
},
/**
* Save the current equalizer config.
*/
saveConfig (name: EqualizerPreset['name'] | null, preamp: number, gains: number[]) {
const preset = name ? this.getPresetByName(name) : null
2022-11-02 19:25:22 +00:00
preferences.equalizer = preset || {
2022-04-15 14:24:30 +00:00
preamp,
2022-11-02 19:25:22 +00:00
gains,
name: null
2022-04-15 14:24:30 +00:00
}
}
}