1,长按录音的部分逻辑优化处理
2,修改PCM 数据转换为 uLaw 格式的音频数据的方法
This commit is contained in:
parent
0124185e8c
commit
4c9ff85e4a
@ -8,7 +8,6 @@ import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:star_lock/talk/call/callTalk.dart';
|
||||
import 'package:star_lock/talk/udp/udp_talkClass.dart';
|
||||
|
||||
import '../../../../talk/call/g711.dart';
|
||||
import '../../../../talk/udp/udp_manage.dart';
|
||||
import '../../../../talk/udp/udp_senderManage.dart';
|
||||
import '../../../../tools/baseGetXController.dart';
|
||||
@ -113,19 +112,6 @@ class LockMonitoringLogic extends BaseGetXController {
|
||||
udpHangUpAction();
|
||||
state.hangUpSeconds++;
|
||||
|
||||
// //如果已经挂断成功,则取消定时器
|
||||
// if (UDPTalkClass().isEndCall == true) {
|
||||
// print('{$clickIndex}已经挂断成功');
|
||||
// state.hangUpTimer.cancel();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (UDPTalkClass().status == 6) {
|
||||
// print('{$clickIndex}被叫中,已经挂断成功');
|
||||
// state.hangUpTimer.cancel();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// 检查条件,如果达到6秒且未得到应答,则认为失败
|
||||
if (state.hangUpSeconds >= 6) {
|
||||
state.hangUpTimer.cancel(); // 取消定时器
|
||||
@ -177,51 +163,13 @@ class LockMonitoringLogic extends BaseGetXController {
|
||||
Get.back();
|
||||
}
|
||||
|
||||
Future<void> _readG711Data() async {
|
||||
String filePath = 'assets/s10-g711.bin';
|
||||
List<int> audioData = await G711().readAssetFile(filePath);
|
||||
// Get.log('发送读取711文件数据为:$audioData');// 数据为:$audioData
|
||||
// return;
|
||||
// print('发送读取711文件数据长度为:${audioData.length}');// 数据为:$audioData
|
||||
if (audioData.isNotEmpty) {
|
||||
// 在这里处理你的音频数据
|
||||
// pcmBytes = G711().convertList(audioData);
|
||||
// print('发送转换pcmBytes数据长度为:${pcmBytes.length}');
|
||||
|
||||
int start = 0;
|
||||
int length = 320;
|
||||
while (start < audioData.length) {
|
||||
// await Future.delayed(const Duration(milliseconds: 50));
|
||||
|
||||
int end = (start + length > audioData.length)
|
||||
? audioData.length
|
||||
: start + length;
|
||||
List<int> sublist = audioData.sublist(start, end);
|
||||
sendRecordData({
|
||||
"bytes": sublist,
|
||||
// "udpSendDataFrameNumber": 0,
|
||||
"lockID": UDPManage().lockId,
|
||||
"lockIP": UDPManage().host,
|
||||
"userMobile": await state.userUid,
|
||||
"userMobileIP": await state.userMobileIP,
|
||||
});
|
||||
print(sublist);
|
||||
start += length;
|
||||
}
|
||||
print('G711数据发送完成');
|
||||
} else {
|
||||
print('Failed to read audio data.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> startProcessing() async {
|
||||
state.sendEnd.value = false;
|
||||
frameListener(List<int> frame) async {
|
||||
for (int i = 0; i < frame.length; i++) {
|
||||
frame[i] = linearToULaw(frame[i]);
|
||||
}
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
List<int> pcmBytes = listLinearToULaw(frame);
|
||||
await Future.delayed(const Duration(milliseconds: 10));
|
||||
sendRecordData({
|
||||
"bytes": frame,
|
||||
"bytes": pcmBytes,
|
||||
// "udpSendDataFrameNumber": 0,
|
||||
"lockID": UDPManage().lockId,
|
||||
"lockIP": UDPManage().host,
|
||||
@ -234,7 +182,6 @@ class LockMonitoringLogic extends BaseGetXController {
|
||||
print("VoiceProcessorException: $error");
|
||||
}
|
||||
|
||||
;
|
||||
state.voiceProcessor?.addFrameListener(frameListener);
|
||||
state.voiceProcessor?.addErrorListener(errorListener);
|
||||
|
||||
@ -360,6 +307,15 @@ class LockMonitoringLogic extends BaseGetXController {
|
||||
// UDPManage().sendData(topBytes);
|
||||
}
|
||||
|
||||
List<int> listLinearToULaw(List<int> pcmList) {
|
||||
List<int> uLawList = [];
|
||||
for (int pcmVal in pcmList) {
|
||||
int uLawVal = linearToULaw(pcmVal);
|
||||
uLawList.add(uLawVal);
|
||||
}
|
||||
return uLawList;
|
||||
}
|
||||
|
||||
// 拿到的音频转化成pcm
|
||||
int linearToULaw(int pcmVal) {
|
||||
int mask;
|
||||
@ -404,18 +360,6 @@ class LockMonitoringLogic extends BaseGetXController {
|
||||
return size;
|
||||
}
|
||||
|
||||
double _calculateVolumeLevel(List<int> frame) {
|
||||
double rms = 0.0;
|
||||
for (int sample in frame) {
|
||||
rms += pow(sample, 2);
|
||||
}
|
||||
rms = sqrt(rms / frame.length);
|
||||
|
||||
double dbfs = 20 * log(rms / 32767.0) / log(10);
|
||||
double normalizedValue = (dbfs + 50) / 50;
|
||||
return normalizedValue.clamp(0.0, 1.0);
|
||||
}
|
||||
|
||||
Future<bool> getPermissionStatus() async {
|
||||
Permission permission = Permission.microphone;
|
||||
//granted 通过,denied 被拒绝,permanentlyDenied 拒绝且不在提示
|
||||
|
||||
@ -340,5 +340,6 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
logic.stopProcessing();
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +24,8 @@ class LockMonitoringState {
|
||||
var listAudioData = <int>[].obs; //得到的音频流字节数据
|
||||
|
||||
late final VoiceProcessor? voiceProcessor;
|
||||
var sendEnd = false.obs; //发送完成
|
||||
late StreamSubscription<List<int>> frameSubscription;
|
||||
|
||||
late Timer oneMinuteTimeTimer =
|
||||
Timer(const Duration(seconds: 1), () {}); // 定时器超过60秒关闭当前界面
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user