mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Update Audio FFT example.
This commit is contained in:
parent
da6a8df6be
commit
7e2df49c5b
@ -1,22 +1,24 @@
|
|||||||
import image, audio, time, array, math, ulab as np
|
import image, audio, time
|
||||||
from ulab import extras, numerical
|
from ulab import numpy as np
|
||||||
|
from ulab import scipy as sp
|
||||||
|
|
||||||
SIZE = 512//4
|
SIZE = 512//4
|
||||||
pcm_buf = None
|
raw_buf = None
|
||||||
fb = image.Image(SIZE+50, SIZE, image.RGB565, copy_to_fb=True)
|
fb = image.Image(SIZE+50, SIZE, image.RGB565, copy_to_fb=True)
|
||||||
audio.init(channels=2, frequency=16000, gain=24, highpass=0.9883)
|
audio.init(channels=2, frequency=16000, gain=24, highpass=0.9883)
|
||||||
|
|
||||||
def audio_callback(buf):
|
def audio_callback(buf):
|
||||||
global pcm_buf
|
# NOTE: do Not call any function that allocates memory.
|
||||||
if (pcm_buf == None):
|
global raw_buf
|
||||||
pcm_buf = np.array(array.array('h', buf))
|
if (raw_buf == None):
|
||||||
|
raw_buf = buf
|
||||||
|
|
||||||
# Start audio streaming
|
# Start audio streaming
|
||||||
audio.start_streaming(audio_callback)
|
audio.start_streaming(audio_callback)
|
||||||
|
|
||||||
def draw_fft(img, fft_buf):
|
def draw_fft(img, fft_buf):
|
||||||
fft_buf = (fft_buf / max(fft_buf)) * SIZE
|
fft_buf = (fft_buf / max(fft_buf)) * SIZE
|
||||||
fft_buf = np.vector.log10(fft_buf + 1) * 20
|
fft_buf = np.log10(fft_buf + 1) * 20
|
||||||
color = (0xFF, 0x0F, 0x00)
|
color = (0xFF, 0x0F, 0x00)
|
||||||
for i in range(0, SIZE):
|
for i in range(0, SIZE):
|
||||||
img.draw_line(i, SIZE, i, SIZE-int(fft_buf[i]), color, 1)
|
img.draw_line(i, SIZE, i, SIZE-int(fft_buf[i]), color, 1)
|
||||||
@ -29,12 +31,13 @@ def draw_audio_bar(img, level, offset):
|
|||||||
fb.draw_rectangle(SIZE+offset, SIZE - ((i+1)*blk_size) + blk_space, 20, blk_size - blk_space, color, 1, True)
|
fb.draw_rectangle(SIZE+offset, SIZE - ((i+1)*blk_size) + blk_space, 20, blk_size - blk_space, color, 1, True)
|
||||||
|
|
||||||
while (True):
|
while (True):
|
||||||
if (pcm_buf != None):
|
if (raw_buf):
|
||||||
|
pcm_buf = np.frombuffer(raw_buf, dtype=np.int16)
|
||||||
|
raw_buf = None
|
||||||
|
fft_buf = sp.signal.spectrogram(pcm_buf[0::2])
|
||||||
|
l_lvl = int((np.mean(abs(pcm_buf[1::2])) / 32768)*100)
|
||||||
|
r_lvl = int((np.mean(abs(pcm_buf[0::2])) / 32768)*100)
|
||||||
fb.clear()
|
fb.clear()
|
||||||
fft_buf = extras.spectrogram(pcm_buf[0::2])
|
|
||||||
l_lvl = int((numerical.mean(abs(pcm_buf[1::2])) / 32768)*100)
|
|
||||||
r_lvl = int((numerical.mean(abs(pcm_buf[0::2])) / 32768)*100)
|
|
||||||
pcm_buf = None
|
|
||||||
draw_fft(fb, fft_buf)
|
draw_fft(fb, fft_buf)
|
||||||
draw_audio_bar(fb, l_lvl, 0)
|
draw_audio_bar(fb, l_lvl, 0)
|
||||||
draw_audio_bar(fb, r_lvl, 25)
|
draw_audio_bar(fb, r_lvl, 25)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user