1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::convert::Into;
use std::convert::TryFrom;
use std::fmt;
use std::time::Duration;

pub const DAYS_IN_A_WEEK: u64 = 7;
pub const HOURS_IN_A_DAY: u64 = 24;
pub const MINUTES_IN_A_HOUR: u64 = 60;
pub const SECONDS_IN_A_MINUTE: u64 = 60;
pub const SECONDS_IN_A_HOUR: u64 = 3600;
pub const SECONDS_IN_A_DAY: u64 = 86400;
pub const SECONDS_IN_A_WEEK: u64 = 604800;

/// Telephone narrow-band, very low quality.
pub const SAMPLING_FREQUENCY_8000: u32 = 8000;

/// Low quality, subwoofer analysis.
pub const SAMPLING_FREQUENCY_11025: u32 = 11025;

/// Telephone wide-band, used in VoIP.
pub const SAMPLING_FREQUENCY_16000: u32 = 16000;

/// Low quality, low frequency analysis.
pub const SAMPLING_FREQUENCY_22050: u32 = 22050;

/// MiniDV, 4-channel audio for DVCAM, long-play DAT.
pub const SAMPLING_FREQUENCY_32000: u32 = 32000;

/// Audio CD.
pub const SAMPLING_FREQUENCY_44100: u32 = 44100;

/// DVD, TV and Film.
pub const SAMPLING_FREQUENCY_48000: u32 = 48000;

/// Pro-audio recording for CD.
pub const SAMPLING_FREQUENCY_88200: u32 = 88200;

/// DVD-Audio, BD-ROM, HD DVD.
pub const SAMPLING_FREQUENCY_96000: u32 = 96000;

/// Pro-audio recording for CD.
pub const SAMPLING_FREQUENCY_176400: u32 = 176400;

/// DVD-Audio, BD-ROM, HD DVD.
pub const SAMPLING_FREQUENCY_192000: u32 = 192000;

/// Digital eXtreme Definition, used for recording SACDs.
pub const SAMPLING_FREQUENCY_352800: u32 = 352800;

/// Very high PCM sampling frequency on current DACs.
pub const SAMPLING_FREQUENCY_384000: u32 = 384000;

/// Digital eXtreme Definition, used for recording SACDs.
pub const SAMPLING_FREQUENCY_705600: u32 = 705600;

/// Highest PCM sampling frequency on current DACs.
pub const SAMPLING_FREQUENCY_768000: u32 = 768000;

/// DSD rate as used in SACDs, also known as DSD64
pub const SAMPLING_FREQUENCY_2822400: u32 = 2822400;

/// Doube-rate DSD, also known as DSD128
pub const SAMPLING_FREQUENCY_5644800: u32 = 5644800;

/// Quad-rate DSD, also known as DSD256.
pub const SAMPLING_FREQUENCY_11289600: u32 = 11289600;

/// Octuple-rate DSD, also known as DSD512.
pub const SAMPLING_FREQUENCY_22579200: u32 = 22579200;

pub struct AudioDuration {
    samples: u64,
    sampling_frequency: u32,
}
impl AudioDuration {
    /// Make a new `AudioDuration`.
    pub fn new(samples: u64, sampling_frequency: u32) -> AudioDuration {
        AudioDuration {
            samples,
            sampling_frequency,
        }
    }

    /// Return the number of samples in this `AudioDuration`.
    pub fn samples(&self) -> u64 {
        self.samples
    }

    /// Return the sampling frequency of this `AudioDuration`.
    pub fn sampling_frequency(&self) -> u32 {
        self.sampling_frequency
    }

    /// Return the whole number of seconds in this `AudioDuration`.
    pub fn as_secs(&self) -> u64 {
        self.samples / self.sampling_frequency as u64
    }

    /// Return the whole number of minutes in this `AudioDuration`.
    pub fn as_mins(&self) -> u64 {
        self.as_secs() / SECONDS_IN_A_MINUTE
    }

    /// Return the whole number of hours in this `AudioDuration`.
    pub fn as_hours(&self) -> u64 {
        self.as_secs() / SECONDS_IN_A_HOUR
    }

