feat: 对讲中APP发送录音到锁声量增大

This commit is contained in:
“DaisyWu” 2025-02-24 09:16:21 +08:00
parent 9aac85f20e
commit 6adc1b41d3
2 changed files with 44 additions and 18 deletions

View File

@ -46,6 +46,8 @@ class TalkViewLogic extends BaseGetXController {
int audioFrameIntervalMs = 20; // 4522FPS
int minFrameIntervalMs = 30; // 33 FPS
int maxFrameIntervalMs = 100; // 1 FPS
//
List<int> _bufferedAudioFrames = <int>[];
///
void _initFlutterPcmSound() {
@ -533,15 +535,17 @@ class TalkViewLogic extends BaseGetXController {
//
Future<void> startProcessingAudio() async {
//
state.voiceProcessor?.addFrameListener(_onFrame);
state.voiceProcessor?.addErrorListener(_onError);
try {
if (await state.voiceProcessor?.hasRecordAudioPermission() ?? false) {
await state.voiceProcessor?.start(state.frameLength, state.sampleRate);
final bool? isRecording = await state.voiceProcessor?.isRecording();
state.isRecordingAudio.value = isRecording!;
state.startRecordingAudioTime.value = DateTime.now();
//
state.voiceProcessor
?.addFrameListeners(<VoiceProcessorFrameListener>[_onFrame]);
state.voiceProcessor?.addErrorListener(_onError);
} else {
// state.errorMessage.value = 'Recording permission not granted';
}
@ -576,23 +580,23 @@ class TalkViewLogic extends BaseGetXController {
//
Future<void> _onFrame(List<int> frame) async {
// 线
// final processedFrame = await compute(preprocessAudio, frame);
// final list = listLinearToALaw(processedFrame);
final List<int> processedFrame = preprocessAudio(frame);
final List<int> list = listLinearToALaw(processedFrame);
_bufferedAudioFrames.addAll(list);
final int ms = DateTime.now().millisecondsSinceEpoch -
state.startRecordingAudioTime.value.millisecondsSinceEpoch;
// UDP
await StartChartManage().sendTalkDataMessage(
talkData: TalkData(
content: list,
contentType: TalkData_ContentTypeE.G711,
durationMs: ms,
),
);
Future.delayed(const Duration(milliseconds: 1000)).whenComplete(() async {
// UDP
await StartChartManage().sendTalkDataMessage(
talkData: TalkData(
content: list,
contentType: TalkData_ContentTypeE.G711,
durationMs: ms,
),
);
});
}
//
@ -662,9 +666,31 @@ class TalkViewLogic extends BaseGetXController {
// return processedList;
// }
List<int> listLinearToALaw(List<int> pcmList) {
final List<int> aLawList = [];
List<int> adjustVolume(List<int> pcmList, double volume) {
final List<int> adjustedPcmList = [];
for (int pcmVal in pcmList) {
//
int adjustedPcmVal = (pcmVal * volume).round();
// 16-bit PCM
if (adjustedPcmVal > 32767) {
adjustedPcmVal = 32767;
} else if (adjustedPcmVal < -32768) {
adjustedPcmVal = -32768;
}
adjustedPcmList.add(adjustedPcmVal);
}
return adjustedPcmList;
}
List<int> listLinearToALaw(List<int> pcmList) {
//
final List<int> adjustedPcmList = adjustVolume(pcmList, 5.0);
// A-law
final List<int> aLawList = [];
for (int pcmVal in adjustedPcmList) {
final int aLawVal = linearToALaw(pcmVal);
aLawList.add(aLawVal);
}

View File

@ -37,7 +37,7 @@ class TalkViewState {
RxList<int> listAudioData = <int>[].obs; //
GlobalKey globalKey = GlobalKey();
Timer? oneMinuteTimeTimer; // 60
Timer? oneMinuteTimeTimer; // 60
RxInt oneMinuteTime = 0.obs; //
// 10
@ -89,4 +89,4 @@ class TalkViewState {
RxBool isLongPressing = false.obs; //
RxBool hasAudioData = false.obs; //
RxInt lastAudioTimestamp = 0.obs; //
}
}