Voice over bluetooth in iOS
Asked Answered
F

1

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 :)

Fallal answered 24/7, 2017 at 4:8 Comment(1)
Bluetooth low energy is unlikely to sustain the transfer rate needed for real-time voice.Silas
N
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.

Nilla answered 27/7, 2017 at 14:1 Comment(11)
Yes it stream audio over Bluetooth, for streaming I am using 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.Fallal
For converting file m4a to mp3 I am using 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.Fallal
Why are you streaming mp3 or m4a audio. You need to stream the actual buffer data... I will edit my answer above.Nilla
When I am streaming audio buffer, It's not sent to the connected peer. So that I am unable to do that.Fallal
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.Fallal
@SaurabhJain What do you mean it's not sent to the connected peer? You need to actually send it to the peer.Nilla
Let me implement your solution, Please stay connected.Fallal
I have updated the code please check this. Where am I wrong?Fallal
@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.Nilla
Let us continue this discussion in chat.Fallal
How can I calculate the data transfer rate?Fallal

© 2022 - 2024 — McMap. All rights reserved.