diff --git a/src/MusicProcessing.jl b/src/MusicProcessing.jl index 8b9e90c..f19b970 100644 --- a/src/MusicProcessing.jl +++ b/src/MusicProcessing.jl @@ -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") end end # module diff --git a/src/display.jl b/src/display.jl index 049fbdd..9ebb828 100644 --- a/src/display.jl +++ b/src/display.jl @@ -1,66 +1,48 @@ +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) - - 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) +""" + specplot(audio::Sample{T,N}, fs = 44100Hz) - @eval import PyPlot - PyPlot.ioff() - PyPlot.figure(figsize=(8, 8)) +Draws spectrogram of an audio - for i = 1:nchannels - PyPlot.subplot(nchannels, 1, i) - draw_heatmap(tfrs[i]) +# Example - if i != nchannels - PyPlot.gca()[:get_xaxis]()[:set_visible](false) - PyPlot.gca()[:spines]["bottom"][:set_visible](false) - end - end - show(io, mime, PyPlot.gcf()) +```julia +using GLMakie +audio_one_channel = SampleBuf(rand(1000), 10) +specplot(audio_one_channel,22100) +``` +""" +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) + +Draws waveplot of a audio + +# Example + +```julia +using GLMakie +audio_one_channel = SampleBuf(rand(1000), 10) +waveplot(audio_one_channel,48000) +``` +""" +function waveplot(audio::SampleBuf{T,N}, fs = 44100) where {T,N} + lines(0:1/fs:(length(mono(audio))-1)/fs, mono(audio)) +end \ No newline at end of file