led矩阵上的音频可视化器

时间:2014-06-27 15:00:00

标签: python audio aubio

我需要播放一个音频文件,并将其内容放在均衡器方面的8x8矩阵上,就像在Piccolo中完成的,就像适用于BeagleBone或RaspberryPI的频谱分析器一样。这不需要麦克风的环境分析:只需在同一块板上播放音乐时进行可视化。

Adafruit创建了一个可以轻松控制LED矩阵的库,缺少的主要是音频分析,直到每个音频块的矩阵。

语言可能是C或C ++,但如果它在Python代码中则最好。为此,有Timesideaubio这样的好库,但我找不到如何填充LED矩阵和Piccolo一样,尽管我已经测试了一些例子。

1 个答案:

答案 0 :(得分:2)

获得粗略的8波段,8级正在进行的频谱估计(在Python中,使用numpy):

import numpy as np

fftsize = 4096  # about 100ms at 44 kHz; each bin will be ~ 10 Hz
# Band edges to define 8 octave-wide ranges in the FFT output
binedges = [8, 16, 32, 64, 128, 256, 512, 1024, 2048]
nbins = len(binedges)-1
# offsets to get our 48 dB range onto something useful, per band
offsets = [4, 4, 4, 4, 6, 8, 10, 12]
# largest value in ledval
nleds = 8
# scaling of LEDs per doubling in amplitude
ledsPerDoubling = 1.0
# initial value of per-band energy history
binval = 0.001 * np.ones(nbins, np.float)
newbinval = np.zeros(nbins, np.float)
# How rapidly the displays decay after a peak (depends on how often we're called)
decayConst = 0.9

if not_done:
    # somehow tap into the most recent 30-100ms of audio.  
    # Assume we get 44 kHz mono back
    waveform = get_latest_waveform()
    # find spectrum
    spectrum = np.abs(np.fft.rfft(waveform[:fftsize]))
    # gather into octave bands
    for i in range(nbins-1):
        newbinval[i] = np.mean(spectrum[binedges[i]:binedges[i+1]])
    # Peak smoothing - decay slowly after large values
    binval = np.maximum(newbinval, decayConst*binval)
    # Quantize into values 0..8 as the number of leds to light in each column
    ledval = np.round(np.maximum(0, np.minimum(nleds, 
                                               ledsPerDoubling * np.log2(binval) 
                                               + offsets)))
    # Now illuminate ledval[i] LEDs in column i (0..7) ...

除了抓住最新的(4096点)波形外,这应该可以为您提供这个想法。