objective-cswiftezaudio

Conversion error OBJ-C to Swift 3


I am trying to convert a EZAudio (https://github.com/syedhali/EZAudio) Objective-C Library to Swift 3. I am doing the "EZAudioPlayFileExample" part in it.

Objective-C

- (void)openFileWithFilePathURL:(NSURL*)filePathURL
{
    self.audioFile = [EZAudioFile audioFileWithURL:filePathURL];
    self.filePathLabel.text = filePathURL.lastPathComponent;

    //
    // Plot the whole waveform
    //
    self.audioPlot.plotType = EZPlotTypeBuffer;
    self.audioPlot.shouldFill = YES;
    self.audioPlot.shouldMirror = YES;

    //
    // Get the audio data from the audio file
    //
    __weak typeof (self) weakSelf = self;
    [self.audioFile getWaveformDataWithCompletionBlock:^(float **waveformData,
                                                         int length)
    {
        [weakSelf.audioPlot updateBuffer:waveformData[0]
                          withBufferSize:length];
    }];
}

Swift 3

func openFileWithFilePathURL(filePathURL: NSURL) {
        self.audioFile = EZAudioFile(url: filePathURL as URL!)

        //
        // Plot the whole waveform
        //
        self.plot.plotType = .buffer
        self.plot.shouldFill = true
        self.plot.shouldMirror = true

        //
        // Get the audio data from the audio file
        //
        weak var weakSelf = self
        self.audioFile.getWaveformData(completionBlock: {(_ waveformData: Float, _ length: Int) -> Void in
            weakSelf?.plot.updateBuffer(waveformData[0], withBufferSize: length) // error in this line as Float as no subscript members
        })
    }

I know Float parameter have no subscript members like [0]. But I want to convert the Objective-C code. OR anyone here use this library's Swift version. NOTE: I want the "EZAudioPlayFileExample" in it.


Solution

  • float **waveformData is actually an unsafemutablepointer. So you have to use unsafemutablepointer in swift 3.0. You can use unsafemutablepointer in swift like

    UnsafeMutablePointer<UnsafeMutablePointer<Float>>
    

    You could use like

    self.audioFile.getWaveformData(completionBlock: {(_ waveformData: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, _ length: Int) -> Void in
                weakSelf?.plot.updateBuffer(waveformData[0], withBufferSize: length)  
            })
    

    I have just downloaded an older version swift implementation of EZAudio and converted to swift 3.0. It is available at this link
    I don't know where this will help you or not but you may build on top of this. You could also refer to this issue being reported on the gitHub page