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
//! Encode and convert audio time spans between representations in number of
//! samples, number of bytes and time duration.
//!
//! ```rust
//! # #![feature(const_option)]
//! # use std::time::Duration;
//! # use audio_time::*;
//! #
//! // the Audio CD standard defines the encoding system as follows:
//! // 2 channels of LPCM audio, each signed 16-bit values sampled at 44100 Hz
//! let samples = Samples::<AUDIO_CD>::from_duration(Duration::from_secs(1));
//! assert_eq!(44_100, samples.get());
//! let bytes = samples.into_bytes();
//! assert_eq!(176_400, bytes.get());
//!
//! // both `Samples` and `Bytes` can be converted back into `Duration`s:
//! assert_eq!(bytes.into_duration(), Duration::from_secs(1));
//! assert_eq!((samples * 2).into_duration(), Duration::from_secs(2));
//!
//! // let's define our own `System`
//! const SYSTEM: System = system!(8_000, Mono, i16);
//! let samples = Samples::<SYSTEM>::from_duration(Duration::from_secs(1));
//! assert_eq!(8_000, samples.get());
//! let bytes = samples.into_bytes();
//! assert_eq!(16_000, bytes.get());
//! ```

#![allow(incomplete_features)]
#![feature(
    adt_const_params,
    const_cmp,
    const_convert,
    const_mut_refs,
    const_num_from_num,
    const_ops,
    const_option_ext,
    const_option,
    const_result_drop,
    const_trait_impl,
    const_try,
    const_type_id,
    core_intrinsics,
    derive_const,
    try_blocks
)]

extern crate self as audio_time;

mod bytes;
mod channel_layout;
mod convert;
mod sample;
mod sample_rate;
mod samples;
mod system;

pub use ChannelLayout::{Mono, Stereo};

pub use crate::{
    bytes::Bytes,
    channel_layout::ChannelLayout,
    sample::SampleType,
    sample_rate::SampleRate,
    samples::Samples,
    system::{System, AUDIO_CD},
};

#[derive(thiserror::Error, Debug)]
#[error("Overflow error")]
pub struct OverflowError(());

#[cfg(test)]
mod tests {

    use std::time::Duration;

    use audio_time::*;

    macro_rules! assert_bidi {
        ($a:expr, $b:expr) => {
            assert_eq!($a, $b.try_into().unwrap());
            assert_eq!($b, $a.try_into().unwrap());
        };
    }

    #[test]
    fn test_samples_to_duration() -> Result<(), OverflowError> {
        assert_bidi!(
            Duration::ZERO,
            Samples::<{ system!(44_100, Mono, i16) }>::new(0)
        );

        assert_bidi!(
            Duration::from_secs(2),
            Samples::<{ system!(44_100, Mono, i16) }>::new(88_200)
        );

        assert_bidi!(
            Duration::from_millis(100),
            Samples::<{ system!(8_000, Mono, i16) }>::new(800)
        );

        // channel layout and sample type should not matter in this conversion
        assert_bidi!(
            Duration::from_millis(100),
            Samples::<{ system!(8_000, Stereo, f64) }>::new(800)
        );

        // test overflow
        assert!(
            Duration::try_from(Samples::<{ system!(8_000, Mono, i16) }>::new(usize::MAX)).is_err()
        );
        assert!(Samples::<{ system!(8_000, Mono, i16) }>::try_from(Duration::MAX).is_err());

        {
            const SYS: System = system!(48_000, Mono, i16);
            let millisecond = Samples::<SYS>::new(48);
            assert_eq!(Duration::from_millis(1), millisecond.try_into()?);

            let sub_millisecond = Samples::<SYS>::new(millisecond.get() - 1);
            // this conversion is lossy for durations of under 1 milliseconds
            assert_eq!(Duration::from_millis(0), sub_millisecond.try_into()?);
            assert_ne!(
                sub_millisecond,
                Duration::try_from(sub_millisecond)?.try_into()?
            );
        }

        Ok(())
    }

    #[test]
    fn test_samples_to_bytes() -> Result<(), OverflowError> {
        assert_bidi!(bytes!(0), Samples::<{ system!(44_100, Mono, i16) }>::new(0));

        assert_bidi!(
            bytes!(2_000),
            Samples::<{ system!(48_000, Mono, i16) }>::new(1_000)
        );

        assert_bidi!(
            bytes!(4_000),
            Samples::<{ system!(48_000, Stereo, i16) }>::new(1_000)
        );

        assert_bidi!(
            bytes!(8_000),
            Samples::<{ system!(48_000, Stereo, i32) }>::new(1_000)
        );

        assert_bidi!(
            bytes!(16_000),
            Samples::<{ system!(48_000, Stereo, i32) }>::new(2_000)
        );

        // sample rate should not matter in this conversion
        assert_bidi!(
            bytes!(8_000),
            Samples::<{ system!(8_000, Stereo, i32) }>::new(1_000)
        );

        Ok(())
    }
}