| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | #![forbid(unsafe_code)] |
| 17 | #![warn(missing_docs)] |
| 18 | |
| 19 | use std::fmt; |
| 20 | use std::fs::File; |
| 21 | use std::io::{self, BufReader, BufWriter, Read, Seek, Write}; |
| 22 | use std::path::Path; |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub trait Sample: ec_riff::Sample { |
| 30 | |
| 31 | const BITS: u16; |
| 32 | |
| 33 | fn from_i32(v: i32) -> Self; |
| 34 | |
| 35 | fn from_f32(v: f32) -> Self; |
| 36 | } |
| 37 | |
| 38 | macro_rules! int_sample { |
| 39 | ($($t:ty => $bits:expr),*) => {$( |
| 40 | impl Sample for $t { |
| 41 | const BITS: u16 = $bits; |
| 42 | fn from_i32(v: i32) -> Self { |
| 43 | v as $t |
| 44 | } |
| 45 | fn from_f32(v: f32) -> Self { |
| 46 | v as $t |
| 47 | } |
| 48 | } |
| 49 | )*}; |
| 50 | } |
| 51 | int_sample!(i8 => 8, i16 => 16, i32 => 32); |
| 52 | |
| 53 | impl Sample for f32 { |
| 54 | const BITS: u16 = 32; |
| 55 | fn from_i32(v: i32) -> Self { |
| 56 | v as f32 |
| 57 | } |
| 58 | fn from_f32(v: f32) -> Self { |
| 59 | v |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 65 | pub enum SampleFormat { |
| 66 | |
| 67 | Float, |
| 68 | |
| 69 | Int, |
| 70 | } |
| 71 | |
| 72 | |
| 73 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 74 | pub struct WavSpec { |
| 75 | |
| 76 | pub channels: u16, |
| 77 | |
| 78 | pub sample_rate: u32, |
| 79 | |
| 80 | pub bits_per_sample: u16, |
| 81 | |
| 82 | pub sample_format: SampleFormat, |
| 83 | } |
| 84 | |
| 85 | impl From<WavSpec> for ec_riff::WavSpec { |
| 86 | fn from(s: WavSpec) -> ec_riff::WavSpec { |
| 87 | ec_riff::WavSpec { |
| 88 | channels: s.channels, |
| 89 | sample_rate: s.sample_rate, |
| 90 | bits_per_sample: s.bits_per_sample, |
| 91 | sample_format: match s.sample_format { |
| 92 | SampleFormat::Float => ec_riff::SampleType::Float, |
| 93 | SampleFormat::Int => ec_riff::SampleType::Int, |
| 94 | }, |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | #[derive(Debug)] |
| 101 | pub enum Error { |
| 102 | |
| 103 | IoError(io::Error), |
| 104 | |
| 105 | FormatError(&'static str), |
| 106 | |
| 107 | TooWide, |
| 108 | |
| 109 | UnfinishedSample, |
| 110 | |
| 111 | Unsupported, |
| 112 | |
| 113 | InvalidSampleFormat, |
| 114 | } |
| 115 | |
| 116 | impl fmt::Display for Error { |
| 117 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 118 | match self { |
| 119 | Error::IoError(e) => e.fmt(f), |
| 120 | Error::FormatError(reason) => write!(f, "Ill-formed WAVE file: {reason}"), |
| 121 | Error::TooWide => f.write_str("The sample has more bits than the destination type."), |
| 122 | Error::UnfinishedSample => f.write_str( |
| 123 | "The number of samples written is not a multiple of the number of channels.", |
| 124 | ), |
| 125 | Error::Unsupported => f.write_str("The wave format of the file is not supported."), |
| 126 | Error::InvalidSampleFormat => { |
| 127 | f.write_str("The sample format differs from the destination format.") |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl std::error::Error for Error { |
| 134 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 135 | match self { |
| 136 | Error::IoError(e) => Some(e), |
| 137 | _ => None, |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl From<io::Error> for Error { |
| 143 | fn from(e: io::Error) -> Error { |
| 144 | Error::IoError(e) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | pub type Result<T> = std::result::Result<T, Error>; |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | fn map(e: ec_riff::Error, corrupt_as: Error) -> Error { |
| 155 | match e { |
| 156 | ec_riff::Error::Io(e) => Error::IoError(e), |
| 157 | ec_riff::Error::Unsupported { .. } => Error::Unsupported, |
| 158 | _ => corrupt_as, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | pub struct WavWriter<W: Write + Seek> { |
| 164 | inner: ec_riff::WavWriter<W>, |
| 165 | } |
| 166 | |
| 167 | impl WavWriter<BufWriter<File>> { |
| 168 | |
| 169 | pub fn create<P: AsRef<Path>>(path: P, spec: WavSpec) -> Result<Self> { |
| 170 | let file = File::create(path)?; |
| 171 | WavWriter::new(BufWriter::new(file), spec) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl<W: Write + Seek> WavWriter<W> { |
| 176 | |
| 177 | pub fn new(writer: W, spec: WavSpec) -> Result<Self> { |
| 178 | ec_riff::WavWriter::new(writer, spec.into()) |
| 179 | .map(|inner| WavWriter { inner }) |
| 180 | .map_err(|e| { |
| 181 | map( |
| 182 | e, |
| 183 | Error::FormatError("unusable channel count or sample rate"), |
| 184 | ) |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | |
| 189 | pub fn write_sample<S: Sample>(&mut self, sample: S) -> Result<()> { |
| 190 | self.inner.write_sample(sample).map_err(|e| match e { |
| 191 | |
| 192 | |
| 193 | |
| 194 | ec_riff::Error::Io(e) => Error::IoError(e), |
| 195 | ec_riff::Error::Unsupported { .. } => Error::InvalidSampleFormat, |
| 196 | _ => Error::TooWide, |
| 197 | }) |
| 198 | } |
| 199 | |
| 200 | |
| 201 | pub fn duration(&self) -> u32 { |
| 202 | self.inner.duration() as u32 |
| 203 | } |
| 204 | |
| 205 | |
| 206 | pub fn spec(&self) -> WavSpec { |
| 207 | let s = self.inner.spec(); |
| 208 | WavSpec { |
| 209 | channels: s.channels, |
| 210 | sample_rate: s.sample_rate, |
| 211 | bits_per_sample: s.bits_per_sample, |
| 212 | sample_format: match s.sample_format { |
| 213 | ec_riff::SampleType::Float => SampleFormat::Float, |
| 214 | ec_riff::SampleType::Int => SampleFormat::Int, |
| 215 | }, |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | pub fn finalize(self) -> Result<()> { |
| 224 | self.inner |
| 225 | .finalize() |
| 226 | .map(|_| ()) |
| 227 | .map_err(|e| map(e, Error::UnfinishedSample)) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | |
| 232 | pub struct WavReader<R: Read> { |
| 233 | inner: ec_riff::WavReader<R>, |
| 234 | } |
| 235 | |
| 236 | impl WavReader<BufReader<File>> { |
| 237 | |
| 238 | pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> { |
| 239 | WavReader::new(BufReader::new(File::open(path)?)) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | impl<R: Read> WavReader<R> { |
| 244 | |
| 245 | pub fn new(reader: R) -> Result<Self> { |
| 246 | ec_riff::WavReader::new(reader) |
| 247 | .map(|inner| WavReader { inner }) |
| 248 | .map_err(|e| map(e, Error::FormatError("not a WAVE file this reader accepts"))) |
| 249 | } |
| 250 | |
| 251 | |
| 252 | pub fn spec(&self) -> WavSpec { |
| 253 | let s = self.inner.spec(); |
| 254 | WavSpec { |
| 255 | channels: s.channels, |
| 256 | sample_rate: s.sample_rate, |
| 257 | bits_per_sample: s.bits_per_sample, |
| 258 | sample_format: match s.sample_format { |
| 259 | ec_riff::SampleType::Float => SampleFormat::Float, |
| 260 | ec_riff::SampleType::Int => SampleFormat::Int, |
| 261 | }, |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | pub fn samples<S: Sample>(&mut self) -> WavSamples<S> { |
| 276 | let spec = self.inner.spec(); |
| 277 | let float_file = spec.sample_format == ec_riff::SampleType::Float; |
| 278 | let items = if float_file != S::FLOAT { |
| 279 | vec![Err(Error::InvalidSampleFormat)] |
| 280 | } else if spec.bits_per_sample > S::BITS { |
| 281 | vec![Err(Error::TooWide)] |
| 282 | } else if float_file { |
| 283 | match self.inner.read_all_f32() { |
| 284 | Ok(v) => v.into_iter().map(|s| Ok(S::from_f32(s))).collect(), |
| 285 | Err(e) => vec![Err(map(e, Error::FormatError("truncated WAVE data")))], |
| 286 | } |
| 287 | } else { |
| 288 | match self.inner.read_all_i32() { |
| 289 | Ok(v) => v.into_iter().map(|s| Ok(S::from_i32(s))).collect(), |
| 290 | Err(e) => vec![Err(map(e, Error::FormatError("truncated WAVE data")))], |
| 291 | } |
| 292 | }; |
| 293 | WavSamples(items.into_iter()) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | pub struct WavSamples<S>(std::vec::IntoIter<Result<S>>); |
| 299 | |
| 300 | impl<S> Iterator for WavSamples<S> { |
| 301 | type Item = Result<S>; |
| 302 | |
| 303 | fn next(&mut self) -> Option<Self::Item> { |
| 304 | self.0.next() |
| 305 | } |
| 306 | |
| 307 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 308 | self.0.size_hint() |
| 309 | } |
| 310 | } |