(data: &[u8])
| 319 | /// Parse an MP3 file into SoundData. |
| 320 | #[cfg(feature = "mp3")] |
| 321 | pub fn parse_mp3(data: &[u8]) -> Option<SoundData> { |
| 322 | let mut decoder = minimp3::Decoder::new(std::io::Cursor::new(data)); |
| 323 | let mut samples = Vec::new(); |
| 324 | let mut sample_rate = 0u32; |
| 325 | let mut channels = 0u16; |
| 326 | |
| 327 | loop { |
| 328 | match decoder.next_frame() { |
| 329 | Ok(frame) => { |
| 330 | if sample_rate == 0 { |
| 331 | sample_rate = frame.sample_rate as u32; |
| 332 | channels = frame.channels as u16; |
| 333 | } |
| 334 | for &s in &frame.data { |
| 335 | samples.push(s as f32 / 32768.0); |
| 336 | } |
| 337 | } |
| 338 | Err(minimp3::Error::Eof) => break, |
| 339 | Err(_) => return None, |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if samples.is_empty() { return None; } |
| 344 | Some(SoundData { samples, sample_rate, channels }) |
| 345 | } |
| 346 | |
| 347 | /// Parse an OGG Vorbis file into SoundData. |
| 348 | pub fn parse_ogg(data: &[u8]) -> Option<SoundData> { |
no test coverage detected