Reading Audio Buffer Data Using AudioQueue

I am trying to read audio data through AudioQueue. When I do this, I can verify that the bit depth of the file is 16 bits. But when I get the actual sample data, I only see values โ€‹โ€‹from -128 to 128. But I also see suspicious striped data being viewed, which makes me pretty sure that I'm just not reading the data correctly.

So, for starters, I can verify that the source file is a 44100, 16-bit, mono-wav file.

My buffer is allocated as follows:

  char * buffer = NULL;
 buffer = malloc (BUFFER_SIZE);
 assert (buffer);

All relevant values โ€‹โ€‹are set and used in:

  AudioFileReadPackets (inAudioFile, false, & bytesRead, NULL, packetNum, & numPackets, buffer);       

As a test, so that I can see the received data, I run:

  for (int i = 0; i <BUFFER_SIZE; i ++) {
   NSLog (@ "% i", buffer [i]);
 }

Now I know that my source files are peaks everywhere, but the values โ€‹โ€‹I see are only max at -128 and 128. Being, since it is a 16-bit file, I would expect the values โ€‹โ€‹instead to be -32768 to 32768.

In addition, there are apparently two patterns in the data. Here is an example of the returned data:

  70
 -thirteen
 31
 -eleven
 -118
 -nine
 -fifteen
 -7
 116
 -4
 31
 -one
 28
 one
 84
 2
 -123
 3
 -97
 4
 110
 5
 54
 6
 126

Now take a look at each other line, starting from the second line: -13. See how it grows, not evenly, but at least smoothly? Lines with odd numbers are not somewhere near smooth.

My first thought was that it was alternating stereo data, but no, it was only one channel, so there shouldn't be any interleaving, right?

My best guess is that I'm just reading the data incorrectly, so the sample data is stretched into two returns. Any idea how to read it right?

Thank you for reading the entire question, and for any understanding you can offer.

+6
objective-c iphone cocoa core-audio audioqueueservices
source share
1 answer
char *buffer= NULL; 

This is the reason. You iterate over signed bytes, not 16-bit patterns.

Declare the variable as holding a pointer to double-byte values:

 SInt16 *buffer = NULL; 

Then iterate over half of the total number in bytes:

 for(int i=0;i < (BUFFER_SIZE / sizeof(*buffer));i++){ NSLog(@"%i", buffer[i]); } 

I would rename BUFFER_SIZE to BUFFER_SIZE_BYTES to clarify it.

+8
source share

All Articles