Skip to content

Commit 4033e68

Browse files
authored
Merge pull request #2336 from adafruit/ulab-spectrogram
fixed ulab spectrogram imports
2 parents b36c88a + 79a9e0f commit 4033e68

File tree

5 files changed

+73
-48
lines changed
  • EyeLights_Audio_Spectrum/EyeLights_Audio_Spectrum_CircuitPython
  • Feather_Sense_Audio_Visualizer_13x9_RGB_LED_Matrix
  • Ukulele
  • ulab_Crunch_Numbers_Fast/waterfall

5 files changed

+73
-48
lines changed

EyeLights_Audio_Spectrum/EyeLights_Audio_Spectrum_CircuitPython/code.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
from adafruit_is31fl3741.adafruit_ledglasses import LED_Glasses
1919
from rainbowio import colorwheel
2020
from ulab import numpy as np
21-
from ulab.scipy.signal import spectrogram
2221

22+
try:
23+
from ulab.utils import spectrogram
24+
except ImportError:
25+
from ulab.scipy.signal import spectrogram
2326

2427
# FFT/SPECTRUM CONFIG ----
2528

Feather_Sense_Audio_Visualizer_13x9_RGB_LED_Matrix/audio_spectrum_lightshow/code.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
from adafruit_is31fl3741.adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
1919
from rainbowio import colorwheel
2020
from ulab import numpy as np
21-
# if using CP7 and below:
22-
from ulab.scipy.signal import spectrogram
23-
# if using CP8 and above:
24-
# from ulab.utils import spectrogram
2521

22+
try:
23+
from ulab.utils import spectrogram
24+
except ImportError:
25+
from ulab.scipy.signal import spectrogram
2626

2727
# FFT/SPECTRUM CONFIG ----
2828

@@ -40,7 +40,7 @@
4040
i2c = I2C(board.SCL, board.SDA, frequency=1000000)
4141

4242
# Initialize the IS31 LED driver, buffered for smoother animation
43-
#glasses = LED_Glasses(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
43+
# glasses = LED_Glasses(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
4444
glasses = Adafruit_RGBMatrixQT(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
4545

4646
glasses.show() # Clear any residue on startup
@@ -152,7 +152,7 @@
152152

153153
# Apply vertical scale to spectrum data. Results may exceed
154154
# matrix height...that's OK, adds impact!
155-
#data = (spectrum - lower) * (7 / (dynamic_level - lower))
155+
# data = (spectrum - lower) * (7 / (dynamic_level - lower))
156156
data = (spectrum - lower) * ((glasses.height + 2) / (dynamic_level - lower))
157157

158158
for column, element in enumerate(column_table):

Feather_Sense_Audio_Visualizer_13x9_RGB_LED_Matrix/waterfall_visualizer/code.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
'''Adapted from the FFT Example: Waterfall Spectrum Analyzer
5+
"""Adapted from the FFT Example: Waterfall Spectrum Analyzer
66
by Jeff Epler
7-
https://learn.adafruit.com/ulab-crunch-numbers-fast-with-circuitpython/overview '''
7+
https://learn.adafruit.com/ulab-crunch-numbers-fast-with-circuitpython/overview """
88

99
import array
1010
import board
1111
import audiobusio
1212
import busio
1313
from ulab import numpy as np
14-
from ulab.scipy.signal import spectrogram
14+
15+
try:
16+
from ulab.utils import spectrogram
17+
except ImportError:
18+
from ulab.scipy.signal import spectrogram
1519
import adafruit_is31fl3741
1620
from adafruit_is31fl3741.adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
1721

@@ -27,6 +31,7 @@
2731
is31.global_current = 0x03
2832
is31.enable = True
2933

34+
# fmt: off
3035
# array of colors for the LEDs
3136
# goes from purple to red
3237
# gradient generated using https://colordesigner.io/gradient-generator
@@ -41,23 +46,26 @@
4146
0xedff00,0xf5eb00,0xfcd600,0xffc100,0xffab00,
4247
0xff9500,0xff7c00,0xff6100,0xff4100,0xff0000,
4348
0xff0000,0xff0000]
49+
# fmt: on
4450

4551
# size of the FFT data sample
4652
fft_size = 64
4753

4854
# setup for onboard mic
49-
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
50-
sample_rate=16000, bit_depth=16)
55+
mic = audiobusio.PDMIn(
56+
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16
57+
)
5158

5259
# use some extra sample to account for the mic startup
53-
samples_bit = array.array('H', [0] * (fft_size+3))
60+
samples_bit = array.array("H", [0] * (fft_size + 3))
5461

