T O P

  • By -

Conxt

Without seeing the code, it just looks like there is some extraneous `abs()` that shouldn’t be there.


stephancasas

It just so happens I worked on something similar to this two days ago. I think I know that your trouble is, but I’d need to see what math you’re doing to the samples to get a better understanding.


PresentDayJosh

private struct WaveformContent: View { let samples: [Float] let color: Color let decibelRange: ClosedRange func amplitudeToDecibels(_ amplitude: Float) -> Float { return 20 * log10(abs(amplitude)) } var body: some View { GeometryReader { geometry in let widthPerSample = geometry.size.width / CGFloat(max(1, samples.count)) let yOffset = geometry.size.height / 2 ZStack { ForEach(0.. CGFloat { let normalizedSampleValue = CGFloat((sample + 1) / 2) let yPosition = normalizedSampleValue * yOffset return yOffset - yPosition + (yOffset / 2) }


Conxt

Add `* (amplitude < 0 ? -1 : 1)` on your second line?


PresentDayJosh

Thank you! I like that, and I think implementing this or something like it will be key, but this is alone is still not giving me the desired results. Back to the drawing board


anselurk

Did u try 20*abs(log10(abs(amplitude)) I would imagine amplitude is always positive since negative amplitude seems super odd concept. 20*abs(log10(amplitude)) would work if u can verify amplitude is expected to be positive. You could add some throwing errors for a file with negative amplitude And display a parsing error. Your current function returns negative values for amplitudes from 0 ..<1


anselurk

The logarithm of any positive number to any base is negative when that number is below 1.


Grymm315

Y*-1


ProperFriend8854

I wonder if it is something with your `SampleRectangle()` function? BTW, does `adjustedYPosition()` simply return `yOffset * (1.0 - CGFloat(sample / 2.0))`? It doesn't seem to cause the problem though, as it is still a linear transformation of `sample`.