From 21d7902b7f8f82f9a090e1eabab79d5f49cdc898 Mon Sep 17 00:00:00 2001 From: Daisy <> Date: Thu, 21 Dec 2023 11:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9G711=E8=A7=A3=E7=A0=81?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=20=E6=96=B0=E5=A2=9EG711=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star_lock/lib/talk/call/callTalk.dart | 23 ++++----- star_lock/lib/talk/call/g711Decoder.dart | 66 +++++++++++------------- star_lock/lib/talk/call/g711Encode.dart | 36 +++++++++++++ 3 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 star_lock/lib/talk/call/g711Encode.dart diff --git a/star_lock/lib/talk/call/callTalk.dart b/star_lock/lib/talk/call/callTalk.dart index 7d62b819..d3418720 100644 --- a/star_lock/lib/talk/call/callTalk.dart +++ b/star_lock/lib/talk/call/callTalk.dart @@ -41,22 +41,19 @@ class CallTalk { CallTalk get manager => _share(); Future getAVData(Uint8List bb, int len) async { - //视频数据 - // String hexData = - // '5858584349449601075439415f396333613730323264346364a5a5a5a5c0a809a3503138363832313530323337000000000000000030303030420e000002000000b33300001f000100b301b301ffd8ffe000114a4649460001010000'; - // Uint8List bb = Uint8List.fromList(hex.decode(hexData)); - //音频数据 - // String hexData = - // '5858584349449601075439415f396333613730323264346364a5a5a5a5c0a809a3503138363832313530323337000000000000000030303030e02900000100050080020000010001004001b30145444444444444444445454444444444444444444444434343434443434344444444444544454545444444444444454545454545454545454445444545454545454545444444444343434344444443434344444444444343434343434343434443434343434343424343434343434242424242424242424243434343434242424242424343434342424343434242424242434243434343434344454344444444444444444444444444454545454544444444444444444445454545454545454545454545454546454545454545454545454545454545454646464646464645454545464646454545454545454545454545454544444444444444444443434343434343434343434343434343434343434242424242424242424242424242424343434242434343434343434343434343434343434343434444444343434343430000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024001d044000000000001d080001000000001d18c03000000000'; - // Uint8List bb = Uint8List.fromList(hex.decode(hexData)); - // 音频数据 if (bb[61] == 1) { - print('dinglingling音频数据来啦啦啦啦啦啦啦啦啦'); + print('dinglingling音频数据来啦啦啦啦啦啦啦啦啦$bb'); // 用你的711音频数据替换这里的Uint8List - Uint8List rawData = G711Decoder().decodeG711uLaw(bb); - _playRawData(rawData); - print('收到音频数据了'); + // Uint8List rawData = G711Decoder().decodeG711uLaw(bb); + // 示例 G711 数据(u-law 或 a-law) + Uint8List g711Data = bb; + // 解码为 PCM 数据 + Uint8List pcmData = G711Decoder().g711Decode(g711Data, G711Type.uLaw); + // 现在你可以使用 pcmData 进行播放或其他处理 + print('得到的pcmData:$pcmData'); + + _playRawData(pcmData); } // 视频数据 else { diff --git a/star_lock/lib/talk/call/g711Decoder.dart b/star_lock/lib/talk/call/g711Decoder.dart index 94d665a3..2b1d5c74 100644 --- a/star_lock/lib/talk/call/g711Decoder.dart +++ b/star_lock/lib/talk/call/g711Decoder.dart @@ -1,44 +1,36 @@ import 'dart:typed_data'; -class G711Decoder { - // G.711 µ-law解码 - Uint8List decodeG711uLaw(Uint8List data) { - List decodedData = []; +enum G711Type { + uLaw, + aLaw, +} - for (int i = 0; i < data.length; i++) { - int sample = _decodeG711uLawSample(data[i]); - decodedData.add(sample); +class G711Decoder { + Uint8List g711Decode(Uint8List g711Data, G711Type g711Type) { + List pcmData = []; + int sign, exponent, mantissa, sample; + + for (int i = 0; i < g711Data.length; i++) { + int value = g711Data[i]; + + if (g711Type == G711Type.uLaw) { + // μ-law decoding + value = ~value & 0xFF; + sign = (value & 0x80) == 0 ? 1 : -1; + exponent = (value & 0x70) >> 4; + mantissa = ((value & 0x0F) << 4) + 0x10; + sample = (mantissa << exponent) * sign; + } else { + // A-law decoding + sign = (value & 0x80) == 0 ? 1 : -1; + exponent = ((value & 0x70) >> 4) + 1; + mantissa = (value & 0x0F) + 0x10; + sample = (mantissa << exponent) * sign; + } + + pcmData.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; // 防止溢出 + return Uint8List.fromList(pcmData); } } - -void main() { - G711Decoder g711Decoder = G711Decoder(); - - // 假设 g711Data 是G.711 µ-law编码的数据 - Uint8List g711Data = - Uint8List.fromList([/* your G.711 µ-law encoded data */]); - - // 解码 - List decodedData = g711Decoder.decodeG711uLaw(g711Data); - - // 处理解码后的数据,例如播放音频 - print(decodedData); -} diff --git a/star_lock/lib/talk/call/g711Encode.dart b/star_lock/lib/talk/call/g711Encode.dart new file mode 100644 index 00000000..5369a366 --- /dev/null +++ b/star_lock/lib/talk/call/g711Encode.dart @@ -0,0 +1,36 @@ +import 'dart:typed_data'; + +enum G711Type { + uLaw, + aLaw, +} + +class G711Decoder { + Uint8List g711Encode(Uint8List pcmData, G711Type g711Type) { + List g711Data = []; + + for (int sample in pcmData) { + int sign = (sample < 0) ? 0x80 : 0; + int magnitude = (sample < 0) ? -sample : sample; + + int exponent = 7; + while (magnitude < (1 << exponent) && exponent > 0) { + exponent--; + } + + int mantissa = (magnitude >> (exponent - 4)) & 0x0F; + + if (g711Type == G711Type.uLaw) { + // μ-law encoding + int value = ~(sign | ((exponent << 4) & 0x70) | (mantissa >> 4)); + g711Data.add(value & 0xFF); + } else { + // A-law encoding + int value = sign | ((exponent << 4) & 0x70) | (mantissa >> 4); + g711Data.add(value & 0xFF); + } + } + + return Uint8List.fromList(g711Data); + } +}