fix:增加音频解码、降噪
This commit is contained in:
parent
784ea4c31b
commit
3205918212
@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
class G711 {
|
class G711 {
|
||||||
@ -20,6 +21,67 @@ class G711 {
|
|||||||
return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
//711解码为pcm数据
|
//711解码为pcm数据
|
||||||
List<int> convertList(List<int> aLawList) {
|
List<int> convertList(List<int> aLawList) {
|
||||||
// 将 ALawToLinear 函数应用于 List<int>
|
// 将 ALawToLinear 函数应用于 List<int>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user