Skip to content

Commit

Permalink
Further simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Jan 26, 2024
1 parent c46be1b commit 50de14e
Showing 1 changed file with 11 additions and 40 deletions.
51 changes: 11 additions & 40 deletions tests/test_audiofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,6 @@ def test_convert_to_wav(tmpdir, normalize, bit_depth, file_extension):
normalize=normalize,
overwrite=True,
)
elif file_extension == 'mp3':
outfile = af.convert_to_wav(
infile,
bit_depth=bit_depth,
normalize=normalize,
)
else:
outfile = str(tmpdir.join('signal_converted.wav'))
af.convert_to_wav(
Expand Down Expand Up @@ -491,47 +485,18 @@ def test_file_type(tmpdir, file_type, magnitude, sampling_rate, channels):
assert info.samplerate == sampling_rate
assert _channels(sig) == channels
assert info.channels == channels
if channels == 1:
assert sig.ndim == 1
else:
assert sig.ndim == 2
assert _samples(sig) == _samples(signal)
assert info.frames == _samples(signal)
if file_type in ['mp3', 'ogg']:
bit_depth = None
assert af.bit_depth(file) == bit_depth


@pytest.mark.parametrize('sampling_rate', [8000, 48000])
@pytest.mark.parametrize("channels", [1, 2])
@pytest.mark.parametrize('magnitude', [0.01])
def test_mp3(tmpdir, magnitude, sampling_rate, channels):

signal = sine(magnitude=magnitude,
sampling_rate=sampling_rate,
channels=channels)
# Create wav file and use ffmpeg to convert to mp3
wav_file = str(tmpdir.join('signal.wav'))
mp3_file = str(tmpdir.join('signal.mp3'))
af.write(wav_file, signal, sampling_rate)
af.write(mp3_file, signal, sampling_rate)
sig, fs = af.read(mp3_file)
assert fs == sampling_rate
assert _channels(sig) == channels
if channels == 1:
assert sig.ndim == 1
else:
assert sig.ndim == 2
assert af.channels(mp3_file) == _channels(sig)
assert af.sampling_rate(mp3_file) == sampling_rate
assert af.samples(mp3_file) == _samples(sig)
assert af.duration(mp3_file) == _duration(sig, sampling_rate)
assert_allclose(
af.duration(mp3_file, sloppy=True),
_duration(sig, sampling_rate),
rtol=0,
atol=0.2,
)
assert af.bit_depth(mp3_file) is None


def test_formats():
def test_other_formats():
files = [
'gs-16b-1c-44100hz.opus',
'gs-16b-1c-8000hz.amr',
Expand Down Expand Up @@ -1322,6 +1287,12 @@ def test_write_errors():
)
with pytest.raises(RuntimeError, match=expected_error):
write_and_read('test.flac', np.zeros((9, 100)), sampling_rate)
expected_error = (
"The maximum number of allowed channels "
"for 'mp3' is 2. Consider using 'wav' instead."
)
with pytest.raises(RuntimeError, match=expected_error):
write_and_read('test.mp3', np.zeros((3, 100)), sampling_rate)
expected_error = (
"The maximum number of allowed channels "
"for 'ogg' is 255. Consider using 'wav' instead."
Expand Down

0 comments on commit 50de14e

Please sign in to comment.