gyetvan-andras / cocoa-waveform

A cocoa audio player component which displays the waveform of the audio file.
260 stars 58 forks source link

Question about dataForResolution:lenght: #10

Closed maroonblazer closed 11 years ago

maroonblazer commented 11 years ago

Hello! Thank you very much for sharing this code. I'm still fairly new to programming and am struggling to understand what's happening inside the dataForResolution:lenght method. I think all the nested loops are confusing me.

I've compared the samples stored in the WaveSampleProvider's normalizedData array with the samples returned by dataForResolution:lenght: in sd and they're identical (I'm using a very short, 2 second, piece of audio).

What is the pixelWide argument? What is the rangeLength variable? and why do you add 1 to pixelWide in the denominator?

Update: Spending more time with this I think I get it...

In the case where we have more samples in our array than we have points/pixels to draw on the screen, to draw them we need to scale the number of samples down to fit the resolution of the area being drawn to. dataForResolution:: is performing that scaling.

gyetvan-andras commented 11 years ago

Yes, exactly. However, please note that all the sample processing/averaging is a bit experimental. I mean that if, for example, I create exactly the same number of averaged samples like the pixels parameter, the drawn bezier path will be ugly (it will be too flat). The same happens If I use all the samples to draw the bezier path: it will be too detailed. The current code used to display average length audio clips (3-4 mins). I have never tried short (1-2-10 secs) or long (more than 5 mins) audio data. A further development would be to rewrite sample calculation to take into account the audio length (and clean out the current mess)