|

楼主 |
发表于 2007-11-3 23:04:43
|
显示全部楼层
安卓端58直播SDK接入降噪方案
通过上章节的优缺点对比以及58直播中已经在使用了WebRTC相关代码逻辑,综合调研和处理结果验证工作之后,最终选择了WebRTC降噪方案。
APM模块集成WebRTC降噪功能
在58多媒体整体架构上选择把降噪模块单独解耦提取一个APM module,方便58视频编辑、58直播等需要降噪业务统一调用。对外暴露工具类AudioNoiseHelp方便业务接入。APM module的规划以后会接入更多的音频处理模块,现在已经接入降噪、增益模块。
由于58直播SDK支持音频采样率种类大于WebRTC支持的种类,因此需要对数据进行最优音频重采样处理。
/**
* 音频重采样
*
* @param sourceData 原始数据
* @param sampleRate 原始采样率
* @param srcSize 原始数据长度
* @param destinationData 重采样之后的数据
* @param newSampleRate 重采样之后的数据长度
*/
void resampleData(const int16_t *sourceData, int32_t sampleRate, uint32_t srcSize,
int16_t *destinationData,int32_t newSampleRate){
if (sampleRate == newSampleRate) {
memcpy(destinationData, sourceData, srcSize * sizeof(int16_t));
return;
}
uint32_t last_pos = srcSize - 1;
uint32_t dstSize = (uint32_t) (srcSize * ((float) newSampleRate / sampleRate));
for (uint32_t idx = 0; idx < dstSize; idx++) {
float index = ((float) idx * sampleRate) / (newSampleRate);
uint32_t p1 = (uint32_t) index;
float coef = index - p1;
uint32_t p2 = (p1 == last_pos) ? last_pos : p1 + 1;
destinationData[idx] = (int16_t) ((1.0f - coef) * sourceData[p1] + coef * sourceData[p2]);
}
}
由于WebRTC只能处理320byte长度正整数倍的数据,但是58直播的音频采集数据在不同手机、不同采样率上得到的音频数据长度是不固定的,需要对数据进行切分处理。录音采集数据如果是byte格式的,假如长度是4096,那么直接把4096数据传入到WebRTC\_NS里处理会出现杂音出现,所以在交给WebRTC\_NS模块之前需要用个缓冲区来处理下,一次最多可以传入(4096/320)*320=3840长度数据,并且在数据处理完毕之后还需要用另外一个缓冲区来保证处理之后的长度仍然是4096个。
//部分逻辑代码如下所示:
//这个数据拆分和缓冲区数据逻辑可以由业务方自行出
/**
* 降噪处理方法,数据异步处理之后通过回调方法通知给调用方。
*
* @param bytes 音频数据
* @param nsProcessListener 异步方法回调
*/
public void webRtcNsProcess(byte[] bytes, INsProcessListener nsProcessListener) {
if (isAudioNsInitOk) {
synchronized (TAG) {
if (null == bytes || bytes.length == 0) {
return;
}
...
int byteLen = bytes.length;
if (inByteLen != byteLen) {
inByteLen = byteLen;
webRtcNsInit(byteLen);
}
int frames = byteLen / FRAME_SIZE;
int lastFrame = byteLen % FRAME_SIZE;
int frameBufferLen = frames * FRAME_SIZE;
byte[] buf = new byte[frameBufferLen];
Log.d(TAG, "webRtcNsProcess inBufferSize:" + inBufferSize);
if (inBufferSize >= frameBufferLen) {
Log.d(TAG, "webRtcNsProcess mInByteBuffer full");
nsProcessInner(buf, nsProcessListener);
}
...
nsProcessInner(buf, nsProcessListener);
}
} else {
if (null != nsProcessListener) {
nsProcessListener.onProcess(bytes);
}
}
}
private void nsProcessInner(byte[] buf, INsProcessListener nsProcessListener) {
mInByteBuffer.rewind();
mInByteBuffer.get(buf, 0, buf.length);
byte[] inBufferLeft = new byte[inBufferSize - mInByteBuffer.position()];
...
byte[] nsProcessData = AudioNoiseUtils.webRtcNsProcess(buf);
byte[] outBuf = new byte[inByteLen];
...
if (outBufferSize >= inByteLen) {
...
byte[] outBufferLeft = new byte[outBufferSize - mOutByteBuffer.position()];
...
mOutByteBuffer.put(outBufferLeft);
outBufferSize += outBufferLeft.length;
if (null != nsProcessListener) {
nsProcessListener.onProcess(outBuf);
}
}
}
|
|