mirror of
https://github.com/correl/turntable.git
synced 2024-11-23 11:09:56 +00:00
Fix up the spectrum graph
This commit is contained in:
parent
7fe5057f33
commit
6194cfcd49
1 changed files with 44 additions and 9 deletions
|
@ -32,20 +32,54 @@ class Plot:
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.bars = bars
|
self.bars = bars
|
||||||
self.audio = b""
|
self.audio = models.PCM(44100, 2)
|
||||||
|
|
||||||
def draw(self) -> None:
|
def spectrum(self) -> np.array:
|
||||||
data = np.fromstring(self.audio, dtype=np.int16)
|
data = np.fromstring(self.audio.raw, dtype=np.int16)
|
||||||
|
if len(data) == 0:
|
||||||
|
return data
|
||||||
|
merged = np.mean(np.reshape(data, (-1, self.audio.channels)), axis=1)
|
||||||
|
fft = np.abs(np.fft.fft(merged))
|
||||||
|
fft = fft[: len(fft) // 2]
|
||||||
|
dbfs = 20 * np.log10(fft * 2 / (len(fft) * 2 ** 15))
|
||||||
|
dbfs = np.maximum(-100, dbfs) + 100
|
||||||
|
return dbfs
|
||||||
|
|
||||||
|
def draw_lines(self) -> None:
|
||||||
|
data = self.spectrum()
|
||||||
if len(data) == 0:
|
if len(data) == 0:
|
||||||
return
|
return
|
||||||
fft = abs(np.fft.fft(data).real)
|
|
||||||
fft = fft[: len(fft) // 2]
|
print((np.min(data), np.max(data)))
|
||||||
fft = abs(scipy.signal.resample(fft, self.bars))
|
nlines = self.width // 4
|
||||||
|
data = np.mean(
|
||||||
|
np.reshape(data[: len(data) // nlines * nlines], (-1, len(data) // nlines)),
|
||||||
|
axis=1,
|
||||||
|
)
|
||||||
|
lines = self.height * (data / 100)
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
pygame.draw.line(
|
||||||
|
self.screen,
|
||||||
|
(128, 128, 128),
|
||||||
|
(self.x + i * 4, self.height),
|
||||||
|
(self.x + i * 4, self.height - int(line)),
|
||||||
|
)
|
||||||
|
|
||||||
|
def draw_bars(self) -> None:
|
||||||
|
data = self.spectrum()
|
||||||
|
if len(data) == 0:
|
||||||
|
return
|
||||||
|
fft = np.mean(
|
||||||
|
np.reshape(
|
||||||
|
data[: len(data) // self.bars * self.bars], (-1, len(data) // self.bars)
|
||||||
|
),
|
||||||
|
axis=1,
|
||||||
|
)
|
||||||
|
|
||||||
light_width = self.width // (2 * self.bars - 1)
|
light_width = self.width // (2 * self.bars - 1)
|
||||||
light_height = self.height // 2 // light_width
|
light_height = self.height // 2 // light_width
|
||||||
light_steps = self.height // light_height // 2
|
light_steps = self.height // light_height // 2
|
||||||
lights = fft * light_steps / 2 ** 16
|
lights = fft * light_steps / 100
|
||||||
|
|
||||||
colors = [
|
colors = [
|
||||||
(0, (0, 255, 0)),
|
(0, (0, 255, 0)),
|
||||||
|
@ -147,11 +181,12 @@ def main():
|
||||||
...
|
...
|
||||||
try:
|
try:
|
||||||
while sample := pcm_in.get(False):
|
while sample := pcm_in.get(False):
|
||||||
plot.audio = sample.raw
|
plot.audio = sample
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
...
|
...
|
||||||
screen.fill((0, 0, 0))
|
screen.fill((0, 0, 0))
|
||||||
plot.draw()
|
plot.draw_lines()
|
||||||
|
plot.draw_bars()
|
||||||
title_text = font.render(title, True, (255, 255, 255))
|
title_text = font.render(title, True, (255, 255, 255))
|
||||||
title_rect = title_text.get_rect()
|
title_rect = title_text.get_rect()
|
||||||
title_rect.left = 25
|
title_rect.left = 25
|
||||||
|
|
Loading…
Reference in a new issue