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

Amplitudes should be halved to stay within available height #57

Closed leberwurstsaft closed 1 year ago

leberwurstsaft commented 1 year ago

Hello,

if I understand correctly, all samples between 0 and 1 should be rendered within the available height and not be cut off. I found that they are cut off though and was wondering what's going on. Trying with more and more extreme values for verticalScalingFactor I suspected that the scaling isn't working correctly.

I think that the drawingAmplitude should be divided by 2 to respect the fact that it's drawn mirrored and pointing both up and down from the vertical middle of the view. Currently, a sample of value 0 would result in a line that stretches outside of the view by 50% on both sides.

https://github.com/dmrschmidt/DSWaveformImage/blob/73d69a3e4f8b3988494c4cdd2e270baaea183ec0/Sources/DSWaveformImage/Renderers/LinearWaveformRenderer.swift#L69

dmrschmidt commented 1 year ago

Yes, that is correct. This is in order to have a consistent frame of reference when changing the waves position. It always uses the full height, so when using the centered position, the scaling factor needs to be 0.5

This has come up as confusing for others prior as well, so I do acknowledge this isn’t ideal. I am considering removing the option to change the position and move that „up the view hierarchy“. This would be a breaking change however and it currently works as designed, so umhabend gotten to that yet. I may mix it with an upcoming release that restructures the rendering pipeline and adds circular drawing styles.

leberwurstsaft commented 1 year ago

Thanks, that's how I'm doing it now. Otherwise the library works great so far. Thanks so much for releasing it! 💯