Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Plots #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/MusicProcessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export melspectrogram, mfcc
export spectrogram, stft, istft, phase_vocoder

function __init__()
@require PyPlot="d330b81b-6aea-500a-939a-2ce795aea3ee" include("display.jl")
@require GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" include("display.jl")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GLMakie -> Makie

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I use Makie instead of GLMakie?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay for now use GLMakie. We will resolve this later.

end

end # module
81 changes: 25 additions & 56 deletions src/display.jl
Original file line number Diff line number Diff line change
@@ -1,66 +1,35 @@
using .GLMakie
export waveplot, specplot, test

# methods to translate
"""
test()
"""

"""return the frequency ticks of a spectrogram, rounded to the nearest integers"""
function yticklabels(tfr::DSP.Periodograms.Spectrogram, yticks::Array)
map(Int, map(round, yticks)), "frequency (Hz)"
function test()
x = range(0, 10, length=100)
y = sin.(x)
lines(x, y)
end

"""return Hz values corresponding to given mel frequencies"""
function yticklabels(tfr::MelSpectrogram, yticks::Array)
map(Int, map(round, mel_to_hz(yticks))), "frequency (Hz)"
end

"""return MFCC coefficient numbers"""
function yticklabels(tfr::MFCC, yticks::Array)
map(Int, map(round, yticks)), "MFCC number"
end

heatmap(tfr::DSP.Periodograms.TFR) = log10(power(tfr))

function draw_heatmap(tfr::DSP.Periodograms.TFR)
X = time(tfr)
Y = freq(tfr)
Z = heatmap(tfr)
"""
specplot(audio::Sample{T,N}, fs = 44100Hz)

PyPlot.pcolormesh(X, Y, Z)
PyPlot.xlim(first(X), last(X))
PyPlot.ylim(first(Y), last(Y))
Draws spectrogram of an audio

(yticks, ylabel) = yticklabels(tfr, PyPlot.yticks()[1])
PyPlot.gca()[:set_yticklabels](yticks)
PyPlot.gca()[:spines]["top"][:set_visible](false)
PyPlot.gca()[:spines]["right"][:set_visible](false)

PyPlot.xlabel("time (seconds)")
PyPlot.ylabel(ylabel)
end

"""Display a spectrogram"""
function Base.show(io::IO, mime::MIME"image/png", tfr::R) where {R <: DSP.Periodograms.TFR}
@eval import PyPlot
PyPlot.ioff()
PyPlot.figure(figsize=(8, 4))
draw_heatmap(tfr)
show(io, mime, PyPlot.gcf())
"""
function specplot(audio::SampleBuf{T,N}, fs = 44100) where {T,N}
n = length(audio.data)
nw = n÷50
spec = spectrogram(mono(audio).data, nw, nw÷2; fs=fs)
Makie.heatmap(spec.time, spec.freq, pow2db.(spec.power))
end
"""
waveplot(audio::Sample{T,N}, fs = 44100Hz)

"""Display multichannel spectrogram"""
function Base.show(io::IO, mime::MIME"image/png", tfrs::Array{R, 1}) where {R <: DSP.Periodograms.TFR}
nchannels = length(tfrs)
Draws waveplot of a audio

@eval import PyPlot
PyPlot.ioff()
PyPlot.figure(figsize=(8, 8))
"""

for i = 1:nchannels
PyPlot.subplot(nchannels, 1, i)
draw_heatmap(tfrs[i])

if i != nchannels
PyPlot.gca()[:get_xaxis]()[:set_visible](false)
PyPlot.gca()[:spines]["bottom"][:set_visible](false)
end
end
show(io, mime, PyPlot.gcf())
end
function waveplot(audio::SampleBuf{T,N}, fs = 44100) where {T,N}
lines(0:1/fs:(length(mono(audio))-1)/fs, mono(audio))
end