Skip to content

Commit 88fc7b6

Browse files
committed
Normalize waveform values to 0-100 range for better visibility
1 parent af21e63 commit 88fc7b6

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,7 +3060,8 @@ export class BaileysStartupService extends ChannelStartupService {
30603060
const waveformLength = 64;
30613061
const samplesPerWaveform = Math.floor(samples.length / waveformLength);
30623062

3063-
const waveform = new Uint8Array(waveformLength);
3063+
// First pass: calculate raw averages
3064+
const rawValues: number[] = [];
30643065
for (let i = 0; i < waveformLength; i++) {
30653066
const start = i * samplesPerWaveform;
30663067
const end = start + samplesPerWaveform;
@@ -3069,9 +3070,24 @@ export class BaileysStartupService extends ChannelStartupService {
30693070
sum += Math.abs(samples[j]);
30703071
}
30713072
const avg = sum / samplesPerWaveform;
3072-
// Convert to 0-100 range and clamp values
3073-
const value = Math.min(100, Math.max(0, Math.floor(avg * 100)));
3074-
waveform[i] = value;
3073+
rawValues.push(avg);
3074+
}
3075+
3076+
// Find max value for normalization
3077+
const maxValue = Math.max(...rawValues);
3078+
this.logger.info(`Raw waveform max value: ${maxValue}`);
3079+
3080+
// Second pass: normalize to 0-100 range
3081+
const waveform = new Uint8Array(waveformLength);
3082+
if (maxValue > 0) {
3083+
for (let i = 0; i < waveformLength; i++) {
3084+
// Normalize: (value / max) * 100, ensure minimum of 5 for non-zero values
3085+
const normalized = Math.floor((rawValues[i] / maxValue) * 100);
3086+
waveform[i] = rawValues[i] > 0 ? Math.max(5, Math.min(100, normalized)) : 0;
3087+
}
3088+
} else {
3089+
// If all values are zero, create a flat waveform at 50
3090+
waveform.fill(50);
30753091
}
30763092

30773093
// Log first 10 values for debugging

0 commit comments

Comments
 (0)