diff --git a/stimuli/audio/_base.py b/stimuli/audio/_base.py index 65311c1..09a13af 100644 --- a/stimuli/audio/_base.py +++ b/stimuli/audio/_base.py @@ -75,7 +75,7 @@ def __init__( # the arguments sample_rate, device, clock, and **kwargs are checked in # the backend initialization. self._backend_kwargs = kwargs - self._backend = BACKENDS[backend](sample_rate, device, clock=clock) + self._backend = BACKENDS[backend](device, sample_rate, clock=clock) self._set_times() self._set_signal() diff --git a/stimuli/audio/sound.py b/stimuli/audio/sound.py index 25a0bf8..44dfb3d 100644 --- a/stimuli/audio/sound.py +++ b/stimuli/audio/sound.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING +import numpy as np from scipy.io import wavfile from ..time import Clock @@ -46,14 +47,14 @@ def __init__( sample_rate, original_signal = wavfile.read(self._fname) _check_signal(original_signal) volume = _extract_volume(original_signal) - signal = _ensure_signal(original_signal) - duration = signal.shape[0] / sample_rate + self._original_signal = _ensure_signal(original_signal) + duration = self._original_signal.shape[0] / sample_rate super().__init__( volume, duration, sample_rate, device, - signal.shape[1] if signal.ndim == 2 else 1, + self._original_signal.shape[1] if self._original_signal.ndim == 2 else 1, backend=backend, clock=clock, **kwargs, @@ -61,8 +62,7 @@ def __init__( @copy_doc(BaseSound._set_signal) def _set_signal(self) -> None: - signal = self._original_signal * self._volume / 100 - super()._set_signal(signal) + super()._set_signal(self._original_signal) @BaseSound.duration.setter def duration(self, duration: float): # noqa: D102 diff --git a/stimuli/audio/tests/test_sound.py b/stimuli/audio/tests/test_sound.py new file mode 100644 index 0000000..269e2ca --- /dev/null +++ b/stimuli/audio/tests/test_sound.py @@ -0,0 +1,17 @@ +from numpy.testing import assert_allclose + +from stimuli.audio import Noise, Sound + + +def test_sound_io_mono(tmp_path): + """Test sound saving/loading.""" + sound = Noise("pink", volume=100, duration=0.5) + sound.save(tmp_path / "test.wav") + sound_loaded = Sound(tmp_path / "test.wav") + assert_allclose(sound.signal, sound_loaded.signal) + + sound = Noise("pink", volume=50, duration=0.5) + sound.save(tmp_path / "test.wav", overwrite=True) + sound_loaded = Sound(tmp_path / "test.wav") + sound_loaded.volume = 50 + assert_allclose(sound.signal, sound_loaded.signal)