用缓冲区数据制作.caf文件

时间:2013-09-10 09:14:05

标签: ios core-audio audio-recording bonjour audioqueueservices

我将inBuffer-> mAudioData转换为来自网络的NSData。我是使用Audio Queue Callback执行此操作的。

如何转换此NSData以便我可以创建.caf声音文件或直接将输出提供给另一方的扬声器?

感谢您的帮助。

编辑1: 下面是我在发送方用于在wifi网络上发送数据的代码:

void AudioInputCallback(void * inUserData,
                    AudioQueueRef inAQ,
                    AudioQueueBufferRef inBuffer,
                    const AudioTimeStamp * inStartTime,
                    UInt32 inNumberPacketDescriptions,
                    const AudioStreamPacketDescription * inPacketDescs)
{
RecordState * recordState = (RecordState*)inUserData;
if (!recordState->recording)
{
    printf("Not recording, returning\n");
}

// if (inNumberPacketDescriptions == 0 && recordState->dataFormat.mBytesPerPacket != 0)
// {
//     inNumberPacketDescriptions = inBuffer->mAudioDataByteSize / recordState->dataFormat.mBytesPerPacket;
// }

printf("Writing buffer %lld\n", recordState->currentPacket);

OSStatus status = AudioFileWritePackets(recordState->audioFile,
                                        false,
                                        inBuffer->mAudioDataByteSize,
                                        inPacketDescs,
                                        recordState->currentPacket,
                                        &inNumberPacketDescriptions,
                                        inBuffer->mAudioData);
NSLog(@"DATA = %@",[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize*NUM_BUFFERS]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"Recording" object:[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize*NUM_BUFFERS]];

if (status == 0)
{
    recordState->currentPacket += inNumberPacketDescriptions;
}

AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);
}

这里我已将inBuffer-> mAudioData转换为NSData,然后我已发送到outputStream。

在另一端是接收方,我使用了以下代码:

-(void)audioMessageData:(NSData *)audioData fromUser:(NSString *)userName {
 NSLog(@"DATA = %@",audioData);
}

每当我获取数据时,上面的函数都被调用,我得到了我从发送者iPhone发送的NSData。现在我想播放我收到的任何audioData。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

最简单的方法是配置音频队列或RemoteIO音频单元以播放相同格式的数据(原始线性PCM?)并开始从无锁循环缓冲区中提取样本(为下溢播放静音)。然后只需将收到的数据从NSData网络数据包中复制到循环缓冲区中。