| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use std::fs::File; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::process::Command; |
| 14 | |
| 15 | use ec_core::{ |
| 16 | AudioParameters, Buf, ChannelLayout, CodecId, CodecParameters, MediaParameters, Muxer, Packet, |
| 17 | StreamInfo, TimeBase, |
| 18 | }; |
| 19 | |
| 20 | fn scratch(name: &str) -> PathBuf { |
| 21 | let dir = std::env::temp_dir().join(format!("rusty-vorbis-shim-{}", std::process::id())); |
| 22 | std::fs::create_dir_all(&dir).expect("scratch dir"); |
| 23 | dir.join(name) |
| 24 | } |
| 25 | |
| 26 | |
| 27 | |
| 28 | fn mix(channels: u16, rate: u32, frames: usize) -> Vec<i16> { |
| 29 | let mut samples = Vec::with_capacity(frames * usize::from(channels)); |
| 30 | for i in 0..frames { |
| 31 | for channel in 0..channels { |
| 32 | let hz = 220.0 * f64::from(channel + 1); |
| 33 | let t = i as f64 / f64::from(rate); |
| 34 | let value = (2.0 * std::f64::consts::PI * hz * t).sin() * 0.6; |
| 35 | samples.push((value * f64::from(i16::MAX)) as i16); |
| 36 | } |
| 37 | } |
| 38 | samples |
| 39 | } |
| 40 | |
| 41 | |
| 42 | fn write_ogg(out: &Path, samples: &[i16], channels: u16, rate: u32) { |
| 43 | let mut encoder = rusty_vorbis::VorbisEncoder::new(rusty_vorbis::VorbisEncoderConfig { |
| 44 | bitrate_bps: rusty_vorbis::BITRATE_NOMINAL, |
| 45 | quality: 0.85, |
| 46 | }); |
| 47 | encoder |
| 48 | .push_pcm_s16(samples, channels, rate) |
| 49 | .expect("vorbis encode"); |
| 50 | encoder.finish(); |
| 51 | let mut packets = Vec::new(); |
| 52 | loop { |
| 53 | match encoder.next_packet() { |
| 54 | Ok(packet) => packets.push(packet), |
| 55 | Err(rusty_vorbis::Error::Eof) => break, |
| 56 | Err(e) => panic!("vorbis encode: {e}"), |
| 57 | } |
| 58 | } |
| 59 | assert!( |
| 60 | packets.len() >= 4, |
| 61 | "{} packets is not a stream", |
| 62 | packets.len() |
| 63 | ); |
| 64 | |
| 65 | let extradata = ec_ogg::xiph_lace(&[ |
| 66 | &packets[0].data[..], |
| 67 | &packets[1].data[..], |
| 68 | &packets[2].data[..], |
| 69 | ]) |
| 70 | .expect("the three headers lace"); |
| 71 | |
| 72 | let time_base = TimeBase::new(1, i64::from(rate)); |
| 73 | let mut params = CodecParameters::new(CodecId::Vorbis); |
| 74 | params.media = MediaParameters::Audio(AudioParameters { |
| 75 | sample_rate: rate, |
| 76 | layout: ChannelLayout::from_count(usize::from(channels)), |
| 77 | format: None, |
| 78 | bits_per_sample: None, |
| 79 | }); |
| 80 | params.extradata = Some(Buf::from_vec(extradata)); |
| 81 | let mut muxer = ec_ogg::OggMuxer::new(File::create(out).expect("create")); |
| 82 | muxer |
| 83 | .add_stream(StreamInfo::new(0, time_base, params)) |
| 84 | .expect("stream"); |
| 85 | muxer.write_headers().expect("headers"); |
| 86 | for packet in &packets[3..] { |
| 87 | |
| 88 | |
| 89 | let mut out = Packet::new(0, time_base, packet.data.clone()); |
| 90 | out.side_data.push(ec_ogg::granule_side_data(packet.pts)); |
| 91 | out.duration = Some(packet.duration); |
| 92 | muxer.write_packet(&out).expect("packet"); |
| 93 | } |
| 94 | muxer.finish().expect("trailer"); |
| 95 | } |
| 96 | |
| 97 | fn probe(path: &Path, field: &str) -> String { |
| 98 | let out = Command::new("ffprobe") |
| 99 | .args([ |
| 100 | "-v", |
| 101 | "error", |
| 102 | "-select_streams", |
| 103 | "a:0", |
| 104 | "-show_entries", |
| 105 | &format!("stream={field}"), |
| 106 | "-of", |
| 107 | "default=noprint_wrappers=1:nokey=1", |
| 108 | ]) |
| 109 | .arg(path) |
| 110 | .output() |
| 111 | .expect("ffprobe"); |
| 112 | String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn writes_an_ogg_44100_mono_widened_to_stereo() { |
| 117 | let path = scratch("write-ogg-44100-widened.ogg"); |
| 118 | let rate = 44_100u32; |
| 119 | let frames = 44_100usize; |
| 120 | let mono = mix(1, rate, frames); |
| 121 | let widened: Vec<i16> = mono.iter().flat_map(|&s| [s, s]).collect(); |
| 122 | write_ogg(&path, &widened, 2, rate); |
| 123 | let decode = Command::new("ffmpeg") |
| 124 | .args(["-v", "error", "-i"]) |
| 125 | .arg(&path) |
| 126 | .args(["-f", "f32le", "-"]) |
| 127 | .output() |
| 128 | .expect("ffmpeg"); |
| 129 | assert!(decode.stderr.is_empty(), "ffmpeg said {}", String::from_utf8_lossy(&decode.stderr)); |
| 130 | println!("duration_ts={}", probe(&path, "duration_ts")); |
| 131 | let decoded = decode.stdout.len() / 4 / 2; |
| 132 | assert_eq!(decoded, frames, "decoded sample count"); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn writes_an_ogg_the_way_the_replica_would_without_its_corrections() { |
| 137 | let rate = 48_000u32; |
| 138 | |
| 139 | |
| 140 | let frames = 33_333usize; |
| 141 | for channels in [1u16, 2, 6] { |
| 142 | let path = scratch(&format!("write-ogg-{channels}ch.ogg")); |
| 143 | write_ogg(&path, &mix(channels, rate, frames), channels, rate); |
| 144 | |
| 145 | |
| 146 | let decode = Command::new("ffmpeg") |
| 147 | .args(["-v", "error", "-i"]) |
| 148 | .arg(&path) |
| 149 | .args(["-f", "f32le", "-"]) |
| 150 | .output() |
| 151 | .expect("ffmpeg"); |
| 152 | assert!( |
| 153 | decode.stderr.is_empty(), |
| 154 | "{channels} ch: ffmpeg said {}", |
| 155 | String::from_utf8_lossy(&decode.stderr) |
| 156 | ); |
| 157 | |
| 158 | assert_eq!( |
| 159 | probe(&path, "duration_ts"), |
| 160 | frames.to_string(), |
| 161 | "{channels} ch: duration" |
| 162 | ); |
| 163 | |
| 164 | assert_eq!( |
| 165 | probe(&path, "channels"), |
| 166 | channels.to_string(), |
| 167 | "{channels} ch: channel count" |
| 168 | ); |
| 169 | let decoded = decode.stdout.len() / 4 / usize::from(channels); |
| 170 | assert_eq!(decoded, frames, "{channels} ch: decoded sample count"); |
| 171 | } |
| 172 | } |