|
|
@ -771,23 +771,27 @@ class MeetingRecordController extends GetxController |
|
|
waveformController.notifyListeners(); |
|
|
waveformController.notifyListeners(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 计算音频数据的振幅 |
|
|
/// 计算音频数据的振幅。 |
|
|
|
|
|
/// |
|
|
|
|
|
/// 输入是小端 PCM_S16LE 字节流,可能是 mono 或 stereo 交织(LRLR)。 |
|
|
|
|
|
/// 这里按"每个 16-bit 样本"遍历(步长 2 字节),不区分声道,把所有样本一起求 RMS: |
|
|
|
|
|
/// - mono:等价于全采样能量 |
|
|
|
|
|
/// - stereo:L 和 R 都纳入计算,得到双方组合能量;任一边说话都能体现在波形里 |
|
|
|
|
|
/// |
|
|
|
|
|
/// 之前用 `i += 4` 想做 2× 跳跃采样减计算量,但碰到 stereo 时变成"只读 L 跳过 R", |
|
|
|
|
|
/// 导致通话录音的图谱只反映本端(自己),听不到对端的波形——已修。 |
|
|
double _calculateAmplitude(Uint8List audioData) { |
|
|
double _calculateAmplitude(Uint8List audioData) { |
|
|
if (audioData.isEmpty) return 0.0; |
|
|
if (audioData.isEmpty) return 0.0; |
|
|
|
|
|
|
|
|
// 使用RMS (Root Mean Square) 算法,更准确反映音频能量 |
|
|
|
|
|
double sumSquares = 0.0; |
|
|
double sumSquares = 0.0; |
|
|
int sampleCount = 0; |
|
|
int sampleCount = 0; |
|
|
|
|
|
|
|
|
for (int i = 0; i < audioData.length; i += 4) { |
|
|
for (int i = 0; i + 1 < audioData.length; i += 2) { |
|
|
// 跳跃采样,减少计算量 |
|
|
|
|
|
if (i + 1 < audioData.length) { |
|
|
|
|
|
final int sample = (audioData[i + 1] << 8) | audioData[i]; |
|
|
final int sample = (audioData[i + 1] << 8) | audioData[i]; |
|
|
final int signedSample = sample > 32767 ? sample - 65536 : sample; |
|
|
final int signedSample = sample > 32767 ? sample - 65536 : sample; |
|
|
sumSquares += (signedSample * signedSample); |
|
|
sumSquares += (signedSample * signedSample); |
|
|
sampleCount++; |
|
|
sampleCount++; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (sampleCount == 0) return 0.0; |
|
|
if (sampleCount == 0) return 0.0; |
|
|
final rms = sqrt(sumSquares / sampleCount); |
|
|
final rms = sqrt(sumSquares / sampleCount); |
|
|
|