Beispiel #1
0
    def _validate_reverb_name(self, reverb_name_tuple):

        # Make sure that type matches
        def __validate_reverb_name_types(reverb_name):
            if not isinstance(reverb_name, str):
                raise AmbiScaperError(
                    'reverb_config: reverb name must be a string')

        # Make sure that the sofa file exists and is valid
        def __validate_reverb_name_configuration(reverb_name):

            reverb_full_path = self.sofa_reverb_folder_path + '/' + reverb_name

            # The provided name should exist in sofa_reverb_folder_path
            if not os.path.exists(os.path.expanduser(reverb_full_path)):
                raise AmbiScaperError('reverb_config: file does not exist: ' +
                                      reverb_full_path)

            # TODO
            # The file should be a valid AmbisonicsDRIR file
            sofa_file = pysofaconventions.SOFAAmbisonicsDRIR(
                reverb_full_path, 'r')
            if not sofa_file.isValid():
                sofa_file.close()
                raise AmbiScaperError(
                    'reverb_config: file is not a valid AmbisonicsDRIR SOFA file: '
                    + reverb_name)
            sofa_file.close()

        # Make sure it's a valid distribution tuple
        _validate_distribution(reverb_name_tuple)

        # If reverb name is specified explicitly
        if reverb_name_tuple[0] == "const":

            # reverb name: allowed string
            if reverb_name_tuple[1] is None:
                raise AmbiScaperError('reverb_config: reverb name is None')
            __validate_reverb_name_types(reverb_name_tuple[1])
            __validate_reverb_name_configuration(reverb_name_tuple[1])

        # Otherwise it must be specified using "choose"
        # Empty list is allowed, meaning all avaiable IRS
        elif reverb_name_tuple[0] == "choose":
            # Empty list
            [
                __validate_reverb_name_types(name)
                for name in reverb_name_tuple[1]
            ]
            [
                __validate_reverb_name_configuration(name)
                for name in reverb_name_tuple[1]
            ]

        # No other labels allowed"
        else:
            raise AmbiScaperError(
                'Reverb name must be specified using a "const" or "choose" tuple.'
            )
