Enhance audio message recorder with waveform and audio length updates

This commit is contained in:
Rye committed 2026-03-12 03:25:40 +01:00
1 parent 230d410721
commit cd90375959
3 files changed
+41 -10

No files matched your search

+19 -7
View File
@@ -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<HTMLDivElement>(null);
const isDismissedRef = useRef(false);
@@ -52,18 +56,26 @@ export function AudioMessageRecorder({
<Text size="H4">Audio Message Recorder</Text>
<VoiceRecorder
autoStart
onStop={(audioFile) => {
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,
}}
+7 -2
View File
@@ -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<HTMLDivElement, RoomInputProps>(
const [toolbar, setToolbar] = useSetting(settingsAtom, 'editorToolbar');
const [showAudioRecorder, setShowAudioRecorder] = useState(false);
const [audioMsgWaveform, setAudioMsgWaveform] = useState<number[] | undefined>(undefined);
const [audioMsgLength, setAudioMsgLength] = useState<number | undefined>(undefined);
const [autocompleteQuery, setAutocompleteQuery] =
useState<AutocompleteQuery<AutocompletePrefix>>();
const [isQuickTextReact, setQuickTextReact] = useState(false);
@@ -409,7 +412,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
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<HTMLDivElement, RoomInputProps>(
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<HTMLDivElement, RoomInputProps>(
// 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)}
/>
}
/>
+15 -1
View File
@@ -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 = {