dmrschmidt / DSWaveformImage

Generate waveform images from audio files on iOS, macOS & visionOS in Swift. Native SwiftUI & UIKit views.
MIT License
1.04k stars 113 forks source link

Is there a way to cancel waveform image rendering process? #34

Closed kartuzovmax closed 2 years ago

kartuzovmax commented 2 years ago

I have a scenario in my app, where user may select between multiple audios which may be 5-10 min long. Usually, it takes quite some time to render one waveform out. In case when one waveform is being rendered and user taps on the new audio, I want to stop the rendering process of the first waveform, so I can start rendering a waveform image of the new audio.

How can I stop the initial rendering process?

dmrschmidt commented 2 years ago

Hmm very good question. As it stands, I don't see a straightforward way to achieve this unfortunately.

The analysis - which is the expensive part - is currently not implemented as an NSOperation. That would be the ideal approach to make these cancellable I'd say spontaneously. This would require a non-trivial (although relatively straightforward) amount of refactoring. To work properly it would also require to be implemented as part of the library itself, so just wrapping NSOperation outside the lib as it is right now wouldn't help.

I'll add this on my to do list, but don't think this will be done anytime soon I'm afraid.

Coincidentally, there is a fork out there that I've noticed a while ago: https://github.com/iruirc/DSWaveformImage which - among quite a few other unrelated things - implements an NSOperation approach. So one alternative approach for you might be to create your own fork and do something similar.

Sorry I don't have a better answer than this right now :/

(I'd of course be more than happy to merge changes in that direction if you'd end up forking and implementing these).

kartuzovmax commented 2 years ago

Thank you for your answer! :)