fix:增加音频解码、降噪

This commit is contained in:
liyi 2025-01-14 13:40:20 +08:00
parent 784ea4c31b
commit 3205918212

View File

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';
import 'package:flutter/services.dart';
class G711 {
@ -20,6 +21,67 @@ class G711 {
return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84;
}
int decodeG711(int encodedValue, bool isALaw) {
if (isALaw) {
// A律解码
encodedValue = ~encodedValue;
int t = ((encodedValue & 0x0F) << 3) + 0x84;
t <<= (encodedValue & 0x70) >> 4;
return (encodedValue & 0x80) != 0 ? 0x84 - t : t - 0x84;
} else {
// μ
encodedValue = ~encodedValue;
int t = ((encodedValue & 0x0F) << 3) + 0x84;
t <<= (encodedValue & 0x70) >> 4;
return (encodedValue & 0x80) != 0 ? 0x84 - t : t - 0x84;
}
}
///
List<int> highPassFilter(
List<int> audioData, int sampleRate, double cutoffFreq) {
final double rc = 1.0 / (2 * pi * cutoffFreq);
final double dt = 1.0 / sampleRate;
final double alpha = rc / (rc + dt);
List<int> filteredData = [];
int prevInput = 0;
int prevOutput = 0;
for (int sample in audioData) {
int output = (alpha * (prevOutput + sample - prevInput)).round();
filteredData.add(output);
prevInput = sample;
prevOutput = output;
}
return filteredData;
}
///
List<int> noiseGate(List<int> audioData, int threshold) {
return audioData
.map((sample) => sample.abs() > threshold ? sample : 0)
.toList();
}
///
List<int> decodeAndDenoise(List<int> encodedData, bool isALaw, int sampleRate,
double cutoffFreq, int threshold) {
// G.711
List<int> decodedData =
encodedData.map((value) => decodeG711(value, isALaw)).toList();
//
List<int> filteredData =
highPassFilter(decodedData, sampleRate, cutoffFreq);
//
List<int> denoisedData = noiseGate(filteredData, threshold);
return denoisedData;
}
//711pcm数据
List<int> convertList(List<int> aLawList) {
// ALawToLinear List<int>