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..4b52b9539 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