Created
January 11, 2018 18:33
-
-
Save ssfrr/89f6e4a0de6eec1828901fffdfbd286a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Makie, GeometryTypes | |
using PortAudio | |
N = 1024 # size of audio read | |
N2 = N÷2+1 # size of rfft output | |
D = 200 # number of bins to display | |
M = 200 # amount of history to keep | |
src = PortAudioStream(1, 2, blocksize=N) | |
buf = Array{Float32}(N) | |
fftbuf = Array{Complex{Float32}}(N2) | |
dispbuf = zeros(Float32, D, M) | |
fftplan = plan_rfft(buf; flags=FFTW.EXHAUSTIVE) | |
""" | |
Slide the values in the given matrix to the right by 1. | |
The rightmosts column is discarded and the leftmost column is | |
left alone. | |
""" | |
function shift1!(buf::AbstractMatrix) | |
for col in size(buf,2):-1:2 | |
@. buf[:, col] = buf[:, col-1] | |
end | |
end | |
""" | |
Read a block of audio, FFT it, and write it to the beginning of the buffer | |
""" | |
function readtobuf() | |
read!(src, buf) | |
A_mul_B!(fftbuf, fftplan, buf) | |
shift1!(dispbuf) | |
@. dispbuf[end:-1:1,1] = log(clamp(abs(fftbuf[1:D]), 0.0001, Inf)) | |
end | |
scene = Scene(resolution=(500,500)) | |
#pre-fill the display buffer so we can do a reasonable colormap | |
for _ in 1:M | |
readtobuf() | |
end | |
hm = heatmap(dispbuf) | |
center!(scene) | |
while isopen(scene[:screen]) | |
readtobuf() | |
hm[:heatmap] = dispbuf | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment