Make a .caf file out of buffer data

388 views Asked by At

I have inBuffer->mAudioData converted into NSData coming via Network. I do this using the Audio Queue Callback.

How do I convert this NSData so that I can either create a .caf sound file or directly give output to the speaker on the other side ?

Thanks for help.

Edit 1: Below is the code I have used on the sender side to send Data on a wifi network:

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);
}

Here I have casted inBuffer->mAudioData to NSData and then I have sent to the outputStream.

On the other end that is the receivers side I have used the below code:

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

Whenever I get data the above function gets called and I get NSData which I have sent from the sender iPhone. Now I want to play whatever audioData I am receiving.

Thanks for the help.

1

There are 1 answers

0
hotpaw2 On

The easiest way is to configure an Audio Queue or RemoteIO Audio Unit to play data of the same format (raw linear PCM?) and start pulling samples from a lock free circular buffer (playing silence for underflow). Then just copy your received data out of the NSData network packets into the circular buffer.