diff --git a/public/index.html b/public/index.html index 1cfe73a..ef5a53a 100644 --- a/public/index.html +++ b/public/index.html @@ -12,6 +12,12 @@ + + + + + +
@@ -524,6 +530,35 @@
+ +
+ + + + + +
+ +
+ +
+
@@ -1168,6 +1203,20 @@ comports: [], + lxmfMessageAudioAttachmentCache: {}, + lxmfAudioModeToCodec2ModeMap: { + // https://github.com/markqvist/LXMF/blob/master/LXMF/LXMF.py#L21 + 0x01: "450PWB", // AM_CODEC2_450PWB + 0x02: "450", // AM_CODEC2_450 + 0x03: "700C", // AM_CODEC2_700C + 0x04: "1200", // AM_CODEC2_1200 + 0x05: "1300", // AM_CODEC2_1300 + 0x06: "1400", // AM_CODEC2_1400 + 0x07: "1600", // AM_CODEC2_1600 + 0x08: "2400", // AM_CODEC2_2400 + 0x09: "3200", // AM_CODEC2_3200 + }, + }; }, mounted: function() { @@ -2317,6 +2366,9 @@ } return window.btoa(binary); }, + base64ToArrayBuffer: function(base64) { + return Uint8Array.from(atob(base64), c => c.charCodeAt(0)); + }, async deleteConversation() { // do nothing if no peer selected @@ -2585,6 +2637,67 @@ return conversation.destination_hash === destinationHash; }); }, + async processAudioForSelectedPeerChatItems() { + for(const chatItem of this.selectedPeerChatItems){ + + // skip if no audio + if(!chatItem.lxmf_message?.fields?.audio){ + continue; + } + + // skip if audio already cached + if(this.lxmfMessageAudioAttachmentCache[chatItem.lxmf_message.hash]){ + continue; + } + + // decode audio to blob url + const objectUrl = await this.decodeLxmfAudioFieldToBlobUrl(chatItem.lxmf_message.fields.audio); + if(!objectUrl){ + continue; + } + + // update audio cache + this.lxmfMessageAudioAttachmentCache[chatItem.lxmf_message.hash] = objectUrl; + + } + }, + async decodeLxmfAudioFieldToBlobUrl(audioField) { + try { + + // get audio mode and audio bytes from audio field + const audioMode = audioField.audio_mode; + const audioBytes = audioField.audio_bytes; + + // determine codec2 mode, or skip if unknown + const codecMode = this.lxmfAudioModeToCodec2ModeMap[audioMode]; + if(!codecMode){ + console.log("unsupported audio mode: " + audioMode) + return null; + } + + // convert base64 to uint8 array + const encoded = this.base64ToArrayBuffer(audioBytes); + + // decode codec2 audio + const decoded = await Codec2Lib.runDecode(codecMode, new Uint8Array(encoded)); + + // convert decoded codec2 to wav audio + const wavAudio = await Codec2Lib.rawToWav(decoded); + + // create blob from wav audio + const blob = new Blob([wavAudio], { + type: "audio/wav", + }); + + // create object url for blob + return URL.createObjectURL(blob); + + } catch(e) { + // failed to decode lxmf audio field + console.log(e); + return null; + } + }, }, computed: { isMobile() { @@ -2701,6 +2814,14 @@ return results; }, }, + watch: { + async selectedPeerChatItems() { + + // chat items for selected peer changed, so lets process any available audio + await this.processAudioForSelectedPeerChatItems(); + + }, + }, }).mount('#app');