5562
# sends visualized data to the RGB matrix with colors
5663
def waves(data, y):
57-
offset = max(0, (13-len(data))//2)
64+
offset = max(0, (13 - len(data)) // 2)
5865

5966
for x in range(min(13, len(data))):
60-
is31.pixel(x+offset, y, heatmap[int(data[x])])
67+
is31.pixel(x + offset, y, heatmap[int(data[x])])
68+
6169

6270
# main loop
6371
def main():
@@ -78,18 +86,18 @@ def main():
7886
# spectrum() is always nonnegative, but add a tiny value
7987
# to change any zeros to nonzero numbers
8088
spectrogram1 = np.log(spectrogram1 + 1e-7)
81-
spectrogram1 = spectrogram1[1:(fft_size//2)-1]
89+
spectrogram1 = spectrogram1[1 : (fft_size // 2) - 1]
8290
# sets range of the spectrogram
8391
min_curr = np.min(spectrogram1)
8492
max_curr = np.max(spectrogram1)
8593
# resets values
8694
if max_curr > max_all:
8795
max_all = max_curr
8896
else:
89-
max_curr = max_curr-1
97+
max_curr = max_curr - 1
9098
min_curr = max(min_curr, 3)
9199
# stores spectrogram in data
92-
data = (spectrogram1 - min_curr) * (51. / (max_all - min_curr))
100+
data = (spectrogram1 - min_curr) * (51.0 / (max_all - min_curr))
93101
# sets negative numbers to zero
94102
data = data * np.array((data > 0))
95103
# resets y
@@ -101,4 +109,5 @@ def main():
101109
# writes data to the RGB matrix
102110
is31.show()
103111

112+
104113
main()

Ukulele/code.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import audiobusio
2525
import board
2626
import neopixel
27-
from ulab.scipy.signal import spectrogram
27+
28+
try:
29+
from ulab.utils import spectrogram
30+
except ImportError:
31+
from ulab.scipy.signal import spectrogram
2832
from ulab import numpy as np
2933
from rainbowio import colorwheel
3034
from adafruit_lsm6ds import lsm6ds33
@@ -46,11 +50,11 @@
4650
WHITE,
4751
)
4852

49-
MAX_BRIGHTNESS = 0.3 #set max brightness for sound reactive mode
50-
NORMAL_BRIGHTNESS = 0.1 #set brightness for non-reactive mode
51-
VOLUME_CALIBRATOR = 50 #multiplier for brightness mapping
52-
ROCKSTAR_TILT_THRESHOLD = 200 #shake threshold
53-
SOUND_THRESHOLD = 430000 #main strum or pluck threshold
53+
MAX_BRIGHTNESS = 0.3 # set max brightness for sound reactive mode
54+
NORMAL_BRIGHTNESS = 0.1 # set brightness for non-reactive mode
55+
VOLUME_CALIBRATOR = 50 # multiplier for brightness mapping
56+
ROCKSTAR_TILT_THRESHOLD = 200 # shake threshold
57+
SOUND_THRESHOLD = 430000 # main strum or pluck threshold
5458

5559
# Set to the length in seconds for the animations
5660
POWER_ON_DURATION = 1.3
@@ -72,11 +76,11 @@
7276
pixels.show()
7377

7478

75-
#PIXEL MAPS: Used for reordering pixels so the animations can run in different configurations.
76-
#My LED strips inside the neck are accidentally swapped left-right,
77-
#so these maps also correct for that
78-
79+
# PIXEL MAPS: Used for reordering pixels so the animations can run in different configurations.
80+
# My LED strips inside the neck are accidentally swapped left-right,
81+
# so these maps also correct for that
7982

83+
# fmt: off
8084
#Bottom up along both sides at once
8185
pixel_map_reverse = PixelMap(pixels, [
8286
0, 103, 1, 102, 2, 101, 3, 100, 4, 99, 5, 98, 6, 97, 7, 96, 8, 95, 9, 94, 10,
@@ -122,6 +126,7 @@
122126
83, 22, 81, 24, 79, 26, 77, 29, 74, 31, 72, 33, 70, 35, 68, 37, 66, 39, 64, 41,
123127
62, 43, 60, 45, 58, 47, 56, 49, 54, 51, 52,
124128
], individual_pixels=True)
129+
# fmt: on
125130

126131
pixel_map = [
127132
pixel_map_reverse,
@@ -131,15 +136,15 @@
131136
pixel_map_skip,
132137
]
133138

134-
#Set up accelerometer & mic
139+
# Set up accelerometer & mic
135140
sensor = lsm6ds33.LSM6DS33(i2c)
136-
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK,
137-
board.MICROPHONE_DATA,
138-
sample_rate=16000,
139-
bit_depth=16)
141+
mic = audiobusio.PDMIn(
142+
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16
143+
)
140144

141145
NUM_SAMPLES = 256
142-
samples_bit = array.array('H', [0] * (NUM_SAMPLES+3))
146+
samples_bit = array.array("H", [0] * (NUM_SAMPLES + 3))
147+
143148

144149
def power_on(duration):
145150
"""
@@ -152,6 +157,7 @@ def power_on(duration):
152157
break # Stop animating
153158
powerup.animate()
154159

160+
155161
def rockstar_tilt(duration):
156162
"""
157163
Tilt animation - lightning effect with a rotating color
@@ -182,6 +188,7 @@ def rockstar_tilt(duration):
182188
pixels.show()
183189
time.sleep(0.03)
184190

191+
185192
# Cusomize LED Animations ------------------------------------------------------
186193
powerup = RainbowComet(pixel_map[3], speed=0, tail_length=25, bounce=False)
187194
rainbow = Rainbow(pixel_map[4], speed=0, period=6, name="rainbow", step=2.4)
@@ -191,13 +198,13 @@ def rockstar_tilt(duration):
191198
rainbow_comet = RainbowComet(pixel_map[2], speed=0, tail_length=80, bounce=True)
192199
rainbow_comet2 = RainbowComet(
193200
pixel_map[0], speed=0, tail_length=104, colorwheel_offset=80, bounce=True
194-
)
201+
)
195202
rainbow_comet3 = RainbowComet(
196203
pixel_map[1], speed=0, tail_length=25, colorwheel_offset=80, step=4, bounce=False
197-
)
204+
)
198205
strum = RainbowComet(
199206
pixel_map[3], speed=0, tail_length=25, bounce=False, colorwheel_offset=50, step=4
200-
)
207+
)
201208
lava = Comet(pixel_map[3], speed=0.01, color=ORANGE, tail_length=40, bounce=False)
202209
sparkle = Sparkle(pixel_map[4], speed=0.01, color=BLUE, num_sparkles=10)
203210
sparkle2 = Sparkle(pixel_map[1], speed=0.05, color=PURPLE, num_sparkles=4)
@@ -214,18 +221,18 @@ def rockstar_tilt(duration):
214221
AnimationGroup(
215222
sparkle,
216223
strum,
217-
),
224+
),
218225
AnimationGroup(
219226
sparkle2,
220227
rainbow_comet3,
221-
),
228+
),
222229
auto_clear=True,
223230
auto_reset=True,
224231
)
225232

