dmrschmidt / DSWaveformImage

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

Empty audio not displayed even with Should Draw Silence #71

Closed ShivamRawat0l closed 1 year ago

ShivamRawat0l commented 1 year ago

Screenshot 2023-06-02 at 7 03 50 PM

                    if(data.count > 0) {
                            WaveformLiveCanvas(samples: [],configuration: liveConfiguration,
                                       renderer: LinearWaveformRenderer(),
                                       shouldDrawSilencePadding: true)
                    }

                WaveformLiveCanvas(samples: [],configuration: liveConfiguration,
                                   renderer: LinearWaveformRenderer(),
                                   shouldDrawSilencePadding: true)

This is the code. Second one is working and giving drawing the silence. But for the first one it is waiting for the data to be fetched from the internet and then it is showing WaveformLiveCanvas. If I give some values in the first samples then it will display it correctly. Just not the silent part.

I am new to swift so might be missing something.

I also tried making the shouldDrawSilencePadding : state assign it to state and making the state false at first. Then setting state = true wont update it but state = !state will do after some clicks don't know why.

ShivamRawat0l commented 1 year ago

Current solution for me is to

@State var statename = false;

WaveformLiveCanvas(samples: [],configuration: liveConfiguration,
                                   renderer: LinearWaveformRenderer(),
                                   shouldDrawSilencePadding: statename).onAppear{  
                                   statename = true; 
                                   } 

Still this would give error inside a List {} theirfore switched to scrollview. Could have tried LazyVStack but currently scrollview is fine.

dmrschmidt commented 1 year ago

Hey @ShivamRawat0l,

I took a quick look at this myself and I can reproduce the behavior you describe.

I cannot yet exactly say why, but it does seem to be a bug in the state handling of WaveformLiveCanvas.

I’ll look into this further, but can’t promise when I may have found a fix for this.

So in the meantime your workaround will have to do. IMHO that looks like a perfectly fine and even simpler approach anyway, no?

Out of curiosity - not sure whether this is related to the library at all - what error are you getting with List?

ShivamRawat0l commented 1 year ago

Hey @ShivamRawat0l,

I took a quick look at this myself and I can reproduce the behavior you describe.

I cannot yet exactly say why, but it does seem to be a bug in the state handling of WaveformLiveCanvas.

I’ll look into this further, but can’t promise when I may have found a fix for this.

So in the meantime your workaround will have to do. IMHO that looks like a perfectly fine and even simpler approach anyway, no?

Out of curiosity - not sure whether this is related to the library at all - what error are you getting with List?

WIth

@State var statename = false;

WaveformLiveCanvas(samples: [],configuration: liveConfiguration,
                                   renderer: LinearWaveformRenderer(),
                                   shouldDrawSilencePadding: statename).onAppear{  
                                   statename = true; 
                                   }

I am putting statename to true inside onAppear. And in List onAppear and onDisappear are called regularly as we scroll through. So went with ScrollView only for the meantime.

dmrschmidt commented 1 year ago

Ah ok, I think I get what you mean.

In any case, I think I've found and fixed the issue. If I'm completely honest with you, I don't 100% understand why SwiftUI behaves the way it does here. Something about @StateObject that behaves different to what I'd expect.

Update to 13.0.1 and the issue should be fixed. Please let me know if it does / doesn't.

ShivamRawat0l commented 1 year ago

Ah ok, I think I get what you mean.

In any case, I think I've found and fixed the issue. If I'm completely honest with you, I don't 100% understand why SwiftUI behaves the way it does here. Something about @StateObject that behaves different to what I'd expect.

Update to 13.0.1 and the issue should be fixed. Please let me know if it does / doesn't.

Wow thanks that was quick. Ill check and update the ticket :)

ShivamRawat0l commented 1 year ago

Working Perfectly.

Thanks for the quick response.