0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Why you want to convert the library to swift you can use objective C libraries in swift project by using bridging headers – Aravind A R Jul 25 '17 at 10:35
  • @AravindAR you are correct I am doing with bridging header. Above code is coming from implementation part (ViewController.m). So I need convert that to Swift –  Jul 25 '17 at 10:39
  • "I know Float parameter have no subscript members like [0]" In Objective-C, it's `float **waveformData`, notice the two `*`. – Larme Jul 25 '17 at 11:45
  • @Larme Then what will be the code in swift 3 –  Jul 25 '17 at 11:48
  • https://stackoverflow.com/questions/833112/what-does-having-two-asterisk-in-objective-c-mean + have a look at the sample method `executeAndReturnError:` (it's an official method in the macOS SDK) and how is it declared in Swift with ` AutoreleasingUnsafeMutablePointer` and related stuff to know how to use it in Swift. – Larme Jul 25 '17 at 11:53
  • @AlwinLazar I have added an answer and hope it might help you – Aravind A R Jul 25 '17 at 13:53

1 Answers1

0

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

Aravind A R
  • 2,674
  • 1
  • 15
  • 25