fix:调整音频结束时继续发送为0的音频包,而不是立即中断
This commit is contained in:
parent
d0b8f76303
commit
22887310c0
@ -602,12 +602,23 @@ class ImageTransmissionLogic extends BaseGetXController {
|
|||||||
} on PlatformException catch (ex) {
|
} on PlatformException catch (ex) {
|
||||||
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
||||||
} finally {
|
} finally {
|
||||||
|
// 延迟关闭定时器,确保剩余数据能发送出去
|
||||||
|
if (_startProcessingAudioTimer != null) {
|
||||||
|
// 插入5个320长度的全0数据包
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
_bufferedAudioFrames.addAll(List.filled(chunkSize, 0));
|
||||||
|
}
|
||||||
|
Future.delayed(const Duration(milliseconds: 300), () {
|
||||||
|
_startProcessingAudioTimer?.cancel();
|
||||||
|
_startProcessingAudioTimer = null;
|
||||||
|
_bufferedAudioFrames.clear();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_bufferedAudioFrames.clear();
|
||||||
|
}
|
||||||
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
||||||
state.isRecordingAudio.value = isRecording!;
|
state.isRecordingAudio.value = isRecording!;
|
||||||
}
|
}
|
||||||
_startProcessingAudioTimer?.cancel();
|
|
||||||
_startProcessingAudioTimer = null;
|
|
||||||
_bufferedAudioFrames.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int chunkSize = 320; // 每次发送320字节(10ms G.711)
|
static const int chunkSize = 320; // 每次发送320字节(10ms G.711)
|
||||||
@ -656,27 +667,11 @@ class ImageTransmissionLogic extends BaseGetXController {
|
|||||||
AppLog.log(error.message!);
|
AppLog.log(error.message!);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加音频增益处理方法
|
|
||||||
List<int> _applyGain(List<int> pcmData, double gainFactor) {
|
List<int> _applyGain(List<int> pcmData, double gainFactor) {
|
||||||
List<int> result = List<int>.filled(pcmData.length, 0);
|
return pcmData.map((sample) {
|
||||||
|
// 增益并裁剪
|
||||||
for (int i = 0; i < pcmData.length; i++) {
|
int amplified = (sample * gainFactor).round();
|
||||||
// PCM数据通常是有符号的16位整数
|
return amplified.clamp(-32768, 32767);
|
||||||
int sample = pcmData[i];
|
}).toList();
|
||||||
|
|
||||||
// 应用增益
|
|
||||||
double amplified = sample * gainFactor;
|
|
||||||
|
|
||||||
// 限制在有效范围内,防止溢出
|
|
||||||
if (amplified > 32767) {
|
|
||||||
amplified = 32767;
|
|
||||||
} else if (amplified < -32768) {
|
|
||||||
amplified = -32768;
|
|
||||||
}
|
|
||||||
|
|
||||||
result[i] = amplified.toInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -782,12 +782,23 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
|
|||||||
} on PlatformException catch (ex) {
|
} on PlatformException catch (ex) {
|
||||||
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
||||||
} finally {
|
} finally {
|
||||||
|
// 延迟关闭定时器,确保剩余数据能发送出去
|
||||||
|
if (_startProcessingAudioTimer != null) {
|
||||||
|
// 插入5个320长度的全0数据包
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
_bufferedAudioFrames.addAll(List.filled(chunkSize, 0));
|
||||||
|
}
|
||||||
|
Future.delayed(const Duration(milliseconds: 300), () {
|
||||||
|
_startProcessingAudioTimer?.cancel();
|
||||||
|
_startProcessingAudioTimer = null;
|
||||||
|
_bufferedAudioFrames.clear();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_bufferedAudioFrames.clear();
|
||||||
|
}
|
||||||
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
||||||
state.isRecordingAudio.value = isRecording!;
|
state.isRecordingAudio.value = isRecording!;
|
||||||
}
|
}
|
||||||
_startProcessingAudioTimer?.cancel();
|
|
||||||
_startProcessingAudioTimer = null;
|
|
||||||
_bufferedAudioFrames.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加音频增益处理方法
|
// 添加音频增益处理方法
|
||||||
|
|||||||
@ -599,12 +599,23 @@ class TalkViewLogic extends BaseGetXController {
|
|||||||
} on PlatformException catch (ex) {
|
} on PlatformException catch (ex) {
|
||||||
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
||||||
} finally {
|
} finally {
|
||||||
|
// 延迟关闭定时器,确保剩余数据能发送出去
|
||||||
|
if (_startProcessingAudioTimer != null) {
|
||||||
|
// 插入5个320长度的全0数据包
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
_bufferedAudioFrames.addAll(List.filled(chunkSize, 0));
|
||||||
|
}
|
||||||
|
Future.delayed(const Duration(milliseconds: 300), () {
|
||||||
|
_startProcessingAudioTimer?.cancel();
|
||||||
|
_startProcessingAudioTimer = null;
|
||||||
|
_bufferedAudioFrames.clear();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_bufferedAudioFrames.clear();
|
||||||
|
}
|
||||||
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
||||||
state.isRecordingAudio.value = isRecording!;
|
state.isRecordingAudio.value = isRecording!;
|
||||||
}
|
}
|
||||||
_startProcessingAudioTimer?.cancel();
|
|
||||||
_startProcessingAudioTimer = null;
|
|
||||||
_bufferedAudioFrames.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int chunkSize = 320; // 每次发送320字节(10ms G.711)
|
static const int chunkSize = 320; // 每次发送320字节(10ms G.711)
|
||||||
@ -656,25 +667,10 @@ class TalkViewLogic extends BaseGetXController {
|
|||||||
|
|
||||||
// 添加音频增益处理方法
|
// 添加音频增益处理方法
|
||||||
List<int> _applyGain(List<int> pcmData, double gainFactor) {
|
List<int> _applyGain(List<int> pcmData, double gainFactor) {
|
||||||
List<int> result = List<int>.filled(pcmData.length, 0);
|
return pcmData.map((sample) {
|
||||||
|
// 增益并裁剪
|
||||||
for (int i = 0; i < pcmData.length; i++) {
|
int amplified = (sample * gainFactor).round();
|
||||||
// PCM数据通常是有符号的16位整数
|
return amplified.clamp(-32768, 32767);
|
||||||
int sample = pcmData[i];
|
}).toList();
|
||||||
|
|
||||||
// 应用增益
|
|
||||||
double amplified = sample * gainFactor;
|
|
||||||
|
|
||||||
// 限制在有效范围内,防止溢出
|
|
||||||
if (amplified > 32767) {
|
|
||||||
amplified = 32767;
|
|
||||||
} else if (amplified < -32768) {
|
|
||||||
amplified = -32768;
|
|
||||||
}
|
|
||||||
|
|
||||||
result[i] = amplified.toInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user