11window . onload = function ( ) {
2+ window . wallpaperRegisterAudioListener ( ( audioArray ) => {
3+ return frequencyArray = audioArray ;
4+ } ) ;
5+
26 window . wallpaperPropertyListener = {
37 applyUserProperties : function ( properties ) {
48 if ( properties . charset )
@@ -31,22 +35,34 @@ window.onload = function () {
3135
3236 if ( properties . colormode )
3337 color_mode = properties . colormode . value ;
34- if ( properties . matrixcolor )
35- color = properties . matrixcolor . value . split ( ' ' ) . map ( function ( c ) {
38+ if ( properties . matrixcolor ) {
39+ var tmp = properties . matrixcolor . value . split ( ' ' ) . map ( function ( c ) {
3640 return Math . ceil ( c * 255 )
3741 } ) ;
42+ color = rgbToHsl ( ...tmp ) [ 0 ] * 360 ;
43+ }
3844 if ( properties . coloranimationspeed )
3945 color_animation_speed = map ( properties . coloranimationspeed . value , - 1 , 1 , 0.05 , - 0.05 ) ;
4046
4147 if ( properties . highlightfirstcharacter )
4248 highlight_first_character = properties . highlightfirstcharacter . value ;
4349
50+ if ( properties . audioresponsive )
51+ isAudioResponsive = properties . audioresponsive . value ;
52+ if ( properties . audiosensetivity )
53+ AudioMultiplier = properties . audiosensetivity . value ;
54+ if ( properties . silenceanimation )
55+ hasSilenceAnimation = properties . silenceanimation . value ;
56+ if ( properties . silencetimeoutseconds )
57+ SilenceTimeoutSeconds = properties . silencetimeoutseconds . value ;
58+
4459 startAnimating ( ) ;
4560 }
4661 } ;
4762
4863 var debug = document . getElementById ( "debug" ) , logs = [ ] ;
4964 var fpsInterval , startTime , now , then , elapsed , letters , columns , rows , drops , drop_chars , trail_length = 0.05 , highlight_first_character = true ;
65+ var isAudioResponsive = false , hasSilenceAnimation = true , AudioTimeout = false , SilenceTimeoutSeconds = 15 , LastSoundTime = new Date ( ) , isSilent = false , frequencyArray , frequencyArrayLength = 128 , AudioMultiplier = 50 , column_frequency ;
5066 var color = "0,255,0" , color_mode = "0" , color_animation_speed = 0 , column_hue , row_hue ;
5167 var char_set = "4" , custom_char_set ;
5268 var font_size , font_fraction , font = "2" , custom_font ;
@@ -127,17 +143,67 @@ window.onload = function () {
127143 fallAnimation ( ) ;
128144 }
129145
146+ function rgbToHsl ( r , g , b ) {
147+ r /= 255 , g /= 255 , b /= 255 ;
148+
149+ var max = Math . max ( r , g , b ) , min = Math . min ( r , g , b ) ;
150+ var h , s , l = ( max + min ) / 2 ;
151+
152+ if ( max == min ) {
153+ h = s = 0 ; // achromatic
154+ } else {
155+ var d = max - min ;
156+ s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min ) ;
157+
158+ switch ( max ) {
159+ case r : h = ( g - b ) / d + ( g < b ? 6 : 0 ) ; break ;
160+ case g : h = ( b - r ) / d + 2 ; break ;
161+ case b : h = ( r - g ) / d + 4 ; break ;
162+ }
163+
164+ h /= 6 ;
165+ }
166+
167+ return [ h , s , l ] ;
168+ }
169+
130170 function map ( value , from_a , from_b , to_a , to_b ) {
131171 return ( ( ( value - from_a ) * ( to_b - to_a ) ) / ( from_b - from_a ) ) + to_a ;
132172 }
133173
174+ function clamp ( min , max , value ) {
175+ if ( value < min )
176+ return min ;
177+ if ( value > max )
178+ return max ;
179+ return value ;
180+ }
181+
134182 function drawmatrix ( ) {
135183 ctx . fillStyle = "rgba(0, 0, 0, " + trail_length + ")" ;
136184 ctx . fillRect ( 0 , 0 , c . width , c . height ) ;
185+ isSilent = true ;
137186
138187 for ( var i = 0 ; i < drops . length ; i ++ ) {
139188 var character = calculateCharacter ( drops [ i ] ) ;
140- var doHighlight = drops [ i ] [ 1 ] > 0 ;
189+ var probability = 0.975 ;
190+ var lightness = 50 ;
191+
192+ if ( isAudioResponsive ) {
193+ var frequency = Math . floor ( i * column_frequency ) ;
194+ var Volume = frequencyArray [ frequency ] + frequencyArray [ frequency + ( frequencyArrayLength / 2 ) ] ;
195+
196+ if ( Volume > 0.01 )
197+ isSilent = false ;
198+
199+ if ( ! AudioTimeout || ! hasSilenceAnimation ) {
200+ probability = 1 - clamp ( 0 , 1 , ( Volume * Volume * Volume * AudioMultiplier ) ) ;
201+ lightness = Math . floor ( clamp ( 40 , 80 , Volume * 100 * AudioMultiplier ) ) ;
202+ }
203+ }
204+
205+ if ( drops [ i ] [ 1 ] > 0 )
206+ lightness = 100 ;
141207
142208 if ( highlight_first_character ) {
143209 ctx . fillStyle = "#000" ;
@@ -150,16 +216,25 @@ window.onload = function () {
150216 ctx . fillStyle = "#FFF" ;
151217 }
152218 else
153- ctx . fillStyle = calculateColor ( i , drops [ i ] [ 0 ] , doHighlight ) ;
219+ ctx . fillStyle = calculateColor ( i , drops [ i ] [ 0 ] , lightness ) ;
154220
155- drop_chars [ i ] = [ character , doHighlight ] ;
221+ drop_chars [ i ] = [ character , lightness ] ;
156222 ctx . fillText ( character , i * font_size , drops [ i ] [ 0 ] * font_size ) ;
157223
158- if ( drops [ i ] [ 0 ] > rows && Math . random ( ) > 0.975 )
224+ if ( drops [ i ] [ 0 ] > rows && Math . random ( ) > probability )
159225 drops [ i ] = [ 0 , 0 , 0 ] ;
160226
161227 drops [ i ] [ 0 ] ++ ;
162228 }
229+
230+ if ( hasSilenceAnimation ) {
231+ if ( ! isSilent ) {
232+ AudioTimeout = false ;
233+ LastSoundTime = new Date ( ) ;
234+ } else if ( ( new Date ( ) - LastSoundTime ) > SilenceTimeoutSeconds * 1000 ) {
235+ AudioTimeout = true ;
236+ }
237+ }
163238 }
164239
165240 function calculateCharacter ( dropItem ) {
@@ -180,10 +255,7 @@ window.onload = function () {
180255 return letters [ Math . floor ( Math . random ( ) * letters . length ) ] ;
181256 }
182257
183- function calculateColor ( i , j , highLight ) {
184- if ( highLight )
185- return "#FFF" ;
186-
258+ function calculateColor ( i , j , lightness ) {
187259 var hue , offset = Math . floor ( color_animation_speed * then ) ;
188260
189261 switch ( color_mode ) {
@@ -204,11 +276,12 @@ window.onload = function () {
204276 }
205277 //Static
206278 default : {
207- return "rgb( " + color + " )" ;
279+ hue = color ;
280+ break ;
208281 }
209282 }
210283
211- return "hsl(" + hue + ", 100%, 50 %)" ; ;
284+ return "hsl(" + hue + ", 100%, " + lightness + " %)"; ;
212285 }
213286
214287 window . addEventListener ( 'resize' , function ( ) {
@@ -224,6 +297,7 @@ window.onload = function () {
224297 rows = c . height / font_size ;
225298 column_hue = Math . floor ( 360 / columns ) ;
226299 row_hue = Math . floor ( 360 / rows ) ;
300+ column_frequency = frequencyArrayLength / ( columns * 2 ) ;
227301 }
228302
229303 function fallAnimation ( ) {
0 commit comments