From cd903759597307ee522e950eef65e6bed1262225 Mon Sep 17 00:00:00 2001 From: Rye Date: Thu, 12 Mar 2026 03:25:40 +0100 Subject: [PATCH] Enhance audio message recorder with waveform and audio length updates --- .../features/room/AudioMessageRecorder.tsx | 26 ++++++++++++++----- src/app/features/room/RoomInput.tsx | 9 +++++-- src/app/features/room/msgContent.ts | 16 +++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/app/features/room/AudioMessageRecorder.tsx b/src/app/features/room/AudioMessageRecorder.tsx index 8d519bd1..38ebe721 100644 --- a/src/app/features/room/AudioMessageRecorder.tsx +++ b/src/app/features/room/AudioMessageRecorder.tsx @@ -1,20 +1,24 @@ import FocusTrap from 'focus-trap-react'; import { Box, Text, color, config } from 'folds'; import { useRef } from 'react'; -import { VoiceRecorder } from 'react-voice-recorder-kit'; +import { VoiceRecorder } from 'src/voice-recorder-kit'; type AudioMessageRecorderProps = { onRecordingComplete: (audioBlob: Blob) => void; onRequestClose: () => void; + onWaveformUpdate: (waveform: number[]) => void; + onAudioLengthUpdate: (length: number) => void; }; // We use a react voice recorder library to handle the recording of audio messages, as it provides a simple API and handles the complexities of recording audio in the browser. // The component is wrapped in a focus trap to ensure that keyboard users can easily navigate and interact with the recorder without accidentally losing focus or interacting with other parts of the UI. // The styling is kept simple and consistent with the rest of the app, using Folds' design tokens for colors, spacing, and typography. -// we use https://www.npmjs.com/package/react-voice-recorder-kit for the recording +// we use a modified version of https://www.npmjs.com/package/react-voice-recorder-kit for the recording export function AudioMessageRecorder({ onRecordingComplete, onRequestClose, + onWaveformUpdate, + onAudioLengthUpdate, }: AudioMessageRecorderProps) { const containerRef = useRef(null); const isDismissedRef = useRef(false); @@ -52,18 +56,26 @@ export function AudioMessageRecorder({ Audio Message Recorder { + onStop={({ + audioFile, + waveform, + audioLength, + }: { + audioFile: Blob; + waveform: number[]; + audioLength: number; + }) => { if (isDismissedRef.current) return; // closes the recorder and sends the audio file back to the parent component to be uploaded and sent as a message onRecordingComplete(audioFile); + onWaveformUpdate(waveform); + onAudioLengthUpdate(audioLength); }} + onWaveformUpdate={onWaveformUpdate} + onAudioLengthUpdate={onAudioLengthUpdate} buttonBackgroundColor={color.SurfaceVariant.Container} buttonHoverBackgroundColor={color.SurfaceVariant.ContainerHover} iconColor={color.Primary.Main} - visualizerBarColor={(level) => { - const hue = (level * 120).toString(); - return `hsl(${hue}, 70%, 50%)`; - }} style={{ backgroundColor: color.Surface.ContainerActive, }} diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 250bbc9c..fee426e5 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -158,6 +158,7 @@ import { } from './msgContent'; import { CommandAutocomplete } from './CommandAutocomplete'; import { AudioMessageRecorder } from './AudioMessageRecorder'; +import { i } from 'node_modules/vite/dist/node/chunks/moduleRunnerTransport'; const getReplyContent = (replyDraft: IReplyDraft | undefined): IEventRelation => { if (!replyDraft) return {}; @@ -228,6 +229,8 @@ export const RoomInput = forwardRef( const [toolbar, setToolbar] = useSetting(settingsAtom, 'editorToolbar'); const [showAudioRecorder, setShowAudioRecorder] = useState(false); + const [audioMsgWaveform, setAudioMsgWaveform] = useState(undefined); + const [audioMsgLength, setAudioMsgLength] = useState(undefined); const [autocompleteQuery, setAutocompleteQuery] = useState>(); const [isQuickTextReact, setQuickTextReact] = useState(false); @@ -409,7 +412,7 @@ export const RoomInput = forwardRef( return getVideoMsgContent(mx, fileItem, upload.mxc); } if (fileItem.file.type.startsWith('audio')) { - return getAudioMsgContent(fileItem, upload.mxc); + return getAudioMsgContent(fileItem, upload.mxc, audioMsgWaveform, audioMsgLength); } return getFileMsgContent(fileItem, upload.mxc); }); @@ -977,7 +980,7 @@ export const RoomInput = forwardRef( onRecordingComplete={(audioBlob) => { const file = new File( [audioBlob], - `sable-audio-message-${Date.now()}.webm`, + `sable-audio-message-${Date.now()}.ogg`, { type: audioBlob.type, } @@ -986,6 +989,8 @@ export const RoomInput = forwardRef( // Close the recorder after handling the file, to give some feedback that the recording was successful setShowAudioRecorder(false); }} + onAudioLengthUpdate={(len) => setAudioMsgLength(len)} + onWaveformUpdate={(w) => setAudioMsgWaveform(w)} /> } /> diff --git a/src/app/features/room/msgContent.ts b/src/app/features/room/msgContent.ts index 3ecc0771..5536f7d0 100644 --- a/src/app/features/room/msgContent.ts +++ b/src/app/features/room/msgContent.ts @@ -143,7 +143,17 @@ export const getVideoMsgContent = async ( return content; }; -export const getAudioMsgContent = (item: TUploadItem, mxc: string): IContent => { +export type AudioMsgContent = IContent & { + waveform?: number[]; + audioLength?: number; +}; + +export const getAudioMsgContent = ( + item: TUploadItem, + mxc: string, + waveform?: number[], + audioLength?: number +): AudioMsgContent => { const { file, encInfo } = item; const content: IContent = { msgtype: MsgType.Audio, @@ -155,6 +165,10 @@ export const getAudioMsgContent = (item: TUploadItem, mxc: string): IContent => mimetype: file.type, size: file.size, }, + 'org.matrix.msc1767.audio': { + waveform: waveform?.map((v) => Math.round(v * 1024)), // scale waveform values to fit in 10 bits (0-1024) for more efficient storage, as per MSC1767 spec + duration: item.metadata.markedAsSpoiler || !audioLength ? 0 : audioLength * 1000, // if marked as spoiler, set duration to 0 to hide it in clients that support msc1767 + }, }; if (encInfo) { content.file = {