koel/resources/assets/js/components/ui/Equalizer.vue

277 lines
5.9 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-11-02 19:25:22 +00:00
<form id="equalizer" ref="root" data-testid="equalizer" tabindex="0" @keydown.esc="close">
<header>
2022-04-15 14:24:30 +00:00
<label class="select-wrapper">
<select v-model="selectedPresetId" title="Select equalizer">
<option disabled value="-1">Preset</option>
2022-11-02 19:25:22 +00:00
<option v-for="preset in presets" :key="preset.id" :value="preset.id">{{ preset.name }}</option>
2022-04-15 14:24:30 +00:00
</select>
2022-10-25 18:25:58 +00:00
<icon :icon="faCaretDown" class="arrow text-highlight" size="sm"/>
2022-04-15 14:24:30 +00:00
</label>
2022-11-02 19:25:22 +00:00
</header>
<main>
<div class="bands">
<span class="band">
<span class="slider"/>
<label>Preamp</label>
</span>
<span class="indicators">
<span>+20</span>
<span>0</span>
<span>-20</span>
</span>
<span v-for="band in bands" :key="band.label" class="band">
<span class="slider"/>
<label>{{ band.label }}</label>
</span>
</div>
</main>
<footer>
<Btn @click.prevent="close">Close</Btn>
</footer>
</form>
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import noUiSlider from 'nouislider'
2022-10-25 18:25:58 +00:00
import { faCaretDown } from '@fortawesome/free-solid-svg-icons'
2022-11-02 19:25:22 +00:00
import { onMounted, ref, watch } from 'vue'
import { equalizerStore } from '@/stores'
import { audioService } from '@/services'
import { equalizerPresets as presets } from '@/config'
import Btn from '@/components/ui/Btn.vue'
2022-04-15 14:24:30 +00:00
2022-11-02 19:25:22 +00:00
const emit = defineEmits(['close'])
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
const bands = audioService.bands
const root = ref<HTMLElement>()
2022-11-02 19:25:22 +00:00
const preampGain = ref(0)
const selectedPresetId = ref(-1)
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
watch(preampGain, value => audioService.changePreampGain(value))
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
watch(selectedPresetId, () => {
if (selectedPresetId.value !== -1) {
loadPreset(equalizerStore.getPresetById(selectedPresetId.value) || presets[0])
}
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
save()
})
2022-04-15 17:00:08 +00:00
const createSliders = () => {
2022-11-02 19:25:22 +00:00
const config = equalizerStore.getConfig()
selectedPresetId.value = config.id
preampGain.value = config.preamp
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
if (!root.value) {
throw new Error('Equalizer config or root element not found')
}
2022-04-15 14:24:30 +00:00
2022-11-02 19:25:22 +00:00
root.value.querySelectorAll<EqualizerBandElement>('.slider').forEach((el, i) => {
noUiSlider.create(el, {
2022-04-15 17:00:08 +00:00
connect: [false, true],
// the first element is the preamp. The rest are gains.
start: i === 0 ? config.preamp : config.gains[i - 1],
range: { min: -20, max: 20 },
orientation: 'vertical',
direction: 'rtl'
})
2022-11-02 19:25:22 +00:00
el.isPreamp = i === 0
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
el.noUiSlider.on('slide', (values, handle) => {
2022-11-02 19:25:22 +00:00
const value = parseFloat(values[handle])
if (el.isPreamp) {
preampGain.value = value
2022-04-15 17:00:08 +00:00
} else {
2022-11-02 19:25:22 +00:00
audioService.changeFilterGain(bands[i - 1].filter, value)
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
// User has customized the equalizer. No preset should be selected.
selectedPresetId.value = -1
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
save()
2022-04-15 17:00:08 +00:00
})
})
}
const loadPreset = (preset: EqualizerPreset) => {
2022-11-02 19:25:22 +00:00
preampGain.value = preset.preamp
root.value?.querySelectorAll<EqualizerBandElement>('.slider').forEach((el, i) => {
2022-04-15 17:00:08 +00:00
if (!el.noUiSlider) {
throw new Error('Preset can only be loaded after sliders have been set up')
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
if (el.isPreamp) {
2022-04-15 17:00:08 +00:00
el.noUiSlider.set(preset.preamp)
} else {
2022-11-02 19:25:22 +00:00
audioService.changeFilterGain(bands[i - 1].filter, preset.gains[i - 1])
2022-04-15 17:00:08 +00:00
el.noUiSlider.set(preset.gains[i - 1])
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
})
}
2022-11-02 19:25:22 +00:00
const save = () => equalizerStore.saveConfig(selectedPresetId.value, preampGain.value, bands.map(band => band.db))
const close = () => emit('close')
2022-04-15 17:00:08 +00:00
2022-11-02 19:25:22 +00:00
onMounted(() => createSliders())
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss">
#equalizer {
user-select: none;
width: 100%;
display: flex;
flex-direction: column;
2022-11-02 19:25:22 +00:00
footer {
border-top: 1px solid rgba(255, 255, 255, .05);
}
2022-04-15 14:24:30 +00:00
label {
margin-top: 8px;
margin-bottom: 0;
text-align: left;
}
2022-11-02 19:25:22 +00:00
header {
padding: 12px 16px;
2022-04-15 14:24:30 +00:00
.select-wrapper {
2022-11-02 19:25:22 +00:00
margin-top: 0;
2022-04-15 14:24:30 +00:00
position: relative;
2022-11-02 19:25:22 +00:00
padding: 0 8px;
background: rgba(0, 0, 0, .2);
border-radius: 5px;
2022-04-15 14:24:30 +00:00
2022-07-15 07:23:55 +00:00
.arrow {
2022-11-02 19:25:22 +00:00
margin-left: -6px;
2022-04-15 14:24:30 +00:00
pointer-events: none;
}
}
select {
background: none;
color: var(--color-text-primary);
padding-left: 0;
width: 100px;
text-transform: none;
option {
color: var(--color-black);
}
}
}
.bands {
2022-11-02 19:25:22 +00:00
padding: 14px 16px 12px;
border-radius: 4px;
background: rgba(0, 0, 0, .2);
2022-04-15 14:24:30 +00:00
display: flex;
justify-content: space-between;
label, .indicators {
font-size: .8rem;
}
.band {
display: flex;
flex-direction: column;
align-items: center;
2022-11-02 19:25:22 +00:00
min-width: 24px;
2022-04-15 14:24:30 +00:00
}
.slider {
height: 100px;
}
.indicators {
height: 100px;
width: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
margin-left: -16px;
2022-11-02 19:25:22 +00:00
opacity: .5;
2022-04-15 14:24:30 +00:00
span:first-child {
line-height: 8px;
}
span:last-child {
line-height: 8px;
}
}
}
.noUi {
&-connect {
background: none;
box-shadow: none;
2022-04-15 17:00:08 +00:00
2022-04-15 14:24:30 +00:00
&::after {
content: " ";
position: absolute;
width: 2px;
height: 100%;
top: 0;
left: 7px;
}
}
2022-04-25 13:08:00 +00:00
&-touch-area {
cursor: ns-resize;
}
2022-04-15 14:24:30 +00:00
&-target {
background: transparent;
border-radius: 0;
border: 0;
box-shadow: none;
width: 16px;
&::after {
content: " ";
position: absolute;
width: 2px;
height: 100%;
background: linear-gradient(to bottom, var(--color-highlight) 0%, var(--color-highlight) 36%, var(--color-green) 100%);
background-size: 2px;
top: 0;
left: 7px;
}
}
&-handle {
border: 0;
border-radius: 0;
box-shadow: none;
cursor: pointer;
&::before, &::after {
display: none;
}
}
&-vertical {
.noUi-handle {
width: 16px;
2022-11-02 19:25:22 +00:00
height: 6px;
2022-04-20 15:57:53 +00:00
left: -16px;
2022-11-02 19:25:22 +00:00
border-radius: 9999px;
2022-04-15 14:24:30 +00:00
top: 0;
}
}
}
}
</style>