dmrschmidt / DSWaveformImage

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

May I know the factors that influence the horizontal scrolling speed of the graph? #103

Closed yccheok closed 1 month ago

yccheok commented 1 month ago

Hi @dmrschmidt

My understanding is, the frequency of calling

waveformLiveView.add(sample: currentAmplitude)

Will determine how fast the graph scroll horizontally.

Hence, by looking at

https://github.com/dmrschmidt/DSWaveformImage/blob/main/Example/DSWaveformImageExample-iOS/SCAudioManager.m#L68

My understand is that, waveformLiveView.add is being called for every 10ms

I have implemented the same in my code using

private func startTimer() {
    if timer != nil {
        return
    }

    timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { [weak self] timer in
        guard let self = self else { return }

        self.updateWaveformLiveView()
    }
}

private func updateWaveformLiveView() {
    guard let currentAmplitude = delegate?.getCurrentAmplitude() else { return }

    waveformLiveView.add(sample: currentAmplitude)
}

However, my graph seems "moving" slower than your example : https://www.youtube.com/shorts/ZMXOlv4-fMc

Do you have any idea what else I should look into? I configure your library using

private func initWaveformLiveView() {
    waveformLiveView.configuration = waveformLiveView.configuration.with(
        style: .striped(.init(color: .black, width: 3, spacing: 3)),
        shouldAntialias: true
    )

    waveformLiveView.shouldDrawSilencePadding = true
}

Thank you for your wonderful library.

dmrschmidt commented 1 month ago

Hey @yccheok. So yeah, that's roughly correct what you summed up. The thing about timers is, that this 10ms is more of a loose suggestion at that low precision and not a hard guarantee. Apart from that, what also matters is the device's display pixel density, which is usually 3x these days. The next thing that kinda comes into play is how the time resolution of your .getCurrentAmplitude() is. It's unlikely to update every 10ms if you ask me, but that depends on a lot of factors I can't see from here.

If you look at the SwiftUI example you'll notice that there I'm simply adding the same value 3 times to speed up the graph. Simple workaround to speed it up.

I'm afraid that's the only suggestion I can provide to you right now if the graph isn't moving fast enough for you, without knowing all the implementation details. Which would then also just take the support I can provide here too deep.

yccheok commented 1 month ago

Thank you so much for your guidance and workaround. I truly appreciate it.