Skip to content
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

fix inconsistent handling of 8-bit audio #439

Merged
merged 9 commits into from
Feb 6, 2020

Conversation

GreyAlien502
Copy link
Collaborator

This solves the problem where 8-bit audio is inconsistently treated as unsigned and signed integers.
This fix converts to and from signed integers when reading or writing 8-bit wav files.

The rms and set_sample_width methods of the AudioSegment class tried to handle the data as unsigned,
but this would not be enough, since audioop's methods all treat the audio like signed integers.

The codec used for reading from 8-bit wav files is changed to pcm_u8 from pcm_s8.

This will fix issues #417. #367, and the garbled audio problem mentioned in #381.

Tests were added to compare the RMS values for 8-bit audio segments created from a variety of ways to ensure they are all the same.

GreyAlien502 and others added 7 commits January 14, 2020 12:29
It was not actually checking anything.
tolerance=0 should mean they have to exactly equal.
It was just a bit more than 1% off.
Since AudioSegment.dBFS calculates the value based on the amplitude,
we should compare against that in our tests.
The expected value for the 8-bit test had to be changed because audioop.rms truncates its result to an integer,
and so the dBFS does not precisely match the value for the 16-bit version.
The RMS values for 8-bit AudioSegments created a variety of ways are compared.
This solves the issue where 8-bit audio is inconsistently treated as unsigned and signed integers.
The audioop library only supports signed integers, so this fix converts to and from signed integers where necessary.

It removes places where the values are treated like unsigned as these partial fixes are no longer necessary.
This includes special handling in the rms and set_sample_width methods of the AudioSegment class.

It changes the codec used for reading from 8-bit wav files to pcm_u8 from pcm_s8
since pcm_u8 is the only valid codec for 8-bit wav files.
@jiaaro
Copy link
Owner

jiaaro commented Jan 31, 2020

This looks great - would you be able to address the failing unit tests?

The test was requiring more precision than we can expect from mp3.
The tolerance is changed to 1.5dB as used elsewhere in the code.
@GreyAlien502
Copy link
Collaborator Author

Okay, i've fixed the tests.
I guess you are fine with merging all these changes in one pull request,
so i'll go ahead and close the one i made for just the tests.

@GreyAlien502 GreyAlien502 reopened this Feb 6, 2020
@GreyAlien502
Copy link
Collaborator Author

This also fixes issue #409.

@jiaaro jiaaro merged commit 0908a38 into jiaaro:master Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants