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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests on 8-bit audio
The RMS values for 8-bit AudioSegments created a variety of ways are compared.
  • Loading branch information
GreyAlien502 committed Jan 20, 2020
commit 9e292e0340533a91026fcab02b4acab78db41fc9
Binary file added test/data/test1-8bit.wav
Binary file not shown.
26 changes: 26 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,32 @@ def test_24_bit_audio(self):
# the data length should have grown by exactly 4:3 (24 bits turn into 32 bits)
self.assertEqual(len(seg24.raw_data) * 3, len24 * 4)

def test_8_bit_audio(self):
original_path = os.path.join(data_dir,'test1.wav')
original_segment = AudioSegment.from_file(original_path)
target_rms = original_segment.rms//2**8

path_with_8bits = os.path.join(data_dir,'test1-8bit.wav')

def check_8bit_segment(segment):
self.assertWithinTolerance(segment.rms,target_rms,tolerance=0)

# check reading directly
check_8bit_segment(AudioSegment.from_file(path_with_8bits))

# check using ffmpeg on it
with open(path_with_8bits,'rb') as file_8bit:
check_8bit_segment(AudioSegment.from_file(file_8bit))

# check conversion from higher-width sample
check_8bit_segment(AudioSegment.from_file(original_path).set_sample_width(1))

# check audio export
with NamedTemporaryFile('w+b', suffix='.wav') as tmp_file:
original_segment.set_sample_width(1).export(tmp_file,format='wav')
tmp_file.seek(0)
check_8bit_segment(AudioSegment.from_file(tmp_file))

def test_192khz_audio(self):
test_files = [('test-192khz-16bit.wav', 16),
('test-192khz-24bit.wav', 32),
Expand Down