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 1 commit
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 Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" 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
87 changes: 11 additions & 76 deletions src/display.jl
Original file line number Diff line number Diff line change
@@ -1,100 +1,35 @@
using .GLMakie
export waveplot, specplot, test

"""
test()
"""

function test()
x = collect(1:10)
y = collect(1:10)
Plots.plot(x,y)
x = range(0, 10, length=100)
y = sin.(x)
lines(x, y)
end

"""
specplot(audio::Sample{T,N}, fs = 44100Hz)

Draws spectrogram of an audio

"""
function specplot(audio::SampleBuf{T,N}, fs = 44100Hz) where {T,N}
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)
Plots.heatmap(spec.time, spec.freq, pow2db.(spec.power), xguide="Time [s]", yguide="Frequency [Hz]")
Makie.heatmap(spec.time, spec.freq, pow2db.(spec.power))
end
"""
waveplot(audio::Sample{T,N}, fs = 44100Hz)

Draws waveplot of a audio

"""

function waveplot(audio::SampleBuf{T,N}, fs = 44100Hz) where {T,N}
Plots.plot(0:1/fs:(length(mono(audio))-1)/fs, mono(audio))
end


# methods to translate

"""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)"
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)

PyPlot.pcolormesh(X, Y, Z)
PyPlot.xlim(first(X), last(X))
PyPlot.ylim(first(Y), last(Y))

(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())
end

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

@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