BlueSTSDK_iOS icon indicating copy to clipboard operation
BlueSTSDK_iOS copied to clipboard

How to get the Audio data BlueSTSDKFeatureAudioADPCM.getLinearPCMAudio(sample) in Swift

Open awerudev opened this issue 6 years ago • 9 comments

awerudev avatar Nov 13 '19 17:11 awerudev

// BlueSTSDKFeatureDelegate

func didUpdate(_ feature: BlueSTSDKFeature, sample: BlueSTSDKFeatureSample) {

    guard let node = self.node else {
        return
    }
    
    let features = node.getFeatures()
    guard let ind = features.firstIndex(of: feature) else {
        return
    }
    
    // Audio Feature
    if feature.isKind(of: BlueSTSDKFeatureAudioADPCM.self), let audio = feature as? BlueSTSDKFeatureAudioADPCM {
        let fileURL = getDocumentsDirectory().appendingPathComponent("\(audio.name).\(audioFileFormat)")
        if let data = BlueSTSDKFeatureAudioADPCM.getLinearPCMAudio(sample) {
             if records["\(ind)"] != nil {
                  var record = records["\(ind)"]
                  objc_sync_enter(record as Any)
                  record?.append(data)
                  objc_sync_exit(record as Any)
                  records["\(ind)"] = record!
             }
             else {
                  records["\(ind)"] = Data()
             }
        }
    }
}

awerudev avatar Nov 13 '19 17:11 awerudev

// So I was able to save the "record".

do { try record.write(to: fileURL, options: .atomic) } catch let error { // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding print("Record Write: (error.localizedDescription)") }

// But I was not able to play in AVAudioPlayer.

awerudev avatar Nov 13 '19 18:11 awerudev

Hi,

did you see this code: https://github.com/STMicroelectronics/STBlueMS_iOS/blob/master/W2STApp/MainApp/Demo/Audio/Util/W2STAudioPlaybackController.swift

it is used here: https://github.com/STMicroelectronics/STBlueMS_iOS/blob/master/W2STApp/MainApp/Demo/Audio/BlueVoice/BlueMSBlueVoiceViewController.swift

GiovanniVisentiniST avatar Nov 14 '19 07:11 GiovanniVisentiniST

Hi, thanks for the info! let me try again with above code.

awerudev avatar Nov 16 '19 17:11 awerudev

Hi @GiovanniVisentini ,

I have integrated the latest BlueSTSDK_iOS. But I am getting the following error in Xcode 11.1.

Screen Shot 2019-11-17 at 10 08 47 AM

Please let me know how to fix it.

Looking forward to hearing from you as soon as possible.

Thank you

awerudev avatar Nov 17 '19 15:11 awerudev

Hi,

I think that the problem is that you are targeting the emulator. with this version we add the opus decoder that is done using a serrated library.. the library is compiled only for arm, so the library doesn't build for the x86 enviroment.

Best Regards Giovanni

GiovanniVisentiniST avatar Nov 18 '19 07:11 GiovanniVisentiniST

Hi @GiovanniVisentini ,

Thanks for the info!

Yes Right. I was targeting the emulator.

Let me try again.

Thank you!

awerudev avatar Nov 18 '19 14:11 awerudev

Hi @GiovanniVisentini ,

Your answer was really helpful for me.

By the way, I am just wondering how to detect once the data transfer is stopped.

I just wanted to know how to check if the data transfer is stopped or started.

Is there any API or something else?

Looking forward to hearing from you.

Thank you!

awerudev avatar Dec 13 '19 17:12 awerudev

you can check if the 2 characteristics/features have the notification enabled, in that case the fw is streaming audio to the mobile.

if your fw stops autonomously to stream, from the feature.lastSample you can see the when the last package arrive an have a threshold on that.

Best Regards Giovanni

GiovanniVisentiniST avatar Dec 18 '19 08:12 GiovanniVisentiniST