/
AudioRate.vue
137 lines (120 loc) · 3.09 KB
/
AudioRate.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<template>
<div class="input-element" :aria-label="$t('A11Y.RATE')">
<label class="spaced" tabindex="0" :aria-label="$t('A11Y.RATE_CURRENT', { rate: toDecimal(rate) })">
<span class="input-label">{{ $t('AUDIO.SPEED') }}</span>
<span class="input-state" id="tab-audio--rate--current">
<input class="input-value" id="tab-audio--rate--value" type="number" :value="toDecimal(rate)" step="0.01" @input="setStateRate($event.target.value)" />
<span class="input-suffix">x</span>
</span>
</label>
<div class="rate-slider centered">
<input-slider-component
:aria-label="$t('A11Y.SET_RATE_IN_PERCENT')"
id="tab-audio--rate--input"
@dblclick="setRate(1)"
min="0" max="1" step="0.001"
:pins="[{
value: 0,
label: '0.5x'
}, {
value: 0.245,
label: '0.75x'
}, {
value: 0.5,
label: '1x'
}, {
value: 0.665,
label: '2x'
}, {
value: 0.84,
label: '3x'
}, {
value: 1,
label: '4x'
}]"
:value="sliderRate" @input="toStateRate"></input-slider-component>
</div>
</div>
</template>
<script>
import { mapState, mapActions } from 'redux-vuex'
import { compose } from 'lodash/fp'
import { toDecimal, roundUp, round } from 'utils/math'
import InputSliderComponent from 'shared/InputSlider'
import ButtonComponent from 'shared/Button'
import PlusIcon from 'icons/PlusIcon'
import MinusIcon from 'icons/MinusIcon'
// Speed Modifiers
const normalizeSliderValue = (value = 0) => {
if (value < 0) {
value = 0
}
if (value > 1) {
value = 1
}
return value
}
const normalizeRateValue = (value = 0) => {
if (value < 0.5) {
value = 0.5
}
if (value > 4) {
value = 4
}
return value
}
const speedSliderToState = (value = 0) => {
value = parseFloat(value)
if (value <= 0.5) {
value = 0.5 + value
} else {
value = 2 * value + (value - 0.5) * 4
}
return value
}
const stateToSpeedSlider = (value = 0) => {
value = parseFloat(value)
if (value <= 1) {
value = value - 0.5
} else {
value = (value + 2) / 6
}
return value
}
export default {
data: mapState('rate', 'theme'),
computed: {
sliderRate () {
return this.toSliderRate(this.rate)
}
},
methods: {
...mapActions('setRate'),
toStateRate (value) {
compose(
this.setRate.bind(this),
round,
speedSliderToState,
normalizeSliderValue
)(value)
},
setStateRate (value) {
compose(
this.setRate.bind(this),
round
)(value)
},
changeRate (offset, rate) {
return compose(this.setRate.bind(this), roundUp(offset))(rate)
},
toSliderRate: compose(round, stateToSpeedSlider, normalizeRateValue),
toDecimal
},
components: {
InputSliderComponent,
ButtonComponent,
PlusIcon,
MinusIcon
}
}
</script>