RPi-Audio-Visualiser
De Wiki levelKro
Permet d'avoir un aperçu des bandes mono sur un écran OLED 0.96", mais peut être ajuster pour d'autres besoins.
Requirement
- Raspberry Pi (Zero v1.3)
- Écran OLED 0.91" I2C/SPI - https://www.waveshare.com/wiki/0.91inch_OLED_Module
- Capteur audio SPH0645 - https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout/raspberry-pi-wiring-test
- Raspbian OS Lite (buster)
Préparation
Installer le module audio et l'écran OLED selon le guide fournis par les liens ou selon la documentation de votre matériel.
Éditer le fichier de configuration (/boot/config.txt) comme ceci;
#Active l'I2C et SPI dtparam=i2c_arm=on #dtparam=i2s=on dtparam=spi=on #Désactive l'audio embarqué dtparam=audio=off #Booster le port I2C pour un rendu plus rapide dtparam=i2c_arm_baudrate=400000 #Le microphone dtoverlay=googlevoicehat-soundcard
Installer les pré-requis du système;
sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install -y \ autoconf automake libtool libfftw3-dev libasound2-dev \ libncursesw5-dev libpulse-dev libconfig-dev libi2c-dev \ i2c-tools python3-pip python3-pil python3-dev \ libfreetype6-dev libjpeg-dev build-essential git cd ~ git clone https://github.com/karlstav/cava.git cd cava ./autogen.sh ./configure make sudo make install pip3 install luma.oled
Ajuster Cava pour avoir les configs de base requis, éditer le fichier /home/pi/.config/cava/config ;
## Configuration file for CAVA. # Remove the ; to change parameters. [general] # Accepts only non-negative values. framerate = 25 autosens = 0 sensitivity = 5000 bars = 64 lower_cutoff_freq = 60 higher_cutoff_freq = 20000 [input] method = alsa source = hw:0,0 framerate = 25 buffer = 256 sample_bits = 8 [output] method = raw raw_target = /tmp/cava.fifo data_format = ascii channels = mono [smoothing] ;integral = 0 ;gravity = 100 ;noise_reduction = 50
Script de visualisation audio
Créer et insérer dans le fichier /home/pi/oledlive.py
import os import select from PIL import Image, ImageDraw from luma.core.interface.serial import i2c from luma.oled.device import ssd1306 WIDTH, HEIGHT = 128, 32 EXPECTED_BARS = 64 FIFO_PATH = "/tmp/cava.fifo" serial = i2c(port=1, address=0x3C) device = ssd1306(serial, width=WIDTH, height=HEIGHT) try: fifo_fd = os.open(FIFO_PATH, os.O_RDONLY | os.O_NONBLOCK) except FileNotFoundError: exit(1) buffer = "" bar_width = WIDTH // EXPECTED_BARS img = Image.new('1', (WIDTH, HEIGHT)) draw = ImageDraw.Draw(img) while True: rlist, _, _ = select.select([fifo_fd], [], []) if fifo_fd in rlist: try: data = os.read(fifo_fd, 2048) if not data: continue buffer += data.decode(errors='ignore') lines = buffer.split('\n') buffer = lines[-1] if buffer[-1] != '\n' else "" for line in lines[:-1]: vals = line.strip().split(';') if len(vals) < EXPECTED_BARS: continue # Parsing optimisé values = [] append = values.append for v in vals[:EXPECTED_BARS]: try: append(int(v)) except: append(0) # Dessin rapide draw.rectangle((0, 0, WIDTH, HEIGHT), fill=0) for i, val in enumerate(values): bar_height = max(1, val * HEIGHT // 200) x0 = i * bar_width x1 = x0 + bar_width - 1 draw.rectangle((x0, HEIGHT - bar_height, x1, HEIGHT), fill=255) device.display(img) except OSError: continue
Alléger le système
Ceci va ajuster le système pour donner plus de puissance, surtout avec un RPi Zero v1.
udo systemctl disable --now \ avahi-daemon \ bluetooth \ triggerhappy \ rsyslog \ systemd-timesyncd \ fake-hwclock sudo systemctl disable getty@tty2.service getty@tty3.service \ getty@tty4.service getty@tty5.service getty@tty6.service sudo dphys-swapfile swapoff sudo systemctl disable dphys-swapfile sudo apt purge --auto-remove \ triggerhappy \ logrotate \ dphys-swapfile \ rsyslog \ fake-hwclock \ manpages \ man-db \ avahi-daemon \ bluez \ sudo journalctl --vacuum-size=10M sudo sed -i 's/#Storage=auto/Storage=volatile/' /etc/systemd/journald.conf sudo systemctl restart systemd-journald
Pour désactiver le HDMI, éditer /etc/rc.local ;
/opt/vc/bin/tvservice -o exit 0