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

View File

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