syedhali / EZAudio

An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.
Other
4.94k stars 821 forks source link

EZAudioPlot GL begins with an offset when loaded from file #319

Open dantarakan opened 8 years ago

dantarakan commented 8 years ago

Hello Syed, thank you (and others) very much for your hard work on this amazing library!

I've been experiencing this weird issue when I try to plot a waveform from audio file with the GL plots, where the plot begins with an offset, like so: image2

I'm using the following code to initialise the plot:

audioPlot.frame = CGRectMake(0, 0, screenWidth, screenHeight * 0.5)
audioPlot.center = CGPointMake(screenWidth*0.5, screenHeight*0.4)
audioPlot.backgroundColor = UIColor(hexString: "#404040")
audioPlot.color = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha:1.0)
audioPlot.plotType = EZPlotType.Buffer
audioPlot.shouldFill = true
audioPlot.shouldMirror = true
audioPlot.gain = 10

And this code to draw the waveform:

let audioFile = EZAudioFile(URL: global_result.filePath)
let waveformData = audioFile!.getWaveformData()
self.audioPlot.updateBuffer(waveformData.buffers[0], withBufferSize: waveformData.bufferSize)

This issue does not occur on simulator, but only on the real device

When I plot the same recording in real time using the EZAudioPlayer, this issue disappears: image1

Interestingly, the non-GL plot doesn't have this issue, but it introduces a bunch of other bugs instead.

Any help would be much appreciated!

dantarakan commented 8 years ago

Just a quick update: Upon inspecting waveformData, which is the Array extracted from the file, I noticed that I'm getting 56 zeros in the beginning of the array. So perhaps the problem lies within EZAudioFile.

As a temporary (dirty) workaround, I stripped zeros from the beginning:

// Get data from file
let waveformData = audioFile!.getWaveformData()
// Get buffer size
let bufferSize = waveformData.bufferSize
// Get the pointer
let dataPointer = waveformData.buffers[0]
// Initialise output array
var dataArray = [Float]()
// Append all non-zero elements
for i in 0..<Int(bufferSize) {
    if dataPointer[i] != Float(0.0) {
        dataArray.append(dataPointer[i])
    }
}
// Update audio plot
self.audioPlot.updateBuffer(&dataArray, withBufferSize: UInt32(dataArray.count))

I understand that this is not the correct way to do it, but it works for my purposes.