Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight Samples not aligned to base waveform #101

Closed
ogrydziak opened this issue Jul 19, 2017 · 4 comments
Closed

Highlight Samples not aligned to base waveform #101

ogrydziak opened this issue Jul 19, 2017 · 4 comments

Comments

@ogrydziak
Copy link

Defined
@IBOutlet weak var grabberContainer_view: FDWaveformView!

In viewdidload

        grabberContainer_view.delegate = self
        grabberContainer_view.audioURL = uurl
        grabberContainer_view.doesAllowScrubbing = false
        grabberContainer_view.doesAllowStretch = false
        grabberContainer_view.doesAllowScroll = false
        grabberContainer_view.backgroundColor = color02

I have the following code attached to a button

            grabberContainer_view.highlightedSamples = grabberContainer_view.totalSamples / 4 ..< grabberContainer_view.totalSamples / 2
            grabberContainer_view.layoutIfNeeded()

http://imgur.com/WCUcETd

What happening in that pic is the full waveform in the light grey, and the result from setting highlightSamples is overlaid in the middle. To me, it looks like the overlay is an incorrect chunk of the waveform (notice that it doesn’t line up). In this SS, I had changed the colors but I just reverted it to the original to make sure I didn’t muss something up inadvertently. Also, I would just be not using this correctly, so I wouldn’t rule that out.

When I changed it to alter the bounds in the setter for highlightedSamples, everything lined up, but then I couldn’t rotate the screen properly.

This may or may not illustrate it better (attached image).

image

@jonandersen
Copy link
Contributor

I've noticed this as well. Looking through the code I see layoutsubviews has a few lines that doesn't seem to calculate the correct values:

if let cachedSampleRange = cachedWaveformRenderOperation?.sampleRange, !cachedSampleRange.isEmpty && !zoomSamples.isEmpty {
          scaledX = CGFloat(cachedSampleRange.lowerBound - zoomSamples.lowerBound) / CGFloat(zoomSamples.count)
          scaledWidth = CGFloat(cachedSampleRange.last! - zoomSamples.lowerBound) / CGFloat(zoomSamples.count)    // forced unwrap is safe
          scaledHighlightedX = CGFloat((highlightedSamples?.lowerBound ?? 0) - zoomSamples.lowerBound) / CGFloat(zoomSamples.count)
          scaledHighlightedWidth = CGFloat((highlightedSamples?.last ?? 0) - zoomSamples.lowerBound) / CGFloat(zoomSamples.count)
      }

Calculating scaledHighlightedWidth should somehow make use of the lower and upper bound from the highlighted sample. I wasn't sure how all this works so I don't have a fix with zoom. I could however remove the zoom functionality and do this instead:

 var scaledX: CGFloat = 0.0
        var scaledWidth: CGFloat = 1.0
        var scaledHighlightedX: CGFloat = CGFloat(highlightedSamples?.startIndex ?? 0) / CGFloat(totalSamples)
        let highlightLastPortion = CGFloat(highlightedSamples?.last ?? 0) / CGFloat(totalSamples)
        var scaledHighlightedWidth: CGFloat = highlightLastPortion - scaledHighlightedX

The image is still offset a little. But the highlights works correctly with that.

@jonandersen
Copy link
Contributor

@ogrydziak I've fixed it in #107

@fulldecent
Copy link
Owner

Thank you for sharing. I agree that this is broken now. I have tried #107 but it is not fixing the issue for me.

@fulldecent
Copy link
Owner

I believe this is fixed now thanks to @reedom. Also released to CocoaPods and through Carthage today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants