How to create audio file from Int array on iOS device swift 3?

Viewed 238

I have bluetooth device with sound membrane, I can scrub by finger on this membrane and produce the sound. Then this analog sound is sending to iPhone via bluetooth and then I can handle those values at the bluetooth delegate methods. I receive data and then convert this data to the values from 0 to 4000, like [10, 125, 3000, 2540, 4000....n]. I need these values to plot sound on the iPhone screen. Then I need to produce sound from these values on my iPhone. The question is how can I convert this [Int] array to audio file or something else. And then play it like stream on audio engine. I want to be able to play this sound from iPhone. I tried this, but it was unsuccessful:

let dataInt = Data(bytes: &receivedInt, 
                   count: MemoryLayout.size(ofValue: receivedInt))
playAudioWithData(data: dataInt)

//or this 

let dataInt = UInt8(receivedInt).data
playAudioWithData(data: dataInt)

//or even directly from bluetoothData

playAudioWithData(data: bluetoothData)
...

func playAudioWithData(data: Data) {

    do {
        try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try! AVAudioSession.sharedInstance().setActive(true)
        player = try AVAudioPlayer(data: data, fileTypeHint: AVFileTypeMPEGLayer3)
        player?.play()
    } catch {
        print("error initializing AVAudioPlayer")
    }
}


///error initializing AVAudioPlayer
0 Answers
Related