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 {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user