1

I am trying to stream some recording from the mic in my iPhone in real-time over NSSocket.

I have callbacks for recording and playback. I am trying to read directly from the buffer using following code:

SInt16 *intFromBuffer;
intFromBuffer = audioBufferList->mBuffers[0].mData;

When I'm trying:

NSLog(@"0x%X", intFromBuffer);

I get for instance: 0xA182E00

And now the important part - playback callback:

static OSStatus playbackCallback(void *inRefCon,
                             AudioUnitRenderActionFlags *ioActionFlags,
                             const AudioTimeStamp *inTimeStamp,
                             UInt32 inBusNumber,
                             UInt32 inNumberFrames,
                             AudioBufferList *ioData) {

/**
 This is the reference to the object who owns the callback.
 */
AudioProcessor *audioProcessor = (__bridge AudioProcessor*) inRefCon;

// iterate over incoming stream an copy to output stream
for (int i=0; i < ioData->mNumberBuffers; i++) {
    AudioBuffer buffer = ioData->mBuffers[i];

    // find minimum size
    UInt32 size = min(buffer.mDataByteSize, [audioProcessor audioBuffer].mDataByteSize);  

    intFromBuffer = (SInt16)([NSString stringWithFormat:@"0x%@", data.output]);

    memcpy(buffer.mData, intFromBuffer, size); //this works fine!
}
return noErr;
}

But now - how do I get this intFromBuffer to work over the NSStream?

When I try to send it directly as NSString and send it back as NSString and parse it back to SInt16 all I get is noise.

Any ideas? I'm running out of ideas...

Tomasz Łoś
  • 79
  • 2
  • 10
  • What you're doing won't work - you're taking a NSString* pointer and overwriting it's memory by a SInt16. You aren't even sure if NSString* has allocated enough memory for your data. This is not the correct approach. Did you have a look at [NSData](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html) class? – maroux Mar 31 '13 at 12:19
  • Thank you, it helped a lot. I stll have a problem with decoding data. My server receives data with something like this: <12345678 12345678 ... > Ofcourse with proper values and stuff. It sends it that that way back to my app, and I am trying to convert this string to NSData, but all it gets is still noise. – Tomasz Łoś Mar 31 '13 at 22:51
  • In general - I need to convert incoming String that in fact is just previously sent NSData, to NSData, without any changes in actual stream. – Tomasz Łoś Mar 31 '13 at 23:23

1 Answers1

0

Ok guys, I figured it out.

The problem was related with string encoding. My server was sending UTF8 encoded text, but my code was trying to interpret it with ASCII, which caused problem.

Another problem I found in my code was wrong data type as well. I was sending it as NSMutableData, but when I received it I converted the data in a wrong way back to NSMutableData, so my AudioProcessor was not able to get access to actual data which seemed to compiler as NSString object, and it's been throwing exception while creating frames from bytes:

SInt16 *frames = (SInt16 *)[data.input bytes];

The last problem I have with my code is actually very lossy transmition, so when data comes back from server the quality is very poor, but I believe it's buffer size related problem. I think I will figure it out soon.

Thanks a lot to StackOverflow community. I am new here, but I see you are a big help, even with simple comments that help with heading the right direction.

Thanks again.

Tomasz Łoś
  • 79
  • 2
  • 10
  • Again - problem was not with buffers but with wrong encoding. Accidently in my code I tried to interpret data as UTF8 string, and this interpreted data again as UTF8 string, which altered my incoming data. My app is working just fine now, both on simulator and phone itself. – Tomasz Łoś Apr 01 '13 at 06:20