iOS Audio Unit playback with constant noise

2019-08-11 03:56发布

问题:

I am using audio unit for audio playback. I have download the tone generator from http://cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html and try to play around with it. For some reason I need to use ulaw instead of linear PCM. Here is my audio format setup:

AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = 8000;
streamFormat.mFormatID = kAudioFormatULaw;
streamFormat.mFormatFlags = 0;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = 2;
streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame;
streamFormat.mChannelsPerFrame = 1;
streamFormat.mBitsPerChannel = 16;

When I run the sample code, I get a constant noise. Does anyone could help on this issue? Thanks a lot.

回答1:

You can use only linearPCM format using Audio Units. Even kAudioUnitType_FormatConverter provides conversions betwean linearPCM formats (with different sample rate, bytesPerPacket, etc). To convert from compressed format you should use AudioConverter.

https://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/AudioConverterServicesReference/Reference/reference.html



回答2:

If you want to use another codec different from Linear PCM, you will need to avoid Audio Units, since they only support Linear PCM.

However, you can use Audio Queues, they support ULaw, ALaw, among others. If latency is not an issue, use audio queues.