Skip to content

Commit

Permalink
add I/O tests on mono sound and fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
mscheltienne committed Sep 18, 2024
1 parent 871a4eb commit 8a52a4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion stimuli/audio/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 5 additions & 5 deletions stimuli/audio/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING

import numpy as np
from scipy.io import wavfile

from ..time import Clock
Expand Down Expand Up @@ -46,23 +47,22 @@ 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,
)

@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
Expand Down
17 changes: 17 additions & 0 deletions stimuli/audio/tests/test_sound.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 8a52a4b

Please sign in to comment.