    /// Return the whole number of days in this `AudioDuration`.
    pub fn as_days(&self) -> u64 {
        self.as_secs() / SECONDS_IN_A_DAY
    }

    /// Return the whole number of weeks in this `AudioDuration`.
    pub fn as_weeks(&self) -> u64 {
        self.as_secs() / SECONDS_IN_A_WEEK
    }

    /// Return the fractional part of this `AudioDuration` in samples.
    ///
    /// This will return a value in the range `0 <= subsec_samples < sampling_frequency`.
    pub fn subsec_samples(&self) -> u32 {
        let remainder = self.samples % self.sampling_frequency as u64;

        u32::try_from(remainder).unwrap()
    }

    /// Return the fractional part of this `AudioDuration` as a whole
    /// number of nanoseconds.
    pub fn subsec_nanos(&self) -> u32 {
        let nanos_as_f64 = self.subsec_secs() * 1000000000.0;

        nanos_as_f64 as u32
    }

    /// Return the fractional part of this `AudioDuration` as seconds.
    ///
    /// This will return a value in the range `0.0 <= subsec_secs < 1.0`.
    pub fn subsec_secs(&self) -> f64 {
        self.subsec_samples() as f64 / self.sampling_frequency as f64
    }

    /// Return the fractional part of minutes in seconds.
    pub fn submin_secs(&self) -> u64 {
        self.as_secs() % SECONDS_IN_A_MINUTE
    }

    /// Return the fractional part of hours in minutes.
    pub fn subhour_mins(&self) -> u64 {
        self.as_mins() % MINUTES_IN_A_HOUR
    }

    /// Return the fractional part of days in hours.
    pub fn subday_hours(&self) -> u64 {
        self.as_hours() % HOURS_IN_A_DAY
    }

    /// Return the fractional part of days in hours.
    pub fn subweek_days(&self) -> u64 {
        self.as_days() % DAYS_IN_A_WEEK
    }

    /// Return this `AudioDuration` as a [Duration](std::time::Duration).
    pub fn duration(&self) -> Duration {
        Duration::new(self.as_secs(), self.subsec_nanos())
    }
}
impl fmt::Display for AudioDuration {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:02}:{:02}:{:02};{}",
            self.as_hours(),
            self.subhour_mins(),
            self.submin_secs(),
            self.subsec_samples(),
        )
    }
}
impl Into<Duration> for AudioDuration {
    fn into(self) -> Duration {
        self.duration()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_and_getters() {
        let samples: u64 = 32572394598;
        let audio_duration = AudioDuration::new(samples, SAMPLING_FREQUENCY_44100);

        assert_eq!(audio_duration.samples(), samples);
        assert_eq!(
            audio_duration.sampling_frequency(),
            SAMPLING_FREQUENCY_44100
        );
        assert_eq!(audio_duration.as_secs(), 738603);
        assert_eq!(audio_duration.as_mins(), 12310);
        assert_eq!(audio_duration.as_hours(), 205);
        assert_eq!(audio_duration.as_days(), 8);
        assert_eq!(audio_duration.as_weeks(), 1);
        assert_eq!(audio_duration.subsec_samples(), 2298);
        assert_eq!(audio_duration.subsec_nanos(), 52108843);
        assert_eq!(audio_duration.subsec_secs(), 0.05210884353741497);
        assert_eq!(audio_duration.submin_secs(), 3);
        assert_eq!(audio_duration.subhour_mins(), 10);
        assert_eq!(audio_duration.subday_hours(), 13);
        assert_eq!(audio_duration.subweek_days(), 1);

        assert_eq!(audio_duration.to_string(), "205:10:03;2298");
    }

    #[test]
    fn duration() {
        let samples: u64 = 52394599;
        let audio_duration = AudioDuration::new(samples, SAMPLING_FREQUENCY_44100);
        let duration: Duration = audio_duration.into();

        assert_eq!(duration, Duration::new(1188, 86145124));
    }
}