Skip to content

Commit d7136ef

Browse files
committed
#2384 Fix the soundscape color
1 parent 17765b1 commit d7136ef

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

apps/website/src/projects/audiodata/visualizer/components/sidebar-soundscape-drawer.vue

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ const getMaxAmplitude = (): number => {
3939
}, maxAmp)
4040
}, 0)
4141
}
42-
4342
return maxAmplitude.value
4443
}
4544
45+
const isNormVectorEmpty = (nv?: NormVector): boolean => {
46+
return !nv || Object.keys(nv).length === 0
47+
}
48+
4649
const draw = () => {
4750
if (!props.soundscapeScidx || !canvasRef.value || !props.palette?.length) return
4851
4952
const canvas = canvasRef.value
5053
const ctx = canvas.getContext('2d')
5154
if (!ctx) return
5255
53-
let vmax = props.visualMax && props.visualMax > 0 ? props.visualMax : 1
56+
let vmax = props.visualMax
5457
let ampTh = props.amplitudeThreshold ?? 0
5558
5659
if (props.amplitudeThresholdType === 'relative-to-peak-maximum') {
@@ -61,15 +64,15 @@ const draw = () => {
6164
const pallen1 = 1.0 * (palette.length - 1)
6265
6366
let color = (v: number, j?: number): string => {
64-
const i = Math.max(0, Math.min(Math.floor((v * pallen1) / vmax), pallen1))
67+
const i = Math.max(0, Math.min(((v * pallen1 / vmax) | 0), pallen1))
6568
return palette[i]
6669
}
6770
68-
if (props.soundscapeNormVector) {
71+
if (!isNormVectorEmpty(props.soundscapeNormVector)) {
6972
vmax = 1
7073
const baseColor = color
7174
color = (v: number, j?: number) => {
72-
const n = (j !== undefined && props.soundscapeNormVector !== undefined && props.soundscapeNormVector[j]) || 1
75+
const n = props.soundscapeNormVector?.[Number(j)] ?? 1
7376
return baseColor(v / n)
7477
}
7578
}
@@ -87,31 +90,39 @@ const draw = () => {
8790
const row = props.soundscapeScidx.index[i]
8891
for (const j in row) {
8992
const cell = row[j]
90-
9193
if (ampTh && cell[1]) {
92-
let active = 0
93-
for (const amp of cell[1]) {
94-
if (amp > ampTh) active++
94+
let act = 0
95+
for (let al = cell[1], ali = 0, ale = al.length; ali < ale; ++ali) {
96+
if (al[ali] > ampTh) { act++ }
9597
}
96-
ctx.fillStyle = color(active, Number(j))
98+
ctx.fillStyle = color(act, +j)
9799
} else {
98-
ctx.fillStyle = color(cell[0], Number(j))
100+
ctx.fillStyle = color(cell[0], +j)
99101
}
100-
101-
ctx.fillRect(Number(j), h - Number(i) - 1, 1, 1)
102+
ctx.fillRect(+j, h - (+i) - 1, 1, 1)
102103
}
103104
}
104105
}
105106
106-
watch(() => [props.normalized,
107-
props.amplitudeThreshold,
108-
props.amplitudeThresholdType,
109-
props.palette,
110-
props.visualMax,
111-
props.soundscapeScidx,
112-
props.soundscapeNormVector], () => {
113-
draw()
114-
}, { deep: true })
107+
watch(
108+
() => props.soundscapeScidx,
109+
() => {
110+
maxAmplitude.value = 0
111+
}
112+
)
113+
114+
watch(
115+
() => [
116+
props.normalized,
117+
props.amplitudeThreshold,
118+
props.amplitudeThresholdType,
119+
props.visualMax
120+
],
121+
draw
122+
)
123+
124+
watch(() => props.palette, draw)
125+
watch(() => props.soundscapeNormVector, draw)
115126
116127
onMounted(() => {
117128
draw()

apps/website/src/projects/audiodata/visualizer/components/sidebar-soundscape-options-modal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ const selectAmplitudeReference = (val: string) => {
257257
}
258258
259259
const initialData = () => {
260-
currentMaxValue.value = props.soundscape.visual_max_value ?? 1
260+
currentMaxValue.value = props.soundscape.visual_max_value ?? props.soundscape.max_value
261261
isNormalize.value = props.soundscape.normalized === 1
262262
amplitudeThreshold.value = props.soundscape.threshold
263263
selectedAmplitudeReference.value = props.soundscape.threshold_type

0 commit comments

Comments
 (0)