0

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.

Geekoder
  • 1,531
  • 10
  • 21
  • Till now I have tried to Write NSData to the file directly on the receiver iPhone using NSHandle, and then read the file using Audio Queue. But unable to read. As you say ExtAudioFile can do the job, can you provide with some code. How excatly do I implement the ExtAudioFileWrite method. Thanks for the help. – Geekoder Sep 11 '13 at 08:35
  • What format is the audio? Please post the code you have so far. – sbooth Sep 11 '13 at 10:50
  • Please expand on "unable to read". How are you calling ExtAudioFileWrite(Async)? What error are you getting? – Nick Sep 11 '13 at 14:49
  • I haven't used ExtAudioFileWrite till now. As you see the last code snippet I have received audioData. Now I want this audioData to be played on the speaker. How do I do that? – Geekoder Sep 12 '13 at 06:55
  • possible duplicate of [How to get array of float audio data from AudioQueueRef in iOS?](http://stackoverflow.com/questions/7880706/how-to-get-array-of-float-audio-data-from-audioqueueref-in-ios) – buildsucceeded Feb 28 '14 at 23:04

1 Answers1

0

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.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153