views:

220

answers:

1

I try to implement an iPhone sound processing app using SpeakHere sample app as a starting point. The app involves manipulating buffered audio samples during playback. When I multiply the samples by a fractional number (0.9 for instance) I get noise as a result. The strangest thing about it is that when multiplying samples by whole numbers (like 1.0, 2.0) or adding or subtracting from them the playback sounds as expected.

I'm doing it in the playbackCallback function of AudioPlayer.m with the following code snippet:

  //------------------
  //If buffer samples are multiplied by 0.9 we get noise. If we multiply it by 1.0 or add to it everything's fine.
  short *buffer = (short *)calloc(1, numPackets * 2);
  memmove(buffer, bufferReference->mAudioData, numPackets * 2);
  for (int i = 0; i < numPackets; i++)
  {
   buffer[i] *= 0.9;
   //buffer[i] *= 1.0;
   //buffer[i] += 10.0;
  }
  memmove(bufferReference->mAudioData, buffer, numPackets * 2);
  free(buffer);
  buffer = NULL;
  //-------------------

The full project could be downloaded here: http://depositfiles.com/files/lmnkq68n8

Could anyone point me to what am I doing wrong? I've been struggling with it for a couple of days already and I'm completely lost.

Thank you!

A: 

I couldn't download your project (the file hoster always tells me that all slots are full), but by reading over the snippet you posted, it seems that you are multiplying signed 16bit integer (short) values with 0.9 (a floating point value). This leads to an implicit cast to short. So you loose everything behind the decimal point, which in turn leads to aliasing effects in your signal.

Update:
I checked your code but couldn't figure out where that noise comes from.
Why are you using memmove to manipulate the buffer?
You can directly access the buffer with:

SInt16* pBuffer = (SInt16*)inCompleteAQBuffer->mAudioData;

I'd be curious to know what causes that noise too. So if you find that out, please post your solution here.(Maybe someone on the Core Audio mailing list can help - they are really helpful there)

If you just have to alter the gain, you could simply use AudioQueueSetParameter.
Another option would be to use the RemoteIO audio unit instead of AudioQueueServices.

weichsel
I've reuploaded to another file hosting: http://www.filefactory.com/file/a2c9c30/n/Speak_Here.zip.The problem does not seem to be about casting floats to shorts. With audio samples amplitude range of -32768 to 32767 the value after a decimal point doesn't matter much. Multiplying by 0.9 should just make the sound quieter but I'm getting white noise instead.
Thorny
Thank you for re-uploading the project. I updated my answer.
weichsel
Thank you, I'll posting this question on core audio mailing list.
Thorny