diff options
author | François Laignel <fengalin@free.fr> | 2021-10-09 13:17:05 +0300 |
---|---|---|
committer | François Laignel <fengalin@free.fr> | 2021-10-18 16:09:47 +0300 |
commit | 27b9f0d868f436e9b2bcc3e51f393c40b56fcc02 (patch) | |
tree | 93c0db7b1cf26ea7d0e3a4d70a7d2844c2e00975 /audio | |
parent | bd8a7e8df7e8ebf751b2d00fe6a096d726683c00 (diff) |
Improve usability thanks to opt-ops
The crate option-operations simplifies usage when dealing with
`Option`s, which is often the case with `ClockTime`.
Diffstat (limited to 'audio')
-rw-r--r-- | audio/audiofx/src/audioloudnorm/imp.rs | 10 | ||||
-rw-r--r-- | audio/audiofx/src/audiornnoise/imp.rs | 8 | ||||
-rw-r--r-- | audio/audiofx/src/ebur128level/imp.rs | 8 | ||||
-rw-r--r-- | audio/csound/src/filter/imp.rs | 5 | ||||
-rw-r--r-- | audio/csound/tests/csound_filter.rs | 2 |
5 files changed, 19 insertions, 14 deletions
diff --git a/audio/audiofx/src/audioloudnorm/imp.rs b/audio/audiofx/src/audioloudnorm/imp.rs index 829258dd..e0512a61 100644 --- a/audio/audiofx/src/audioloudnorm/imp.rs +++ b/audio/audiofx/src/audioloudnorm/imp.rs @@ -235,7 +235,9 @@ impl State { let distance_ts = distance_samples .mul_div_floor(*gst::ClockTime::SECOND, self.info.rate() as u64) .map(gst::ClockTime::from_nseconds); - let pts = pts.zip(distance_ts).map(|(pts, dist)| pts + dist); + let pts = pts + .opt_checked_add(distance_ts) + .map_err(|_| gst::FlowError::Error)?; let inbuf = self .adapter @@ -276,7 +278,9 @@ impl State { let distance_ts = distance_samples .mul_div_floor(*gst::ClockTime::SECOND, self.info.rate() as u64) .map(gst::ClockTime::from_nseconds); - let pts = pts.zip(distance_ts).map(|(pts, dist)| pts + dist); + let pts = pts + .opt_checked_add(distance_ts) + .map_err(|_| gst::FlowError::Error)?; let mut _mapped_inbuf = None; let src = if self.adapter.available() > 0 { @@ -1702,7 +1706,7 @@ impl AudioLoudNorm { q.set( live, min_latency + 3 * gst::ClockTime::SECOND, - max_latency.map(|max| max + 3 * gst::ClockTime::SECOND), + max_latency.opt_add(3 * gst::ClockTime::SECOND), ); true } else { diff --git a/audio/audiofx/src/audiornnoise/imp.rs b/audio/audiofx/src/audiornnoise/imp.rs index 33ae01b8..6a75658b 100644 --- a/audio/audiofx/src/audiornnoise/imp.rs +++ b/audio/audiofx/src/audiornnoise/imp.rs @@ -74,8 +74,9 @@ impl State { // pts at the beginning of the adapter. let samples = distance / self.in_info.bpf() as u64; prev_pts - .zip(self.samples_to_time(samples)) - .map(|(prev_pts, time_offset)| prev_pts + time_offset) + .opt_checked_add(self.samples_to_time(samples)) + .ok() + .flatten() } fn needs_more_data(&self) -> bool { @@ -367,8 +368,7 @@ impl BaseTransformImpl for AudioRNNoise { ); min += gst::ClockTime::from_seconds((FRAME_SIZE / 48000) as u64); - max = max - .map(|max| max + gst::ClockTime::from_seconds((FRAME_SIZE / 48000) as u64)); + max = max.opt_add(gst::ClockTime::from_seconds((FRAME_SIZE / 48000) as u64)); q.set(live, min, max); return true; } diff --git a/audio/audiofx/src/ebur128level/imp.rs b/audio/audiofx/src/ebur128level/imp.rs index c1641073..18fd5df1 100644 --- a/audio/audiofx/src/ebur128level/imp.rs +++ b/audio/audiofx/src/ebur128level/imp.rs @@ -481,12 +481,12 @@ impl BaseTransformImpl for EbuR128Level { // The timestamp we report in messages is always the timestamp until which measurements // are included, not the starting timestamp. - timestamp = timestamp.map(|ts| { - ts + to_process + timestamp = timestamp.opt_add( + to_process .mul_div_floor(*gst::ClockTime::SECOND, state.info.rate() as u64) .map(gst::ClockTime::from_nseconds) - .unwrap() - }); + .unwrap(), + ); // Post a message whenever an interval is full if state.interval_frames_remaining.is_zero() { diff --git a/audio/csound/src/filter/imp.rs b/audio/csound/src/filter/imp.rs index b3c649ae..c6f479c9 100644 --- a/audio/csound/src/filter/imp.rs +++ b/audio/csound/src/filter/imp.rs @@ -120,8 +120,9 @@ impl State { // pts at the beginning of the adapter. let samples = distance / self.in_info.bpf() as u64; prev_pts - .zip(self.samples_to_time(samples)) - .map(|(prev_pts, time_offset)| prev_pts + time_offset) + .opt_checked_add(self.samples_to_time(samples)) + .ok() + .flatten() } fn buffer_duration(&self, buffer_size: u64) -> Option<gst::ClockTime> { diff --git a/audio/csound/tests/csound_filter.rs b/audio/csound/tests/csound_filter.rs index 702d47e5..54f856a2 100644 --- a/audio/csound/tests/csound_filter.rs +++ b/audio/csound/tests/csound_filter.rs @@ -181,7 +181,7 @@ fn csound_filter_eos() { let samples_at_eos = (EOS_NUM_BUFFERS * EOS_NUM_SAMPLES) % ksmps; assert_eq!( buffer.as_ref().pts(), - duration_from_samples(samples_at_eos as _, sr as _).map(|duration| in_pts - duration) + in_pts.opt_sub(duration_from_samples(samples_at_eos as _, sr as _)) ); let map = buffer.into_mapped_buffer_readable().unwrap(); |