我的iOS应用使用采样率为8000赫兹的音频单元返回了失真的声音 [英] my iOS app using audio units with an 8000 hertz sample rate returns a distorted voice

查看:545
本文介绍了我的iOS应用使用采样率为8000赫兹的音频单元返回了失真的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上,我真的需要帮助.我正在开发带有音频单元的iOS应用程序,所记录的音频需要使用alaw格式以8bit/8000赫兹的采样率进行采样.我怎么会听到扬声器发出失真的声音.

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/

在尝试调试我的应用程序时,我在他的应用程序中使用了我的audioFormat,并且得到了同样的失真声音.我猜我要么设置不正确,要么需要做其他事情才能使它起作用.给定链接中的应用程序和下面的audioFormat,谁能告诉我我做错了什么还是缺少了什么?谢谢,我对此并不了解.

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.

音频格式:

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;

推荐答案

最终使它能够正确播放.我在这里发布的内容是为了帮助其他面临类似问题的人.

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.

这篇关于我的iOS应用使用采样率为8000赫兹的音频单元返回了失真的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