From 2b9bb14e9f77311c64df97ebcf45a563c53fea4a Mon Sep 17 00:00:00 2001 From: Jonathon Menz Date: Tue, 17 Dec 2024 16:43:37 -0800 Subject: [PATCH 1/2] Fix(Spectrogram): fix cropping and scaling issues --- examples/spectrogram.js | 7 ++++- src/plugins/spectrogram.ts | 61 +++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/examples/spectrogram.js b/examples/spectrogram.js index 20af90fa9..36defea1d 100644 --- a/examples/spectrogram.js +++ b/examples/spectrogram.js @@ -9,7 +9,7 @@ const ws = WaveSurfer.create({ waveColor: 'rgb(200, 0, 200)', progressColor: 'rgb(100, 0, 100)', url: '/examples/audio/audio.wav', - sampleRate: 22050, + sampleRate: 44100, }) // Initialize the Spectrogram plugin @@ -18,6 +18,11 @@ ws.registerPlugin( labels: true, height: 200, splitChannels: true, + scale: 'mel', // or 'linear' + frequencyMax: 8000, + frequencyMin: 0, + fftSamples: 1024, + labelsBackground: 'rgba(0, 0, 0, 0.1)', }), ) diff --git a/src/plugins/spectrogram.ts b/src/plugins/spectrogram.ts index c6c0556e0..70d04efcd 100644 --- a/src/plugins/spectrogram.ts +++ b/src/plugins/spectrogram.ts @@ -247,7 +247,7 @@ export type SpectrogramPluginOptions = { alpha?: number /** Min frequency to scale spectrogram. */ frequencyMin?: number - /** Max frequency to scale spectrogram. Set this to samplerate/2 to draw whole range of spectrogram. */ + /** Max frequency to scale spectrogram. Set this to samplerate/4 to draw whole range of spectrogram. */ frequencyMax?: number /** * Based on: https://manual.audacityteam.org/man/spectrogram_settings.html @@ -336,7 +336,8 @@ class SpectrogramPlugin extends BasePlugin { - spectrCc.drawImage( - renderer, - 0, - height * (1 - freqMax / freqFrom), // source x, y - width, - (height * (freqMax - freqMin)) / freqFrom, // source width, height - 0, - height * c, // destination x, y - width, - height, // destination width, height - ) + createImageBitmap( + imageData, + 0, + Math.round(bitmapHeight - bitmapHeight * (freqMax / freqFrom)) / 2, + width, + Math.round(bitmapHeight * ((freqMax - freqMin) / freqFrom)), + ).then((bitmap) => { + spectrCc.drawImage(bitmap, 0, height * c, width, height * 2) }) } @@ -562,11 +552,15 @@ class SpectrogramPlugin extends BasePlugin Date: Fri, 20 Dec 2024 16:42:26 -0800 Subject: [PATCH 2/2] Fix(Spectrogram): fix mel scale labels Credit: Lucas Theis https://github.com/lucastheis --- src/plugins/spectrogram.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/plugins/spectrogram.ts b/src/plugins/spectrogram.ts index 70d04efcd..4b52b9539 100644 --- a/src/plugins/spectrogram.ts +++ b/src/plugins/spectrogram.ts @@ -674,22 +674,23 @@ class SpectrogramPlugin extends BasePlugin