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
use std::num::NonZeroU8;

use crate::{ChannelLayout, SampleRate, SampleType};

/// A struct that encodes all parameters that are needed to interpret an audio
/// time span as number of samples and/or the number of bytes needed to
/// represent it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct System {
    pub sample_rate: SampleRate,
    pub channel_layout: ChannelLayout,
    pub sample_type: SampleType,
}

impl System {
    /// The number of bytes used to represent a single sample.
    ///
    /// Equal to the sample's byte depth
    /// ([`self.sample_type.byte_depth()`](crate::SampleType::byte_depth)) times
    /// the number of channels
    /// ([`self.channel_layout.channels()`](crate::ChannelLayout::channels)).
    #[inline]
    #[track_caller]
    pub const fn sample_size(&self) -> NonZeroU8 {
        self.channel_layout
            .channels()
            .checked_mul(self.sample_type.byte_depth())
            .expect("Overflow trying to calculate system's sample size")
    }
}

/// Macro for easily creating a [`System`].
///
/// # Example
/// ```
/// use audio_time::system;
///
/// let _ = system!(44_100, Mono, i16);
/// let _ = system!(8_000, Stereo, f64);
/// ```
#[macro_export]
macro_rules! system {
    ($sample_rate:literal, $channel_layout:ident, $sample:ty) => {
        ::audio_time::System {
            sample_rate: ::audio_time::sample_rate!($sample_rate),
            channel_layout: ::audio_time::ChannelLayout::$channel_layout,
            sample_type: ::audio_time::SampleType::new::<$sample>(),
        }
    };
}

/// Audio CD encoding system.
///
/// <https://en.wikipedia.org/wiki/Compact_Disc_Digital_Audio>:
/// ```text
/// 2 channels of LPCM audio, each signed 16-bit values sampled at 44100 Hz
/// ```
pub const AUDIO_CD: System = system!(44_100, Stereo, i16);