226233

227234
MODE = 0
228-
LASTMODE = 1 # start up in sound reactive mode
235+
LASTMODE = 1 # start up in sound reactive mode
229236
i = 0
230237

231238
# Main loop
@@ -246,9 +253,9 @@ def rockstar_tilt(duration):
246253
spectrum[1] = 0
247254
peak_idx = np.argmax(spectrum)
248255
peak_freq = peak_idx * 16000 / 256
249-
# print((peak_idx, peak_freq, spectrum[peak_idx]))
256+
# print((peak_idx, peak_freq, spectrum[peak_idx]))
250257
magnitude = spectrum[peak_idx]
251-
# time.sleep(1)
258+
# time.sleep(1)
252259
if peak_freq == 812.50 and magnitude > SOUND_THRESHOLD:
253260
animations.next()
254261
time.sleep(1)
@@ -263,20 +270,20 @@ def rockstar_tilt(duration):
263270
print("mode = 1")
264271
LASTMODE = 1
265272
time.sleep(1)
266-
# Read accelerometer
273+
# Read accelerometer
267274
x, y, z = sensor.acceleration
268-
accel_total = x * x + y * y # x=tilt, y=rotate
269-
# print (accel_total)
275+
accel_total = x * x + y * y # x=tilt, y=rotate
276+
# print (accel_total)
270277
if accel_total > ROCKSTAR_TILT_THRESHOLD:
271278
MODE = 3
272279
print("Tilted: ", accel_total)
273280
if MODE == 1:
274281
VOLUME = magnitude / (VOLUME_CALIBRATOR * 100000)
275282
if VOLUME > MAX_BRIGHTNESS:
276283
VOLUME = MAX_BRIGHTNESS
277-
# print(VOLUME)
284+
# print(VOLUME)
278285
pixels.brightness = VOLUME
279-
# time.sleep(2)
286+
# time.sleep(2)
280287
animations.animate()
281288
elif MODE == 2:
282289
pixels.brightness = NORMAL_BRIGHTNESS

ulab_Crunch_Numbers_Fast/waterfall/code.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
import audiobusio
1313
import displayio
1414
from ulab import numpy as np
15-
from ulab.scipy.signal import spectrogram
15+
16+
try:
17+
from ulab.utils import spectrogram
18+
except ImportError:
19+
from ulab.scipy.signal import spectrogram
1620

1721
display = board.DISPLAY
1822

1923
# Create a heatmap color palette
2024
palette = displayio.Palette(52)
25+
# fmt: off
2126
for i, pi in enumerate((0xff0000, 0xff0a00, 0xff1400, 0xff1e00,
2227
0xff2800, 0xff3200, 0xff3c00, 0xff4600,
2328
0xff5000, 0xff5a00, 0xff6400, 0xff6e00,
@@ -31,6 +36,7 @@
3136
0x00a4ff, 0x0094ff, 0x0084ff, 0x0074ff,
3237
0x0064ff, 0x0054ff, 0x0044ff, 0x0032ff,
3338
0x0022ff, 0x0012ff, 0x0002ff, 0x0000ff)):
39+
# fmt: on
3440
palette[51-i] = pi
3541

3642
class RollingGraph(displayio.TileGrid):

0 commit comments

Comments
 (0)