Beispiel #2
0
    def _validate_reverb_wrap(self, reverb_wrap_tuple):
        '''

        :param reverb_wrap:
        :return:
        '''

        # Make sure it's a valid distribution tuple
        _validate_distribution(reverb_wrap_tuple)

        # Make sure that type matches
        def __valid_reverb_wrap_types(reverb_wrap):
            if (not isinstance(reverb_wrap, str)):
                return False
            else:
                return True

        def __valid_reverb_wrap_values(reverb_wrap):
            if reverb_wrap not in self.valid_wrap_values:
                return False
            else:
                return True

        # If reverb wrap is specified explicitly
        if reverb_wrap_tuple[0] == "const":

            # reverb wrap: allowed string
            if reverb_wrap_tuple[1] is None:
                raise AmbiScaperError('reverb_config: reverb wrap is None')
            elif not __valid_reverb_wrap_types(reverb_wrap_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: reverb wrap must be a string')
            elif not __valid_reverb_wrap_values(reverb_wrap_tuple[1]):
                raise AmbiScaperError('reverb_config: reverb wrap not valid:' +
                                      reverb_wrap_tuple[1])

        # Otherwise it must be specified using "choose"
        # Empty list is allowed, meaning all avaiable IRS
        elif reverb_wrap_tuple[0] == "choose":

            if not all(
                    __valid_reverb_wrap_types(length)
                    for length in reverb_wrap_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: reverb wrap must be a string')
            elif not all(
                    __valid_reverb_wrap_values(name)
                    for name in reverb_wrap_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: reverb names not valid: ' +
                    str(reverb_wrap_tuple[1]))

        # No other labels allowed"
        else:
            raise AmbiScaperError(
                'Reverb wrap must be specified using a "const" or "choose" tuple.'
            )
Beispiel #3
0
    def _validate_room_dimensions(self, room_dimensions_tuple):
        '''
        TODO
        :param room_dimensions_tuple:
        :return:
        '''
        def _valid_room_dimensions_values(room_dimensions):
            # room_dimensions: list of 3 Numbers
            if (room_dimensions is None
                    or not isinstance(room_dimensions, list) or not all(
                        isinstance(dim, Number) for dim in room_dimensions)
                    or len(room_dimensions) is not 3):
                return False
            else:
                return True

        # Make sure it's a valid distribution tuple
        _validate_distribution(room_dimensions_tuple)

        # If room_dimensions is specified explicitly
        if room_dimensions_tuple[0] == "const":
            if not _valid_room_dimensions_values(room_dimensions_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: room dimensions must be a list of 3 elements'
                )

        elif room_dimensions_tuple[0] == "choose":
            if not room_dimensions_tuple[1]:  # list is empty
                raise AmbiScaperError(
                    'reverb_config: room_dimensions_tuple list empty')
            elif not all(
                    _valid_room_dimensions_values(room_dimensions)
                    for room_dimensions in room_dimensions_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: room dimensions must be a list of 3 elements'
                )

        elif room_dimensions_tuple[0] == "uniform":
            if room_dimensions_tuple[1] < 0:
                raise AmbiScaperError(
                    'A "uniform" distribution tuple for room dimensions must have '
                    'min_value >= 0')

        elif room_dimensions_tuple[0] == "normal":
            warnings.warn(
                'A "normal" distribution tuple for room dimensions can result in '
                'negative values, in which case the distribution will be '
                're-sampled until a positive value is returned: this can result '
                'in an infinite loop!', AmbiScaperWarning)

        elif room_dimensions_tuple[0] == "truncnorm":
            if room_dimensions_tuple[3] < 0:
                raise AmbiScaperError(
                    'A "truncnorm" distirbution tuple for room dimensions must specify a non-'
                    'negative trunc_min value.')
Beispiel #4
0
    def _validate_t60(self, t60_tuple):
        '''
        TODO
        :param beta_tuple:
        :return:
        '''
        def _valid_t60_values(t60):
            # t60: float bigger than 0
            if (t60 is None or not isinstance(t60, float) or t60 <= 0):
                return False
            else:
                return True

        # Make sure it's a valid distribution tuple
        _validate_distribution(t60_tuple)

        # If t60 is specified explicitly
        if t60_tuple[0] == "const":
            if not _valid_t60_values(t60_tuple[1]):
                raise AmbiScaperError('reverb_config: t60 must be a float >0')

        elif t60_tuple[0] == "choose":
            if not t60_tuple[1]:  # list is empty
                raise AmbiScaperError('reverb_config: t60_tuple list empty')
            elif not all(_valid_t60_values(t60) for t60 in t60_tuple[1]):
                raise AmbiScaperError('reverb_config: t60 must be a float >0')

        elif t60_tuple[0] == "uniform":
            if t60_tuple[1] < 0:
                raise AmbiScaperError(
                    'A "uniform" distribution tuple for t60 must have '
                    'min_value >= 0')

        elif t60_tuple[0] == "normal":
            warnings.warn(
                'A "normal" distribution tuple for t60 can result in '
                'negative values, in which case the distribution will be '
                're-sampled until a positive value is returned: this can result '
                'in an infinite loop!', AmbiScaperWarning)

        elif t60_tuple[0] == "truncnorm":
            if t60_tuple[3] < 0:
                raise AmbiScaperError(
                    'A "truncnorm" distirbution tuple for t60 must specify a non-'
                    'negative trunc_min value.')
Beispiel #5
0
    def _validate_IR_length(self, IRlenght_tuple):
        '''
        TODO
        :param IRlenght_tuple:
        :return:
        '''

        # Make sure it's a valid distribution tuple
        _validate_distribution(IRlenght_tuple)

        def __valid_IR_length_values(IRlength):
            if (not isinstance(IRlength, int) or IRlength <= 0):
                return False
            else:
                return True

        # If IR length is specified explicitly
        if IRlenght_tuple[0] == "const":

            # IR length: positive integer
            if IRlenght_tuple[1] is None:
                raise AmbiScaperError('reverb_config: IR length is None')
            elif not __valid_IR_length_values(IRlenght_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: IR length must be a positive integer')

        # Otherwise it must be specified using "choose"
        elif IRlenght_tuple[0] == "choose":
            if not IRlenght_tuple[1]:  # list is empty
                raise AmbiScaperError('reverb_config: IR length list empty')
            elif not all(
                    __valid_IR_length_values(length)
                    for length in IRlenght_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: IR length must be a positive integer')

        # No other labels allowed"
        else:
            raise AmbiScaperError(
                'IR length must be specified using a "const" or "choose" tuple.'
            )
Beispiel #6
0
    def _validate_microphone_type(self, mic_type_tuple):
        '''
        Validate that a mic_type tuple is in the right format and that it's values
        are valid.

        Parameters
        ----------
        mic_type_tuple : tuple
            Label tuple (see ```AmbiScaper.add_event``` for required format).

        Raises
        ------
        AmbiScaperError
            If the validation fails.

        '''
        # Make sure it's a valid distribution tuple
        _validate_distribution(mic_type_tuple)

        # Make sure it's one of the allowed distributions for a mic_type and that the
        # mic_type value is one of the allowed labels.
        if mic_type_tuple[0] == "const":
            if not mic_type_tuple[1] in self.supported_virtual_mics.keys():
                raise AmbiScaperError(
                    'Microphone type value must match one of the available labels: '
                    '{:s}'.format(str(self.supported_virtual_mics.keys())))
        elif mic_type_tuple[0] == "choose":
            if mic_type_tuple[1]:  # list is not empty
                if not set(mic_type_tuple[1]).issubset(
                        set(self.supported_virtual_mics.keys())):
                    raise AmbiScaperError(
                        'Microphone type provided must be a subset of the available '
                        'labels: {:s}'.format(
                            str(self.supported_virtual_mics.keys())))
        else:
            raise AmbiScaperError(
                'Microphone type must be specified using a "const" or "choose" tuple.'
            )
Beispiel #7
0
    def _validate_source_type(self, source_type_tuple):
        '''
        Validate that a source_type tuple is in the right format and that it's values
        are valid.

        Parameters
        ----------
        source_type_tuple : tuple
            Label tuple (see ```AmbiScaper.add_event``` for required format).

        Raises
        ------
        AmbiScaperError
            If the validation fails.

        '''
        # Make sure it's a valid distribution tuple
        _validate_distribution(source_type_tuple)

        # Make sure it's one of the allowed distributions for a source_type and that the
        # source_type value is one of the allowed labels.
        if source_type_tuple[0] == "const":
            if not source_type_tuple[1] in SMIR_ALLOWED_SOURCE_TYPES:
                raise AmbiScaperError(
                    'Source type value must match one of the available labels: '
                    '{:s}'.format(str(SMIR_ALLOWED_SOURCE_TYPES)))
        elif source_type_tuple[0] == "choose":
            if source_type_tuple[1]:  # list is not empty
                if not set(source_type_tuple[1]).issubset(
                        set(SMIR_ALLOWED_SOURCE_TYPES)):
                    raise AmbiScaperError(
                        'Source type provided must be a subset of the available '
                        'labels: {:s}'.format(str(SMIR_ALLOWED_SOURCE_TYPES)))
        else:
            raise AmbiScaperError(
                'Source type must be specified using a "const" or "choose" tuple.'
            )
Beispiel #8
0
    def _validate_wall_reflectivity(self, wall_reflectivity_tuple):
        '''
        TODO
        :param wall_reflectivity_tuple:
        :return:
        '''
        def _valid_wall_reflectivity_values(wall_reflectivity):
            # wall_reflectivity: list of 6 floats in the range [0,1]
            if (wall_reflectivity is None
                    or not isinstance(wall_reflectivity, list)
                    or len(wall_reflectivity) is not 6 or not all(
                        isinstance(r, float) and 0 <= r <= 1
                        for r in wall_reflectivity)):
                return False
            else:
                return True

        # Make sure it's a valid distribution tuple
        _validate_distribution(wall_reflectivity_tuple)

        # If wall_reflectivity is specified explicitly
        if wall_reflectivity_tuple[0] == "const":
            if not _valid_wall_reflectivity_values(wall_reflectivity_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: wall_reflectivity must be a list of 6 floats between 0 and 1'
                )

        elif wall_reflectivity_tuple[0] == "choose":
            if not wall_reflectivity_tuple[1]:  # list is empty
                raise AmbiScaperError(
                    'reverb_config: wall_reflectivity_tuple list empty')
            elif not all(
                    _valid_wall_reflectivity_values(wall_reflectivity)
                    for wall_reflectivity in wall_reflectivity_tuple[1]):
                raise AmbiScaperError(
                    'reverb_config: wall_reflectivity must be a list of 6 floats between 0 and 1'
                )

        elif wall_reflectivity_tuple[0] == "uniform":
            if wall_reflectivity_tuple[1] < 0:
                raise AmbiScaperError(
                    'A "uniform" distribution tuple for wall_reflectivity must have '
                    'min_value >= 0')
            elif wall_reflectivity_tuple[2] > 1:
                raise AmbiScaperError(
                    'A "uniform" distribution tuple for wall_reflectivity must have '
                    'max_value <= 1')

        elif wall_reflectivity_tuple[0] == "normal":
            warnings.warn(
                'A "normal" distribution tuple for wall_reflectivity can result in '
                'values outside [0,1], in which case the distribution will be '
                're-sampled until a positive value is returned: this can result '
                'in an infinite loop!', AmbiScaperWarning)

        elif wall_reflectivity_tuple[0] == "truncnorm":
            if wall_reflectivity_tuple[3] < 0:
                raise AmbiScaperError(
                    'A "uniform" distribution tuple for wall_reflectivity must have '
                    'min_value >= 0')
            elif wall_reflectivity_tuple[4] > 1:
                raise AmbiScaperError(
                    'A "uniform" distribution tuple for wall_reflectivity must have '
                    'max_value <= 1')