//! The voice player's core half on one phone (the voice player, 23 Nov 2026, //! `assets/design/chat/voice-player.html`): the bars, played, the speed, and //! waveforms for old voice messages. Invented people and recordings. use super::*; /// A voice message in Asha's chat: hers (`out` false) or the person's own, live or imported, `ms` long. fn voice(handle: &CottonHandle, id: &str, out: bool, origin: &str, ms: u64, wave: &[u8], at: &str, seq: i64) { let channel = if origin == "live" { "cotton" } else { "whatsapp" }; message(handle, id, ASHA, out, channel, origin, "", at, seq, None); let meta = json!({ "cotton": { "written_at": at, "pictures": [{ "file": format!("f-{id}"), "key": "A".repeat(43), "sha256": "00", "mime": "audio/mp4", "bytes": 4_000, "kind": "voice", "duration_ms": ms, "waveform": wave }] } }); sql(handle, "UPDATE cotton_msg_messages SET meta = ? WHERE id = ?", &[t(&meta.to_string()), t(id)]); } fn asha(handle: &CottonHandle) { contact(handle, "c-asha", "Asha Example", "15550100001"); thread(handle, ASHA, "dm", Some("cotton:+15550100001"), None, Some("c-asha"), &iso(0)); } fn meta_of(handle: &CottonHandle, id: &str) -> Value { let id = id.to_string(); let raw = handle .client() .with_writer(|replica| Ok(replica.driver.get("SELECT meta FROM cotton_msg_messages WHERE id = ?", &[t(&id)]).map_err(|e| e.to_string())?.map(|r| crate::phone::passwords::json_row(&r)))) .unwrap() .unwrap(); crate::phone::cotton_chat::meta_value(&raw["meta"]) } // RULE: A voice part carries its length as a number and the core's 36 bars, scaled to its loudest sample, so // both phones draw the same bars; a short note's few samples are stretched. #[test] fn a_voice_part_carries_its_length_and_36_bars() { let (_dir, handle) = phone(); asha(&handle); let long: Vec = (0..64).map(|i| (40 + (i * 37) % 120) as u8).collect(); voice(&handle, "m-v1", false, "live", 42_000, &long, &iso(20), 1); voice(&handle, "m-v2", false, "live", 2_000, &[20, 60, 100, 60, 20, 10, 50, 80, 30], &iso(19), 2); let page = answer(&handle, "chat.thread", json!({ "thread": ASHA })); let part = &item(&page, "m-v1")["media"][0]; assert_eq!((part["durationMs"].as_u64(), part["length"].as_str()), (Some(42_000), Some("0:42")), "{part}"); let bars = part["bars"].as_array().expect("bars"); assert_eq!(bars.len(), 36); assert_eq!(bars.iter().filter_map(Value::as_f64).fold(0.0, f64::max), 1.0, "scaled to its loudest"); assert_eq!(part["waveform"].as_array().map(Vec::len), Some(64), "the waveform stays for phones that know nothing of bars"); let short = item(&page, "m-v2")["media"][0]["bars"].as_array().expect("a short note's bars").clone(); assert_eq!(short.len(), 36, "nine samples stretched to 36"); assert_eq!(short[0], short[3], "each bar takes the sample under it"); } // RULE: Only their voice messages can be not played. Once played a second on one of the person's phones // (chat.act played) it is played on their own copy (meta.cotton.played_at), for good, and nothing is told to the // sender. Their own and an imported note are always played. #[test] fn a_voice_message_of_theirs_is_not_played_until_played() { let (_dir, handle) = phone(); asha(&handle); let wave = [30u8, 90, 200, 120, 60]; voice(&handle, "m-theirs", false, "live", 9_000, &wave, &iso(30), 1); voice(&handle, "m-mine", true, "live", 9_000, &wave, &iso(29), 2); voice(&handle, "m-imported", false, "imported", 9_000, &wave, &iso(28), 0); let page = answer(&handle, "chat.thread", json!({ "thread": ASHA })); assert_eq!(item(&page, "m-theirs")["media"][0]["played"], false, "{page}"); assert!(item(&page, "m-mine")["media"][0].get("played").is_none(), "the person's own is played"); assert!(item(&page, "m-imported")["media"][0].get("played").is_none(), "an imported note is drawn played"); let played = answer(&handle, "chat.act", json!({ "act": "played", "thread": ASHA, "message": "m-theirs" })); assert!(played["bubble"]["media"][0].get("played").is_none(), "the bubble answered is played: {played}"); assert_eq!(played["flush"], false, "nothing to send: the sender is never told"); let at = meta_of(&handle, "m-theirs")["cotton"]["played_at"].clone(); assert!(at.is_string(), "kept on the person's own copy"); // Once played, always played: again, or after its words land, it keeps its first time. answer(&handle, "chat.act", json!({ "act": "played", "thread": ASHA, "message": "m-theirs" })); assert_eq!(meta_of(&handle, "m-theirs")["cotton"]["played_at"], at); let page = answer(&handle, "chat.thread", json!({ "thread": ASHA })); assert!(item(&page, "m-theirs")["media"][0].get("played").is_none()); assert!(meta_of(&handle, "m-mine")["cotton"].get("played_at").is_none()); assert_eq!(refused(&handle, "chat.act", json!({ "act": "played", "thread": ASHA, "message": "m-nobody" })), "not_found"); } // RULE: The voice speed is one for every voice message: 1 until chosen, then 1, 1.5 or 2, kept with the person's // Comms settings (the face_settings row keyed comms), so it is synced to their other phones; any other is refused. #[test] fn the_voice_speed_is_kept_with_the_persons_settings() { let (_dir, handle) = phone(); assert_eq!(answer(&handle, "chat.settings", json!({}))["voiceSpeed"], 1.0); assert_eq!(answer(&handle, "chat.settings", json!({ "voiceSpeed": 1.5 }))["voiceSpeed"], 1.5); assert_eq!(answer(&handle, "chat.settings", json!({}))["voiceSpeed"], 1.5, "remembered"); assert_eq!(answer(&handle, "chat.settings", json!({ "readReceipts": true }))["voiceSpeed"], 1.5, "the switches leave it as it is"); assert_eq!(refused(&handle, "chat.settings", json!({ "voiceSpeed": 1.25 })), "invalid_request"); assert_eq!(refused(&handle, "chat.settings", json!({ "voiceSpeed": "fast" })), "invalid_request"); assert_eq!(answer(&handle, "chat.settings", json!({ "voiceSpeed": 2 }))["voiceSpeed"], 2.0); // A synced row of the person's own, not this phone's file. let row = crate::phone::records::call(&handle.client(), "records.get", &json!({ "table": "face_settings", "id": crate::phone::chat_voice::SETTINGS_ROW })).expect("the row"); assert_eq!(row["data"]["speed"], 2.0, "{row}"); assert_eq!(row["data"]["table"], crate::phone::chat_voice::SETTINGS_ROW); let file = handle.client().layout().client_dir().join("chat-preferences.json"); assert!(!std::fs::read_to_string(&file).unwrap_or_default().contains("voiceSpeed"), "never this phone's own file"); // The person's other phone: the row as sync lands it there, and that phone starts at the same speed. let (_other_dir, other) = phone(); assert_eq!(answer(&other, "chat.settings", json!({}))["voiceSpeed"], 1.0); let synced = json!({ "table": "face_settings", "data": { "table": row["data"]["table"], "face": row["data"]["face"], "speed": row["data"]["speed"] } }); crate::phone::records::call(&other.client(), "records.create", &synced).expect("the row, as synced"); assert_eq!(answer(&other, "chat.settings", json!({}))["voiceSpeed"], 2.0, "the other phone reads the synced row"); } // RULE: Played follows the person to their other phones: it is a column of their own copy of the message // (meta.cotton.played_at), so the row as sync lands it on another phone draws the note played there too. #[test] fn played_follows_the_row_to_the_persons_other_phones() { let (_dir, handle) = phone(); let (_other_dir, other) = phone(); for h in [&handle, &other] { asha(h); voice(h, "m-theirs", false, "live", 9_000, &[30, 90, 200], &iso(30), 1); } answer(&handle, "chat.act", json!({ "act": "played", "thread": ASHA, "message": "m-theirs" })); let meta = meta_of(&handle, "m-theirs"); let page = answer(&other, "chat.thread", json!({ "thread": ASHA })); assert_eq!(item(&page, "m-theirs")["media"][0]["played"], false, "not yet synced"); // The row's meta as the object hands it over. sql(&other, "UPDATE cotton_msg_messages SET meta = ? WHERE id = 'm-theirs'", &[t(&meta.to_string())]); let page = answer(&other, "chat.thread", json!({ "thread": ASHA })); assert!(item(&page, "m-theirs")["media"][0].get("played").is_none(), "played on the other phone too: {page}"); } // RULE: A voice message with no waveform (sent by an older phone, or imported) gets one from the first phone that // holds its file: chat.waveforms lists it, chat.waveform writes the phone's peaks into the message once, and its // bubble draws the core's bars from then on. #[test] fn an_old_voice_message_gets_its_waveform_from_the_first_phone_holding_it() { let (dir, handle) = phone(); imported_group(&handle); message(&handle, "m-gv", WEEKEND, false, "whatsapp", "imported", "", &iso(3), 0, Some("s-bimal")); // Under the person's own database, as every key of theirs is. let key = format!("{}/messages/{WEEKEND}/00000044-AUDIO.opus", workspace(&handle)); let media = json!([{ "key": key, "contentType": "audio/ogg", "name": "00000044-AUDIO.opus", "kind": "voice", "durationMs": 31_000 }]); sql(&handle, "UPDATE cotton_msg_messages SET media = ? WHERE id = 'm-gv'", &[t(&media.to_string())]); let page = answer(&handle, "chat.thread", json!({ "thread": WEEKEND })); let part = &item(&page, "m-gv")["media"][0]; assert!(part.get("bars").is_none() && part.get("played").is_none(), "no waveform: the track, and an import is played: {part}"); assert_eq!(answer(&handle, "chat.waveforms", json!({}))["items"], json!([]), "not on this phone yet"); handle.client().set_media_fetch(Some(std::sync::Arc::new(OwnMedia::default()))); answer(&handle, "chat.file", json!({ "message": "m-gv", "index": 0, "dir": dir.path().join("out").to_string_lossy() })); let wanted = answer(&handle, "chat.waveforms", json!({})); assert_eq!(wanted["items"].as_array().map(Vec::len), Some(1), "{wanted}"); assert_eq!((wanted["items"][0]["message"].as_str(), wanted["items"][0]["index"].as_u64()), (Some("m-gv"), Some(0))); assert!(std::path::Path::new(wanted["items"][0]["path"].as_str().unwrap()).is_file()); let peaks: Vec = (0..64).map(|i| (i % 8) as f64 / 8.0).collect(); assert_eq!(answer(&handle, "chat.waveform", json!({ "items": [{ "message": "m-gv", "index": 0, "waveform": peaks }] }))["set"], 1); let page = answer(&handle, "chat.thread", json!({ "thread": WEEKEND })); assert_eq!(item(&page, "m-gv")["media"][0]["bars"].as_array().map(Vec::len), Some(36)); assert_eq!(answer(&handle, "chat.waveforms", json!({}))["items"], json!([]), "listed no more"); // Once only, and nothing that is not a waveform. assert_eq!(answer(&handle, "chat.waveform", json!({ "items": [{ "message": "m-gv", "index": 0, "waveform": [1.0] }] }))["set"], 0); assert_eq!(answer(&handle, "chat.waveform", json!({ "items": [{ "message": "m-g7", "index": 0, "waveform": vec![0.5; 65] }] }))["set"], 0); assert_eq!(answer(&handle, "chat.waveform", json!({ "items": [{ "message": "m-g7", "index": 0, "waveform": [0.5] }] }))["set"], 0, "a picture takes no waveform"); }