The sound has a bitrate you pick, in every file that codes one
Browse files at this commit2169fb84ce631cc83a53f4c4c5a197c7b45dedf4Parent: dea8a27- feat
- the sound has a bitrate you pick, in every file that codes one
5 changed files +424 -66
- crates/app/src/keymap.rs +6 -1
- crates/app/src/main.rs +166 -38
- crates/engine/src/export.rs +83 -27
- crates/engine/src/playback.rs +7 -0
- crates/engine/tests/audio_export.rs +162 -0
crates/app/src/keymap.rsmodified+6 -1
| @@ -326,7 +326,7 @@ pub struct Fixed { | |||
| 326 | pub category: Category, | 326 | pub category: Category, |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | -pub const FIXED: [Fixed; 21] = [ | 329 | +pub const FIXED: [Fixed; 22] = [ |
| 330 | // Not a chord at all but a way of pressing one, and the only place the | 330 | // Not a chord at all but a way of pressing one, and the only place the |
| 331 | // editor can say so: holding a key that moves a *value* runs it, and | 331 | // editor can say so: holding a key that moves a *value* runs it, and |
| 332 | // holding anything else still does what one press did. | 332 | // holding anything else still does what one press did. |
| @@ -365,6 +365,11 @@ pub const FIXED: [Fixed; 21] = [ | |||
| 365 | label: "Step through the export quality rows", | 365 | label: "Step through the export quality rows", |
| 366 | category: Category::File, | 366 | category: Category::File, |
| 367 | }, | 367 | }, |
| 368 | + Fixed { | ||
| 369 | + chord: "b", | ||
| 370 | + label: "Step through the export sound bitrates", | ||
| 371 | + category: Category::File, | ||
| 372 | + }, | ||
| 368 | Fixed { | 373 | Fixed { |
| 369 | chord: "d", | 374 | chord: "d", |
| 370 | label: "Choose where the export is written", | 375 | label: "Choose where the export is written", |
crates/app/src/main.rsmodified+166 -38
| @@ -13,7 +13,7 @@ use engine::audio::StreamInfo; | |||
| 13 | use engine::color::ColorParams; | 13 | use engine::color::ColorParams; |
| 14 | use engine::decode::Backend; | 14 | use engine::decode::Backend; |
| 15 | use engine::eq::{Band, BandKind, EqParams}; | 15 | use engine::eq::{Band, BandKind, EqParams}; |
| 16 | -use engine::export::{ExportSettings, Format}; | 16 | +use engine::export::{AUDIO_KBPS, DEFAULT_AUDIO_KBPS, ExportSettings, Format}; |
| 17 | use engine::limiter::Limiter; | 17 | use engine::limiter::Limiter; |
| 18 | use engine::project::{Edge, Lane, LaneKind, Source, Speed}; | 18 | use engine::project::{Edge, Lane, LaneKind, Source, Speed}; |
| 19 | use engine::scale::FitPolicy; | 19 | use engine::scale::FitPolicy; |
| @@ -757,6 +757,12 @@ struct Player { | |||
| 757 | /// first one chose. | 757 | /// first one chose. |
| 758 | quality: Quality, | 758 | quality: Quality, |
| 759 | custom_mbps: u32, | 759 | custom_mbps: u32, |
| 760 | + /// What the *sound* is coded at, in kbps, for every format that encodes it | ||
| 761 | + /// -- the AAC inside a video export as much as an MP3. Kept across closes | ||
| 762 | + /// like the picture's quality, and starts at the figure this program wrote | ||
| 763 | + /// before the row existed ([`engine::export::DEFAULT_AUDIO_KBPS`]), so a | ||
| 764 | + /// user who never touches the row gets the file they always got. | ||
| 765 | + audio_kbps: u32, | ||
| 760 | /// Which file the card will write. Kept across closes like the quality, and | 766 | /// Which file the card will write. Kept across closes like the quality, and |
| 761 | /// what [`Player::export_path`](Player) is named after. | 767 | /// what [`Player::export_path`](Player) is named after. |
| 762 | format: Format, | 768 | format: Format, |
| @@ -2214,7 +2220,8 @@ impl Player { | |||
| 2214 | let Some(session) = &self.session else { | 2220 | let Some(session) = &self.session else { |
| 2215 | return; | 2221 | return; |
| 2216 | }; | 2222 | }; |
| 2217 | - let settings = export_settings(self.quality, self.custom_mbps, self.format); | 2223 | + let settings = |
| 2224 | + export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps); | ||
| 2218 | if !self.export_open { | 2225 | if !self.export_open { |
| 2219 | return; | 2226 | return; |
| 2220 | } | 2227 | } |
| @@ -3262,6 +3269,29 @@ impl Player { | |||
| 3262 | self.quality = Quality::ALL[(at + 1) % Quality::ALL.len()]; | 3269 | self.quality = Quality::ALL[(at + 1) % Quality::ALL.len()]; |
| 3263 | } | 3270 | } |
| 3264 | 3271 | ||
| 3272 | + /// The sound's rate by keyboard, wrapping through the offered ones -- the | ||
| 3273 | + /// picture's quality row for the other half of the file. Refused by name | ||
| 3274 | + /// where this timeline in this format has no rate to pick, exactly as | ||
| 3275 | + /// [`Player::cycle_quality`] is: a key that silently does nothing is the | ||
| 3276 | + /// card looking broken. | ||
| 3277 | + fn cycle_audio_kbps(&mut self) { | ||
| 3278 | + if let Some(why) = self.audio_rate_refusal() { | ||
| 3279 | + self.notice = Some(why.into()); | ||
| 3280 | + return; | ||
| 3281 | + } | ||
| 3282 | + self.audio_kbps = next_audio_kbps(self.audio_kbps); | ||
| 3283 | + } | ||
| 3284 | + | ||
| 3285 | + /// Why the sound row is not a choice right now, the engine answering about | ||
| 3286 | + /// the very project it would export. No session is the same answer as no | ||
| 3287 | + /// sound: there is nothing to write either way. | ||
| 3288 | + fn audio_rate_refusal(&self) -> Option<&'static str> { | ||
| 3289 | + match &self.session { | ||
| 3290 | + Some(session) => session.audio_rate_refusal(self.format), | ||
| 3291 | + None => Some("no sound to write"), | ||
| 3292 | + } | ||
| 3293 | + } | ||
| 3294 | + | ||
| 3265 | /// The custom bitrate by pointer: the typed digits were the only control in | 3295 | /// The custom bitrate by pointer: the typed digits were the only control in |
| 3266 | /// this card a mouse could not reach. Clamped to the range the row states | 3296 | /// this card a mouse could not reach. Clamped to the range the row states |
| 3267 | /// (the engine's own 1..20 Mbps), and picking the row is part of the step -- | 3297 | /// (the engine's own 1..20 Mbps), and picking the row is part of the step -- |
| @@ -3316,7 +3346,8 @@ impl Player { | |||
| 3316 | if self.export.is_some() { | 3346 | if self.export.is_some() { |
| 3317 | return; | 3347 | return; |
| 3318 | } | 3348 | } |
| 3319 | - let settings = export_settings(self.quality, self.custom_mbps, self.format); | 3349 | + let settings = |
| 3350 | + export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps); | ||
| 3320 | let Some(session) = &mut self.session else { | 3351 | let Some(session) = &mut self.session else { |
| 3321 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); | 3352 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); |
| 3322 | cx.notify(); | 3353 | cx.notify(); |
| @@ -3521,6 +3552,10 @@ impl Render for Player { | |||
| 3521 | this.cycle_container(); | 3552 | this.cycle_container(); |
| 3522 | } else if key == "q" { | 3553 | } else if key == "q" { |
| 3523 | this.cycle_quality(); | 3554 | this.cycle_quality(); |
| 3555 | + } else if key == "b" { | ||
| 3556 | + // The sound's rate, `q`'s pair for the other half of | ||
| 3557 | + // the file. Not a digit: those are the picture's. | ||
| 3558 | + this.cycle_audio_kbps(); | ||
| 3524 | } else if key == "d" { | 3559 | } else if key == "d" { |
| 3525 | // The save dialog, which was the one row here a | 3560 | // The save dialog, which was the one row here a |
| 3526 | // keyboard could not open. | 3561 | // keyboard could not open. |
| @@ -5103,6 +5138,36 @@ impl Player { | |||
| 5103 | } | 5138 | } |
| 5104 | } | 5139 | } |
| 5105 | } | 5140 | } |
| 5141 | + // The other half of the file, and the one this card used to write at a | ||
| 5142 | + // fixed rate without saying so: what the *sound* is coded at, for every | ||
| 5143 | + // format that codes it -- the AAC inside a video export as much as an | ||
| 5144 | + // MP3. One row that cycles, like the container's, rather than four more | ||
| 5145 | + // rows above the fold; dimmed with the reason where this timeline has | ||
| 5146 | + // no rate to pick, like the quality rows are. | ||
| 5147 | + let sound = self.audio_rate_refusal(); | ||
| 5148 | + let mut r = entry( | ||
| 5149 | + ("sound", 0), | ||
| 5150 | + "b", | ||
| 5151 | + "Sound".into(), | ||
| 5152 | + match sound { | ||
| 5153 | + Some(why) => why.into(), | ||
| 5154 | + None => format!( | ||
| 5155 | + "{} kbps — b for {}", | ||
| 5156 | + self.audio_kbps, | ||
| 5157 | + next_audio_kbps(self.audio_kbps) | ||
| 5158 | + ) | ||
| 5159 | + .into(), | ||
| 5160 | + }, | ||
| 5161 | + false, | ||
| 5162 | + sound.is_none(), | ||
| 5163 | + ); | ||
| 5164 | + if sound.is_none() { | ||
| 5165 | + r = r.on_click(cx.listener(|this, _: &ClickEvent, _, cx| { | ||
| 5166 | + this.cycle_audio_kbps(); | ||
| 5167 | + cx.notify(); | ||
| 5168 | + })); | ||
| 5169 | + } | ||
| 5170 | + list.push(r.into_any_element()); | ||
| 5106 | let destination = entry( | 5171 | let destination = entry( |
| 5107 | ("destination", 0), | 5172 | ("destination", 0), |
| 5108 | "d", | 5173 | "d", |
| @@ -5147,7 +5212,8 @@ impl Player { | |||
| 5147 | .session | 5212 | .session |
| 5148 | .as_ref() | 5213 | .as_ref() |
| 5149 | .map_or("", |s| s.planned_audio(self.format)); | 5214 | .map_or("", |s| s.planned_audio(self.format)); |
| 5150 | - let settings = export_settings(self.quality, self.custom_mbps, self.format); | 5215 | + let settings = |
| 5216 | + export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps); | ||
| 5151 | let mb = estimated_mb( | 5217 | let mb = estimated_mb( |
| 5152 | settings.bitrate.filter(|_| self.format.has_video()), | 5218 | settings.bitrate.filter(|_| self.format.has_video()), |
| 5153 | self.session | 5219 | self.session |
| @@ -7552,7 +7618,7 @@ impl Quality { | |||
| 7552 | Quality::Custom => format!("{custom_mbps} Mbps — type a number, 1–20"), | 7618 | Quality::Custom => format!("{custom_mbps} Mbps — type a number, 1–20"), |
| 7553 | other => format!( | 7619 | other => format!( |
| 7554 | "{} Mbps", | 7620 | "{} Mbps", |
| 7555 | - export_settings(other, 0, Format::Mp4) | 7621 | + export_settings(other, 0, Format::Mp4, DEFAULT_AUDIO_KBPS) |
| 7556 | .bitrate | 7622 | .bitrate |
| 7557 | .unwrap_or_default() | 7623 | .unwrap_or_default() |
| 7558 | / 1_000_000 | 7624 | / 1_000_000 |
| @@ -7606,12 +7672,7 @@ const FORMATS: [(&[Format], &str, &str, &str); 8] = [ | |||
| 7606 | ), | 7672 | ), |
| 7607 | (&[Format::Wav], "w", "WAV", "16-bit PCM — audio only"), | 7673 | (&[Format::Wav], "w", "WAV", "16-bit PCM — audio only"), |
| 7608 | (&[Format::Flac], "f", "FLAC", "lossless — audio only"), | 7674 | (&[Format::Flac], "f", "FLAC", "lossless — audio only"), |
| 7609 | - ( | 7675 | + (&[Format::Mp3], "p", "MP3", "MPEG-1 Layer III — audio only"), |
| 7610 | - &[Format::Mp3], | ||
| 7611 | - "p", | ||
| 7612 | - "MP3", | ||
| 7613 | - "MPEG-1 Layer III, 256 kbps — audio only", | ||
| 7614 | - ), | ||
| 7615 | (&[], "", "OGG", "no pure-Rust Vorbis or Opus encoder"), | 7676 | (&[], "", "OGG", "no pure-Rust Vorbis or Opus encoder"), |
| 7616 | (&[], "", "VP9", "AV1 above replaces it"), | 7677 | (&[], "", "VP9", "AV1 above replaces it"), |
| 7617 | ]; | 7678 | ]; |
| @@ -7660,6 +7721,14 @@ fn next_container(format: Format) -> Format { | |||
| 7660 | .unwrap_or(format) | 7721 | .unwrap_or(format) |
| 7661 | } | 7722 | } |
| 7662 | 7723 | ||
| 7724 | +/// The next rate the Sound row offers, wrapping -- what its key does, and what | ||
| 7725 | +/// the row itself names so the stroke is never a guess. [`next_container`]'s | ||
| 7726 | +/// shape, for the same reason: one place decides what "next" means. | ||
| 7727 | +fn next_audio_kbps(kbps: u32) -> u32 { | ||
| 7728 | + let at = AUDIO_KBPS.iter().position(|&k| k == kbps).unwrap_or(0); | ||
| 7729 | + AUDIO_KBPS[(at + 1) % AUDIO_KBPS.len()] | ||
| 7730 | +} | ||
| 7731 | + | ||
| 7663 | /// Why the quality rows say nothing about this format, or `None` where they | 7732 | /// Why the quality rows say nothing about this format, or `None` where they |
| 7664 | /// decide the picture. Only a picture encoder is given a bitrate here: the two | 7733 | /// decide the picture. Only a picture encoder is given a bitrate here: the two |
| 7665 | /// lossless audio formats have none to give and MP3 is written at one fixed | 7734 | /// lossless audio formats have none to give and MP3 is written at one fixed |
| @@ -7667,7 +7736,7 @@ fn next_container(format: Format) -> Format { | |||
| 7667 | fn bitrate_refusal(format: Format) -> Option<&'static str> { | 7736 | fn bitrate_refusal(format: Format) -> Option<&'static str> { |
| 7668 | match format { | 7737 | match format { |
| 7669 | Format::Wav | Format::Flac => Some("lossless audio — no bitrate to pick"), | 7738 | Format::Wav | Format::Flac => Some("lossless audio — no bitrate to pick"), |
| 7670 | - Format::Mp3 => Some("MP3 is written at a fixed 256 kbps"), | 7739 | + Format::Mp3 => Some("sound only — its rate is the Sound row"), |
| 7671 | _ => None, | 7740 | _ => None, |
| 7672 | } | 7741 | } |
| 7673 | } | 7742 | } |
| @@ -7871,7 +7940,7 @@ fn format_line(format: Format) -> &'static str { | |||
| 7871 | // only field on this line that says nothing. | 7940 | // only field on this line that says nothing. |
| 7872 | Format::Wav => "16-bit PCM · WAV", | 7941 | Format::Wav => "16-bit PCM · WAV", |
| 7873 | Format::Flac => "FLAC · lossless", | 7942 | Format::Flac => "FLAC · lossless", |
| 7874 | - Format::Mp3 => "MP3 · 256 kbps", | 7943 | + Format::Mp3 => "MP3 · lossy", |
| 7875 | } | 7944 | } |
| 7876 | } | 7945 | } |
| 7877 | 7946 | ||
| @@ -7902,9 +7971,18 @@ fn retarget(path: &std::path::Path, format: Format) -> PathBuf { | |||
| 7902 | /// | 7971 | /// |
| 7903 | /// The bitrate travels even for an audio format, where the engine ignores it: | 7972 | /// The bitrate travels even for an audio format, where the engine ignores it: |
| 7904 | /// one settings value, and a row the card has dimmed cannot have been changed. | 7973 | /// one settings value, and a row the card has dimmed cannot have been changed. |
| 7905 | -fn export_settings(quality: Quality, custom_mbps: u32, format: Format) -> ExportSettings { | 7974 | +fn export_settings( |
| 7975 | + quality: Quality, | ||
| 7976 | + custom_mbps: u32, | ||
| 7977 | + format: Format, | ||
| 7978 | + audio_kbps: u32, | ||
| 7979 | +) -> ExportSettings { | ||
| 7906 | ExportSettings { | 7980 | ExportSettings { |
| 7907 | format, | 7981 | format, |
| 7982 | + // Always travels, exactly as the picture's bitrate does above: the | ||
| 7983 | + // engine ignores it where nothing encodes the sound, and a row the card | ||
| 7984 | + // has dimmed cannot have been changed. | ||
| 7985 | + audio_kbps: Some(audio_kbps), | ||
| 7908 | bitrate: match quality { | 7986 | bitrate: match quality { |
| 7909 | Quality::Auto => None, | 7987 | Quality::Auto => None, |
| 7910 | Quality::Low => Some(2_000_000), | 7988 | Quality::Low => Some(2_000_000), |
| @@ -8845,22 +8923,23 @@ fn timecode(t: f64, fps: f64) -> String { | |||
| 8845 | #[cfg(test)] | 8923 | #[cfg(test)] |
| 8846 | mod tests { | 8924 | mod tests { |
| 8847 | use super::{ | 8925 | use super::{ |
| 8848 | - ACCENT, COLOR_BANDS, COLOR_BAR_W, COLOR_STEP, COLOR_W, CONTROL_H, Clip, EQ_CURVE_STEPS, | 8926 | + ACCENT, AUDIO_KBPS, COLOR_BANDS, COLOR_BAR_W, COLOR_STEP, COLOR_W, CONTROL_H, Clip, |
| 8849 | - EQ_FFT, EQ_FREQ_HIGH, EQ_FREQ_LOW, EQ_GAIN_LIMIT, EQ_GRAPH_H, EQ_HANDLE, EQ_SPECTRUM_DB, | 8927 | + DEFAULT_AUDIO_KBPS, EQ_CURVE_STEPS, EQ_FFT, EQ_FREQ_HIGH, EQ_FREQ_LOW, EQ_GAIN_LIMIT, |
| 8850 | - EQ_TICKS, ESCAPE, EXPORT_FIXED_H, EXPORT_ROWS_H, EXPORT_W, FORMATS, Format, HEADER_GAP, | 8928 | + EQ_GRAPH_H, EQ_HANDLE, EQ_SPECTRUM_DB, EQ_TICKS, ESCAPE, EXPORT_FIXED_H, EXPORT_ROWS_H, |
| 8851 | - HEADER_H, HEADER_W, HIST_BINS, HIST_H, HIST_SAMPLES, HIT_MIN, INK, INK_DIM, KEYS_ROW_H, | 8929 | + EXPORT_W, FORMATS, Format, HEADER_GAP, HEADER_H, HEADER_W, HIST_BINS, HIST_H, HIST_SAMPLES, |
| 8852 | - KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX, LETTERBOX, LIBRARY_MAX_W, | 8930 | + HIT_MIN, INK, INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, |
| 8853 | - LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, MENU_ROWS_H, MENU_W, NO_FILE, | 8931 | + LANES_MAX, LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, |
| 8854 | - PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS, | 8932 | + MENU_ROWS_H, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, |
| 8855 | - SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, VOLUME_W, Volume, WAVE_BPS, | 8933 | + SOURCE_TINTS, SPEED_PRESETS, SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, |
| 8856 | - WAVE_COL, Wave, applicable, band_label, bitrate_refusal, can_add, cancels_export, | 8934 | + VOLUME_W, Volume, WAVE_BPS, WAVE_COL, Wave, applicable, band_label, bitrate_refusal, |
| 8857 | - clipboard_after_remove, color_snap, containers, envelope, eq_spectrum, eq_x, eq_y, | 8935 | + can_add, cancels_export, clipboard_after_remove, color_snap, containers, envelope, |
| 8858 | - estimated_mb, export_path, export_settings, format_key, format_line, format_refusal, | 8936 | + eq_spectrum, eq_x, eq_y, estimated_mb, export_path, export_settings, format_key, |
| 8859 | - fps_label, frac_along, frac_down, frame_at, histogram, is_bare_modifier, is_project, | 8937 | + format_line, format_refusal, fps_label, frac_along, frac_down, frame_at, histogram, |
| 8860 | - keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h, | 8938 | + is_bare_modifier, is_project, keymap, lanes_h, marked, menu_at, next_audio_kbps, |
| 8861 | - project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped, | 8939 | + next_container, normalise, nothing_to_play, panel_h, project_path, push_digit, retarget, |
| 8862 | - source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, unseen_paths, | 8940 | + scrub_due, show_label, silence_rate, snapped, source_tint, span_partner, speed_at, |
| 8863 | - unseen_sources, whole_take, width_frac, window_title, | 8941 | + summary_head, summary_tail, timecode, unseen_paths, unseen_sources, whole_take, width_frac, |
| 8942 | + window_title, | ||
| 8864 | }; | 8943 | }; |
| 8865 | use super::{ | 8944 | use super::{ |
| 8866 | LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows, | 8945 | LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows, |
| @@ -10771,34 +10850,40 @@ mod tests { | |||
| 10771 | // Auto is the one row that says nothing: the exporter derives it, and | 10850 | // Auto is the one row that says nothing: the exporter derives it, and |
| 10772 | // a number typed against the custom row must not leak into it. | 10851 | // a number typed against the custom row must not leak into it. |
| 10773 | let mp4 = Format::Mp4; | 10852 | let mp4 = Format::Mp4; |
| 10774 | - assert_eq!(export_settings(Quality::Auto, 7, mp4).bitrate, None); | ||
| 10775 | assert_eq!( | 10853 | assert_eq!( |
| 10776 | - export_settings(Quality::Low, 0, mp4).bitrate, | 10854 | + export_settings(Quality::Auto, 7, mp4, DEFAULT_AUDIO_KBPS).bitrate, |
| 10855 | + None | ||
| 10856 | + ); | ||
| 10857 | + assert_eq!( | ||
| 10858 | + export_settings(Quality::Low, 0, mp4, DEFAULT_AUDIO_KBPS).bitrate, | ||
| 10777 | Some(2_000_000) | 10859 | Some(2_000_000) |
| 10778 | ); | 10860 | ); |
| 10779 | assert_eq!( | 10861 | assert_eq!( |
| 10780 | - export_settings(Quality::Medium, 0, mp4).bitrate, | 10862 | + export_settings(Quality::Medium, 0, mp4, DEFAULT_AUDIO_KBPS).bitrate, |
| 10781 | Some(6_000_000) | 10863 | Some(6_000_000) |
| 10782 | ); | 10864 | ); |
| 10783 | assert_eq!( | 10865 | assert_eq!( |
| 10784 | - export_settings(Quality::High, 0, mp4).bitrate, | 10866 | + export_settings(Quality::High, 0, mp4, DEFAULT_AUDIO_KBPS).bitrate, |
| 10785 | Some(12_000_000) | 10867 | Some(12_000_000) |
| 10786 | ); | 10868 | ); |
| 10787 | // Megabits as typed, and as the row says it back. | 10869 | // Megabits as typed, and as the row says it back. |
| 10788 | assert_eq!( | 10870 | assert_eq!( |
| 10789 | - export_settings(Quality::Custom, 7, mp4).bitrate, | 10871 | + export_settings(Quality::Custom, 7, mp4, DEFAULT_AUDIO_KBPS).bitrate, |
| 10790 | Some(7_000_000) | 10872 | Some(7_000_000) |
| 10791 | ); | 10873 | ); |
| 10792 | assert_eq!(Quality::Low.detail(0), "2 Mbps"); | 10874 | assert_eq!(Quality::Low.detail(0), "2 Mbps"); |
| 10793 | // The picked format travels, or the card's rows would be a picture of a | 10875 | // The picked format travels, or the card's rows would be a picture of a |
| 10794 | // choice the engine never hears about. | 10876 | // choice the engine never hears about. |
| 10795 | for format in [Format::Mp4, Format::Wav, Format::Flac] { | 10877 | for format in [Format::Mp4, Format::Wav, Format::Flac] { |
| 10796 | - assert_eq!(export_settings(Quality::Auto, 0, format).format, format); | 10878 | + assert_eq!( |
| 10879 | + export_settings(Quality::Auto, 0, format, DEFAULT_AUDIO_KBPS).format, | ||
| 10880 | + format | ||
| 10881 | + ); | ||
| 10797 | } | 10882 | } |
| 10798 | // Every fixed row sits inside the engine's clamp (export.rs:290), so no | 10883 | // Every fixed row sits inside the engine's clamp (export.rs:290), so no |
| 10799 | // row can promise a bitrate the exporter silently changes. | 10884 | // row can promise a bitrate the exporter silently changes. |
| 10800 | for quality in Quality::ALL { | 10885 | for quality in Quality::ALL { |
| 10801 | - let settings = export_settings(quality, 7, mp4); | 10886 | + let settings = export_settings(quality, 7, mp4, DEFAULT_AUDIO_KBPS); |
| 10802 | if let Some(bitrate) = settings.bitrate { | 10887 | if let Some(bitrate) = settings.bitrate { |
| 10803 | assert!( | 10888 | assert!( |
| 10804 | (1_000_000..=20_000_000).contains(&bitrate), | 10889 | (1_000_000..=20_000_000).contains(&bitrate), |
| @@ -10810,6 +10895,40 @@ mod tests { | |||
| 10810 | } | 10895 | } |
| 10811 | } | 10896 | } |
| 10812 | 10897 | ||
| 10898 | + /// The Sound row: what it offers, and that the pick travels to the engine | ||
| 10899 | + /// for a *video* export as much as for an MP3 -- both files carry sound, so | ||
| 10900 | + /// a row that only reached the audio formats would be half a control. | ||
| 10901 | + #[test] | ||
| 10902 | + fn the_sound_row_carries_its_rate_into_both_kinds_of_file() { | ||
| 10903 | + // Sorted and unique, so a row that says "b for the next one" is stepping | ||
| 10904 | + // up and not shuffling. | ||
| 10905 | + assert!(AUDIO_KBPS.windows(2).all(|w| w[0] < w[1])); | ||
| 10906 | + // The untouched figure is one of the offered rows, or the first press of | ||
| 10907 | + // `b` would jump somewhere nobody chose. | ||
| 10908 | + assert!(AUDIO_KBPS.contains(&DEFAULT_AUDIO_KBPS)); | ||
| 10909 | + // ...and it is what this program wrote before the row existed: the | ||
| 10910 | + // export of a user who never opens it must not change under them. | ||
| 10911 | + assert_eq!(DEFAULT_AUDIO_KBPS, 256); | ||
| 10912 | + for format in [Format::Mp4, Format::Av1, Format::Hevc, Format::Mp3] { | ||
| 10913 | + for kbps in AUDIO_KBPS { | ||
| 10914 | + assert_eq!( | ||
| 10915 | + export_settings(Quality::Auto, 0, format, kbps).audio_kbps, | ||
| 10916 | + Some(kbps), | ||
| 10917 | + "{format:?} at {kbps} kbps" | ||
| 10918 | + ); | ||
| 10919 | + } | ||
| 10920 | + } | ||
| 10921 | + // The wrap the row's key does, which is the row's own step function. | ||
| 10922 | + assert_eq!( | ||
| 10923 | + next_audio_kbps(AUDIO_KBPS[AUDIO_KBPS.len() - 1]), | ||
| 10924 | + AUDIO_KBPS[0] | ||
| 10925 | + ); | ||
| 10926 | + assert_eq!(next_audio_kbps(DEFAULT_AUDIO_KBPS), 320); | ||
| 10927 | + // A rate no list holds (a stale one, say) lands back on the first row | ||
| 10928 | + // rather than nowhere. | ||
| 10929 | + assert_eq!(next_audio_kbps(7), AUDIO_KBPS[1]); | ||
| 10930 | + } | ||
| 10931 | + | ||
| 10813 | #[test] | 10932 | #[test] |
| 10814 | fn a_typed_bitrate_stops_at_two_digits() { | 10933 | fn a_typed_bitrate_stops_at_two_digits() { |
| 10815 | assert_eq!(push_digit(0, 8), 8); | 10934 | assert_eq!(push_digit(0, 8), 8); |
| @@ -11187,7 +11306,14 @@ mod tests { | |||
| 11187 | ); | 11306 | ); |
| 11188 | } | 11307 | } |
| 11189 | assert!(bitrate_refusal(Format::Wav).unwrap().contains("lossless")); | 11308 | assert!(bitrate_refusal(Format::Wav).unwrap().contains("lossless")); |
| 11190 | - assert!(bitrate_refusal(Format::Mp3).unwrap().contains("256 kbps")); | 11309 | + // MP3 has a rate and it is the *Sound* row's: the quality rows are the |
| 11310 | + // picture's, and this refusal used to claim a fixed 256 kbps that the | ||
| 11311 | + // Sound row can now change under it. | ||
| 11312 | + assert!(bitrate_refusal(Format::Mp3).unwrap().contains("Sound row")); | ||
| 11313 | + assert!( | ||
| 11314 | + !format_line(Format::Mp3).contains("256"), | ||
| 11315 | + "the summary states a rate the Sound row can change under it" | ||
| 11316 | + ); | ||
| 11191 | // The destination follows the format and keeps the stem, mp4 included. | 11317 | // The destination follows the format and keeps the stem, mp4 included. |
| 11192 | assert_eq!( | 11318 | assert_eq!( |
| 11193 | retarget(std::path::Path::new("/a/take.export.mp4"), Format::Wav), | 11319 | retarget(std::path::Path::new("/a/take.export.mp4"), Format::Wav), |
| @@ -11773,6 +11899,8 @@ fn main() { | |||
| 11773 | // bitrate the picture asks for. | 11899 | // bitrate the picture asks for. |
| 11774 | quality: Quality::Auto, | 11900 | quality: Quality::Auto, |
| 11775 | custom_mbps: 0, | 11901 | custom_mbps: 0, |
| 11902 | + // ...and the rate the sound has always been written at. | ||
| 11903 | + audio_kbps: DEFAULT_AUDIO_KBPS, | ||
| 11776 | // Picture and sound, which is what an export was before | 11904 | // Picture and sound, which is what an export was before |
| 11777 | // there was anything to pick. | 11905 | // there was anything to pick. |
| 11778 | format: Format::default(), | 11906 | format: Format::default(), |
crates/engine/src/export.rsmodified+83 -27
| @@ -76,6 +76,21 @@ const BITS_PER_PIXEL: f64 = 0.1; | |||
| 76 | const MIN_BITRATE: u64 = 1_000_000; | 76 | const MIN_BITRATE: u64 = 1_000_000; |
| 77 | const MAX_BITRATE: u64 = 20_000_000; | 77 | const MAX_BITRATE: u64 = 20_000_000; |
| 78 | 78 | ||
| 79 | +/// The rates the sound may be coded at, smallest first -- what a front-end | ||
| 80 | +/// offers and what [`ExportSettings::audio_kbps`] is clamped into. Every one of | ||
| 81 | +/// them is a legal MPEG-1 Layer III value, so the MP3 path writes the figure it | ||
| 82 | +/// was given rather than the nearest one `rusty_mp3` snaps to. | ||
| 83 | +pub const AUDIO_KBPS: [u32; 4] = [128, 192, 256, 320]; | ||
| 84 | + | ||
| 85 | +/// What the sound is coded at when nobody picked: the rate this project wrote | ||
| 86 | +/// before the choice existed. 256 and not the encoders' own 128, because an | ||
| 87 | +/// export is a master a person edits from again and this is a second lossy | ||
| 88 | +/// generation over sources that may already have been one -- measured on the | ||
| 89 | +/// suite's fixtures, an untouched clip re-encoded moves 0.15 dB here against | ||
| 90 | +/// 0.46 dB at 128, and 32 KB/s beside a 2.76 Mbps picture is not a size anyone | ||
| 91 | +/// is counting. | ||
| 92 | +pub const DEFAULT_AUDIO_KBPS: u32 = 256; | ||
| 93 | + | ||
| 79 | /// `rav1e`'s fastest preset. Anything slower is minutes per second of timeline | 94 | /// `rav1e`'s fastest preset. Anything slower is minutes per second of timeline |
| 80 | /// on a build with no assembly, which is what this one is. | 95 | /// on a build with no assembly, which is what this one is. |
| 81 | const AV1_SPEED: u8 = 10; | 96 | const AV1_SPEED: u8 = 10; |
| @@ -137,7 +152,8 @@ pub enum Format { | |||
| 137 | Wav, | 152 | Wav, |
| 138 | /// The audio lanes alone, losslessly compressed. | 153 | /// The audio lanes alone, losslessly compressed. |
| 139 | Flac, | 154 | Flac, |
| 140 | - /// The audio lanes alone, MPEG-1 Layer III at 256 kbps CBR (`rusty_mp3`). | 155 | + /// The audio lanes alone, MPEG-1 Layer III CBR (`rusty_mp3`) at the rate |
| 156 | + /// [`ExportSettings::audio_kbps`] asks for. | ||
| 141 | Mp3, | 157 | Mp3, |
| 142 | } | 158 | } |
| 143 | 159 | ||
| @@ -205,6 +221,13 @@ pub struct ExportSettings { | |||
| 205 | /// The file to write. Defaults to [`Format::Mp4`], which is what every | 221 | /// The file to write. Defaults to [`Format::Mp4`], which is what every |
| 206 | /// caller wrote before there was a choice. | 222 | /// caller wrote before there was a choice. |
| 207 | pub format: Format, | 223 | pub format: Format, |
| 224 | + /// What the *sound* is coded at, in kbps, wherever an encoder writes it -- | ||
| 225 | + /// the AAC of a video export as much as an MP3. `None` is | ||
| 226 | + /// [`DEFAULT_AUDIO_KBPS`], which is what every caller wrote before there | ||
| 227 | + /// was a choice, so a settings value nobody touched writes the same bytes | ||
| 228 | + /// it used to. Means nothing to WAV and FLAC, which code no rate, and | ||
| 229 | + /// nothing to a *copied* AAC track, which carries its source's. | ||
| 230 | + pub audio_kbps: Option<u32>, | ||
| 208 | } | 231 | } |
| 209 | 232 | ||
| 210 | struct Shared { | 233 | struct Shared { |
| @@ -313,7 +336,7 @@ pub fn start( | |||
| 313 | ) | 336 | ) |
| 314 | .into()), | 337 | .into()), |
| 315 | format if format.has_video() => run(&project, &meta, &part, &worker, &settings), | 338 | format if format.has_video() => run(&project, &meta, &part, &worker, &settings), |
| 316 | - format => run_audio(&project, &meta, &part, &worker, format), | 339 | + _ => run_audio(&project, &meta, &part, &worker, &settings), |
| 317 | }; | 340 | }; |
| 318 | let result = written.and_then(|()| std::fs::rename(&part, &out).map_err(Into::into)); | 341 | let result = written.and_then(|()| std::fs::rename(&part, &out).map_err(Into::into)); |
| 319 | if result.is_err() { | 342 | if result.is_err() { |
| @@ -377,6 +400,16 @@ fn bitrate_of(meta: &VideoMeta, settings: &ExportSettings) -> u64 { | |||
| 377 | .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)) | 400 | .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)) |
| 378 | } | 401 | } |
| 379 | 402 | ||
| 403 | +/// The rate the sound codes at: the caller's pick clamped into the offered | ||
| 404 | +/// range, [`DEFAULT_AUDIO_KBPS`] where there was none. Clamped and not refused, | ||
| 405 | +/// for [`bitrate_of`]'s reason -- a number out of range is a caller's slip and | ||
| 406 | +/// not a file worth failing. | ||
| 407 | +fn audio_kbps_of(settings: &ExportSettings) -> u32 { | ||
| 408 | + settings.audio_kbps.map_or(DEFAULT_AUDIO_KBPS, |k| { | ||
| 409 | + k.clamp(AUDIO_KBPS[0], AUDIO_KBPS[3]) | ||
| 410 | + }) | ||
| 411 | +} | ||
| 412 | + | ||
| 380 | /// The hardware seat for these settings, opened exactly as the export opens it | 413 | /// The hardware seat for these settings, opened exactly as the export opens it |
| 381 | /// -- the same pins, the same AV1 opt-in, the same dimensions a driver may | 414 | /// -- the same pins, the same AV1 opt-in, the same dimensions a driver may |
| 382 | /// refuse -- or `None` where the software encoder takes the file. The single | 415 | /// refuse -- or `None` where the software encoder takes the file. The single |
| @@ -473,6 +506,25 @@ pub fn planned_audio(project: &Project, format: Format) -> &'static str { | |||
| 473 | audio_label(project, format, has_sound(project), None) | 506 | audio_label(project, format, has_sound(project), None) |
| 474 | } | 507 | } |
| 475 | 508 | ||
| 509 | +/// Why the sound's rate is nothing this timeline can be asked about in this | ||
| 510 | +/// format, or `None` where it is a real choice: there is no sound to write, the | ||
| 511 | +/// format codes the samples themselves, or the packets are the source's own and | ||
| 512 | +/// carry the rate *it* was coded at. Pure, like [`planned_audio`], and a | ||
| 513 | +/// prediction in the same way -- a copy that turns out impossible (a source no | ||
| 514 | +/// mp4 sample table holds) falls through to [`encode_audio`], which codes at | ||
| 515 | +/// [`ExportSettings::audio_kbps`] like every other encode. So the value travels | ||
| 516 | +/// even where this refuses; what it cannot do is silently write a rate nobody | ||
| 517 | +/// picked. | ||
| 518 | +pub fn audio_rate_refusal(project: &Project, format: Format) -> Option<&'static str> { | ||
| 519 | + match format { | ||
| 520 | + _ if !has_sound(project) => Some("no sound to write"), | ||
| 521 | + Format::Wav | Format::Flac => Some("lossless — the samples themselves, at no rate"), | ||
| 522 | + Format::Mp3 => None, | ||
| 523 | + _ if forces_encode(project) => None, | ||
| 524 | + _ => Some("the source's own packets are copied — at the rate they hold"), | ||
| 525 | + } | ||
| 526 | +} | ||
| 527 | + | ||
| 476 | /// What [`start`] would encode the picture with, probed the way the export | 528 | /// What [`start`] would encode the picture with, probed the way the export |
| 477 | /// probes it -- the very encoder is opened and closed again -- so this is a | 529 | /// probes it -- the very encoder is opened and closed again -- so this is a |
| 478 | /// measurement and not a promise. `None` for a format that carries no picture. | 530 | /// measurement and not a promise. `None` for a format that carries no picture. |
| @@ -517,7 +569,11 @@ pub(crate) struct ExportAudio { | |||
| 517 | /// ponytail: this holds the whole exported AAC track in memory (~3 kB per | 569 | /// ponytail: this holds the whole exported AAC track in memory (~3 kB per |
| 518 | /// 23 ms packet, so ~500 MB for an hour). Upgrade path is a streaming | 570 | /// 23 ms packet, so ~500 MB for an hour). Upgrade path is a streaming |
| 519 | /// `copy_segments` that yields packets instead of collecting them. | 571 | /// `copy_segments` that yields packets instead of collecting them. |
| 520 | -fn copy_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<ExportAudio>> { | 572 | +fn copy_audio( |
| 573 | + project: &Project, | ||
| 574 | + meta: &VideoMeta, | ||
| 575 | + kbps: u32, | ||
| 576 | +) -> crate::Result<Option<ExportAudio>> { | ||
| 521 | // The segments name their source, and the copy carries its packet-rounding | 577 | // The segments name their source, and the copy carries its packet-rounding |
| 522 | // debt across a source join exactly as across a cut, so a timeline spanning | 578 | // debt across a source join exactly as across a cut, so a timeline spanning |
| 523 | // files stays in sync. A source whose AAC parameters disagree with the | 579 | // files stays in sync. A source whose AAC parameters disagree with the |
| @@ -541,7 +597,7 @@ fn copy_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<Expor | |||
| 541 | // whole comment is about. | 597 | // whole comment is about. |
| 542 | let lanes = project.audio_segments_from(0, meta.frame_rate); | 598 | let lanes = project.audio_segments_from(0, meta.frame_rate); |
| 543 | let [segments] = &lanes[..] else { | 599 | let [segments] = &lanes[..] else { |
| 544 | - return encode_audio(project, meta); | 600 | + return encode_audio(project, meta, kbps); |
| 545 | }; | 601 | }; |
| 546 | // ...and the same list names *which* lane those clips sit on, which is the | 602 | // ...and the same list names *which* lane those clips sit on, which is the |
| 547 | // only way to ask what has been done to them. | 603 | // only way to ask what has been done to them. |
| @@ -579,7 +635,7 @@ fn copy_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<Expor | |||
| 579 | // such a lane would write it at unity and unlimited, silently. | 635 | // such a lane would write it at unity and unlimited, silently. |
| 580 | let speeded = project.lane(lane).iter().any(|c| !c.speed.is_normal()); | 636 | let speeded = project.lane(lane).iter().any(|c| !c.speed.is_normal()); |
| 581 | if speeded || equalized(project) || mastered(project) { | 637 | if speeded || equalized(project) || mastered(project) { |
| 582 | - return encode_audio(project, meta); | 638 | + return encode_audio(project, meta, kbps); |
| 583 | } | 639 | } |
| 584 | // What is left is a copy the *sources* may still not be able to give: AAC | 640 | // What is left is a copy the *sources* may still not be able to give: AAC |
| 585 | // inside a Matroska file (readable, but not out of a sample table this walks | 641 | // inside a Matroska file (readable, but not out of a sample table this walks |
| @@ -595,7 +651,7 @@ fn copy_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<Expor | |||
| 595 | packets, | 651 | packets, |
| 596 | copied: true, | 652 | copied: true, |
| 597 | })), | 653 | })), |
| 598 | - Err(_) => encode_audio(project, meta), | 654 | + Err(_) => encode_audio(project, meta, kbps), |
| 599 | } | 655 | } |
| 600 | } | 656 | } |
| 601 | 657 | ||
| @@ -616,7 +672,11 @@ fn copy_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<Expor | |||
| 616 | /// minute of 48 kHz stereo), for [`run_audio`]'s reason -- `rusty_aac` buffers | 672 | /// minute of 48 kHz stereo), for [`run_audio`]'s reason -- `rusty_aac` buffers |
| 617 | /// the whole stream anyway, since it encodes its frames in parallel. Upgrade | 673 | /// the whole stream anyway, since it encodes its frames in parallel. Upgrade |
| 618 | /// path is a chunked push once that encoder streams. | 674 | /// path is a chunked push once that encoder streams. |
| 619 | -fn encode_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<ExportAudio>> { | 675 | +fn encode_audio( |
| 676 | + project: &Project, | ||
| 677 | + meta: &VideoMeta, | ||
| 678 | + kbps: u32, | ||
| 679 | +) -> crate::Result<Option<ExportAudio>> { | ||
| 620 | let sources = project.audio_sources(); | 680 | let sources = project.audio_sources(); |
| 621 | let segs = project.audio_segments_from(0, meta.frame_rate); | 681 | let segs = project.audio_segments_from(0, meta.frame_rate); |
| 622 | let eqs = project.audio_eqs_from(0, meta.frame_rate); | 682 | let eqs = project.audio_eqs_from(0, meta.frame_rate); |
| @@ -654,14 +714,10 @@ fn encode_audio(project: &Project, meta: &VideoMeta) -> crate::Result<Option<Exp | |||
| 654 | } | 714 | } |
| 655 | samples.resize(total, 0.0); | 715 | samples.resize(total, 0.0); |
| 656 | 716 | ||
| 657 | - // 256 kbps, not the encoder's 128 default: this is a *second* generation of | 717 | + // The caller's rate, never the encoder's own 128 default -- see |
| 658 | - // lossy coding over a source that was already AAC, and the export is a | 718 | + // [`DEFAULT_AUDIO_KBPS`] for why the untouched figure is 256. |
| 659 | - // master a person edits from again, not a delivery file. Measured on the | ||
| 660 | - // suite's fixtures, an untouched clip re-encoded here moves 0.15 dB at this | ||
| 661 | - // rate against 0.46 dB at 128 -- and 32 KB/s beside a 2.76 Mbps picture is | ||
| 662 | - // not a size anyone is counting. | ||
| 663 | let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig { | 719 | let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig { |
| 664 | - bitrate_bps: 256_000, | 720 | + bitrate_bps: kbps * 1_000, |
| 665 | ..Default::default() | 721 | ..Default::default() |
| 666 | }); | 722 | }); |
| 667 | encoder.push_pcm(&samples, audio.channels.max(1), audio.sample_rate)?; | 723 | encoder.push_pcm(&samples, audio.channels.max(1), audio.sample_rate)?; |
| @@ -702,7 +758,7 @@ fn run( | |||
| 702 | // muxer wants the packets themselves that early too, because it interleaves | 758 | // muxer wants the packets themselves that early too, because it interleaves |
| 703 | // them into the clusters as it writes. Every video format gets the same | 759 | // them into the clusters as it writes. Every video format gets the same |
| 704 | // track: none of them is picture-only any more. | 760 | // track: none of them is picture-only any more. |
| 705 | - let audio = copy_audio(project, meta)?; | 761 | + let audio = copy_audio(project, meta, audio_kbps_of(settings))?; |
| 706 | let audio_params = audio.as_ref().map(|track| AudioParams { | 762 | let audio_params = audio.as_ref().map(|track| AudioParams { |
| 707 | freq_index: track.params.freq_index, | 763 | freq_index: track.params.freq_index, |
| 708 | chan_conf: track.params.chan_conf, | 764 | chan_conf: track.params.chan_conf, |
| @@ -947,8 +1003,9 @@ fn run_audio( | |||
| 947 | meta: &VideoMeta, | 1003 | meta: &VideoMeta, |
| 948 | out: &Path, | 1004 | out: &Path, |
| 949 | shared: &Shared, | 1005 | shared: &Shared, |
| 950 | - format: Format, | 1006 | + settings: &ExportSettings, |
| 951 | ) -> crate::Result<()> { | 1007 | ) -> crate::Result<()> { |
| 1008 | + let format = settings.format; | ||
| 952 | let sources = project.audio_sources(); | 1009 | let sources = project.audio_sources(); |
| 953 | let segs = project.audio_segments_from(0, meta.frame_rate); | 1010 | let segs = project.audio_segments_from(0, meta.frame_rate); |
| 954 | // The equalizers with them, so what is written is what is heard down to the | 1011 | // The equalizers with them, so what is written is what is heard down to the |
| @@ -1011,7 +1068,7 @@ fn run_audio( | |||
| 1011 | match format { | 1068 | match format { |
| 1012 | Format::Wav => write_wav(out, &samples, &audio)?, | 1069 | Format::Wav => write_wav(out, &samples, &audio)?, |
| 1013 | Format::Flac => write_flac(out, &samples, &audio)?, | 1070 | Format::Flac => write_flac(out, &samples, &audio)?, |
| 1014 | - Format::Mp3 => write_mp3(out, &samples, &audio)?, | 1071 | + Format::Mp3 => write_mp3(out, &samples, &audio, audio_kbps_of(settings))?, |
| 1015 | _ => unreachable!("the picture formats are `run`"), | 1072 | _ => unreachable!("the picture formats are `run`"), |
| 1016 | } | 1073 | } |
| 1017 | shared.progress.store(PROGRESS_SCALE, Ordering::Relaxed); | 1074 | shared.progress.store(PROGRESS_SCALE, Ordering::Relaxed); |
| @@ -1043,18 +1100,17 @@ fn write_wav(out: &Path, samples: &[i32], audio: &AudioMeta) -> crate::Result<() | |||
| 1043 | /// Rust like every other encoder here, and the reason this format is a row at | 1100 | /// Rust like every other encoder here, and the reason this format is a row at |
| 1044 | /// all: the LGPL `shine-rs` was the only one when it was not. | 1101 | /// all: the LGPL `shine-rs` was the only one when it was not. |
| 1045 | /// | 1102 | /// |
| 1046 | -/// 256 kbps CBR, for [`encode_audio`]'s reason: an export is a master a person | 1103 | +/// CBR at the caller's rate, [`DEFAULT_AUDIO_KBPS`] where there was none. Every |
| 1047 | -/// edits from again, not a delivery file, and this is a lossy generation over | 1104 | +/// offered rate is already a legal Layer III value, so the encoder's snap is a |
| 1048 | -/// sources that may already have been one. The rate is snapped to a legal Layer | 1105 | +/// backstop and not a silent substitution; a sample rate MPEG has no frame for |
| 1049 | -/// III value by the encoder, and a sample rate MPEG has no frame for (anything | 1106 | +/// (anything but 8-48 kHz) is refused there by name rather than written as |
| 1050 | -/// but 8-48 kHz) is refused there by name rather than written as something else. | 1107 | +/// something else. |
| 1051 | /// | 1108 | /// |
| 1052 | -/// ponytail: CBR only, and no rate is offered to the caller -- the export card | 1109 | +/// ponytail: CBR only -- the card offers rates and not a quality index. Upgrade |
| 1053 | -/// has one bitrate control and it is the *picture's*. Upgrade path is | 1110 | +/// path is `Mp3EncoderConfig::vbr_quality` behind a setting of its own. |
| 1054 | -/// `Mp3EncoderConfig::vbr_quality` behind a setting of its own. | 1111 | +fn write_mp3(out: &Path, samples: &[i32], audio: &AudioMeta, kbps: u32) -> crate::Result<()> { |
| 1055 | -fn write_mp3(out: &Path, samples: &[i32], audio: &AudioMeta) -> crate::Result<()> { | ||
| 1056 | let mut encoder = rusty_mp3::Mp3Encoder::new(rusty_mp3::Mp3EncoderConfig { | 1112 | let mut encoder = rusty_mp3::Mp3Encoder::new(rusty_mp3::Mp3EncoderConfig { |
| 1057 | - bitrate_kbps: 256, | 1113 | + bitrate_kbps: kbps, |
| 1058 | vbr_quality: None, | 1114 | vbr_quality: None, |
| 1059 | }); | 1115 | }); |
| 1060 | // The samples are the 16-bit ones a WAV of this timeline holds, so the two | 1116 | // The samples are the 16-bit ones a WAV of this timeline holds, so the two |
crates/engine/src/playback.rsmodified+7 -0
| @@ -741,6 +741,13 @@ impl PlaybackSession { | |||
| 741 | crate::export::planned_audio(&self.project, format) | 741 | crate::export::planned_audio(&self.project, format) |
| 742 | } | 742 | } |
| 743 | 743 | ||
| 744 | + /// Why the sound's rate is not a choice for this timeline in this format -- | ||
| 745 | + /// [`crate::export::audio_rate_refusal`], asked of the project a front-end | ||
| 746 | + /// is holding, and pure for the same reason. | ||
| 747 | + pub fn audio_rate_refusal(&self, format: crate::export::Format) -> Option<&'static str> { | ||
| 748 | + crate::export::audio_rate_refusal(&self.project, format) | ||
| 749 | + } | ||
| 750 | + | ||
| 744 | /// Why this session plays silent although the file has sound -- an audio | 751 | /// Why this session plays silent although the file has sound -- an audio |
| 745 | /// track in a codec we cannot decode, or one the decoder refused. `None` | 752 | /// track in a codec we cannot decode, or one the decoder refused. `None` |
| 746 | /// when there is audio, and for a file that never had any. A front-end | 753 | /// when there is audio, and for a file that never had any. A front-end |
crates/engine/tests/audio_export.rsmodified+162 -0
| @@ -11,9 +11,13 @@ | |||
| 11 | //! No picture is decoded anywhere here, so nothing needs the plugin and the | 11 | //! No picture is decoded anywhere here, so nothing needs the plugin and the |
| 12 | //! whole file runs on any machine. | 12 | //! whole file runs on any machine. |
| 13 | 13 | ||
| 14 | +use std::fs::File; | ||
| 15 | +use std::io::BufReader; | ||
| 14 | use std::path::{Path, PathBuf}; | 16 | use std::path::{Path, PathBuf}; |
| 15 | use std::time::{Duration, Instant}; | 17 | use std::time::{Duration, Instant}; |
| 16 | 18 | ||
| 19 | +use mp4::{MediaType, Mp4Reader}; | ||
| 20 | + | ||
| 17 | use engine::export::{ExportSettings, Format}; | 21 | use engine::export::{ExportSettings, Format}; |
| 18 | use engine::project::{Lane, LaneKind, Source, Speed}; | 22 | use engine::project::{Lane, LaneKind, Source, Speed}; |
| 19 | use engine::scale::FitPolicy; | 23 | use engine::scale::FitPolicy; |
| @@ -511,6 +515,164 @@ fn a_cancelled_audio_export_leaves_no_file() { | |||
| 511 | assert!(!part_path(&out).exists(), "no half-written .part either"); | 515 | assert!(!part_path(&out).exists(), "no half-written .part either"); |
| 512 | } | 516 | } |
| 513 | 517 | ||
| 518 | +/// The rate an MP3 file *declares*, read out of its own frame headers -- the | ||
| 519 | +/// only place the claim can be checked, since a caller's kbps is a request until | ||
| 520 | +/// the bytes say so. MPEG-1 Layer III, whose bitrate index is the high nibble of | ||
| 521 | +/// the third header byte. | ||
| 522 | +fn mp3_declared_kbps(path: &Path) -> u32 { | ||
| 523 | + const KBPS: [u32; 16] = [ | ||
| 524 | + 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0, | ||
| 525 | + ]; | ||
| 526 | + let bytes = std::fs::read(path).expect("read the mp3 back"); | ||
| 527 | + let at = bytes | ||
| 528 | + .windows(4) | ||
| 529 | + .position(|w| w[0] == 0xFF && w[1] & 0xE0 == 0xE0) | ||
| 530 | + .expect("an MPEG frame sync"); | ||
| 531 | + let header = &bytes[at..at + 4]; | ||
| 532 | + assert_eq!(header[1] >> 3 & 0x3, 0b11, "MPEG-1"); | ||
| 533 | + assert_eq!(header[1] >> 1 & 0x3, 0b01, "Layer III"); | ||
| 534 | + let kbps = KBPS[usize::from(header[2] >> 4)]; | ||
| 535 | + assert!(kbps > 0, "a free-format or invalid bitrate index"); | ||
| 536 | + kbps | ||
| 537 | +} | ||
| 538 | + | ||
| 539 | +/// What an mp4's AAC track is really coded at: its sample bytes over the time | ||
| 540 | +/// they play, in kbps. Every AAC-LC packet is 1024 frames per channel, so the | ||
| 541 | +/// duration comes out of the packet count and needs no timescale of the file's | ||
| 542 | +/// own -- and the bytes are what a player downloads, which is what a bitrate is. | ||
| 543 | +fn mp4_aac_kbps(path: &Path, sample_rate: f64) -> f64 { | ||
| 544 | + let file = File::open(path).expect("reopen the mp4"); | ||
| 545 | + let size = file.metadata().unwrap().len(); | ||
| 546 | + let mut reader = Mp4Reader::read_header(BufReader::new(file), size).expect("mp4 header"); | ||
| 547 | + let track = reader | ||
| 548 | + .tracks() | ||
| 549 | + .values() | ||
| 550 | + .find(|t| matches!(t.media_type(), Ok(MediaType::AAC))) | ||
| 551 | + .expect("an AAC track") | ||
| 552 | + .track_id(); | ||
| 553 | + let count = reader.sample_count(track).expect("sample count"); | ||
| 554 | + let mut bytes = 0usize; | ||
| 555 | + for id in 1..=count { | ||
| 556 | + bytes += reader | ||
| 557 | + .read_sample(track, id) | ||
| 558 | + .expect("read a sample") | ||
| 559 | + .expect("a sample at every id") | ||
| 560 | + .bytes | ||
| 561 | + .len(); | ||
| 562 | + } | ||
| 563 | + let seconds = f64::from(count) * 1024.0 / sample_rate; | ||
| 564 | + bytes as f64 * 8.0 / seconds / 1000.0 | ||
| 565 | +} | ||
| 566 | + | ||
| 567 | +/// The sound's rate is the caller's, in both kinds of file -- and the file | ||
| 568 | +/// nobody asked a rate of is the one this program always wrote. | ||
| 569 | +/// | ||
| 570 | +/// MP3 declares its rate in every frame header, so that half is exact. The AAC | ||
| 571 | +/// half is measured off the mp4's own sample table: a rate control lands *near* | ||
| 572 | +/// its target rather than on it, so the tolerance is a fifth, which is far | ||
| 573 | +/// tighter than the gap between two offered rates. | ||
| 574 | +#[test] | ||
| 575 | +fn the_sound_is_written_at_the_rate_that_was_asked_for() { | ||
| 576 | + // Untouched settings first: this is the byte-behaviour a user who never | ||
| 577 | + // opens the row is entitled to keep. | ||
| 578 | + for (kbps, want) in [ | ||
| 579 | + (None, engine::export::DEFAULT_AUDIO_KBPS), | ||
| 580 | + (Some(128), 128), | ||
| 581 | + (Some(320), 320), | ||
| 582 | + ] { | ||
| 583 | + let out = out_path(&format!("mp3_{}", want), "mp3"); | ||
| 584 | + let handle = engine::export::start( | ||
| 585 | + mixed_project(), | ||
| 586 | + meta(), | ||
| 587 | + &out, | ||
| 588 | + &ExportSettings { | ||
| 589 | + format: Format::Mp3, | ||
| 590 | + audio_kbps: kbps, | ||
| 591 | + ..Default::default() | ||
| 592 | + }, | ||
| 593 | + ); | ||
| 594 | + wait(&handle, Duration::from_secs(120)).expect("mp3 export"); | ||
| 595 | + let got = mp3_declared_kbps(&out); | ||
| 596 | + println!("mp3 asked {kbps:?}, header says {got} kbps"); | ||
| 597 | + assert_eq!(got, want, "the mp3 frames declare the rate that was picked"); | ||
| 598 | + // ...and it is still the edit, not just bytes at the right rate. | ||
| 599 | + let (meta, samples) = decode(&out); | ||
| 600 | + let channels = usize::from(meta.channels); | ||
| 601 | + assert!(rms(&samples, channels, 0.05, 0.95) > 0.01, "audible"); | ||
| 602 | + std::fs::remove_file(&out).unwrap(); | ||
| 603 | + } | ||
| 604 | + | ||
| 605 | + // The same choice inside a video export, where the sound is AAC: a song | ||
| 606 | + // under a still, which no mp4 sample table can be copied from, so the | ||
| 607 | + // encoder really runs. | ||
| 608 | + let project = Project::from_parts( | ||
| 609 | + vec![ | ||
| 610 | + Source::new(asset("test_still.png"), 0), | ||
| 611 | + Source::new(asset("test_tone.mp3"), 0), | ||
| 612 | + ], | ||
| 613 | + vec![ | ||
| 614 | + ( | ||
| 615 | + LaneKind::Video, | ||
| 616 | + vec![Clip { | ||
| 617 | + start: 0, | ||
| 618 | + in_frame: 0, | ||
| 619 | + out_frame: 60, | ||
| 620 | + source: 0, | ||
| 621 | + link: None, | ||
| 622 | + eq: None, | ||
| 623 | + color: None, | ||
| 624 | + fit: FitPolicy::default(), | ||
| 625 | + speed: Speed::NORMAL, | ||
| 626 | + }], | ||
| 627 | + ), | ||
| 628 | + ( | ||
| 629 | + LaneKind::Audio, | ||
| 630 | + vec![Clip { | ||
| 631 | + start: 0, | ||
| 632 | + in_frame: 0, | ||
| 633 | + out_frame: 60, | ||
| 634 | + source: 1, | ||
| 635 | + link: None, | ||
| 636 | + eq: None, | ||
| 637 | + color: None, | ||
| 638 | + fit: FitPolicy::default(), | ||
| 639 | + speed: Speed::NORMAL, | ||
| 640 | + }], | ||
| 641 | + ), | ||
| 642 | + ], | ||
| 643 | + Vec::new(), | ||
| 644 | + Vec::new(), | ||
| 645 | + ) | ||
| 646 | + .expect("a still under a song"); | ||
| 647 | + let meta = engine::VideoMeta { | ||
| 648 | + width: 640, | ||
| 649 | + height: 360, | ||
| 650 | + frame_rate: 30.0, | ||
| 651 | + frame_count: 60, | ||
| 652 | + codec: engine::Codec::H264, | ||
| 653 | + }; | ||
| 654 | + for want in [128u32, 320] { | ||
| 655 | + let out = out_path(&format!("mp4_aac_{want}"), "mp4"); | ||
| 656 | + let handle = engine::export::start( | ||
| 657 | + project.clone(), | ||
| 658 | + meta, | ||
| 659 | + &out, | ||
| 660 | + &ExportSettings { | ||
| 661 | + audio_kbps: Some(want), | ||
| 662 | + ..Default::default() | ||
| 663 | + }, | ||
| 664 | + ); | ||
| 665 | + wait(&handle, Duration::from_secs(300)).expect("an mp4 of a still under a song"); | ||
| 666 | + let got = mp4_aac_kbps(&out, f64::from(RATE)); | ||
| 667 | + println!("mp4 asked {want} kbps, the track measures {got:.1}"); | ||
| 668 | + assert!( | ||
| 669 | + (got - f64::from(want)).abs() < f64::from(want) / 5.0, | ||
| 670 | + "{got:.1} kbps for a {want} kbps request" | ||
| 671 | + ); | ||
| 672 | + std::fs::remove_file(&out).unwrap(); | ||
| 673 | + } | ||
| 674 | +} | ||
| 675 | + | ||
| 514 | /// A silent timeline cannot become an audio file, and says so rather than | 676 | /// A silent timeline cannot become an audio file, and says so rather than |
| 515 | /// writing zero samples that a user would take for a broken microphone. | 677 | /// writing zero samples that a user would take for a broken microphone. |
| 516 | #[test] | 678 | #[test] |