Cómo reproducir audio de AVAudioPCMBuffer convertido de NSData

Recibo datos PCM mono 16bit de audio de packages udp como este:

(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext { ... } 

Estoy convirtiendo estos datos en el búfer PCM llamando a una function rápida como se muestra a continuación:

 func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer { let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false) // given NSData audio format var PCMBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity:1024*10) PCMBuffer.frameLength = PCMBuffer.frameCapacity let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount)) data.getBytes(UnsafeMutablePointer<Void>(channels[0]) , length: data.length) return PCMBuffer } 

Los datos se convierten en buffer de PCM y puedo ver su longitud en los loggings. Pero cuando trato de reproducir el búfer, no escucho ninguna voz. Aquí está el código para recibir:

 func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer { let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false) // given NSData audio format var PCMBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity:1024*10) PCMBuffer.frameLength = PCMBuffer.frameCapacity let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount)) data.getBytes(UnsafeMutablePointer<Void>(channels[0]) , length: data.length) var mainMixer = audioEngine.mainMixerNode audioEngine.attachNode(audioFilePlayer) audioEngine.connect(audioFilePlayer, to:mainMixer, format: PCMBuffer.format) audioEngine.startAndReturnError(nil) audioFilePlayer.play() audioFilePlayer.scheduleBuffer(PCMBuffer, atTime: nil, options: nil, completionHandler: nil) return PCMBuffer } 

terminó usando una function objective-c: los datos se están convirtiendo bien

 -(AudioBufferList *) getBufferListFromData: (NSData *) data { if (data.length > 0) { NSUInteger len = [data length]; //NSData *d2 = [data subdataWithRange:NSMakeRange(4, 1028)]; //I guess you can use Byte*, void* or Float32*. I am not sure if that makes any difference. Byte* byteData = (Byte*) malloc (len); memcpy (byteData, [data bytes], len); if (byteData) { AudioBufferList * theDataBuffer =(AudioBufferList*)malloc(sizeof(AudioBufferList) * 1); theDataBuffer->mNumberBuffers = 1; theDataBuffer->mBuffers[0].mDataByteSize =(UInt32) len; theDataBuffer->mBuffers[0].mNumberChannels = 1; theDataBuffer->mBuffers[0].mData = byteData; // Read the data into an AudioBufferList return theDataBuffer; } } return nil; }