45 lines
1.0 KiB
Dart
45 lines
1.0 KiB
Dart
import 'dart:typed_data';
|
|
|
|
class G711Decoder {
|
|
// G.711 µ-law解码
|
|
Uint8List decodeG711uLaw(Uint8List data) {
|
|
List<int> decodedData = [];
|
|
|
|
for (int i = 0; i < data.length; i++) {
|
|
int sample = _decodeG711uLawSample(data[i]);
|
|
decodedData.add(sample);
|
|
}
|
|
|
|
return Uint8List.fromList(decodedData);
|
|
}
|
|
|
|
// G.711 µ-law样本解码
|
|
int _decodeG711uLawSample(int uLawByte) {
|
|
const int bias = 0x84;
|
|
const int mask = 0x7F;
|
|
|
|
uLawByte = ~uLawByte;
|
|
int sign = uLawByte & 0x80;
|
|
int exponent = (uLawByte & 0x70) >> 4;
|
|
int mantissa = uLawByte & 0x0F;
|
|
|
|
int sample = ((bias << exponent) + mantissa) * (sign == 0 ? 1 : -1);
|
|
|
|
return sample & mask; // 防止溢出
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
G711Decoder g711Decoder = G711Decoder();
|
|
|
|
// 假设 g711Data 是G.711 µ-law编码的数据
|
|
Uint8List g711Data =
|
|
Uint8List.fromList([/* your G.711 µ-law encoded data */]);
|
|
|
|
// 解码
|
|
List<int> decodedData = g711Decoder.decodeG711uLaw(g711Data);
|
|
|
|
// 处理解码后的数据,例如播放音频
|
|
print(decodedData);
|
|
}
|