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;
pub const SAMPLING_FREQUENCY_8000: u32 = 8000;
pub const SAMPLING_FREQUENCY_11025: u32 = 11025;
pub const SAMPLING_FREQUENCY_16000: u32 = 16000;
pub const SAMPLING_FREQUENCY_22050: u32 = 22050;
pub const SAMPLING_FREQUENCY_32000: u32 = 32000;
pub const SAMPLING_FREQUENCY_44100: u32 = 44100;
pub const SAMPLING_FREQUENCY_48000: u32 = 48000;
pub const SAMPLING_FREQUENCY_88200: u32 = 88200;
pub const SAMPLING_FREQUENCY_96000: u32 = 96000;
pub const SAMPLING_FREQUENCY_176400: u32 = 176400;
pub const SAMPLING_FREQUENCY_192000: u32 = 192000;
pub const SAMPLING_FREQUENCY_352800: u32 = 352800;
pub const SAMPLING_FREQUENCY_384000: u32 = 384000;
pub const SAMPLING_FREQUENCY_705600: u32 = 705600;
pub const SAMPLING_FREQUENCY_768000: u32 = 768000;
pub const SAMPLING_FREQUENCY_2822400: u32 = 2822400;
pub const SAMPLING_FREQUENCY_5644800: u32 = 5644800;
pub const SAMPLING_FREQUENCY_11289600: u32 = 11289600;
pub const SAMPLING_FREQUENCY_22579200: u32 = 22579200;
pub struct AudioDuration {
samples: u64,
sampling_frequency: u32,
}
impl AudioDuration {
pub fn new(samples: u64, sampling_frequency: u32) -> AudioDuration {
AudioDuration {
samples,
sampling_frequency,
}
}
pub fn samples(&self) -> u64 {
self.samples
}
pub fn sampling_frequency(&self) -> u32 {
self.sampling_frequency
}
pub fn as_secs(&self) -> u64 {
self.samples / self.sampling_frequency as u64
}
pub fn as_mins(&self) -> u64 {
self.as_secs() / SECONDS_IN_A_MINUTE
}
pub fn as_hours(&self) -> u64 {
self.as_secs() / SECONDS_IN_A_HOUR
}
pub fn as_days(&self) -> u64 {
self.as_secs() / SECONDS_IN_A_DAY
}
pub fn as_weeks(&self) -> u64 {
self.as_secs() / SECONDS_IN_A_WEEK
}
pub fn subsec_samples(&self) -> u32 {
let remainder = self.samples % self.sampling_frequency as u64;
u32::try_from(remainder).unwrap()
}
pub fn subsec_nanos(&self) -> u32 {
let nanos_as_f64 = self.subsec_secs() * 1000000000.0;
nanos_as_f64 as u32
}
pub fn subsec_secs(&self) -> f64 {
self.subsec_samples() as f64 / self.sampling_frequency as f64
}
pub fn submin_secs(&self) -> u64 {
self.as_secs() % SECONDS_IN_A_MINUTE
}
pub fn subhour_mins(&self) -> u64 {
self.as_mins() % MINUTES_IN_A_HOUR
}
pub fn subday_hours(&self) -> u64 {
self.as_hours() % HOURS_IN_A_DAY
}
pub fn subweek_days(&self) -> u64 {
self.as_days() % DAYS_IN_A_WEEK
}
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));
}
}