github.com/noriah/catnip@v1.8.5/CatnipJulia/src/run_catnip.jl (about) 1 using Colors 2 using DataStructures 3 using GLMakie 4 5 set_theme!(theme_black()) 6 7 GLMakie.activate!(; 8 vsync=false, 9 framerate=60.0, 10 float=false, 11 pause_renderloop=false, 12 focus_on_show=false, 13 decorated=true, 14 title="catnip" 15 ) 16 17 function run_catnip(; timeout=false) 18 println("Start") 19 # 6 seconds at 60 samples per second out of catnip 20 numSets = 300 21 # numSets = 120 22 23 fig = Figure(fontsize=14; size=(740, 976)) 24 ax1 = Axis3(fig[1, 1]; aspect=(1.5, 1.00, 0.25), elevation=(2 * pi) / 4, perspectiveness=0.25, azimuth=0 * pi) 25 26 hidedecorations!(ax1) 27 hidespines!(ax1) 28 29 println("Allocate") 30 31 data = DataStructures.CircularBuffer{Vector{Float64}}(numSets) 32 33 push!(data, [0.0, 0.0]) 34 push!(data, [0.0, 0.0]) 35 36 z = Observable(mapreduce(permutedims, vcat, data)) 37 38 mSize = @lift(Vec3f.(1, 1, $z[:])) 39 40 zZ = @lift(0 * $z) 41 42 sets = @lift(size($z, 1)) 43 bars = @lift(size($z, 2)) 44 45 x = @lift(range(1, $sets, $sets)) 46 y = @lift(range(1, $bars, $bars)) 47 48 idx = Observable(1) 49 50 function makeColorMap(s, b, x) 51 mat = zeros(s, b) 52 53 for i = 0:s-1 54 # v = 1 - min(0.8, i / s) 55 v = 1.0 - (i / s) 56 # v = i / s 57 58 for j = 0:b-1 59 mat[i+(j*s)+1] = v 60 end 61 end 62 63 for j = 0:b-1 64 mat[x+(j*s)] = 0.0 65 end 66 67 return mat 68 end 69 70 function makeColorMap2(data, sets, bars, idx) 71 d = data[:] 72 73 for n in eachindex(d) 74 d[n] > 100.0 && (d[n] = 100.0) 75 # d[n] < 1.0 && (d[n] = 100.0) 76 end 77 78 # for j = 0:bars-1 79 # d[idx+(j*sets)] = 100.0 80 # end 81 82 for j = 0:bars-1 83 d[1+(j*sets)] = 100.0 84 # d[idx+(j*sets)] = 100.0 85 end 86 87 d[idx] = 0.0 88 d[idx+((bars-1)*sets)] = 100.0 89 90 return d 91 end 92 93 zC = @lift($z[:]) 94 95 staticColorMap = @lift(makeColorMap2($z, $sets, $bars, $idx)) 96 97 98 function updateZ() 99 z[] = mapreduce(permutedims, vcat, data) 100 end 101 102 rectMesh = Rect3f(Vec3f(-0.5, -0.5, 0), Vec3f(1, 1, 1)) 103 104 display(fig) 105 106 mymagma = GLMakie.to_colormap(:magma) 107 # mymagma = GLMakie.to_colormap(:BuPu_9) 108 mymagma[1] = RGBA(0.0, 0.0, 0.0, 0.0) 109 110 command = `catnip -d spotify -r 122880 -n 2048 -sm 4 -sas 6 -sf 45 -i -nw -nwb 82` 111 # command = `catnip -d "Google Chrome" -r 122880 -n 2048 -sas 6 -sf 45 -i -nw -nwb 60` 112 #command = `go run ./cmd/catnip -d spotify -r 122880 -n 2048 -sas 5 -sf 45 -i -nw -nwb 50` 113 114 115 try 116 println("Open StdOut") 117 open(command, "r", stdout) do io 118 count = 0 119 println("Loop") 120 while count < numSets + 2 121 count += 1 122 123 line = readline(io) 124 line = strip(line) 125 elms = split(line, " ") 126 elms = [elm for elm in elms if !isempty(elm)] 127 nums = map(x -> parse(Float64, strip(x)), elms) 128 #println(nums) 129 pushfirst!(data, reverse(nums)) 130 end 131 132 updateZ() 133 134 limits!(ax1, 0, sets[], 0, bars[], 0, 100) 135 136 meshscatter!(ax1, x, y, zZ; marker=rectMesh, color=staticColorMap, 137 markersize=mSize, colormap=mymagma, 138 # markersize=mSize, colormap=:plasma, 139 shading=MultiLightShading) 140 141 idx[] = numSets 142 143 144 #while !eof(io) && (count < numSets + 4 || !timeout) 145 while !eof(io) && !timeout 146 count += 1 147 if count >= numSets 148 count = 0 149 end 150 151 line = readline(io) 152 line = strip(line) 153 elms = split(line, " ") 154 elms = [elm for elm in elms if !isempty(elm)] 155 nums = map(x -> parse(Float64, strip(x)), elms) 156 #nums = reverse(nums) 157 158 # idx[] = numSets - count 159 #println(nums) 160 161 # data[idx[]] = nums 162 163 pushfirst!(data, nums) 164 165 updateZ() 166 end 167 end 168 catch e 169 @show e 170 end 171 end 172 173 run_catnip()