Skip to content

Instantly share code, notes, and snippets.

@yurivish yurivish/windytan.jl
Created Dec 12, 2016

Embed
What would you like to do?
using LibSndFile, Images, Colors
# Load and parse the viridis color scheme
viridis_data = "44015444025645045745055946075a46085c460a5d460b5e470d60470e6147106347116447136548146748166848176948186a481a6c481b6d481c6e481d6f481f70482071482173482374482475482576482677482878482979472a7a472c7a472d7b472e7c472f7d46307e46327e46337f463480453581453781453882443983443a83443b84433d84433e85423f854240864241864142874144874045884046883f47883f48893e49893e4a893e4c8a3d4d8a3d4e8a3c4f8a3c508b3b518b3b528b3a538b3a548c39558c39568c38588c38598c375a8c375b8d365c8d365d8d355e8d355f8d34608d34618d33628d33638d32648e32658e31668e31678e31688e30698e306a8e2f6b8e2f6c8e2e6d8e2e6e8e2e6f8e2d708e2d718e2c718e2c728e2c738e2b748e2b758e2a768e2a778e2a788e29798e297a8e297b8e287c8e287d8e277e8e277f8e27808e26818e26828e26828e25838e25848e25858e24868e24878e23888e23898e238a8d228b8d228c8d228d8d218e8d218f8d21908d21918c20928c20928c20938c1f948c1f958b1f968b1f978b1f988b1f998a1f9a8a1e9b8a1e9c891e9d891f9e891f9f881fa0881fa1881fa1871fa28720a38620a48621a58521a68522a78522a88423a98324aa8325ab8225ac8226ad8127ad8128ae8029af7f2ab07f2cb17e2db27d2eb37c2fb47c31b57b32b67a34b67935b77937b87838b9773aba763bbb753dbc743fbc7340bd7242be7144bf7046c06f48c16e4ac16d4cc26c4ec36b50c46a52c56954c56856c66758c7655ac8645cc8635ec96260ca6063cb5f65cb5e67cc5c69cd5b6ccd5a6ece5870cf5773d05675d05477d1537ad1517cd2507fd34e81d34d84d44b86d54989d5488bd6468ed64590d74393d74195d84098d83e9bd93c9dd93ba0da39a2da37a5db36a8db34aadc32addc30b0dd2fb2dd2db5de2bb8de29bade28bddf26c0df25c2df23c5e021c8e020cae11fcde11dd0e11cd2e21bd5e21ad8e219dae319dde318dfe318e2e418e5e419e7e419eae51aece51befe51cf1e51df4e61ef6e620f8e621fbe723fde725"
codes = matchall(r".{6}", viridis_data)
colors = [matchall(r".{2}", code) for code in codes]
viridis = [RGB.(map(c -> parse(Int, c, 16)/255, c)...) for c in colors]
# Image functions
open(img::Image) = (save("out.png", img); run(`open out.png`))
img(data) = convert(Image{RGB}, map(v -> sample(viridis, v * length(viridis)), data'))
# Take just the left channels
load_sound(name) = load("/Users/yurivish/Dropbox/Projects/Julia Notebooks/$name")[:, 1]
square = load_sound("sqr_1000Hz_-6dBFS_3s.wav")[:, 1]
sine = load_sound("sin_1000Hz_-6dBFS_3s.wav")
white = load_sound("white_-6dBFS_3s.wav")
pink = load_sound("pink_-6dBFS_3s.wav")
freesound = load_sound("freesound.wav")
train = load_sound("370744__tybedour__train-sound.wav")
piano = load_sound("370745__erokia__piano-ambiance-2.wav")
ding = load_sound("ding.wav")
rescale(val, lo, hi) = (val .- lo) ./ (hi - lo)
rescale(val) = rescale(val, extrema(val)...)
function sample(arr, i)
pc, lo = modf(i)
lo < 1 && return first(arr)
lo == length(arr) && return last(arr)
(1 - pc) * arr[Int(lo)] + pc * arr[Int(lo) + 1] # linear interpolation
end
function bilinear_incr(arr, x, y)
xpc, xi = modf(x)
ypc, yi = modf(y)
xi = Int(xi)
yi = Int(yi)
arr[yi, xi ] += (1-xpc)*(1-ypc)
arr[yi+1, xi ] += (1-xpc)*( ypc)
arr[yi, xi+1] += ( xpc)*(1-ypc)
arr[yi+1, xi+1] += ( xpc)*( ypc)
end
sound = convert(Array{Float64}, train);
arr = zeros(Float64, 1000, 3000)
Y, X = map(eltype(arr), size(arr))
lo, hi = 1.2 * collect(extrema(sound)) # 1.2 works best for train, and 0.3 for ding
len = Float64(length(sound))
supersample = 20
for i in 1:1/supersample:length(sound)
val = sample(sound, i)
# hacky clamping
y = clamp(Y * rescale(val, lo, hi), 1, Y - 1)
x = clamp(X * Float64(i)/len, 1, X - 1)
bilinear_incr(arr, x, y)
end
out = img(rescale(arr).^(1/4))
open(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.