-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support duration extraction from WAV files
- Loading branch information
1 parent
6fb0fb5
commit ba2e3ad
Showing
5 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::io; | ||
use std::path::Path; | ||
|
||
use mp3_metadata::MP3Metadata; | ||
|
||
use wavers::Wav; | ||
|
||
use crate::util::duration::DurationExtractor; | ||
use crate::util::Duration; | ||
|
||
pub struct WavDurationExtractor; | ||
|
||
impl DurationExtractor for WavDurationExtractor { | ||
fn supports_ext(&self, ext_lowercase: &str) -> bool { | ||
"wav" == ext_lowercase | ||
} | ||
|
||
fn try_read_duration( | ||
&self, | ||
path: &Path, | ||
_: &Option<MP3Metadata>, | ||
) -> io::Result<Option<Duration>> { | ||
let wav: Wav<i16> = Wav::from_path(path).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; | ||
Ok(Some(Duration { length: wav.duration() as usize })) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::util::duration::DurationExtractor; | ||
use crate::util::Duration; | ||
use std::error::Error; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn test_success() -> Result<(), Box<dyn Error>> { | ||
let path_string = | ||
std::env::var("CARGO_MANIFEST_DIR")? + "/resources/test/" + "audio/silent.wav"; | ||
let path = PathBuf::from(path_string); | ||
assert_eq!( | ||
WavDurationExtractor.try_read_duration(&path, &None)?, | ||
Some(Duration { length: 15 }), | ||
); | ||
Ok(()) | ||
} | ||
} |