0

I really need help with this issue. I'm developing an iOS application with audio units, the recorded audio needs to at 8bit / 8000 hertz sample rate using alaw format. How ever I'm getting a distorted voice coming out the speaker.

I came across this sample online:

http://www.stefanpopp.de/2011/capture-iphone-microphone/comment-page-1/

while trying to debug my app I used my audioFormat in his application and I am getting the same distorted sound. I guessing I either have incorrect settings or I need to do something else to enable this to work. Given the application in the link and the below audioFormat can anyone tell me if I'm doing something wrong or missing something ? I don't know a lot about this stuff, thanks.

Audio Format:

AudioStreamBasicDescription audioFormat;
    audioFormat.mSampleRate         = 8000;
    audioFormat.mFormatID           = kAudioFormatALaw;
    audioFormat.mFormatFlags        = kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
    audioFormat.mFramesPerPacket    = 1;
    audioFormat.mChannelsPerFrame   = 1;
    audioFormat.mBitsPerChannel     = 8;
    audioFormat.mBytesPerPacket     = 1;
    audioFormat.mBytesPerFrame      = 1;
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • What kind of distortion are you getting? – hotpaw2 Jul 27 '12 at 17:01
  • 1
    it's not clear from your question where you are using this AudioStreamBasicDescription. You could have several AudioStreamBasicDescriptions, all different, in a typical AUGraph setup that (eg) filters, draws and records to disk. Also, where did you get these values? Is that your spec? Also, you should make a robot voice app. – hooleyhoop Jul 27 '12 at 19:20
  • -hotpaw2 The distortion is that I can hear my voice but there's static in the background. Someone else described it as sounding like the input and output are using different sample rates. @hooleyhoop I'm only using the one description, I place of the one in the link. My spec is 8000 hertz using alaw, the rest of the settings are either from the link or through playing around with different values to see the best result. I've just read an online source that claims I must use 44k linear PCM and convert to what I want. Is this necessary ? – Simon McLoughlin Jul 28 '12 at 14:57
  • But you don't want to send ALaw to the speaker - that's what the audioFormat in the example project is configuring. If you wanted to save audio to a file - you wouldn't change that. – hooleyhoop Jul 28 '12 at 23:26
  • no what im saying is that I replaced the audioFormat in the example with the one I posted above, this sets both the input and output to be 8000 hertz alaw. I don't understand why it now sounds distorted as both are set to the same thing – Simon McLoughlin Jul 29 '12 at 12:22

1 Answers1

1

Eventually got it to play correctly. I'm posting here to help out anyone else facing similar issues.

Main issue I was facing is that there is a huge difference between the simulator and an actual device. Running the app on the device the sound quality was better but it kept skipping every second or 2, I found a setting that seemed to fix this and a setting to change the buffer size / duration. (The duration setting does not work on the simulator, some of my issues were needing it to run at a certain rate to sync with something else, this was causing distorted sounds)

status = AudioSessionInitialize(NULL, kCFRunLoopDefaultMode, NULL, audioUnit);
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
status = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);
[self hasError:status:__FILE__:__LINE__];

Float32 preferredBufferSize = 0.005805; // in seconds
status = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &preferredBufferSize);
[self hasError:status:__FILE__:__LINE__];

status = AudioSessionSetActive(true);

The first audio session property is what stopped the skipping making it play much more smoothly. The second adjusts the buffer duration, this is in seconds how often the callbacks are fired and will give you a different buffer size. Its best effort meaning it will get as close as it can to the value you provide but it seems to have a list of available sizes and picks the closest.

See the post I link to in my question for a very good tutorial / sample program to get started with this stuff.

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • i am also facing the same issue but doesnt look like your issue. could you take a look http://stackoverflow.com/questions/21964477/ios7-robotic-garbled-in-speaker-mode-on-iphone5s – Titus Apr 03 '14 at 01:37