0

I am doing research over four days, But I am not found any solution for calling over Bluetooth between two iOS devices within a distance.

I found that audio streaming is possible between two iOS devices using multipeer connectivity framework but this is not helpful for me. I want real time voice chat between two devices over Bluetooth.

Is there any CO-DAC for voice over Bluetooth?

My code is:

     var engine = AVAudioEngine()
        var file: AVAudioFile?
        var player = AVAudioPlayerNode()
        var input:AVAudioInputNode?
        var mixer:AVAudioMixerNode?

override func viewDidLoad() {
        super.viewDidLoad()
        mixer = engine.mainMixerNode
        input = engine.inputNode  
        engine.connect(input!, to: mixer!, format: input!.inputFormat(forBus: 0))
}

@IBAction func btnStremeDidClicked(_ sender: UIButton) {
mixer?.installTap(onBus: 0, bufferSize: 2048, format: mixer?.outputFormat(forBus: 0), block: { (buffer: AVAudioPCMBuffer, AVAudioTime) in
            let byteWritten = self.audioBufferToData(audioBuffer: buffer).withUnsafeBytes {
                self.appDelegate.mcManager.outputStream?.write($0, maxLength: self.audioBufferToData(audioBuffer: buffer).count)
            }
            print(byteWritten ?? 0)
            print("Write")
        })
        do {
            try engine.start()
        }catch {
            print(error.localizedDescription)
        }
}

func audioBufferToData(audioBuffer: AVAudioPCMBuffer) -> Data {
        let channelCount = 1
        let bufferLength = (audioBuffer.frameCapacity * audioBuffer.format.streamDescription.pointee.mBytesPerFrame)

        let channels = UnsafeBufferPointer(start: audioBuffer.floatChannelData, count: channelCount)
        let data = Data(bytes: channels[0], count: Int(bufferLength))

        return data
    }

Thanks in Advance :)

Saurabh Jain
  • 1,688
  • 14
  • 28

1 Answers1

2

Why is MultipeerConnectivity not helpful for you? It is a great way to stream audio over bluetooth or even wifi.

When you call this:

audioEngine.installTap(onBus: 0, bufferSize: 17640, format: localInputFormat) {
    (buffer, when) -> Void in

You need to use the buffer, which has type AVAudioPCMBuffer. You then need to convert that to NSData and write to the outputStream that you would've opened with the peer:

data = someConverstionMethod(buffer)
_ = stream!.write(data.bytes.assumingMemoryBound(to: UInt8.self), maxLength: data.length)

Then on the other device you need to read from the stream and convert from NSData back to an AVAudioPCMBuffer, and then you can use an AVAudioPlayer to playback the buffer.

I have done this before with a very minimal delay.

Logan
  • 1,047
  • 10
  • 33
  • Yes it stream audio over Bluetooth, for streaming I am using https://github.com/tonyd256/TDAudioStreamer, I successfully transfer the voice other side. First I record the audio using AVAudioRecoder then I stream the audio to connected peer. But the problem is that TDAudioStramer only transfer the .mp3 format and my recording save in .m4a format. So I convert the m4a format to mp3 format and send to the other side, When audio length is long it's take too time for converting and it give a delay or more than 10 second So its not look like live. – Saurabh Jain Jul 27 '17 at 14:59
  • For converting file m4a to mp3 I am using https://github.com/lixing123/ExtAudioFileConverter this class. Thank you so much for your reply. Please help me. I research more than 5 day but I can't find any solution for live audio streaming. – Saurabh Jain Jul 27 '17 at 15:00
  • Why are you streaming mp3 or m4a audio. You need to stream the actual buffer data... I will edit my answer above. – Logan Jul 27 '17 at 15:01
  • When I am streaming audio buffer, It's not sent to the connected peer. So that I am unable to do that. – Saurabh Jain Jul 27 '17 at 15:03
  • I am found only above solution for streaming audio using TDAudioStreamer. If you have another solution please provide me. Thank you so much for reply. – Saurabh Jain Jul 27 '17 at 15:06
  • @SaurabhJain What do you mean it's not sent to the connected peer? You need to actually send it to the peer. – Logan Jul 27 '17 at 15:07
  • Let me implement your solution, Please stay connected. – Saurabh Jain Jul 27 '17 at 15:08
  • I have updated the code please check this. Where am I wrong? – Saurabh Jain Jul 27 '17 at 15:23
  • @SaurabhJain First of all, what happens with the code, does it run correctly or is there an error thrown? Secondly, why are you calling audioBufferToData twice? Lastly, convert to NSData not Data. – Logan Jul 27 '17 at 15:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150333/discussion-between-saurabh-jain-and-logan). – Saurabh Jain Jul 27 '17 at 15:41
  • How can I calculate the data transfer rate? – Saurabh Jain Nov 18 '17 at 13:16