Dear community,
I would like to code a page where a sound file is displayed as a spectrogram (a heatmap based on a fast fourier transform - this part I have working) and then a user selects a region in that heatmap (i.e. a time- and frequency range) and audio of only that part is played back.
My question is:
can the html.Audio element play back programmatically generated audio (which can be saved to a file if it makes life easier) triggered by an event such as a selection of a region in a heatmap?
A quick search of this forum suggested to me that nobody has so far succeeded in actually getting html.Audio to do something like this, but I am a noob to Dash, so I thought I’d ask directly.
I had to make some modifications to your code to make it work, but this helped me a lot.
For anyone else looking for something similar, below is my code (full disclosure: it’s a mash-up of your code above and some ideas I got from the Microsoft Bing Copilot), here are a few things I found out:
You need the app.clientside_callback part to start the audio playing. Most code examples I saw just use audioElement.play(); but I found that audioElement.load(); with autoPlay=True set in the html.Audio element works more reliably.
I found that the sound file name needs to be different if the sound changes. Just overwriting the same mySound.wav file did not reliably get the new sound played - presumably, this is due to caching at the browser level and can somehow be overcome (if someone knows how, please comment below!).
So in a scenario where theoretically a very large/infinite number of distinct sounds can be generated, I’ll probably need to think about mechanisms that a) ensure unique names, and b) cleans up older/unused ones.