@@ -44,14 +44,23 @@ def create_sound_instruction(start_freq: int, end_freq: int, start_vol: int,
4444 max (end_freq , 1 )
4545 ).hex ()
4646
47- def moving_average (arr , window_size = 5 ):
47+ def moving_average_1d (arr , window_size = 3 ):
48+ """Smooth a 1D array."""
4849 return np .convolve (arr , np .ones (window_size )/ window_size , mode = "same" )
4950
51+ def moving_average_2d (arr , window_size = 3 ):
52+ """Smooth each row (slice) of a 2D array independently."""
53+ smoothed = np .zeros_like (arr )
54+ for i in range (arr .shape [0 ]):
55+ smoothed [i ] = moving_average_1d (arr [i ], window_size )
56+ return smoothed
57+
5058def audio_to_makecode_arcade (data , sample_rate , period ) -> str :
59+ spectrogram_frequency = period / 1000
5160 f , t , Sxx = scipy .signal .spectrogram (
5261 data ,
5362 sample_rate ,
54- nperseg = round (( period / 1000 ) * sample_rate )
63+ nperseg = round (spectrogram_frequency * sample_rate )
5564 )
5665
5766 frequency_buckets = [50 , 159 , 200 , 252 , 317 , 400 , 504 , 635 , 800 , 1008 ,
@@ -63,8 +72,9 @@ def audio_to_makecode_arcade(data, sample_rate, period) -> str:
6372 loudest_amplitudes = Sxx [loudest_indices , np .arange (Sxx .shape [1 ])].transpose ()
6473 max_amp = np .max (Sxx )
6574
66- # Smooth amplitudes and optionally smooth over slices
67- loudest_amplitudes = moving_average (loudest_amplitudes , window_size = 3 )
75+ # Smooth each slice for cleaner sound
76+ loudest_amplitudes = moving_average_2d (loudest_amplitudes , window_size = 3 )
77+
6878 sound_instruction_buffers = ["" ] * len (frequency_buckets )
6979
7080 for slice_index in range (len (loudest_frequencies )):
@@ -86,8 +96,10 @@ def audio_to_makecode_arcade(data, sample_rate, period) -> str:
8696 else :
8797 sound_instruction_buffers [bucket_index ] += create_sound_instruction (0 , 0 , 0 , 0 , period )
8898
99+ # Wrap each buffer in hex`` properly
89100 sound_instruction_buffers = [f"hex`{ buf } `" for buf in sound_instruction_buffers ]
90101
102+ # Final MakeCode output
91103 code = (
92104 "namespace music {\n "
93105 " //% shim=music::queuePlayInstructions\n "
0 commit comments