fix:调整对讲时的音频问题

This commit is contained in:
liyi 2025-04-02 16:44:46 +08:00
parent 3e565ab7d7
commit ee66d43c15

View File

@ -658,8 +658,22 @@ class TalkViewLogic extends BaseGetXController {
// //
Future<void> _onFrame(List<int> frame) async { 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> processedFrame = preprocessAudio(frame);
final List<int> list = listLinearToALaw(processedFrame); //
final List<int> smoothedFrame = smoothAudio(processedFrame);
final List<int> list = listLinearToALaw(smoothedFrame);
_bufferedAudioFrames.addAll(list); _bufferedAudioFrames.addAll(list);
final int ms = DateTime.now().millisecondsSinceEpoch - final int ms = DateTime.now().millisecondsSinceEpoch -
@ -671,6 +685,15 @@ class TalkViewLogic extends BaseGetXController {
} }
if (_bufferedAudioFrames.length >= getFrameLength) { 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 // UDP
await StartChartManage() await StartChartManage()
.sendTalkDataMessage( .sendTalkDataMessage(
@ -691,19 +714,46 @@ class TalkViewLogic extends BaseGetXController {
AppLog.log(error.message!); AppLog.log(error.message!);
} }
//
List<int> preprocessAudio(List<int> pcmList) { List<int> preprocessAudio(List<int> pcmList) {
//
final List<int> processedList = []; final List<int> processedList = [];
final int noiseThreshold = 300; //
for (int pcmVal in pcmList) { for (int pcmVal in pcmList) {
// 0 //
if (pcmVal.abs() < 200) { if (pcmVal.abs() < noiseThreshold) {
pcmVal = 0; 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); processedList.add(pcmVal);
} }
return processedList; 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测试降噪算法 //test测试降噪算法
// List<int> preprocessAudio(List<int> pcmList) { // List<int> preprocessAudio(List<int> pcmList) {
// final List<int> processedList = []; // final List<int> processedList = [];
@ -753,34 +803,31 @@ class TalkViewLogic extends BaseGetXController {
// return processedList; // return processedList;
// } // }
//
List<int> adjustVolume(List<int> pcmList, double volume) { List<int> adjustVolume(List<int> pcmList, double volume) {
final List<int> adjustedPcmList = <int>[]; final List<int> adjustedPcmList = <int>[];
for (final int pcmVal in pcmList) { for (final int pcmVal in pcmList) {
//
int adjustedPcmVal = (pcmVal * volume).round(); int adjustedPcmVal = (pcmVal * volume).round();
adjustedPcmVal = adjustedPcmVal.clamp(-32768, 32767);
// 16-bit PCM
if (adjustedPcmVal > 32767) {
adjustedPcmVal = 32767;
} else if (adjustedPcmVal < -32768) {
adjustedPcmVal = -32768;
}
adjustedPcmList.add(adjustedPcmVal); adjustedPcmList.add(adjustedPcmVal);
} }
return adjustedPcmList; return adjustedPcmList;
} }
// A-law编码
List<int> listLinearToALaw(List<int> pcmList) { 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>[]; final List<int> aLawList = <int>[];
for (final int pcmVal in adjustedPcmList) { for (final int pcmVal in adjustedPcmList) {
final int aLawVal = linearToALaw(pcmVal); final int aLawVal = linearToALaw(pcmVal);
aLawList.add(aLawVal); aLawList.add(aLawVal);
} }
return aLawList; return aLawList;
} }