diff --git a/lib/talk/starChart/views/talkView/talk_view_logic.dart b/lib/talk/starChart/views/talkView/talk_view_logic.dart index fb7abd92..7ed217f6 100644 --- a/lib/talk/starChart/views/talkView/talk_view_logic.dart +++ b/lib/talk/starChart/views/talkView/talk_view_logic.dart @@ -658,8 +658,22 @@ class TalkViewLogic extends BaseGetXController { // 音频帧处理 Future _onFrame(List 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 processedFrame = preprocessAudio(frame); - final List list = listLinearToALaw(processedFrame); + // 添加平滑处理 + final List smoothedFrame = smoothAudio(processedFrame); + final List 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 preprocessAudio(List pcmList) { - // 简单的降噪处理 final List 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 smoothAudio(List pcmList) { + final List 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 preprocessAudio(List pcmList) { // final List processedList = []; @@ -753,34 +803,31 @@ class TalkViewLogic extends BaseGetXController { // return processedList; // } +// 简化的音量调整 List adjustVolume(List pcmList, double volume) { final List adjustedPcmList = []; + 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 listLinearToALaw(List pcmList) { - // 先调节音量 - final List adjustedPcmList = adjustVolume(pcmList, 5.0); + // 调整音量,使用适中的增益值 + final List adjustedPcmList = adjustVolume(pcmList, 2.2); - // 再进行 A-law 编码 + // 执行A-law编码 final List aLawList = []; for (final int pcmVal in adjustedPcmList) { final int aLawVal = linearToALaw(pcmVal); aLawList.add(aLawVal); } + return aLawList; }