Skip to content

Commit e268876

Browse files
committed
Fix: Use Uint8Array for waveform generation and convert to array for Baileys
- Changed getAudioWaveform return type from number[] to Uint8Array - Add value clamping to ensure 0-100 range - Convert Uint8Array to regular array when passing to Baileys - Add detailed logging for waveform values and types - Update default waveform to use Uint8Array This matches the expected format from Baileys issue #1587 and should properly display waveforms in WhatsApp audio messages.
1 parent ae62168 commit e268876

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,15 +3052,15 @@ export class BaileysStartupService extends ChannelStartupService {
30523052
}
30533053
}
30543054

3055-
private async getAudioWaveform(audioBuffer: Buffer): Promise<number[]> {
3055+
private async getAudioWaveform(audioBuffer: Buffer): Promise<Uint8Array> {
30563056
try {
30573057
this.logger.info('Generating audio waveform...');
30583058
const audioData = await audioDecode(audioBuffer);
30593059
const samples = audioData.getChannelData(0); // Get first channel
30603060
const waveformLength = 64;
30613061
const samplesPerWaveform = Math.floor(samples.length / waveformLength);
30623062

3063-
const waveform: number[] = [];
3063+
const waveform = new Uint8Array(waveformLength);
30643064
for (let i = 0; i < waveformLength; i++) {
30653065
const start = i * samplesPerWaveform;
30663066
const end = start + samplesPerWaveform;
@@ -3069,14 +3069,22 @@ export class BaileysStartupService extends ChannelStartupService {
30693069
sum += Math.abs(samples[j]);
30703070
}
30713071
const avg = sum / samplesPerWaveform;
3072-
waveform.push(Math.floor(avg * 100));
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;
30733075
}
30743076

3075-
this.logger.info(`Generated waveform with ${waveform.length} values`);
3077+
// Log first 10 values for debugging
3078+
const firstValues = Array.from(waveform.slice(0, 10));
3079+
this.logger.info(`Generated waveform with ${waveform.length} values. First 10: [${firstValues.join(', ')}]`);
3080+
this.logger.info(`Waveform type: ${waveform.constructor.name}, isUint8Array: ${waveform instanceof Uint8Array}`);
3081+
30763082
return waveform;
30773083
} catch (error) {
30783084
this.logger.warn(`Failed to generate waveform: ${error.message}, using default`);
3079-
return new Array(64).fill(50);
3085+
const defaultWaveform = new Uint8Array(64);
3086+
defaultWaveform.fill(50);
3087+
return defaultWaveform;
30803088
}
30813089
}
30823090

@@ -3099,11 +3107,14 @@ export class BaileysStartupService extends ChannelStartupService {
30993107

31003108
if (Buffer.isBuffer(convert)) {
31013109
const seconds = await this.getAudioDuration(convert);
3102-
const waveform = await this.getAudioWaveform(convert);
3110+
const waveformU8 = await this.getAudioWaveform(convert);
3111+
// Convert Uint8Array to regular array as required by Baileys
3112+
const waveform = Array.from(waveformU8);
3113+
3114+
this.logger.info(`Sending audio with waveform - seconds: ${seconds}, waveform length: ${waveform.length}, waveform type: ${typeof waveform}, isArray: ${Array.isArray(waveform)}`);
3115+
this.logger.info(`First 10 waveform values: [${waveform.slice(0, 10).join(', ')}]`);
31033116

31043117
const messageContent = { audio: convert, ptt: true, mimetype: 'audio/ogg; codecs=opus', seconds, waveform };
3105-
this.logger.info(`Sending audio with waveform - seconds: ${seconds}, waveform length: ${waveform.length}`);
3106-
this.logger.info(`Message content: ${JSON.stringify({ ptt: messageContent.ptt, mimetype: messageContent.mimetype, seconds: messageContent.seconds, waveformLength: messageContent.waveform?.length })}`);
31073118

31083119
const result = this.sendMessageWithTyping<AnyMessageContent>(
31093120
data.number,

0 commit comments

Comments
 (0)