Duration

Struct Duration 

Source
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

Source

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.

Source

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

Source

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),
);
Source

pub const fn samples(self) -> u64

§Total Samples.

Return the total number of samples.

§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().samples(),
    5_073_852,
);
Source

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),
);
Source

pub const fn sectors(self) -> u64

§Number of Sectors.

Return the total number of sectors.

§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().sectors(),
    8629,
);
Source

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,
);
Source

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,
);
Source

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> Add<T> for Duration
where u64: From<T>,

Source§

type Output = Duration

The resulting type after applying the + operator.
Source§

fn add(self, other: T) -> Self

Performs the + operation. Read more
Source§

impl<T> AddAssign<T> for Duration
where u64: From<T>,

Source§

fn add_assign(&mut self, other: T)

Performs the += operation. Read more
Source§

impl Clone for Duration

Source§

fn clone(&self) -> Duration

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Duration

Source§

fn default() -> Duration

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Duration

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div<T> for Duration
where u64: From<T>,

Source§

type Output = Duration

The resulting type after applying the / operator.
Source§

fn div(self, other: T) -> Self

Performs the / operation. Read more
Source§

impl<T> DivAssign<T> for Duration
where u64: From<T>,

Source§

fn div_assign(&mut self, other: T)

Performs the /= operation. Read more
Source§

impl From<Duration> for u64

Source§

fn from(src: Duration) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Duration

Source§

fn from(src: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Duration

Source§

fn from(src: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Duration

Source§

fn from(src: usize) -> Self

Converts to this type from the input type.
Source§

impl Hash for Duration

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Mul<T> for Duration
where u64: From<T>,

Source§

type Output = Duration

The resulting type after applying the * operator.
Source§

fn mul(self, other: T) -> Self

Performs the * operation. Read more
Source§

impl<T> MulAssign<T> for Duration
where u64: From<T>,

Source§

fn mul_assign(&mut self, other: T)

Performs the *= operation. Read more
Source§

impl Ord for Duration

Source§

fn cmp(&self, other: &Duration) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Duration

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Duration

Source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Duration

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> Sub<T> for Duration
where u64: From<T>,

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, other: T) -> Self

Performs the - operation. Read more
Source§

impl<T> SubAssign<T> for Duration
where u64: From<T>,

Source§

fn sub_assign(&mut self, other: T)

Performs the -= operation. Read more
Source§

impl Sum for Duration

Source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = Self>,

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Copy for Duration

Source§

impl Eq for Duration

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,