Skip to content

Commit 31afc0e

Browse files
committed
Added clamp util, remove unused utils
1 parent 936a545 commit 31afc0e

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

src/components/AudioControls.vue

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
<i v-show="!microphone.enabled" class="material-icons off">mic_off</i>
1616
<i v-show="microphone.enabled" class="material-icons">mic</i>
1717
</div>
18-
<div class="button playlist" @click="showPlaylist = !showPlaylist" :class="{ off: !showPlaylist }">
18+
<div
19+
class="button playlist"
20+
@click="showPlaylist = !showPlaylist"
21+
:class="{ off: !showPlaylist }"
22+
>
1923
<i class="material-icons">queue_music</i>
2024
<transition name="playlist">
2125
<div class="list" v-show="showPlaylist">
@@ -35,6 +39,8 @@
3539
</template>
3640

3741
<script>
42+
import Utils from '@/js/Utils'
43+
3844
export default {
3945
props: {
4046
audio: {
@@ -70,6 +76,7 @@ export default {
7076
playlistNext() {
7177
this.playlist.next()
7278
},
79+
7380
timeUpdate() {
7481
let p = this.audio.currentTime / this.audio.duration
7582
let bar = document.querySelector('.progress > .scrubber')
@@ -79,42 +86,50 @@ export default {
7986
this.timePassed = t
8087
this.timeRemaining = Math.floor(this.audio.duration) - t
8188
},
89+
8290
togglePlay() {
8391
if (this.audio.ended && this.playlist.hasNext()) this.playlist.next()
8492
8593
if (this.audio.paused && this.audio.readyState == 4) this.audio.play()
8694
else this.audio.pause()
8795
},
96+
8897
updatePaused() {
8998
this.paused = this.audio.paused
9099
},
100+
91101
stopMicOnPlay() {
92102
if (this.microphone.enabled) this.microphone.toggle()
93103
},
104+
94105
scrub(e) {
95106
let { progress } = this.$refs
96107
let p = e.offsetX / progress.offsetWidth
97108
let paused = this.audio.paused
98109
this.audio.currentTime = p * this.audio.duration
99110
if (paused) this.audio.pause()
100111
},
112+
101113
mousedown(e) {
102114
if (!this.audio.src) return
103115
this.seeking = e.pageX - e.offsetX
104116
this.scrub(e)
105117
},
118+
106119
mousemove(e) {
107120
if (this.seeking) {
108121
let { progress } = this.$refs
109122
let offset = e.pageX - this.seeking
110-
offset = Math.max(0, Math.min(progress.offsetWidth, offset))
123+
offset = Utils.clamp(offset, 0, progress.offsetWidth)
111124
if (offset == progress.offsetWidth) this.seeking = false
112125
this.scrub({ offsetX: offset })
113126
}
114127
},
128+
115129
mouseup() {
116130
this.seeking = false
117131
},
132+
118133
keyup(e) {
119134
e.preventDefault()
120135
switch (e.keyCode) {

src/components/VisSettings.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<script>
3838
import { mapMutations, mapState } from 'vuex'
39+
import Utils from '@/js/Utils'
3940
4041
export default {
4142
props: ['visualizer'],
@@ -62,7 +63,7 @@ export default {
6263
changeScale(dir) {
6364
const v = this.visualizer
6465
let scale = Math.floor(v.scale * 2 + dir) / 2
65-
scale = Math.max(0.5, Math.min(2.5, scale))
66+
scale = Utils.clamp(scale, 0.5, 2.5)
6667
6768
if (scale !== v.scale) {
6869
v.scale = scale

src/js/Utils.js

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export default {
1818
return avg
1919
},
2020

21+
clamp(x, min, max) {
22+
return Math.max(min, Math.min(max, x))
23+
},
24+
2125
lerp(a, b, n) {
2226
return n * (b - a) + a
2327
},
@@ -53,44 +57,6 @@ export default {
5357
return maxes
5458
},
5559

56-
avgbin(data, n, lengths) {
57-
n = Math.floor(n)
58-
let tmpdata = new Array(n).fill(0)
59-
let bl = Math.floor(data.length / n)
60-
61-
if (lengths) {
62-
n = lengths.length
63-
for (let i = 0; i < n; i++) {
64-
bl = lengths
65-
if (bl[i][1] !== undefined) {
66-
for (let c = 0; c < bl[i][1]; c++) {
67-
for (let j = 0; j < bl[i][0]; j++) {
68-
tmpdata[i] += data[bl[i][0] * i + j]
69-
}
70-
tmpdata[i] = tmpdata[i] / bl[i][0]
71-
}
72-
} else {
73-
for (let j = 0; j < bl[i]; j++) {
74-
tmpdata[i] += data[bl[i] * i + j]
75-
}
76-
tmpdata[i] = tmpdata[i] / bl[i]
77-
}
78-
}
79-
} else {
80-
for (let i = 0; i < n; i++) {
81-
for (let j = 0; j < bl; j++) {
82-
tmpdata[i] += data[bl * i + j]
83-
}
84-
tmpdata[i] = tmpdata[i] / bl
85-
}
86-
}
87-
return tmpdata
88-
},
89-
90-
remap(value, low1, high1, low2, high2) {
91-
return low2 + ((high2 - low2) * (value - low1)) / (high1 - low1)
92-
},
93-
9460
dist(x1, y1, x2, y2) {
9561
let dx = x2 - x1
9662
let dy = y2 - y1

0 commit comments

Comments
 (0)