fix:调整对讲时的音频问题
This commit is contained in:
parent
3e565ab7d7
commit
ee66d43c15
@ -658,8 +658,22 @@ class TalkViewLogic extends BaseGetXController {
|
||||
|
||||
// 音频帧处理
|
||||
Future<void> _onFrame(List<int> frame) async {
|
||||
// 添加数据监控
|
||||
int maxVal = 0;
|
||||
int minVal = 0;
|
||||
int sum = 0;
|
||||
for (int val in frame) {
|
||||
maxVal = max(maxVal, val);
|
||||
minVal = min(minVal, val);
|
||||
sum += val;
|
||||
}
|
||||
double average = sum / frame.length;
|
||||
AppLog.log('音频数据特征 - 最大值: $maxVal, 最小值: $minVal, 平均值: $average');
|
||||
|
||||
final List<int> processedFrame = preprocessAudio(frame);
|
||||
final List<int> list = listLinearToALaw(processedFrame);
|
||||
// 添加平滑处理
|
||||
final List<int> smoothedFrame = smoothAudio(processedFrame);
|
||||
final List<int> list = listLinearToALaw(smoothedFrame);
|
||||
_bufferedAudioFrames.addAll(list);
|
||||
|
||||
final int ms = DateTime.now().millisecondsSinceEpoch -
|
||||
@ -671,6 +685,15 @@ class TalkViewLogic extends BaseGetXController {
|
||||
}
|
||||
|
||||
if (_bufferedAudioFrames.length >= getFrameLength) {
|
||||
// 添加数据监控
|
||||
int maxVal = 0;
|
||||
int minVal = 255;
|
||||
for (int val in _bufferedAudioFrames) {
|
||||
maxVal = max(maxVal, val);
|
||||
minVal = min(minVal, val);
|
||||
}
|
||||
AppLog.log(
|
||||
'发送音频数据 - G711编码后 - 最大值: $maxVal, 最小值: $minVal, 长度: ${_bufferedAudioFrames.length}');
|
||||
// 发送音频数据到UDP
|
||||
await StartChartManage()
|
||||
.sendTalkDataMessage(
|
||||
@ -691,19 +714,46 @@ class TalkViewLogic extends BaseGetXController {
|
||||
AppLog.log(error.message!);
|
||||
}
|
||||
|
||||
// 简化的音频预处理函数
|
||||
List<int> preprocessAudio(List<int> pcmList) {
|
||||
// 简单的降噪处理
|
||||
final List<int> processedList = [];
|
||||
final int noiseThreshold = 300; // 噪音阈值
|
||||
|
||||
for (int pcmVal in pcmList) {
|
||||
// 简单的降噪示例:将小于阈值的信号置为0
|
||||
if (pcmVal.abs() < 200) {
|
||||
pcmVal = 0;
|
||||
// 简单的噪音门控:小于阈值的信号会被减弱
|
||||
if (pcmVal.abs() < noiseThreshold) {
|
||||
pcmVal = (pcmVal * 0.3).round(); // 减弱噪音
|
||||
}
|
||||
|
||||
// 简单的压缩:防止大信号失真
|
||||
if (pcmVal.abs() > 20000) {
|
||||
double factor = 1.0 - ((pcmVal.abs() - 20000) / 12768) * 0.3;
|
||||
pcmVal = (pcmVal * factor).round();
|
||||
}
|
||||
|
||||
processedList.add(pcmVal);
|
||||
}
|
||||
|
||||
return processedList;
|
||||
}
|
||||
|
||||
// 简单的平滑处理
|
||||
List<int> smoothAudio(List<int> pcmList) {
|
||||
final List<int> smoothedList = [];
|
||||
|
||||
for (int i = 0; i < pcmList.length; i++) {
|
||||
if (i > 0 && i < pcmList.length - 1) {
|
||||
// 简单的三点平均
|
||||
int avg = (pcmList[i - 1] + pcmList[i] * 2 + pcmList[i + 1]) ~/ 4;
|
||||
smoothedList.add(avg);
|
||||
} else {
|
||||
smoothedList.add(pcmList[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return smoothedList;
|
||||
}
|
||||
|
||||
//test测试降噪算法
|
||||
// List<int> preprocessAudio(List<int> pcmList) {
|
||||
// final List<int> processedList = [];
|
||||
@ -753,34 +803,31 @@ class TalkViewLogic extends BaseGetXController {
|
||||
// return processedList;
|
||||
// }
|
||||
|
||||
// 简化的音量调整
|
||||
List<int> adjustVolume(List<int> pcmList, double volume) {
|
||||
final List<int> adjustedPcmList = <int>[];
|
||||
|
||||
for (final int pcmVal in pcmList) {
|
||||
// 调整音量
|
||||
int adjustedPcmVal = (pcmVal * volume).round();
|
||||
|
||||
// 裁剪到 16-bit PCM 范围
|
||||
if (adjustedPcmVal > 32767) {
|
||||
adjustedPcmVal = 32767;
|
||||
} else if (adjustedPcmVal < -32768) {
|
||||
adjustedPcmVal = -32768;
|
||||
}
|
||||
|
||||
adjustedPcmVal = adjustedPcmVal.clamp(-32768, 32767);
|
||||
adjustedPcmList.add(adjustedPcmVal);
|
||||
}
|
||||
|
||||
return adjustedPcmList;
|
||||
}
|
||||
|
||||
// 简化的A-law编码
|
||||
List<int> listLinearToALaw(List<int> pcmList) {
|
||||
// 先调节音量
|
||||
final List<int> adjustedPcmList = adjustVolume(pcmList, 5.0);
|
||||
// 调整音量,使用适中的增益值
|
||||
final List<int> adjustedPcmList = adjustVolume(pcmList, 2.2);
|
||||
|
||||
// 再进行 A-law 编码
|
||||
// 执行A-law编码
|
||||
final List<int> aLawList = <int>[];
|
||||
for (final int pcmVal in adjustedPcmList) {
|
||||
final int aLawVal = linearToALaw(pcmVal);
|
||||
aLawList.add(aLawVal);
|
||||
}
|
||||
|
||||
return aLawList;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user