listeningpy.stimuli

Module for playing back stimuli.

 1"""Module for playing back stimuli."""
 2from soundfile import SoundFile
 3from sounddevice import play
 4from numpy import ndarray
 5import logging
 6from typing import Callable
 7from listeningpy.processing import straight
 8
 9# logging.basicConfig(level=logging.INFO)
10
11def play_sound(
12        path: str=None,
13        sound: ndarray=None,
14        processing_func: Callable[[ndarray, int], ndarray]=straight,
15        fs: int=None,
16        **kwargs
17        ) -> None:
18    '''Opens the file, performs requested processing and plays it back.
19
20    This will be probably (not tested yet) useful for no or light-weight
21    signal processing. Heavy-weight processing (convolution, etc.) should
22    be probably prebaked when moving to another step inside
23    a listening test.
24
25    Parameters
26    ----------
27    path : str
28        path to a Wave file
29    processing_func : function
30        processing to be performed in real time on a sound
31    '''
32    if path is not None:
33        with SoundFile(path, 'r') as f:
34            logging.info(f'File {path} was succesfully opened.')
35            sound = f.read(always_2d=True)
36            sound, fs = processing_func(sound, f.samplerate, **kwargs)
37            play(sound, fs)
38            logging.info(f'Stimuli of a of a shape {sound.shape} was played.')
39    elif sound is not None and fs is not None:
40        play(sound, fs)
41        logging.info(f'Provided stimuli of a shape {sound.shape} was' +
42            ' played without further processing.')
43    else:
44        logging.warning('You need to specify either path to soundfile' + 
45            ' or sound and its sample rate.')
def play_sound( path: str = None, sound: numpy.ndarray = None, processing_func: Callable[[numpy.ndarray, int], numpy.ndarray] = <function straight>, fs: int = None, **kwargs) -> None:
12def play_sound(
13        path: str=None,
14        sound: ndarray=None,
15        processing_func: Callable[[ndarray, int], ndarray]=straight,
16        fs: int=None,
17        **kwargs
18        ) -> None:
19    '''Opens the file, performs requested processing and plays it back.
20
21    This will be probably (not tested yet) useful for no or light-weight
22    signal processing. Heavy-weight processing (convolution, etc.) should
23    be probably prebaked when moving to another step inside
24    a listening test.
25
26    Parameters
27    ----------
28    path : str
29        path to a Wave file
30    processing_func : function
31        processing to be performed in real time on a sound
32    '''
33    if path is not None:
34        with SoundFile(path, 'r') as f:
35            logging.info(f'File {path} was succesfully opened.')
36            sound = f.read(always_2d=True)
37            sound, fs = processing_func(sound, f.samplerate, **kwargs)
38            play(sound, fs)
39            logging.info(f'Stimuli of a of a shape {sound.shape} was played.')
40    elif sound is not None and fs is not None:
41        play(sound, fs)
42        logging.info(f'Provided stimuli of a shape {sound.shape} was' +
43            ' played without further processing.')
44    else:
45        logging.warning('You need to specify either path to soundfile' + 
46            ' or sound and its sample rate.')

Opens the file, performs requested processing and plays it back.

This will be probably (not tested yet) useful for no or light-weight signal processing. Heavy-weight processing (convolution, etc.) should be probably prebaked when moving to another step inside a listening test.

Parameters
  • path (str): path to a Wave file
  • processing_func (function): processing to be performed in real time on a sound