pub struct Duration(/* private fields */);Expand description
§(CDDA Sector) Duration.
This struct holds a non-lossy — at least up to about 7.8 billion years — CD sector duration (seconds + frames) for one or more tracks.
§Examples
use cdtoc::Toc;
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let track = toc.audio_track(9).unwrap();
let duration = track.duration();
// The printable format is Dd HH:MM:SS+FF, though the day part is only
// present if non-zero.
assert_eq!(duration.to_string(), "00:01:55+04");
// The same as intelligible pieces:
assert_eq!(duration.dhmsf(), (0, 0, 1, 55, 4));
// If that's too many pieces, you can get just the seconds and frames:
assert_eq!(duration.seconds_frames(), (115, 4));The value can also be lossily converted to more familiar formats via
Duration::to_std_duration_lossy or Duration::to_f64_lossy.
Durations can also be combined every which way, for example:
use cdtoc::{Toc, Duration};
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let duration: Duration = toc.audio_tracks()
.map(|t| t.duration())
.sum();
assert_eq!(duration.to_string(), "00:34:41+63");Implementations§
Source§impl Duration
impl Duration
Sourcepub const fn from_cdda_samples(total_samples: u64) -> Result<Self, TocError>
pub const fn from_cdda_samples(total_samples: u64) -> Result<Self, TocError>
§From CDDA Samples.
Derive the duration from the total number of a track’s CDDA-quality samples.
This method assumes the count was captured at a rate of 44.1 kHz, and
requires it divide evenly into the samples-per-sector size used by
standard audio CDs (588).
For more flexible (and/or approximate) sample/duration conversions, use
Duration::from_samples instead.
§Examples
use cdtoc::Duration;
let duration = Duration::from_cdda_samples(5_073_852).unwrap();
assert_eq!(
duration.to_string(),
"00:01:55+04",
);§Errors
This will return an error if the sample count is not evenly divisible
by 588, the number of samples-per-sector for a standard audio CD.
Sourcepub fn from_samples(sample_rate: u32, total_samples: u64) -> Self
pub fn from_samples(sample_rate: u32, total_samples: u64) -> Self
§From Samples (Rescaled).
Derive the equivalent CDDA duration for a track with an arbitrary sample rate (i.e. not 44.1 kHz) or sample count.
This operation is potentially lossy and may result in a duration that is off by ±1 frame.
For standard CDDA tracks, use Duration::from_cdda_samples instead.
§Examples
use cdtoc::Duration;
let duration = Duration::from_samples(96_000, 17_271_098);
assert_eq!(
duration.to_string(),
"00:02:59+68",
);Source§impl Duration
impl Duration
Sourcepub const fn dhmsf(self) -> (u64, u8, u8, u8, u8)
pub const fn dhmsf(self) -> (u64, u8, u8, u8, u8)
§Days, Hours, Minutes, Seconds, Frames.
Carve up the duration into a quintuple of days, hours, minutes, seconds, and frames.
§Examples
use cdtoc::Toc;
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let track = toc.audio_track(9).unwrap();
assert_eq!(
track.duration().dhmsf(),
(0, 0, 1, 55, 4),
);Sourcepub const fn seconds_frames(self) -> (u64, u8)
pub const fn seconds_frames(self) -> (u64, u8)
§Seconds + Frames.
Return the duration as a tuple containing the total number of seconds and remaining frames (some fraction of a second).
Audio CDs have 75 frames per second, so the frame portion will always
be in the range of 0..75.
§Examples
use cdtoc::Toc;
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let track = toc.audio_track(9).unwrap();
assert_eq!(
track.duration().seconds_frames(),
(115, 4),
);Sourcepub const fn to_f64_lossy(self) -> f64
pub const fn to_f64_lossy(self) -> f64
§To f64 (Lossy).
Return the duration as a float (seconds.subseconds).
Given that 75ths don’t always make the cleanest of fractions, there will likely be some loss in precision.
§Examples
use cdtoc::Toc;
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let track = toc.audio_track(9).unwrap();
assert_eq!(
track.duration().to_f64_lossy(),
115.05333333333333,
);Sourcepub const fn to_std_duration_lossy(self) -> Duration
pub const fn to_std_duration_lossy(self) -> Duration
§To std::time::Duration (Lossy).
Return the value as a “normal” std::time::Duration.
Note that the std struct only counts time down to the nanosecond, so
this value might be off by a few frames.
§Examples
use cdtoc::Toc;
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let track = toc.audio_track(9).unwrap();
assert_eq!(
track.duration().to_std_duration_lossy().as_nanos(),
115_053_333_333,
);Sourcepub fn to_string_pretty(self) -> String
pub fn to_string_pretty(self) -> String
§To String Pretty.
Return a string reprsentation of the non-zero parts with English labels, separated Oxford-comma-style.
§Examples
use cdtoc::{Toc, Duration};
let toc = Toc::from_cdtoc("9+96+5766+A284+E600+11FE5+15913+19A98+1E905+240CB+26280").unwrap();
let track = toc.audio_track(9).unwrap();
assert_eq!(
track.duration().to_string_pretty(),
"1 minute, 55 seconds, and 4 frames",
);
// Empty durations look like this:
assert_eq!(
Duration::default().to_string_pretty(),
"0 seconds",
);Trait Implementations§
Source§impl<T> AddAssign<T> for Duration
impl<T> AddAssign<T> for Duration
Source§fn add_assign(&mut self, other: T)
fn add_assign(&mut self, other: T)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for Duration
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Duration
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T> DivAssign<T> for Duration
impl<T> DivAssign<T> for Duration
Source§fn div_assign(&mut self, other: T)
fn div_assign(&mut self, other: T)
/= operation. Read moreSource§impl<T> MulAssign<T> for Duration
impl<T> MulAssign<T> for Duration
Source§fn mul_assign(&mut self, other: T)
fn mul_assign(&mut self, other: T)
*= operation. Read moreSource§impl Ord for Duration
impl Ord for Duration
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§impl<T> SubAssign<T> for Duration
impl<T> SubAssign<T> for Duration
Source§fn sub_assign(&mut self, other: T)
fn sub_assign(&mut self, other: T)
-= operation. Read more