refactor(sdk): Change waveform to be a list of values between 0 and 1

Most clients will probably work with values between 0 and 1 and need to
convert it just to send it, so we can move that conversion into the SDK.

This is also more forwards-compatible, because MSC3246 now has a
different max value for the amplitude, so when this becomes stable, the
only change needed will be in the SDK.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2025-10-01 11:16:44 +02:00
committed by Damir Jelić
parent eb1ee434b3
commit bfc96181dd
3 changed files with 10 additions and 4 deletions

View File

@@ -424,7 +424,7 @@ impl Timeline {
self: Arc<Self>,
params: UploadParameters,
audio_info: AudioInfo,
waveform: Vec<u16>,
waveform: Vec<f32>,
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let mut info =
BaseAudioInfo::try_from(&audio_info).map_err(|_| RoomError::InvalidAttachmentData)?;

View File

@@ -67,7 +67,9 @@ pub struct BaseAudioInfo {
/// The file size of the audio clip in bytes.
pub size: Option<UInt>,
/// The waveform of the audio clip.
pub waveform: Option<Vec<u16>>,
///
/// Must only include values between 0 and 1.
pub waveform: Option<Vec<f32>>,
}
/// Base metadata about a file.

View File

@@ -116,7 +116,7 @@ use ruma::{
message::{
AudioInfo, AudioMessageEventContent, FileInfo, FileMessageEventContent,
ImageMessageEventContent, MessageType, RoomMessageEventContent,
TextMessageEventContent, UnstableAudioDetailsContentBlock,
TextMessageEventContent, UnstableAmplitude, UnstableAudioDetailsContentBlock,
UnstableVoiceContentBlock, VideoInfo, VideoMessageEventContent,
},
name::RoomNameEventContent,
@@ -326,7 +326,11 @@ macro_rules! make_media_type {
if let Some(AttachmentInfo::Audio(audio_info) | AttachmentInfo::Voice(audio_info)) = &$info &&
let Some(duration) = audio_info.duration && let Some(waveform_vec) = &audio_info.waveform {
let waveform = waveform_vec.iter().map(|v| (*v).into()).collect();
let waveform = waveform_vec
.iter()
.map(|v| ((*v).clamp(0.0, 1.0) * UnstableAmplitude::MAX as f32) as u16)
.map(Into::into)
.collect();
content.audio =
Some(UnstableAudioDetailsContentBlock::new(duration, waveform));
}