How to record 16bit audio swift?

504 views Asked by At

I was implementing functionality to record and to get the audio buffer from AVAudioEngine and play it with player, But It is with 32 bit I need it with 16 bit with sample rate 8000 can you please suggest me what I need to to for that.

engine = AVAudioEngine()
guard let engine = engine, let input = engine.inputNode else {
    // @TODO: error out
    return
}

let format = input.inputFormat(forBus: 0)
input.installTap(onBus: 0, bufferSize:4096, format:format, block: { [weak self] buffer, when in

    guard let this = self else {
        return
    }


    if let channel1Buffer = buffer.floatChannelData?[0] {
       let test = self?.copyAudioBufferBytes(buffer)
        let stram:NSData = (self?.toNSData(PCMBuffer: buffer))!
        SocketIOManager.sharedInstance.socket.emit("talk",stram);

    }
})

engine.prepare()

do {
    try engine.start()
} catch {
    // @TODO: error out
}

I Try above code to record audio and its woking fine but not able to get 16bit data. I send this buffer to socket and play it in android device as well, But Not able to play it in android because it is not support 32 bit data. so please provide your suggestions.

0

There are 0 answers