feat: Add waveforms to voice messages (#180)

Co-authored-by: Edgard <edgardmessias@gmail.com>
This commit is contained in:
Codeboss
2023-07-14 03:08:11 +03:00
committed by GitHub
parent e0e7d40847
commit 2c76713565
7 changed files with 622 additions and 455 deletions

View File

@@ -26,7 +26,7 @@ import {
import { isJidGroup, jidNormalizedUser } from '../WABinary'
import { sha256 } from './crypto'
import { generateMessageID, getKeyAuthor, unixTimestampSeconds } from './generics'
import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, MediaDownloadOptions } from './messages-media'
import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, getAudioWaveform, MediaDownloadOptions } from './messages-media'
type MediaUploadData = {
media: WAMediaUpload
@@ -39,6 +39,7 @@ type MediaUploadData = {
mimetype?: string
width?: number
height?: number
waveform?: Uint8Array
}
const MIMETYPE_MAP: { [T in MediaType]?: string } = {
@@ -138,6 +139,7 @@ export const prepareWAMessageMedia = async(
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined'
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
(typeof uploadData['jpegThumbnail'] === 'undefined')
const requiresWaveformProcessing = mediaType === 'audio' && uploadData?.ptt === true
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
const {
mediaKey,
@@ -188,6 +190,11 @@ export const prepareWAMessageMedia = async(
uploadData.seconds = await getAudioDuration(bodyPath!)
logger?.debug('computed audio duration')
}
if(requiresWaveformProcessing) {
uploadData.waveform = await getAudioWaveform(bodyPath!, logger)
logger?.debug('processed waveform')
}
} catch(error) {
logger?.warn({ trace: error.stack }, 'failed to obtain extra info')
}