Struct cdtoc::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 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 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 Durationwhere u64: From<T>,

§

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 Durationwhere 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 copy 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 Durationwhere u64: From<T>,

§

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 Durationwhere 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 Durationwhere u64: From<T>,

§

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 Durationwhere 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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

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

impl PartialEq for Duration

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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 Durationwhere u64: From<T>,

§

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 Durationwhere 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) -> Selfwhere I: Iterator<Item = Self>,

Method which 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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 Twhere T: for<'de> Deserialize<'de>,