feat: send audio with waveform (#2309)
* added generateWaveform * docs updated * added usage example * ESLint fix * fixed: undefined returned on waveform generation failure * fix: ESLint
This commit is contained in:
parent
b47998878a
commit
df1b070d54
4 changed files with 45 additions and 2 deletions
|
|
@ -390,6 +390,9 @@ exports.LoadUtils = () => {
|
|||
|
||||
if (forceVoice && mediaData.type === 'audio') {
|
||||
mediaData.type = 'ptt';
|
||||
const waveform = mediaObject.contentInfo.waveform;
|
||||
mediaData.waveform =
|
||||
waveform ?? await window.WWebJS.generateWaveform(file);
|
||||
}
|
||||
|
||||
if (forceGif && mediaData.type === 'video') {
|
||||
|
|
@ -632,6 +635,42 @@ exports.LoadUtils = () => {
|
|||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Referenced from and modified:
|
||||
* @see https://github.com/wppconnect-team/wa-js/commit/290ebfefe6021b3d17f7fdfdda5545bb0473b26f
|
||||
*/
|
||||
window.WWebJS.generateWaveform = async (audioFile) => {
|
||||
try {
|
||||
const audioData = await audioFile.arrayBuffer();
|
||||
const audioContext = new AudioContext();
|
||||
const audioBuffer = await audioContext.decodeAudioData(audioData);
|
||||
|
||||
const rawData = audioBuffer.getChannelData(0);
|
||||
const samples = 64;
|
||||
const blockSize = Math.floor(rawData.length / samples);
|
||||
const filteredData = [];
|
||||
for (let i = 0; i < samples; i++) {
|
||||
const blockStart = blockSize * i;
|
||||
let sum = 0;
|
||||
for (let j = 0; j < blockSize; j++) {
|
||||
sum = sum + Math.abs(rawData[blockStart + j]);
|
||||
}
|
||||
filteredData.push(sum / blockSize);
|
||||
}
|
||||
|
||||
const multiplier = Math.pow(Math.max(...filteredData), -1);
|
||||
const normalizedData = filteredData.map((n) => n * multiplier);
|
||||
|
||||
const waveform = new Uint8Array(
|
||||
normalizedData.map((n) => Math.floor(100 * n))
|
||||
);
|
||||
|
||||
return waveform;
|
||||
} catch (e) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
window.WWebJS.sendClearChat = async (chatId) => {
|
||||
let chat = window.Store.Chat.get(chatId);
|
||||
if (chat !== undefined) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue