录制中rampVolume参数溢出的问题

来源:互联网 发布:java stg环境 编辑:程序博客网 时间:2024/05/19 12:26

在录音的过程中发现在最前面的1s中总是会有“噗”的一声的噪音。一直在跟踪查找,发现是Android源码中有一处溢出造成的。stagefright中的AudioSource的void rampVolume(int32_t startFrame, int32_t rampDurationFrames, uint8_t *data, size_t bytes);函数(raise the volume from mute to the actual level linearly)。在AudioSource的read()函数将从AudioRecord读入的数据经过计算

int32_t autoRampDurationFrames =
(kAutoRampDurationUs * mSampleRate + 500000LL) / 1000000LL;

int32_t autoRampStartFrames =
(kAutoRampStartUs * mSampleRate + 500000LL) / 1000000LL;

int32_t nFrames = mNumFramesReceived - autoRampStartFrames;

rampVolume(nFrames, autoRampDurationFrames,
(uint8_t *) buffer->data(), buffer->range_length());

其中nFrames和autoRampDurationFrames的计算存在溢出,kAutoRampDurationUs和kAutoRampDurationUs都是int64_t类型的值,计算结果直接强制转换为int32_t类型。于是修改后的代码为

int64_t autoRamp = kAutoRampDurationUs;
autoRamp *= mSampleRate;
int32_t autoRampDurationFrames = (autoRamp +500000LL) / 1000000LL;

autoRamp = kAutoRampStartUs;
autoRamp *= mSampleRate;
int32_t autoRampStartFrames =
(autoRamp + 500000LL) / 1000000LL;

int32_t nFrames = mNumFramesReceived - autoRampStartFrames;

rampVolume(nFrames, autoRampDurationFrames,
(uint8_t *) buffer->data(), buffer->range_length());

这样就解决了rampVolume函数处理的nFrames帧的数据出现爆音的问题。

举一反三,播放器的文件大小限制的问题也是由于溢出的问题引起的,文件大小使用int32_t类型的地方限制了大小只能支持到2G,因此需要将这些地方修改int64_t使得文件大小能够支持到fat32的存储文件的最大值4G。


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击