2023-12-26 16:30:06 +08:00

112 lines
3.5 KiB
Dart

import 'dart:async';
import 'dart:ffi' as ffi;
import 'dart:io';
import 'dart:typed_data';
import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter/return_code.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
class G711 {
Future<List<int>> readAssetFile(String assetPath) async {
ByteData data = await rootBundle.load(assetPath);
List<int> bytes = data.buffer.asUint8List();
return bytes;
}
int ALawToLinear(int aVal) {
// 取反
aVal = ~aVal;
// 计算偏移
int t = ((aVal & 0x0F) << 3) + 0x84;
t <<= (aVal & 0x70) >> 4;
// 根据符号位决定返回值的正负
return (aVal & 0x80) != 0 ? 0x84 - t : t - 0x84;
}
List<int> convertList(List<int> aLawList) {
// 将 ALawToLinear 函数应用于 List<int>
List<int> linearList = aLawList.map(ALawToLinear).toList();
return linearList;
}
Future<Uint8List?> decodeG711ToPCM(List<int> g711Data) async {
// FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
// Save G.711 data to a temporary file
Directory appDocDir = await getApplicationDocumentsDirectory();
String g711FilePath = '${appDocDir.path}/input.g711';
await File(g711FilePath).writeAsBytes(g711Data);
// Replace with the desired output PCM file path
String pcmFilePath = '${appDocDir.path}/output.pcm';
// Run FFmpeg command to decode G.711 to PCM
// String command =
// '-y -f u8 -ar 8000 -ac 1 -i $g711FilePath -f s16le -ar 8000 -ac 1 $pcmFilePath';
try {
final session = await FFmpegKit.execute(
'-y -f u8 -ar 8000 -ac 1 -i $g711FilePath -f s16le -ar 8000 -ac 1 $pcmFilePath',
);
final returnCode = await session.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
Uint8List pcmBytes = await File(pcmFilePath).readAsBytes();
return pcmBytes;
} else {
print('FFmpeg execution failed with rc=$returnCode');
// 处理执行失败的情况
}
} catch (e) {
print('Error executing FFmpeg command: $e');
// 处理异常情况
}
return null;
}
// Future<Uint8List?> encodePCMToG711(Uint8List pcmData) async {
// try {
// // FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
// // Save PCM data to a temporary file
// Directory appDocDir = await getApplicationDocumentsDirectory();
// String pcmFilePath = '${appDocDir.path}/input.pcm';
// await File(pcmFilePath).writeAsBytes(pcmData);
// // Replace with the desired output G.711 file path
// String g711FilePath = '${appDocDir.path}/output.g711';
// // Run FFmpeg command to encode PCM to G.711
// String command =
// '-y -f s16le -ar 8000 -ac 1 -i $pcmFilePath -f g711 -ar 8000 -ac 1 $g711FilePath';
// int result = await flutterFFmpeg.execute(command);
// if (result == 0) {
// print(
// 'PCM encoding to G.711 successful! G.711 file saved at: $g711FilePath');
// // Read G.711 data from file
// Uint8List g711Bytes = await File(g711FilePath).readAsBytes();
// // Delete the temporary PCM file
// await File(pcmFilePath).delete();
// // Delete the temporary G.711 file
// await File(g711FilePath).delete();
// return g711Bytes;
// } else {
// print('Error during PCM encoding to G.711: $result');
// return null;
// }
// } catch (e) {
// print('Error: $e');
// return null;
// }
// }
}