@@ -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+
4649const 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
116127onMounted (() => {
117128 draw ()
0 commit comments