fix:调整音频增益逻辑
This commit is contained in:
parent
404ecf724f
commit
4bbbac11a3
@ -104,10 +104,10 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
|
|||||||
codecType: 'h264',
|
codecType: 'h264',
|
||||||
);
|
);
|
||||||
// 初始化解码器并获取textureId
|
// 初始化解码器并获取textureId
|
||||||
AppLog.log('StartChartManage().videoWidth:${StartChartManage()
|
AppLog.log(
|
||||||
.videoWidth}');
|
'StartChartManage().videoWidth:${StartChartManage().videoWidth}');
|
||||||
AppLog.log('StartChartManage().videoHeight:${StartChartManage()
|
AppLog.log(
|
||||||
.videoHeight}');
|
'StartChartManage().videoHeight:${StartChartManage().videoHeight}');
|
||||||
final textureId = await VideoDecodePlugin.initDecoder(config);
|
final textureId = await VideoDecodePlugin.initDecoder(config);
|
||||||
if (textureId != null) {
|
if (textureId != null) {
|
||||||
Future.microtask(() => state.textureId.value = textureId);
|
Future.microtask(() => state.textureId.value = textureId);
|
||||||
@ -746,7 +746,6 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
|
|||||||
|
|
||||||
//开始录音
|
//开始录音
|
||||||
Future<void> startProcessingAudio() async {
|
Future<void> startProcessingAudio() async {
|
||||||
|
|
||||||
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);
|
||||||
@ -793,27 +792,13 @@ class TalkViewNativeDecodeLogic 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int chunkSize = 320; // 每次发送320字节(10ms G.711)
|
static const int chunkSize = 320; // 每次发送320字节(10ms G.711)
|
||||||
static const int intervalMs = 35; // 每40ms发送一次(4个chunk)
|
static const int intervalMs = 35; // 每40ms发送一次(4个chunk)
|
||||||
void _sendAudioChunk(Timer timer) async {
|
void _sendAudioChunk(Timer timer) async {
|
||||||
@ -849,10 +834,11 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
|
|||||||
List<int> encodedData = G711Tool.encode(applyGain, 0); // 0表示A-law
|
List<int> encodedData = G711Tool.encode(applyGain, 0); // 0表示A-law
|
||||||
_bufferedAudioFrames.addAll(encodedData);
|
_bufferedAudioFrames.addAll(encodedData);
|
||||||
|
|
||||||
|
|
||||||
// 启动定时发送器(仅启动一次)
|
// 启动定时发送器(仅启动一次)
|
||||||
if (_startProcessingAudioTimer == null && _bufferedAudioFrames.length > chunkSize) {
|
if (_startProcessingAudioTimer == null &&
|
||||||
_startProcessingAudioTimer = Timer.periodic(Duration(milliseconds: intervalMs), _sendAudioChunk);
|
_bufferedAudioFrames.length > chunkSize) {
|
||||||
|
_startProcessingAudioTimer =
|
||||||
|
Timer.periodic(Duration(milliseconds: intervalMs), _sendAudioChunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user