Spectrogram: async load using webworker
See original GitHub issueThank you for this amazing piece of software!
I have noticed that plugins load on the main thread, so a heavy cpu bound plugin like the spectrogram
here:
WaveSurfer.spectrogram.create({
container: '#wave-spectrogram',
labels: true,
colorMap: colorMap
})
will freeze the UI running on the main thread while calculating the FFT
etc. It would be possibile to run it in background like using a worker thread?
My use case is when using a generated colorMap
, annotations
like this:
WaveSurfer.util
.fetchFile({ url: 'data/hot-colormap.json', responseType: 'json' })
.on('success', colorMap => {
initAndLoadSpectrogram(colorMap);
});
where the initAndLoadSpectrogram
will do something like
var plugins = [
WaveSurfer.regions.create(
{
/*regions: [
{ "id": 1, "color": "red", "start": 2.96, "end": 3.5, "data": {}, "attributes": {"label": "stars", "highlight":true}}
]*/
}
),
WaveSurfer.minimap.create({
height: 100,
waveColor: '#ddd',
progressColor: '#999',
cursorColor: '#999'
}),
WaveSurfer.timeline.create({
container: '#wave-timeline'
})
]
if (colorMap) {
plugins.push(WaveSurfer.spectrogram.create({
container: '#wave-spectrogram',
labels: true,
colorMap: colorMap
}))
};
// create waveform
wavesurfer = WaveSurfer.create({
container: document.querySelector('#waveform'),
waveColor: '#D9DCFF',
progressColor: '#4353FF',
cursorColor: '#4353FF',
normalize: true,
//LP: danger higher values will crash the computer
pixelRatio: 1,
//LP: this depends on the waveform json...
minPxPerSec: 100,
scrollParent: true,
//// bar visualization
barWidth: 3,
barRadius: 3,
barGap: 1,
barHeight: 1,
//// bar visualization
hideScrollbar: true,
cursorWidth: 1,
height: 300,
plugins: plugins
});
and then the loading part like
wavesurfer.util
.fetchFile({
responseType: 'json',
url: `http://localhost:3000/waveform/${file}`
})
.on('success', function (data) {
var points = data.data || data;
wavesurfer.load(
`http://localhost:3000/audio/${file}`,
points
);
wavesurfer.seekTo(0);
});
and then I load the regions on the ready
event:
wavesurfer.on('ready', function () {
wavesurfer.util
.ajax({
responseType: 'json',
url: `data/${region}.json`
})
.on('success', function (data) {
loadRegions(data);
saveRegions();
});
//...
etc.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:7
Top Results From Across the Web
Using async functions in a web worker results in typeError
If I remove the async keyword from the function definition then all is fine. Are async/await supported for webworker with webpack?
Read more >How to use Web Workers to schedule consistent ...
by Danny Mcwaves How to use Web Workers to schedule consistent asynchronous tasks in JavaScript With the continuous improvements being made ...
Read more >Building an Async React Renderer with Diffing in Web Worker
When you interact with the page after page load, Synchronous Rendering makes the website freeze (unresponsive) for 20s.
Read more >Using Web Workers - Web APIs - MDN Web Docs
The worker thread can perform tasks without interfering with the user ... shared workers cannot be shared between documents loaded in ...
Read more >Optimized media loading using the Web Workers API
We can use promises to load images asynchronously alone with the Image() ... the array of image URLs to the web worker through...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
sounds good! Pull requests are welcome.
https://github.com/katspaugh/wavesurfer.js/pull/2127 (v4.3.0) brings a big performance boost for the spectrogram plugin.