Skip to content

Instantly share code, notes, and snippets.

@ssfrr
Last active January 11, 2018 17:38
Show Gist options
  • Save ssfrr/5e1660c726f47e141d53fcea619f4cca to your computer and use it in GitHub Desktop.
Save ssfrr/5e1660c726f47e141d53fcea619f4cca to your computer and use it in GitHub Desktop.
using Makie, GeometryTypes, Colors
using Reactive
using Juno
using DSP
using LibSndFile, FileIO
using PortAudio, SampledSignals
using BenchmarkTools
N = 1024 # size of audio read
N2 = N÷2+1 # size of rfft output
D = 200 # number of bins to display
M = 20 # number of lines to draw
src = PortAudioStream(1, 2, blocksize=N)
buf = Array{Float32}(N)
fftbuf = Array{Complex{Float32}}(N2)
magbuf = Array{Float32}(N2)
fftplan = plan_rfft(buf; flags=FFTW.EXHAUSTIVE)
scene = Scene(resolution=(500,500))
ax = axis(0:0.1:1, 0:0.1:1, 0:0.1:0.5)
center!(scene)
ls = map(1:M) do _
yoffset = to_node(to_value(scene[:time]))
offset = lift_node(scene[:time], yoffset) do t, yoff
Point3f0(0.0f0, (t-yoff)*4, 0.0f0)
end
l = lines(linspace(0,1,D), 0.0f0, zeros(Float32, D),
offset=offset, color=(:black, 0.1))
(yoffset, l)
end
while isopen(scene[:screen])
for (yoffset, line) in ls
isopen(scene[:screen]) || break
read!(src, buf)
A_mul_B!(fftbuf, fftplan, buf)
@. magbuf = log(clamp(abs(fftbuf), 0.0001, Inf))/10+1
line[:z] = magbuf[1:D]
push!(yoffset, to_value(scene[:time]))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment