-
Notifications
You must be signed in to change notification settings - Fork 39
Add bit_rate parameter to encoder #623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -1115,6 +1115,13 @@ def test_bad_input(self, tmp_path): | |
sample_rate=10, | ||
filename=valid_output_file, | ||
) | ||
with pytest.raises(RuntimeError, match="bit_rate=-1 must be >= 0"): | ||
create_audio_encoder( | ||
wf=self.decode(NASA_AUDIO_MP3), | ||
sample_rate=NASA_AUDIO_MP3.sample_rate, | ||
filename=valid_output_file, | ||
bit_rate=-1, # bad | ||
) | ||
|
||
def test_round_trip(self, tmp_path): | ||
# Check that decode(encode(samples)) == samples | ||
|
@@ -1127,28 +1134,26 @@ def test_round_trip(self, tmp_path): | |
) | ||
encode_audio(encoder) | ||
|
||
# TODO-ENCODING: tol should be stricter. We need to increase the encoded | ||
# bitrate, and / or encode into a lossless format. | ||
# TODO-ENCODING: tol should be stricter. We probably need to encode | ||
# into a lossless format. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to pass bit_rate=40_000 and was able to decrease tolerance from 0.07 to 0.05, but that's clearly not strict enough still. |
||
torch.testing.assert_close( | ||
self.decode(encoded_path), source_samples, rtol=0, atol=0.07 | ||
) | ||
|
||
# TODO-ENCODING: test more encoding formats | ||
@pytest.mark.parametrize("asset", (NASA_AUDIO_MP3, SINE_MONO_S32)) | ||
def test_against_cli(self, asset, tmp_path): | ||
@pytest.mark.parametrize("bit_rate", (None, 0, 44_100, 999_999_999)) | ||
def test_against_cli(self, asset, bit_rate, tmp_path): | ||
# Encodes samples with our encoder and with the FFmpeg CLI, and checks | ||
# that both decoded outputs are equal | ||
|
||
encoded_by_ffmpeg = tmp_path / "ffmpeg_output.mp3" | ||
encoded_by_us = tmp_path / "our_output.mp3" | ||
|
||
subprocess.run( | ||
[ | ||
"ffmpeg", | ||
"-i", | ||
str(asset.path), | ||
"-b:a", | ||
"0", # bitrate hardcoded to 0, see corresponding TODO. | ||
["ffmpeg", "-i", str(asset.path)] | ||
+ (["-b:a", f"{bit_rate}"] if bit_rate is not None else []) | ||
+ [ | ||
str(encoded_by_ffmpeg), | ||
], | ||
capture_output=True, | ||
|
@@ -1159,6 +1164,7 @@ def test_against_cli(self, asset, tmp_path): | |
wf=self.decode(asset), | ||
sample_rate=asset.sample_rate, | ||
filename=str(encoded_by_us), | ||
bit_rate=bit_rate, | ||
) | ||
encode_audio(encoder) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I don't think there is a way to list the supported bit rates of the encoder. It's possible for sample rates, but not for bit rates.