|
import os |
|
import sys |
|
from io import BytesIO |
|
import threading |
|
|
|
import numpy as np |
|
from numpy.testing import (assert_equal, assert_, assert_array_equal, |
|
break_cycles, suppress_warnings, IS_PYPY) |
|
import pytest |
|
from pytest import raises, warns |
|
|
|
from scipy.io import wavfile |
|
|
|
|
|
def datafile(fn): |
|
return os.path.join(os.path.dirname(__file__), 'data', fn) |
|
|
|
|
|
def test_read_1(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-44100Hz-le-1ch-4bytes.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 44100) |
|
assert_(np.issubdtype(data.dtype, np.int32)) |
|
assert_equal(data.shape, (4410,)) |
|
|
|
del data |
|
|
|
|
|
def test_read_2(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-8000Hz-le-2ch-1byteu.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.uint8)) |
|
assert_equal(data.shape, (800, 2)) |
|
|
|
del data |
|
|
|
|
|
def test_read_3(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-44100Hz-2ch-32bit-float-le.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 44100) |
|
assert_(np.issubdtype(data.dtype, np.float32)) |
|
assert_equal(data.shape, (441, 2)) |
|
|
|
del data |
|
|
|
|
|
def test_read_4(): |
|
|
|
for mmap in [False, True]: |
|
with suppress_warnings() as sup: |
|
sup.filter(wavfile.WavFileWarning, |
|
"Chunk .non-data. not understood, skipping it") |
|
filename = 'test-48000Hz-2ch-64bit-float-le-wavex.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 48000) |
|
assert_(np.issubdtype(data.dtype, np.float64)) |
|
assert_equal(data.shape, (480, 2)) |
|
|
|
del data |
|
|
|
|
|
def test_read_5(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-44100Hz-2ch-32bit-float-be.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 44100) |
|
assert_(np.issubdtype(data.dtype, np.float32)) |
|
assert_(data.dtype.byteorder == '>' or (sys.byteorder == 'big' and |
|
data.dtype.byteorder == '=')) |
|
assert_equal(data.shape, (441, 2)) |
|
|
|
del data |
|
|
|
|
|
def test_5_bit_odd_size_no_pad(): |
|
|
|
|
|
|
|
for mmap in [False, True]: |
|
filename = 'test-8000Hz-le-5ch-9S-5bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.uint8)) |
|
assert_equal(data.shape, (9, 5)) |
|
|
|
|
|
assert_equal(data & 0b00000111, 0) |
|
|
|
|
|
assert_equal(data.max(), 0b11111000) |
|
assert_equal(data[0, 0], 128) |
|
assert_equal(data.min(), 0) |
|
|
|
del data |
|
|
|
|
|
def test_12_bit_even_size(): |
|
|
|
|
|
for mmap in [False, True]: |
|
filename = 'test-8000Hz-le-4ch-9S-12bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.int16)) |
|
assert_equal(data.shape, (9, 4)) |
|
|
|
|
|
assert_equal(data & 0b00000000_00001111, 0) |
|
|
|
|
|
assert_equal(data.max(), 0b01111111_11110000) |
|
assert_equal(data[0, 0], 0) |
|
assert_equal(data.min(), -0b10000000_00000000) |
|
|
|
del data |
|
|
|
|
|
def test_24_bit_odd_size_with_pad(): |
|
|
|
|
|
filename = 'test-8000Hz-le-3ch-5S-24bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=False) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.int32)) |
|
assert_equal(data.shape, (5, 3)) |
|
|
|
|
|
assert_equal(data & 0xff, 0) |
|
|
|
|
|
|
|
assert_equal(data, [[-0x8000_0000, -0x7fff_ff00, -0x200], |
|
[-0x4000_0000, -0x3fff_ff00, -0x100], |
|
[+0x0000_0000, +0x0000_0000, +0x000], |
|
[+0x4000_0000, +0x3fff_ff00, +0x100], |
|
[+0x7fff_ff00, +0x7fff_ff00, +0x200]]) |
|
|
|
|
|
|
|
def test_20_bit_extra_data(): |
|
|
|
|
|
filename = 'test-1234Hz-le-1ch-10S-20bit-extra.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=False) |
|
|
|
assert_equal(rate, 1234) |
|
assert_(np.issubdtype(data.dtype, np.int32)) |
|
assert_equal(data.shape, (10,)) |
|
|
|
|
|
assert_equal(data & 0xff, 0) |
|
|
|
|
|
assert_((data & 0xf00).any()) |
|
|
|
|
|
assert_equal(data, [+0x7ffff000, |
|
-0x7ffff000, |
|
+0x7ffff000 >> 1, |
|
-0x7ffff000 >> 1, |
|
+0x7ffff000 >> 2, |
|
-0x7ffff000 >> 2, |
|
+0x7ffff000 >> 3, |
|
-0x7ffff000 >> 3, |
|
+0x7ffff000 >> 4, |
|
-0x7ffff000 >> 4, |
|
]) |
|
|
|
|
|
def test_36_bit_odd_size(): |
|
|
|
filename = 'test-8000Hz-le-3ch-5S-36bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=False) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.int64)) |
|
assert_equal(data.shape, (5, 3)) |
|
|
|
|
|
assert_equal(data & 0xfffffff, 0) |
|
|
|
|
|
|
|
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_f000_0000, -0x2000_0000], |
|
[-0x4000_0000_0000_0000, -0x3fff_ffff_f000_0000, -0x1000_0000], |
|
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0000_0000], |
|
[+0x4000_0000_0000_0000, +0x3fff_ffff_f000_0000, +0x1000_0000], |
|
[+0x7fff_ffff_f000_0000, +0x7fff_ffff_f000_0000, +0x2000_0000]] |
|
|
|
|
|
assert_equal(data, correct) |
|
|
|
|
|
def test_45_bit_even_size(): |
|
|
|
filename = 'test-8000Hz-le-3ch-5S-45bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=False) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.int64)) |
|
assert_equal(data.shape, (5, 3)) |
|
|
|
|
|
assert_equal(data & 0x7ffff, 0) |
|
|
|
|
|
|
|
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_fff8_0000, -0x10_0000], |
|
[-0x4000_0000_0000_0000, -0x3fff_ffff_fff8_0000, -0x08_0000], |
|
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x00_0000], |
|
[+0x4000_0000_0000_0000, +0x3fff_ffff_fff8_0000, +0x08_0000], |
|
[+0x7fff_ffff_fff8_0000, +0x7fff_ffff_fff8_0000, +0x10_0000]] |
|
|
|
|
|
assert_equal(data, correct) |
|
|
|
|
|
def test_53_bit_odd_size(): |
|
|
|
filename = 'test-8000Hz-le-3ch-5S-53bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=False) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.int64)) |
|
assert_equal(data.shape, (5, 3)) |
|
|
|
|
|
assert_equal(data & 0x7ff, 0) |
|
|
|
|
|
|
|
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_ffff_f800, -0x1000], |
|
[-0x4000_0000_0000_0000, -0x3fff_ffff_ffff_f800, -0x0800], |
|
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0000], |
|
[+0x4000_0000_0000_0000, +0x3fff_ffff_ffff_f800, +0x0800], |
|
[+0x7fff_ffff_ffff_f800, +0x7fff_ffff_ffff_f800, +0x1000]] |
|
|
|
|
|
assert_equal(data, correct) |
|
|
|
|
|
def test_64_bit_even_size(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-8000Hz-le-3ch-5S-64bit.wav' |
|
rate, data = wavfile.read(datafile(filename), mmap=mmap) |
|
|
|
assert_equal(rate, 8000) |
|
assert_(np.issubdtype(data.dtype, np.int64)) |
|
assert_equal(data.shape, (5, 3)) |
|
|
|
|
|
|
|
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_ffff_ffff, -0x2], |
|
[-0x4000_0000_0000_0000, -0x3fff_ffff_ffff_ffff, -0x1], |
|
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0], |
|
[+0x4000_0000_0000_0000, +0x3fff_ffff_ffff_ffff, +0x1], |
|
[+0x7fff_ffff_ffff_ffff, +0x7fff_ffff_ffff_ffff, +0x2]] |
|
|
|
|
|
assert_equal(data, correct) |
|
|
|
del data |
|
|
|
|
|
def test_unsupported_mmap(): |
|
|
|
for filename in {'test-8000Hz-le-3ch-5S-24bit.wav', |
|
'test-8000Hz-le-3ch-5S-36bit.wav', |
|
'test-8000Hz-le-3ch-5S-45bit.wav', |
|
'test-8000Hz-le-3ch-5S-53bit.wav', |
|
'test-1234Hz-le-1ch-10S-20bit-extra.wav'}: |
|
with raises(ValueError, match="mmap.*not compatible"): |
|
rate, data = wavfile.read(datafile(filename), mmap=True) |
|
|
|
|
|
def test_rifx(): |
|
|
|
for rifx, riff in {('test-44100Hz-be-1ch-4bytes.wav', |
|
'test-44100Hz-le-1ch-4bytes.wav'), |
|
('test-8000Hz-be-3ch-5S-24bit.wav', |
|
'test-8000Hz-le-3ch-5S-24bit.wav')}: |
|
rate1, data1 = wavfile.read(datafile(rifx), mmap=False) |
|
rate2, data2 = wavfile.read(datafile(riff), mmap=False) |
|
assert_equal(rate1, rate2) |
|
assert_equal(data1, data2) |
|
|
|
|
|
def test_rf64(): |
|
|
|
for rf64, riff in {('test-44100Hz-le-1ch-4bytes-rf64.wav', |
|
'test-44100Hz-le-1ch-4bytes.wav'), |
|
('test-8000Hz-le-3ch-5S-24bit-rf64.wav', |
|
'test-8000Hz-le-3ch-5S-24bit.wav')}: |
|
rate1, data1 = wavfile.read(datafile(rf64), mmap=False) |
|
rate2, data2 = wavfile.read(datafile(riff), mmap=False) |
|
assert_array_equal(rate1, rate2) |
|
assert_array_equal(data1, data2) |
|
|
|
|
|
@pytest.mark.xslow |
|
def test_write_roundtrip_rf64(tmpdir): |
|
dtype = np.dtype("<i8") |
|
tmpfile = str(tmpdir.join('temp.wav')) |
|
rate = 44100 |
|
data = np.random.randint(0, 127, (2**29,)).astype(dtype) |
|
|
|
wavfile.write(tmpfile, rate, data) |
|
|
|
rate2, data2 = wavfile.read(tmpfile, mmap=True) |
|
|
|
assert_equal(rate, rate2) |
|
msg = f"{data2.dtype} byteorder not in ('<', '=', '|')" |
|
assert data2.dtype.byteorder in ('<', '=', '|'), msg |
|
assert_array_equal(data, data2) |
|
|
|
data2[0] = 0 |
|
|
|
|
|
def test_read_unknown_filetype_fail(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'example_1.nc' |
|
with open(datafile(filename), 'rb') as fp: |
|
with raises(ValueError, match="CDF.*'RIFF', 'RIFX', and 'RF64' supported"): |
|
wavfile.read(fp, mmap=mmap) |
|
|
|
|
|
def test_read_unknown_riff_form_type(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'Transparent Busy.ani' |
|
with open(datafile(filename), 'rb') as fp: |
|
with raises(ValueError, match='Not a WAV file.*ACON'): |
|
wavfile.read(fp, mmap=mmap) |
|
|
|
|
|
def test_read_unknown_wave_format(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-8000Hz-le-1ch-1byte-ulaw.wav' |
|
with open(datafile(filename), 'rb') as fp: |
|
with raises(ValueError, match='Unknown wave file format.*MULAW.*' |
|
'Supported formats'): |
|
wavfile.read(fp, mmap=mmap) |
|
|
|
|
|
@pytest.mark.thread_unsafe |
|
def test_read_early_eof_with_data(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-44100Hz-le-1ch-4bytes-early-eof.wav' |
|
with open(datafile(filename), 'rb') as fp: |
|
with warns(wavfile.WavFileWarning, match='Reached EOF'): |
|
rate, data = wavfile.read(fp, mmap=mmap) |
|
assert data.size > 0 |
|
assert rate == 44100 |
|
|
|
data[0] = 0 |
|
|
|
|
|
def test_read_early_eof(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-44100Hz-le-1ch-4bytes-early-eof-no-data.wav' |
|
with open(datafile(filename), 'rb') as fp: |
|
with raises(ValueError, match="Unexpected end of file."): |
|
wavfile.read(fp, mmap=mmap) |
|
|
|
|
|
def test_read_incomplete_chunk(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-44100Hz-le-1ch-4bytes-incomplete-chunk.wav' |
|
with open(datafile(filename), 'rb') as fp: |
|
with raises(ValueError, match="Incomplete chunk ID.*b'f'"): |
|
wavfile.read(fp, mmap=mmap) |
|
|
|
|
|
def test_read_inconsistent_header(): |
|
|
|
for mmap in [False, True]: |
|
filename = 'test-8000Hz-le-3ch-5S-24bit-inconsistent.wav' |
|
with open(datafile(filename), 'rb') as fp: |
|
with raises(ValueError, match="header is invalid"): |
|
wavfile.read(fp, mmap=mmap) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("dt_str", ["<i2", "<i4", "<i8", "<f4", "<f8", |
|
">i2", ">i4", ">i8", ">f4", ">f8", '|u1']) |
|
@pytest.mark.parametrize("channels", [1, 2, 5]) |
|
@pytest.mark.parametrize("rate", [8000, 32000]) |
|
@pytest.mark.parametrize("mmap", [False, True]) |
|
@pytest.mark.parametrize("realfile", [False, True]) |
|
def test_write_roundtrip(realfile, mmap, rate, channels, dt_str, tmpdir): |
|
dtype = np.dtype(dt_str) |
|
if realfile: |
|
tmpfile = str(tmpdir.join(str(threading.get_native_id()), 'temp.wav')) |
|
os.makedirs(os.path.dirname(tmpfile), exist_ok=True) |
|
else: |
|
tmpfile = BytesIO() |
|
data = np.random.rand(100, channels) |
|
if channels == 1: |
|
data = data[:, 0] |
|
if dtype.kind == 'f': |
|
|
|
data = data.astype(dtype) |
|
else: |
|
data = (data*128).astype(dtype) |
|
|
|
wavfile.write(tmpfile, rate, data) |
|
|
|
rate2, data2 = wavfile.read(tmpfile, mmap=mmap) |
|
|
|
assert_equal(rate, rate2) |
|
assert_(data2.dtype.byteorder in ('<', '=', '|'), msg=data2.dtype) |
|
assert_array_equal(data, data2) |
|
|
|
if realfile: |
|
data2[0] = 0 |
|
else: |
|
with pytest.raises(ValueError, match='read-only'): |
|
data2[0] = 0 |
|
|
|
if realfile and mmap and IS_PYPY and sys.platform == 'win32': |
|
|
|
|
|
break_cycles() |
|
break_cycles() |
|
|
|
|
|
@pytest.mark.parametrize("dtype", [np.float16]) |
|
def test_wavfile_dtype_unsupported(tmpdir, dtype): |
|
tmpfile = str(tmpdir.join('temp.wav')) |
|
rng = np.random.default_rng(1234) |
|
data = rng.random((100, 5)).astype(dtype) |
|
rate = 8000 |
|
with pytest.raises(ValueError, match="Unsupported"): |
|
wavfile.write(tmpfile, rate, data) |
|
|