forked from jheinen/GR.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectogram.jl
47 lines (37 loc) · 918 Bytes
/
spectogram.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import PortAudio, LibSndFile, FileIO
using GR
using FFTW
function play(c::Channel)
data = FileIO.load("Monty_Python.wav")
stream = PortAudio.PortAudioStream(0, 1)
spectrum = zeros(Int32, 300, 225)
offset = 1
while offset + 1024 < length(data)
amplitudes = data[offset:offset+1024]
offset += 1024
PortAudio.write(stream, amplitudes)
power = log.(abs.(fft(float(amplitudes))) .+ 1) * 50
spectrum[1, :] = round.(Int, power[1:225])
spectrum = circshift(spectrum, [-1, 0])
put!(c, spectrum')
end
put!(c, nothing)
end
function main()
c = Channel(0)
task = @task play(c::Channel)
bind(c, task)
schedule(task)
start = time_ns()
while isopen(c)
spectrum = take!(c)
if spectrum == nothing
break
end
if time_ns() - start > 20 * 1000000 # 20ms
imshow(spectrum, colormap=-13, yflip=true)
start = time_ns()
end
end
end
main()