From 24e4307f1f2ae6623e707a47b70a4db21010e59f Mon Sep 17 00:00:00 2001 From: Ashwani Rathee Date: Sun, 23 May 2021 20:46:20 +0530 Subject: [PATCH 1/3] Initial Base --- src/MusicProcessing.jl | 2 +- src/display.jl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/MusicProcessing.jl b/src/MusicProcessing.jl index 8b9e90c..bab1948 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 Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" include("display.jl") end end # module diff --git a/src/display.jl b/src/display.jl index 049fbdd..96c134b 100644 --- a/src/display.jl +++ b/src/display.jl @@ -1,3 +1,36 @@ +export waveplot, specplot, test + +""" + test() +""" + +function test() + x = collect(1:10) + y = collect(1:10) + Plots.plot(x,y) +end + +""" + specplot(audio::Sample{T,N}, fs = 44100Hz) + + +""" +function specplot(audio::SampleBuf{T,N}, fs = 44100Hz) 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]") +end +""" + waveplot(audio::Sample{T,N}, fs = 44100Hz) + + +""" + +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 @@ -64,3 +97,4 @@ function Base.show(io::IO, mime::MIME"image/png", tfrs::Array{R, 1}) where {R <: end show(io, mime, PyPlot.gcf()) end + From 03fe7a2c719f09e0bdbe687c6caa44483e32262e Mon Sep 17 00:00:00 2001 From: Ashwani Rathee Date: Sun, 13 Jun 2021 19:51:51 +0530 Subject: [PATCH 2/3] Conversion to Makie --- src/MusicProcessing.jl | 2 +- src/display.jl | 87 ++++++------------------------------------ 2 files changed, 12 insertions(+), 77 deletions(-) diff --git a/src/MusicProcessing.jl b/src/MusicProcessing.jl index bab1948..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 Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" 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 96c134b..99f6690 100644 --- a/src/display.jl +++ b/src/display.jl @@ -1,3 +1,4 @@ +using .GLMakie export waveplot, specplot, test """ @@ -5,96 +6,30 @@ export waveplot, specplot, 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 \ No newline at end of file From f039b6d9b4e0327709cbc2d6bba309441b94cea2 Mon Sep 17 00:00:00 2001 From: Ashwani Rathee Date: Sun, 20 Jun 2021 15:09:20 +0530 Subject: [PATCH 3/3] Example --- src/display.jl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index 99f6690..9ebb828 100644 --- a/src/display.jl +++ b/src/display.jl @@ -16,6 +16,13 @@ end Draws spectrogram of an audio +# Example + +```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) @@ -28,8 +35,14 @@ end 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