commit 3cf105d0917244537521eec56f4a7df6847d6ea0
parent 0a5802d200bb784ea23e916247f7d09b9650014c
Author: Kostas Emmanouilidis <[email protected]>
Date: Sun, 11 Jun 2023 07:16:03 +0200
Support wavio latest (#222)
* Add support for wavio.write new kwargs
* Update requirements.txt
* Update setup.py
* Update environment_cpu.yml
* Update environment_gpu.yml
* Updated np_to_wav function on data.py to set default value for 'scale' argument based on wavio module version. 'clip' set by **kwargs for versions >= 0.0.5
* Removed wavio version constraint on requirements.txt.
Added TestWav test cases for np to wav round test.
* Support Python 2 testing for TestWav
* Used temp directory for test.wav file. Used pytest.approx with abs tolerance for test assertions.
---------
Co-authored-by: Steven Atkinson <[email protected]>
Diffstat:
6 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/environment_cpu.yml b/environment_cpu.yml
@@ -31,5 +31,5 @@ dependencies:
- pre-commit
- pytorch_lightning
- sounddevice
- - wavio
+ - wavio >=0.0.5
- -e .
diff --git a/environment_gpu.yml b/environment_gpu.yml
@@ -33,5 +33,5 @@ dependencies:
- pre-commit
- pytorch_lightning
- sounddevice
- - wavio
+ - wavio >=0.0.5
- -e .
diff --git a/nam/data.py b/nam/data.py
@@ -122,14 +122,18 @@ def np_to_wav(
filename: Union[str, Path],
rate: int = 48_000,
sampwidth: int = 3,
- scale="none",
+ scale = None,
+ **kwargs
):
+ if wavio.__version__ <= "0.0.4" and scale is None:
+ scale = "none"
wavio.write(
str(filename),
(np.clip(x, -1.0, 1.0) * (2 ** (8 * sampwidth - 1))).astype(np.int32),
rate,
scale=scale,
sampwidth=sampwidth,
+ **kwargs
)
diff --git a/requirements.txt b/requirements.txt
@@ -19,5 +19,5 @@ scipy
sounddevice
torch
tqdm
-wavio<=0.0.4 # Breaking change in 0.0.5
+wavio
wheel
diff --git a/setup.py b/setup.py
@@ -21,7 +21,7 @@ requirements = [
"tensorboard",
"torch",
"tqdm",
- "wavio<=0.0.4", # Breaking change in 0.0.5
+ "wavio>=0.0.5", # Breaking change with older versions
]
setup(
diff --git a/tests/test_nam/test_data.py b/tests/test_nam/test_data.py
@@ -3,6 +3,7 @@
# Author: Steven Atkinson ([email protected])
import math
+import os
from enum import Enum
from pathlib import Path
from tempfile import TemporaryDirectory
@@ -243,6 +244,47 @@ class TestDataset(object):
return x_out, y_out
+class TestWav(object):
+ tolerance = 1e-6
+
+ @pytest.fixture(scope="class")
+ def tmpdir(self):
+ with TemporaryDirectory() as tmp:
+ yield tmp
+
+ def test_np_to_wav_to_np(self, tmpdir):
+ # Create random numpy array
+ x = np.random.rand(1000)
+ # Save numpy array as WAV file
+ filename = os.path.join(tmpdir, "test.wav")
+ data.np_to_wav(x, filename)
+ # Load WAV file
+ y = data.wav_to_np(filename)
+ # Check if the two arrays are equal
+ assert y == pytest.approx(x, abs=self.tolerance)
+
+ def test_np_to_wav_to_np_44khz(self, tmpdir):
+ # Create random numpy array
+ x = np.random.rand(1000)
+ # Save numpy array as WAV file with sampling rate of 44 kHz
+ filename = os.path.join(tmpdir, "test.wav")
+ data.np_to_wav(x, filename, rate=44100)
+ # Load WAV file with sampling rate of 44 kHz
+ y = data.wav_to_np(filename, rate=44100)
+ # Check if the two arrays are equal
+ assert y == pytest.approx(x, abs=self.tolerance)
+
+ def test_np_to_wav_to_np_scale_arg(self, tmpdir):
+ # Create random numpy array
+ x = np.random.rand(100)
+ # Save numpy array as WAV file with scaling
+ filename = os.path.join(tmpdir, "test.wav")
+ data.np_to_wav(x, filename, scale=None)
+ # Load WAV file
+ y = data.wav_to_np(filename)
+ # Check if the two arrays are equal
+ assert y == pytest.approx(x, abs=self.tolerance)
+
def test_audio_mismatch_shapes_in_order():
"""
https://github.com/sdatkinson/neural-amp-modeler/issues/257
@@ -267,6 +309,7 @@ def test_audio_mismatch_shapes_in_order():
# x is loaded first; we expect that y matches.
assert e.shape_expected == (x_samples, num_channels)
assert e.shape_actual == (y_samples, num_channels)
+
if __name__ == "__main__":
pytest.main()