Skip to content

Commit

Permalink
feat: add support for Ogg Opus format. fixes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Oct 29, 2022
1 parent e83741f commit 8798e33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ matcher_map!(
"m4a",
matchers::audio::is_m4a
),
// has to come before ogg
(
MatcherType::Audio,
"audio/opus",
"opus",
matchers::audio::is_ogg_opus
),
(
MatcherType::Audio,
"audio/ogg",
Expand Down
17 changes: 17 additions & 0 deletions src/matchers/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ pub fn is_ogg(buf: &[u8]) -> bool {
buf.len() > 3 && buf[0] == 0x4F && buf[1] == 0x67 && buf[2] == 0x67 && buf[3] == 0x53
}

/// Returns whether a buffer is OGG Opus data.
pub fn is_ogg_opus(buf: &[u8]) -> bool {
if !is_ogg(buf) {
return false;
}

buf.len() > 35
&& buf[28] == 0x4F
&& buf[29] == 0x70
&& buf[30] == 0x75
&& buf[31] == 0x73
&& buf[32] == 0x48
&& buf[33] == 0x65
&& buf[34] == 0x61
&& buf[35] == 0x64
}

/// Returns whether a buffer is FLAC data.
pub fn is_flac(buf: &[u8]) -> bool {
buf.len() > 3 && buf[0] == 0x66 && buf[1] == 0x4C && buf[2] == 0x61 && buf[3] == 0x43
Expand Down
Binary file added testdata/sample_48kbps.opus
Binary file not shown.
1 change: 1 addition & 0 deletions tests/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ mod common;
test_format!(Audio, "audio/mpeg", "mp3", mp3, "sample.mp3");
test_format!(Audio, "audio/x-dsf", "dsf", dsf, "sample.dsf");
test_format!(Audio, "audio/x-ape", "ape", ape, "sample.ape");
test_format!(Audio, "audio/opus", "opus", opus, "sample_48kbps.opus");

0 comments on commit 8798e33

Please sign in to comment.