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
//! Structures related to `Wav`
//! 
//! Wav is a file which is a standard audio format.

use crate::format::Sample;
use std::fmt;

/// Details of the Wav file.
#[derive(Clone, Copy, Debug, Default, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct Wav {
    // Riff Chunk
    /// Riff Size is (file size - 8).
    pub riff_size: u32,
    // Format Chunk
    /// Format Size
    pub format_size: u32,
    /// Format Tag
    pub format_tag: u16,
    /// Channels
    pub channels: u16,
    /// Sampling Rate
    pub sampling_rate: u32,
    /// Data Rate
    pub data_rate: u32,
    /// Data Block Size
    pub data_block_size: u16,
    /// Bits Per Sample 
    pub bits_per_sample: u16,
    // Data Chunk
    /// Data Size
    pub data_size: u32,
    // Other Chunk
    /// Other Size is not exsists in a file.
    /// However, this will help erasing other chunk.
    pub other_size: u32
}

impl Wav {
    /// This method casts bits per sample to `Wav`.
    /// 
    /// # Examples
    /// ```
    /// use floaout::format::wav::Wav;
    /// 
    /// let wav = Wav::from_bits_per_sample(32);
    /// 
    /// assert_eq!(
    ///     wav,
    ///     Wav {
    ///         bits_per_sample: 32,
    ///         ..Default::default()
    ///     }
    /// );
    /// ```
    pub fn from_bits_per_sample(bits_per_sample: u16) -> Self {
        Self {
            bits_per_sample,
            ..Default::default()
        }
    }

    /// This method returns bytes per sample.
    /// 
    /// # Examples
    /// ```
    /// use floaout::format::wav::Wav;
    /// 
    /// let wav = Wav {
    ///     bits_per_sample: 32,
    ///     ..Default::default()
    /// };
    /// 
    /// let bytes_per_sample = wav.bytes_per_sample();
    /// 
    /// assert_eq!(bytes_per_sample, 4);
    /// ```
    pub fn bytes_per_sample(self) -> u16 {
        self.bits_per_sample / 8
    }

    /// This method predicts blocks from `Wav`.
    /// 
    /// # Examples
    /// ```
    /// use floaout::format::wav::Wav;
    /// 
    /// let wav = Wav {
    ///     channels: 1,
    ///     bits_per_sample: 32,
    ///     data_size: 16,
    ///     ..Default::default()
    /// };
    /// 
    /// let blocks = wav.blocks();
    /// 
    /// assert_eq!(blocks, 4);
    /// ```
    pub fn blocks(self) -> u64 {
        (self.data_size / (self.bytes_per_sample() * self.channels) as u32) as u64
    }
}

impl fmt::Display for Wav {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        write!(
            f,
            "\n File Size( - 8 ): {} Bytes\nFormat Chunk Size: {} Bytes\n      Format Tag: {}\n         Channels: {} Channels\n    Sampling Rate: {} Hz\n        Data Rate: {} kbps\n  Data Block Size: {} Bytes\n  Bits Per Sample: {} Bits\n   Wave Data Size: {} Bytes\n Other Chunk Size: {} Bytes\n",
            self.riff_size,
            self.format_size,
            self.format_tag,
            self.channels,
            self.sampling_rate,
            self.data_rate,
            self.data_block_size,
            self.bits_per_sample,
            self.data_size,
            self.other_size
        )
    }
}

/// Block of Wav
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct WavBlock(pub Sample);

impl From<f32> for WavBlock {
    fn from(sample: f32) -> Self {
        WavBlock(Sample::Float32(sample))
    }
}

impl Into<f32> for WavBlock {
    fn into(self) -> f32 {
        (self.0).into()
    }
}

impl From<f64> for WavBlock {
    fn from(sample: f64) -> Self {
        WavBlock(Sample::Float64(sample))
    }
}

impl Into<f64> for WavBlock {
    fn into(self) -> f64 {
        (self.0).into()
    }
}

impl From<Sample> for WavBlock {
    fn from(sample: Sample) -> Self {
        WavBlock(sample)
    }
}

impl Into<Sample> for WavBlock {
    fn into(self) -> Sample {
        self.0
    }
}

/// Blocks of Wav
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct WavBlocks(pub Box<[WavBlock]>);

impl From<Box<[WavBlock]>> for WavBlocks {
    fn from(buf: Box<[WavBlock]>) -> Self {
        WavBlocks(buf)
    }
}

impl Into<Box<[WavBlock]>> for WavBlocks {
    fn into(self) -> Box<[WavBlock]> {
        self.0
    }
}