ledstick/python/sim.py
Patrick Moessler fc4005cc49 v2
2025-02-24 00:29:15 +01:00

79 lines
1.7 KiB
Python

from pathlib import Path
from typing import Generator, Iterable
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.artist import Artist
import numpy as np
import numpy.typing as npt
import soundfile
SAMPLE_TYPE = np.int16
SAMPLE_COUNT = 256
def mic_sim_file(
filename: Path, start: float = 0, duration: float = 60
) -> Generator[npt.NDArray[SAMPLE_TYPE]]:
sf = soundfile.SoundFile(file=filename)
pos = start * sf.samplerate
sf.seek(pos)
yield from sf.blocks(
blocksize=SAMPLE_COUNT, dtype="int16", frames=duration * sf.samplerate
)
class Sim:
"""Simulator"""
def __init__(self) -> None:
self.mic = mic_sim_file(Path("24000_s16.wav"), 115)
self.x1 = np.arange(SAMPLE_COUNT)
self.y1 = np.zeros(SAMPLE_COUNT)
self.x2 = np.arange(SAMPLE_COUNT)
self.y2 = np.zeros(SAMPLE_COUNT)
self.fig, self.ax = plt.subplots(2)
self.graph1 = self.ax.plot(self.x1, self.y1)[0]
self.graph2 = self.ax.plot(self.x2, self.y2)[0]
plt.ylim(-32767, 32768)
np.set_printoptions(suppress=True, precision=2)
self.anim = FuncAnimation(
self.fig,
self.update,
frames=None,
cache_frame_data=False,
interval=64 / 24000,
)
plt.show()
def process(self)->None:
np.fft.
def update(self, frame: npt.NDArray[SAMPLE_TYPE]) -> Iterable[Artist]:
try:
values = next(self.mic)
except StopIteration:
return []
self.y1[:] = values[:SAMPLE_COUNT]
self.graph1.set_ydata(self.y1)
return [self.graph1]
if __name__ == "__main__":
sim = Sim()