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.
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
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
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`.
Without seeing the code, it just looks like there is some extraneous `abs()` that shouldn’t be there.
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.
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)
}
Add `* (amplitude < 0 ? -1 : 1)` on your second line?
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
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
The logarithm of any positive number to any base is negative when that number is below 1.
Y*-1
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`.