Beispiel #1
0
    def __init__(
            self,
            context,
            name,
            parent,
            *,  # No positional arguments past this point.
            sofaFile=None,
            hrirPositions=None,
            hrirData=None,
            hrirDelays=None,
            headOrientation=None,
            dynamicITD=True,
            hrirInterpolation=True,
            irTruncationLength=None,
            filterCrossfading=False,
            interpolatingConvolver=False,
            staticLateSofaFile=None,
            staticLateFilters=None,
            staticLateDelays=None,
            headTrackingReceiver=None,
            headTrackingPositionalArguments=None,
            headTrackingKeywordArguments=None,
            fftImplementation='default'):
        """
        Constructor.

        Parameters
        ----------
        context : visr.SignalFlowContext
            Standard visr.Component construction argument, a structure holding the block size and the sampling frequency
        name : string
            Name of the component, Standard visr.Component construction argument
        parent : visr.CompositeComponent
            Containing component if there is one, None if this is a top-level component of the signal flow.
        sofaFile: string
            BRIR database provided as a SOFA file. This is an alternative to the hrirPosition, hrirData
            (and optionally hrirDelays) argument. Default None means that hrirData and hrirPosition must be provided.
        hrirPositions : numpy.ndarray
            Optional way to provide the measurement grid for the BRIR listener view directions. If a
            SOFA file is provided, this is optional and overrides the listener view data in the file.
            Otherwise this argument is mandatory. Dimension #grid directions x (dimension of position argument)
        hrirData: numpy.ndarray
            Optional way to provide the BRIR data. Dimension: #grid directions  x #ears (2) # x #loudspeakers x #ir length
        hrirDelays: numpy.ndarray
            Optional BRIR delays. If a SOFA file is given, this  argument overrides a potential delay setting from the file. Otherwise, no extra delays
            are applied unless this option is provided. Dimension: #grid directions  x #ears(2) x # loudspeakers
        headOrientation : array-like
            Head orientation in spherical coordinates (2- or 3-element vector or list). Either a static orientation (when no tracking is used),
            or the initial view direction
        headTracking: bool
            Whether dynamic headTracking is active. If True, an control input "tracking" is created.
        dynamicITD: bool
            Whether the delay part of th BRIRs is applied separately to the (delay-free) BRIRs.
        hrirInterpolation: bool
            Whether BRIRs are interpolated for the current head oriention. If False, a nearest-neighbour interpolation is used.
        irTruncationLength: int
            Maximum number of samples of the BRIR impulse responses. Functional only if the BRIR is provided in a SOFA file.
        filterCrossfading: bool
            Whether dynamic BRIR changes are crossfaded (True) or switched immediately (False)
        interpolatingConvolver: bool
            Whether the interpolating convolver option is used. If True, the convolver stores all BRIR filters, and the controller sends only
            interpolation coefficient messages to select the BRIR filters and their interpolation ratios.
        staticLateSofaFile: string, optional
            Name of a file containing a static (i.e., head orientation-independent) late part of the BRIRs.
            Optional argument, might be used as an alternative to the staticLateFilters argument, but these options are mutually exclusive.
            If neither is given, no static late part is used. The fields 'Data.IR' and the 'Data.Delay' are used.
        staticLateFilters: numpy.ndarray, optional
            Matrix containing a static, head position-independent part of the BRIRs. This option is mutually exclusive to
            staticLateSofaFile. If none of these is given, no separate static late part  is rendered.
            Dimension: 2 x #numberOfLoudspeakers x firLength
        staticLateDelays: numpy.ndarray, optional
            Time delay of the late static BRIRs per loudspeaker. Optional attribute,
            only used if late static BRIR coefficients are provided.
            Dimension: 2 x #loudspeakers
        fftImplementation: string
            The FFT implementation to be used in the convolver. the default value selects the system default.
        """

        super(RealtimeVirtualLoudspeakerRenderer,
              self).__init__(context, name, parent)

        # Handle loading of HRIR data from either a SOFA file or the the matrix arguments.
        if (hrirData is not None) and (sofaFile is not None):
            raise ValueError(
                "Exactly one of the arguments sofaFile and hrirData must be present."
            )
        if sofaFile is not None:
            [sofaHrirPositions, hrirData, sofaHrirDelays
             ] = readSofaFile(sofaFile,
                              truncationLength=irTruncationLength,
                              truncationWindowLength=16)
            # If hrirDelays is not provided as an argument, use the one retrieved from the SOFA file
            if hrirDelays is None:
                hrirDelays = sofaHrirDelays
            # Use the positions obtained from the SOFA file only if the argument is not set
            if hrirPositions is None:
                hrirPositions = sofaHrirPositions

        # Crude check for 'horizontal-only' listener view directions
        if np.max(np.abs(hrirPositions[:, 1])) < deg2rad(1):
            hrirPositions = hrirPositions[:,
                                          [0, 2
                                           ]]  # transform to polar coordinates

        numberOfLoudspeakers = hrirData.shape[2]

        self.objectSignalInput = visr.AudioInputFloat("audioIn", self,
                                                      numberOfLoudspeakers)
        self.binauralOutput = visr.AudioOutputFloat("audioOut", self, 2)

        enableTracking = (headTrackingReceiver is not None)

        self.virtualLoudspeakerRenderer = VirtualLoudspeakerRenderer(
            context,
            "VirtualLoudspeakerRenderer",
            self,
            hrirPositions=hrirPositions,
            hrirData=hrirData,
            hrirDelays=hrirDelays,
            headOrientation=None,
            headTracking=enableTracking,
            dynamicITD=dynamicITD,
            hrirInterpolation=hrirInterpolation,
            irTruncationLength=irTruncationLength,
            filterCrossfading=filterCrossfading,
            interpolatingConvolver=interpolatingConvolver,
            staticLateSofaFile=staticLateSofaFile,
            staticLateFilters=staticLateFilters,
            staticLateDelays=staticLateDelays,
            fftImplementation=fftImplementation)
        if enableTracking:
            if headTrackingPositionalArguments == None:
                headTrackingPositionalArguments = ()
            if headTrackingKeywordArguments == None:
                headTrackingKeywordArguments = {}
            self.trackingDevice = headTrackingReceiver(
                context, "HeadTrackingReceiver", self,
                *headTrackingPositionalArguments,
                **headTrackingKeywordArguments)
            self.parameterConnection(
                self.trackingDevice.parameterPort("orientation"),
                self.virtualLoudspeakerRenderer.parameterPort("tracking"))

        self.audioConnection(
            self.objectSignalInput,
            self.virtualLoudspeakerRenderer.audioPort("audioIn"))
        self.audioConnection(
            self.virtualLoudspeakerRenderer.audioPort("audioOut"),
            self.binauralOutput)
    def __init__(
            self,
            context,
            name,
            parent,  # Standard arguments for a VISR component
            numberOfObjects,  # Number of audo objects to be rendered.
            *,  # No positional arguments beyond this point
            sofaFile=None,  # whether a SOFA file is used to loaded the HRIR data.
            hrirPositions=None,  # Optional way to provide the measurement grid for the BRIR listener view directions. If a SOFA file is provided, this is optional and
            # overrides the listener view data in the file. Otherwise this argument is mandatory. Dimension #grid directions x (dimension of position argument)
        hrirData=None,  # Optional way to provide the BRIR data. Dimension: #grid directions  x #ears (2) # x #loudspeakers x #ir length
            hrirDelays=None,  # Optional BRIR delays. If a SOFA file is given, this  argument overrides a potential delay setting from the file. Otherwise, no extra delays
            # are applied unless this option is provided. Dimension: #grid directions  x #ears(2) x # loudspeakers
        headOrientation=None,  # Head orientation in spherical coordinates (2- or 3-element vector or list). Either a static orientation (when no tracking is used),
            # or the initial view direction
        headTracking=True,  # Whether dynamic racking is used.
            dynamicITD=True,  # Whether the ITD is applied separately. That requires preprocessed HRIR data
            dynamicILD=True,  # Whether the ILD is computed and applied separately. At the moment this feature is not used (apart from applying the object gains)
            hrirInterpolation=True,  # Whether the controller supports interpolation between neighbouring HRTF gridpoints. False means nearest neighbour (no interpolation),
            # True enables barycentric interpolation.
        filterCrossfading=False,  # Use a crossfading FIR filter matrix to avoid switching artifacts.
            interpolatingConvolver=False,
            fftImplementation="default"  # The FFT implementation to use.
    ):
        """
        Constructor.

        Parameters
        ----------
        context : visr.SignalFlowContext
            Standard visr.Component construction argument, holds the block size and the sampling frequency
        name : string
            Name of the component, Standard visr.Component construction argument
        parent : visr.CompositeComponent
            Containing component if there is one, None if this is a top-level component of the signal flow.
        numberOfObjects: int
            Maximum number of audio objects
        sofaFile: str, optional
            Optional SOFA for loading loaded the HRIR and associated data (HRIR measurement positions and delays)
            If not provided, the information must be provided by the hrirPositions and hrirData arguments.
        hrirPositions: numpy.ndarray, optional
            Optional way to provide the measurement grid for the BRIR listener view directions.
            If a SOFA file is provided, this is optional and overrides the listener view data
            in the file. Otherwise this argument is mandatory.
            Dimension #grid directions x (dimension of position argument)
        hrirData: numpy.ndarray, optional
            Optional way to provide the BRIR data.
            Dimension: #grid directions  x #ears (2) # x #loudspeakers x #ir length
        hrirDelays: numpy.ndarray, optional
            Optional BRIR delays. If a SOFA file is given, this  argument overrides
            a potential delay setting from the file. Otherwise, no extra delays
            are applied unless this option is provided.
            Dimension: #grid directions  x #ears(2) x # loudspeakers
        headOrientation: array-like, optional
            Head orientation in spherical coordinates (2- or 3-element vector or list).
            Either a static orientation (when no tracking is used), or the
            initial view direction
        headTracking: bool
            Whether dynamic head tracking is supported. If True, a parameter input with type
            pml.ListenerPosition and protocol pml.DoubleBufffering is created.
        dynamicITD: bool, optional
            Whether the ITD is applied separately. That requires preprocessed HRIR data
        dynamicILD: bool, optional
            Whether the ILD is computed and applied separately. At the moment this feature is not used (apart from applying the object gains)
        hrirInterpolation: bool, optional
            Whether the controller supports interpolation between neighbouring HRTF grid
            points. False means nearest neighbour (no interpolation), True
            enables barycentric interpolation.
        filterCrossfading: bool, optional
            Use a crossfading FIR filter matrix to avoid switching artifacts.
        fftImplementation: string, optional
            The FFT implementation to use. Default value enables VISR's default
            FFT library for the platform.
        """
        super(DynamicHrirRenderer, self).__init__(context, name, parent)
        self.objectSignalInput = visr.AudioInputFloat("audioIn", self,
                                                      numberOfObjects)
        self.binauralOutput = visr.AudioOutputFloat("audioOut", self, 2)
        self.objectVectorInput = visr.ParameterInput(
            "objectVector", self, pml.ObjectVector.staticType,
            pml.DoubleBufferingProtocol.staticType, pml.EmptyParameterConfig())
        if headTracking:
            self.trackingInput = visr.ParameterInput(
                "tracking", self, pml.ListenerPosition.staticType,
                pml.DoubleBufferingProtocol.staticType,
                pml.EmptyParameterConfig())

        if (hrirData is not None) and (sofaFile is not None):
            raise ValueError(
                "Exactly one of the arguments sofaFile and hrirData must be present."
            )
        if sofaFile is not None:
            # We don't support HRIR truncation here because they are usually quite short.
            [sofaHrirPositions, hrirData,
             sofaHrirDelays] = readSofaFile(sofaFile)
            # If hrirDelays is not provided as an argument, use the one retrieved from the SOFA file
            if hrirDelays is None:
                hrirDelays = sofaHrirDelays
            # Use the positions obtained from the SOFA file only if the argument is not set
            if hrirPositions is None:
                hrirPositions = sofaHrirPositions

        if dynamicITD:
            if (hrirDelays is None) or (hrirDelays.ndim !=
                                        2) or (hrirDelays.shape !=
                                               (hrirData.shape[0], 2)):
                raise ValueError(
                    'If the "dynamicITD" option is given, the parameter "delays" must be a #hrirs x 2 matrix.'
                )

        self.dynamicHrirController = DynamicHrirController(
            context,
            "DynamicHrirController",
            self,
            numberOfObjects,
            hrirPositions,
            hrirData,
            useHeadTracking=headTracking,
            dynamicITD=dynamicITD,
            dynamicILD=dynamicILD,
            hrirInterpolation=hrirInterpolation,
            interpolatingConvolver=interpolatingConvolver,
            hrirDelays=hrirDelays)

        self.parameterConnection(
            self.objectVectorInput,
            self.dynamicHrirController.parameterPort("objectVector"))
        if headTracking:
            self.parameterConnection(
                self.trackingInput,
                self.dynamicHrirController.parameterPort("headTracking"))

        firLength = hrirData.shape[-1]

        # Used if the InterpolatingConvolver is selected.
        numberOfInterpolants = 3 if hrirInterpolation else 1
        interpolationSteps = context.period if filterCrossfading else 0

        if dynamicITD or dynamicILD:
            if dynamicITD:
                delayControls = rcl.DelayVector.ControlPortConfig.Delay
            else:
                delayControls = rcl.DelayVector.ControlPortConfig.No
            if dynamicILD:
                delayControls = delayControls | rcl.DelayVector.ControlPortConfig.Gain
                initialGain = 0.0  # If the ILD is applied in the DelayVector, start from zero.
            else:
                initialGain = 1.0  # Fixed setting as the gain of the delay vector is not used

            self.delayVector = rcl.DelayVector(
                context,
                "delayVector",
                self,
                numberOfObjects * 2,
                interpolationType="lagrangeOrder3",
                initialDelay=0,
                controlInputs=delayControls,
                methodDelayPolicy=rcl.DelayMatrix.MethodDelayPolicy.Add,
                initialGain=initialGain,
                interpolationSteps=context.period)

            inConnections = [
                i % numberOfObjects for i in range(numberOfObjects * 2)
            ]
            self.audioConnection(self.objectSignalInput, inConnections,
                                 self.delayVector.audioPort("in"),
                                 range(2 * numberOfObjects))

            # Define the routing for the binaural convolver such that it match the layout of the
            # flat BRIR matrix.
            filterRouting = rbbl.FilterRoutingList()
            for idx in range(0, numberOfObjects):
                filterRouting.addRouting(idx, 0, idx, 1.0)
                filterRouting.addRouting(idx + numberOfObjects, 1,
                                         idx + numberOfObjects, 1.0)
            numMatrixInputs = 2 * numberOfObjects
        else:
            filterRouting = rbbl.FilterRoutingList()
            for idx in range(0, numberOfObjects):
                filterRouting.addRouting(idx, 0, idx, 1.0)
                filterRouting.addRouting(idx, 1, idx + numberOfObjects, 1.0)
            filterRouting2 = rbbl.FilterRoutingList([
                rbbl.FilterRouting(i % numberOfObjects, i // numberOfObjects,
                                   i, 1.0) for i in range(2 * numberOfObjects)
            ])
            numMatrixInputs = numberOfObjects

        if interpolatingConvolver:
            numFilters = np.prod(np.array(hrirData.shape[0:-1]))
            filterReshaped = np.reshape(hrirData, (numFilters, firLength), 'C')
            self.convolver = rcl.InterpolatingFirFilterMatrix(
                context,
                'convolutionEngine',
                self,
                numberOfInputs=numMatrixInputs,
                numberOfOutputs=2,
                maxFilters=numFilters,
                filterLength=firLength,
                maxRoutings=2 * numberOfObjects,
                numberOfInterpolants=numberOfInterpolants,
                transitionSamples=interpolationSteps,
                filters=filterReshaped,
                routings=filterRouting,
                controlInputs=rcl.InterpolatingFirFilterMatrix.
                ControlPortConfig.Interpolants,
                fftImplementation=fftImplementation)
        elif filterCrossfading:
            self.convolver = rcl.CrossfadingFirFilterMatrix(
                context,
                'convolutionEngine',
                self,
                numberOfInputs=numMatrixInputs,
                numberOfOutputs=2,
                maxFilters=2 * numberOfObjects,
                filterLength=firLength,
                maxRoutings=2 * numberOfObjects,
                routings=filterRouting,
                transitionSamples=context.period,
                controlInputs=rcl.CrossfadingFirFilterMatrix.ControlPortConfig.
                Filters,
                fftImplementation=fftImplementation)
        else:
            self.convolver = rcl.FirFilterMatrix(
                context,
                'convolutionEngine',
                self,
                numberOfInputs=numMatrixInputs,
                numberOfOutputs=2,
                maxFilters=2 * numberOfObjects,
                filterLength=firLength,
                maxRoutings=2 * numberOfObjects,
                routings=filterRouting,
                controlInputs=rcl.FirFilterMatrix.ControlPortConfig.Filters,
                fftImplementation=fftImplementation)
        if dynamicITD or dynamicILD:
            self.audioConnection(self.delayVector.audioPort("out"),
                                 self.convolver.audioPort("in"))
            if dynamicITD:
                self.parameterConnection(
                    self.dynamicHrirController.parameterPort("delayOutput"),
                    self.delayVector.parameterPort("delayInput"))
            if dynamicILD:
                self.parameterConnection(
                    self.dynamicHrirController.parameterPort("gainOutput"),
                    self.delayVector.parameterPort("gainInput"))
        else:
            self.audioConnection(self.objectSignalInput,
                                 self.convolver.audioPort("in"))

        self.audioConnection(self.convolver.audioPort("out"),
                             self.binauralOutput)
        if interpolatingConvolver:
            self.parameterConnection(
                self.dynamicHrirController.parameterPort("interpolatorOutput"),
                self.convolver.parameterPort("interpolantInput"))
        else:
            self.parameterConnection(
                self.dynamicHrirController.parameterPort("filterOutput"),
                self.convolver.parameterPort("filterInput"))
Beispiel #3
0
context = visr.SignalFlowContext( period=bufferSize, samplingFrequency=fs)

sofaFile = './data/dtf b_nh169.sofa'
if not os.path.exists( sofaFile ):
    if not os.path.exists( './data/' ):
        os.mkdir( './data/' )
    urlretrieve( 'http://sofacoustics.org/data/database/ari%20(artificial)/dtf%20b_nh169.sofa',sofaFile )

if dynamicITD:
    sofaFileTD = './data/dtf b_nh169_timedelay.sofa'
    if not os.path.exists( sofaFileTD ):
        sofaExtractDelay( sofaFile, sofaFileTD )
    sofaFile = sofaFileTD

[ hrirPos, hrirData, hrirDelays ] = readSofaFile( sofaFile )


controller = DynamicHrirController( context, "Controller", None,
                  numBinauralObjects,
                  hrirPos, hrirData,
                  useHeadTracking = headTrackEnabled,
                  dynamicITD = dynamicITD,
                  dynamicILD = dynamicILD,
                  hrirInterpolation = True,
                  hrirDelays = hrirDelays
                  )

flow = rrl.AudioSignalFlow( controller )

objectInput = flow.parameterReceivePort( "objectVector" )
Beispiel #4
0
    def __init__(
            self,
            context,
            name,
            parent,
            *,  # This ensures that the remaining arguments are given as keyword arguments.
            sofaFile=None,
            hrirPositions=None,
            hrirData=None,
            hrirDelays=None,
            headOrientation=None,
            headTracking=True,
            dynamicITD=False,
            hrirInterpolation=False,
            irTruncationLength=None,
            filterCrossfading=False,
            interpolatingConvolver=False,
            staticLateSofaFile=None,
            staticLateFilters=None,
            staticLateDelays=None,
            fftImplementation='default'):
        """
        Constructor.

        Parameters
        ----------
        context : visr.SignalFlowContext
            Standard visr.Component construction argument, a structure holding the block size and the sampling frequency
        name : string
            Name of the component, Standard visr.Component construction argument
        parent : visr.CompositeComponent
            Containing component if there is one, None if this is a top-level component of the signal flow.
        sofaFile: string
            BRIR database provided as a SOFA file. This is an alternative to the hrirPosition, hrirData
            (and optionally hrirDelays) argument. Default None means that hrirData and hrirPosition must be provided.
        hrirPositions : numpy.ndarray
            Optional way to provide the measurement grid for the BRIR listener view directions. If a
            SOFA file is provided, this is optional and overrides the listener view data in the file.
            Otherwise this argument is mandatory. Dimension #grid directions x (dimension of position argument)
        hrirData: numpy.ndarray
            Optional way to provide the BRIR data. Dimension: #grid directions  x #ears (2) # x #loudspeakers x #ir length
        hrirDelays: numpy.ndarray
            Optional BRIR delays. If a SOFA file is given, this  argument overrides a potential delay setting from the file. Otherwise, no extra delays
            are applied unless this option is provided. Dimension: #grid directions  x #ears(2) x # loudspeakers
        headOrientation : array-like
            Head orientation in spherical coordinates (2- or 3-element vector or list). Either a static orientation (when no tracking is used),
            or the initial view direction
        headTracking: bool
            Whether dynamic headTracking is active. If True, an control input "tracking" is created.
        dynamicITD: bool
            Whether the delay part of th BRIRs is applied separately to the (delay-free) BRIRs.
        hrirInterpolation: bool
            Whether BRIRs are interpolated for the current head oriention. If False, a nearest-neighbour interpolation is used.
        irTruncationLength: int
            Maximum number of samples of the BRIR impulse responses. Functional only if the BRIR is provided in a SOFA file.
        filterCrossfading: bool
            Whether dynamic BRIR changes are crossfaded (True) or switched immediately (False)
        interpolatingConvolver: bool
            Whether the interpolating convolver option is used. If True, the convolver stores all BRIR filters, and the controller sends only
            interpolation coefficient messages to select the BRIR filters and their interpolation ratios.
        staticLateSofaFile: string, optional
            Name of a file containing a static (i.e., head orientation-independent) late part of the BRIRs.
            Optional argument, might be used as an alternative to the staticLateFilters argument, but these options are mutually exclusive.
            If neither is given, no static late part is used. The fields 'Data.IR' and the 'Data.Delay' are used.
        staticLateFilters: numpy.ndarray, optional
            Matrix containing a static, head position-independent part of the BRIRs. This option is mutually exclusive to
            staticLateSofaFile. If none of these is given, no separate static late part  is rendered.
            Dimension: 2 x #numberOfLoudspeakers x firLength
        staticLateDelays: numpy.ndarray, optional
            Time delay of the late static BRIRs per loudspeaker. Optional attribute,
            only used if late static BRIR coefficients are provided.
            Dimension: 2 x #loudspeakers
        fftImplementation: string
            The FFT implementation to be used in the convolver. the default value selects the system default.
        """
        if (hrirData is not None) and (sofaFile is not None):
            raise ValueError(
                "Exactly one of the arguments sofaFile and hrirData must be present."
            )
        if sofaFile is not None:
            [sofaHrirPositions, hrirData, sofaHrirDelays
             ] = readSofaFile(sofaFile,
                              truncationLength=irTruncationLength,
                              truncationWindowLength=16)
            # If hrirDelays is not provided as an argument, use the one retrieved from the SOFA file
            if hrirDelays is None:
                hrirDelays = sofaHrirDelays
            # Use the positions obtained from the SOFA file only if the argument is not set
            if hrirPositions is None:
                hrirPositions = sofaHrirPositions

        # Crude check for 'horizontal-only' listener view directions
        if np.max(np.abs(hrirPositions[:, 1])) < deg2rad(1):
            hrirPositions = hrirPositions[:,
                                          [0, 2
                                           ]]  # transform to polar coordinates

        numberOfLoudspeakers = hrirData.shape[2]

        super(VirtualLoudspeakerRenderer, self).__init__(context, name, parent)
        self.loudspeakerSignalInput = visr.AudioInputFloat(
            "audioIn", self, numberOfLoudspeakers)
        self.binauralOutput = visr.AudioOutputFloat("audioOut", self, 2)
        if headTracking:
            self.trackingInput = visr.ParameterInput(
                "tracking", self, pml.ListenerPosition.staticType,
                pml.DoubleBufferingProtocol.staticType,
                pml.EmptyParameterConfig())

        # Check consistency between HRIR positions and HRIR data
        if (hrirPositions.shape[0] != hrirData.shape[0]):
            raise ValueError(
                "The number of HRIR positions is inconsistent with the dimension of the HRIR data."
            )

        # Additional safety check (is tested in the controller anyway)
        if dynamicITD:
            if (hrirDelays is
                    None) or (hrirDelays.ndim != hrirData.ndim - 1) or (
                        hrirDelays.shape != hrirData.shape[0:-1]):
                raise ValueError(
                    'If the "dynamicITD" option is given, the parameter "delays" must match the first dimensions of the hrir data matrix.'
                )

        self.virtualLoudspeakerController = VirtualLoudspeakerController(
            context,
            "VirtualLoudspeakerController",
            self,
            hrirPositions=hrirPositions,
            hrirData=hrirData,
            headTracking=headTracking,
            dynamicITD=dynamicITD,
            hrirInterpolation=hrirInterpolation,
            hrirDelays=hrirDelays,
            interpolatingConvolver=interpolatingConvolver)

        if headTracking:
            self.parameterConnection(
                self.trackingInput,
                self.virtualLoudspeakerController.parameterPort(
                    "headTracking"))

        # Define the routing for the binaural convolver such that it matches the organisation of the
        # flat BRIR matrix.
        filterRouting = rbbl.FilterRoutingList()

        firLength = hrirData.shape[-1]

        if dynamicITD:
            self.delayVector = rcl.DelayVector(
                context,
                "delayVector",
                self,
                numberOfLoudspeakers * 2,
                interpolationType="lagrangeOrder3",
                initialDelay=0,
                controlInputs=rcl.DelayVector.ControlPortConfig.Delay,
                methodDelayPolicy=rcl.DelayMatrix.MethodDelayPolicy.Add,
                initialGain=1.0,
                interpolationSteps=context.period)

            self.audioConnection(self.loudspeakerSignalInput, [
                i % numberOfLoudspeakers
                for i in range(numberOfLoudspeakers * 2)
            ], self.delayVector.audioPort("in"),
                                 range(0, 2 * numberOfLoudspeakers))

            for idx in range(0, numberOfLoudspeakers):
                filterRouting.addRouting(idx, 0, idx, 1.0)
                filterRouting.addRouting(idx + numberOfLoudspeakers, 1,
                                         idx + numberOfLoudspeakers, 1.0)

            if interpolatingConvolver:
                if filterCrossfading:
                    interpolationSteps = context.period
                else:
                    interpolationSteps = 0

                numFilters = np.prod(hrirData.shape[:-1])
                filterReshaped = np.reshape(hrirData, (numFilters, firLength))
                self.convolver = rcl.InterpolatingFirFilterMatrix(
                    context,
                    'convolutionEngine',
                    self,
                    numberOfInputs=2 * numberOfLoudspeakers,
                    numberOfOutputs=2,
                    maxFilters=numFilters,
                    filterLength=firLength,
                    maxRoutings=2 * numberOfLoudspeakers,
                    numberOfInterpolants=2,  # TODO: Find out from
                    transitionSamples=interpolationSteps,
                    filters=filterReshaped,
                    routings=filterRouting,
                    controlInputs=rcl.InterpolatingFirFilterMatrix.
                    ControlPortConfig.Filters,
                    fftImplementation=fftImplementation)

            elif filterCrossfading:
                self.convolver = rcl.CrossfadingFirFilterMatrix(
                    context,
                    'convolutionEngine',
                    self,
                    numberOfInputs=2 * numberOfLoudspeakers,
                    numberOfOutputs=2,
                    maxFilters=2 * numberOfLoudspeakers,
                    filterLength=firLength,
                    maxRoutings=2 * numberOfLoudspeakers,
                    transitionSamples=context.period,
                    routings=filterRouting,
                    controlInputs=rcl.CrossfadingFirFilterMatrix.
                    ControlPortConfig.Filters,
                    fftImplementation=fftImplementation)
            else:
                self.convolver = rcl.FirFilterMatrix(
                    context,
                    'convolutionEngine',
                    self,
                    numberOfInputs=2 * numberOfLoudspeakers,
                    numberOfOutputs=2,
                    maxFilters=2 * numberOfLoudspeakers,
                    filterLength=firLength,
                    maxRoutings=2 * numberOfLoudspeakers,
                    routings=filterRouting,
                    controlInputs=rcl.FirFilterMatrix.ControlPortConfig.
                    Filters,
                    fftImplementation=fftImplementation)

            self.audioConnection(
                self.delayVector.audioPort("out"),
                self.convolver.audioPort("in"),
            )
            self.parameterConnection(
                self.virtualLoudspeakerController.parameterPort("delayOutput"),
                self.delayVector.parameterPort("delayInput"))

        else:  # no dynamic ITD
            for idx in range(0, numberOfLoudspeakers):
                filterRouting.addRouting(idx, 0, idx, 1.0)
                filterRouting.addRouting(idx, 1, idx + numberOfLoudspeakers,
                                         1.0)
            if interpolatingConvolver:
                if filterCrossfading:
                    interpolationSteps = context.period
                else:
                    interpolationSteps = 0

                #filterReshaped = np.concatenate( (hrirData[:,0,...],hrirData[:,1,...]), axis=1 )
                numFilters = np.prod(np.array(hrirData.shape[0:-1]))
                filterReshaped = np.reshape(hrirData, (numFilters, firLength))
                self.convolver = rcl.InterpolatingFirFilterMatrix(
                    context,
                    'convolutionEngine',
                    self,
                    numberOfInputs=numberOfLoudspeakers,
                    numberOfOutputs=2,
                    maxFilters=numFilters,
                    filterLength=firLength,
                    maxRoutings=2 * numberOfLoudspeakers,
                    numberOfInterpolants=2,  # TODO: Find out from
                    transitionSamples=interpolationSteps,
                    filters=filterReshaped,
                    routings=filterRouting,
                    controlInputs=rcl.InterpolatingFirFilterMatrix.
                    ControlPortConfig.Interpolants,
                    fftImplementation=fftImplementation)
            elif filterCrossfading:
                self.convolver = rcl.CrossfadingFirFilterMatrix(
                    context,
                    'convolutionEngine',
                    self,
                    numberOfInputs=numberOfLoudspeakers,
                    numberOfOutputs=2,
                    maxFilters=2 * numberOfLoudspeakers,
                    filterLength=firLength,
                    maxRoutings=2 * numberOfLoudspeakers,
                    transitionSamples=context.period,
                    routings=filterRouting,
                    controlInputs=rcl.CrossfadingFirFilterMatrix.
                    ControlPortConfig.Filters,
                    fftImplementation=fftImplementation)
            else:
                self.convolver = rcl.FirFilterMatrix(
                    context,
                    'convolutionEngine',
                    self,
                    numberOfInputs=numberOfLoudspeakers,
                    numberOfOutputs=2,
                    maxFilters=2 * numberOfLoudspeakers,
                    filterLength=firLength,
                    maxRoutings=2 * numberOfLoudspeakers,
                    routings=filterRouting,
                    controlInputs=rcl.FirFilterMatrix.ControlPortConfig.
                    Filters,
                    fftImplementation=fftImplementation)
            self.audioConnection(self.loudspeakerSignalInput,
                                 self.convolver.audioPort("in"))

        if interpolatingConvolver:
            self.parameterConnection(
                self.virtualLoudspeakerController.parameterPort(
                    "interpolatorOutput"),
                self.convolver.parameterPort("interpolantInput"))
        else:
            self.parameterConnection(
                self.virtualLoudspeakerController.parameterPort(
                    "filterOutput"),
                self.convolver.parameterPort("filterInput"))

        # Optionally use static filters for the late part.
        if (staticLateSofaFile is not None) and (staticLateFilters
                                                 is not None):
            raise ValueError(
                "The arguments 'staticLateSofaFile' and 'staticLateFilters' cannot both be given."
            )
        if (staticLateSofaFile is not None):
            latePos, lateFilters, lateDelay = readSofaFile(staticLateSofaFile)
            staticLateDelays = np.squeeze(lateDelay)
            staticLateFilters = np.squeeze(lateFilters)

        if (staticLateFilters is not None):
            flatDelays = staticLateDelays.flatten(order='C')
            self.staticLateDelays = rcl.DelayVector(
                context,
                'staticLateDelays',
                self,
                2 * numberOfLoudspeakers,
                interpolationSteps=context.period,
                interpolationType='nearestSample',
                initialGain=np.ones((numberOfLoudspeakers), dtype=np.float32),
                initialDelay=flatDelays)
            lateFilterRouting = rbbl.FilterRoutingList([
                rbbl.FilterRouting(i, i // numberOfLoudspeakers, i, 1.0)
                for i in range(2 * numberOfLoudspeakers)
            ])

            flatLateFilters = np.reshape(staticLateFilters,
                                         (2 * numberOfLoudspeakers, -1),
                                         order='C')
            self.staticLateFilters = rcl.FirFilterMatrix(
                context,
                "staticlateFilters",
                self,
                numberOfInputs=2 * numberOfLoudspeakers,
                numberOfOutputs=2,
                filterLength=staticLateFilters.shape[-1],
                maxFilters=2 * numberOfLoudspeakers,
                maxRoutings=2 * numberOfLoudspeakers,
                routings=lateFilterRouting,
                filters=flatLateFilters,
                fftImplementation=fftImplementation)
            self.audioConnection(
                sendPort=self.loudspeakerSignalInput,
                sendIndices=list(range(numberOfLoudspeakers)) +
                list(range(numberOfLoudspeakers)),
                receivePort=self.staticLateDelays.audioPort("in"))
            self.audioConnection(
                sendPort=self.staticLateDelays.audioPort("out"),
                receivePort=self.staticLateFilters.audioPort("in"))
            self.earlyLateSum = rcl.Add(context,
                                        "earlyLateSum",
                                        self,
                                        numInputs=2,
                                        width=2)
            self.audioConnection(self.convolver.audioPort("out"),
                                 self.earlyLateSum.audioPort("in0"))
            self.audioConnection(self.staticLateFilters.audioPort("out"),
                                 self.earlyLateSum.audioPort("in1"))
            self.audioConnection(self.earlyLateSum.audioPort("out"),
                                 self.binauralOutput)
        else:
            self.audioConnection(self.convolver.audioPort("out"),
                                 self.binauralOutput)
fullSofaPath = os.path.join(sofaDirectory, sofaFile)
fullSofaPathLate = os.path.join(sofaDirectory, sofaFileLate)

if not os.path.exists(fullSofaPath):
    if not os.path.exists(sofaDirectory):
        os.mkdir(sofaDirectory)
    urlretrieve('http://data.bbcarp.org.uk/bbcrd-brirs/sofa/' + sofaFile,
                fullSofaPath)
if not os.path.exists(fullSofaPathLate):
    if not os.path.exists(sofaDirectory):
        os.mkdir(sofaDirectory)
    urlretrieve('http://data.bbcarp.org.uk/bbcrd-brirs/sofa/' + sofaFileLate,
                fullSofaPathLate)

hrirPos, hrirData, hrirDelays = readSofaFile(fullSofaPath)
latePos, lateFilters, lateDelays = readSofaFile(fullSofaPathLate)

# Crude check for 'horizontal-only' listener view directions
if np.max(np.abs(hrirPos[:, 1])) < deg2rad(1):
    hrirPos = hrirPos[:, [0, 2]]  # transform to polar coordinates

# Dimension of hrirData is #measurement positions x #ears x # lsp x ir length
numLoudspeakers = hrirData.shape[2]

context = visr.SignalFlowContext(period=blockSize, samplingFrequency=fs)

renderer = VirtualLoudspeakerRenderer(
    context,
    "VirtualLoudspeakerRenderer",
    None,
Beispiel #6
0
    def __init__(self,
                 context,
                 name,
                 parent,
                 hoaOrder=None,
                 sofaFile=None,
                 decodingFilters=None,
                 interpolationSteps=None,
                 headOrientation=None,
                 headTracking=True,
                 fftImplementation='default'):
        """
        Constructor.

        Parameters
        ----------
        context : visr.SignalFlowContext
            Standard visr.Component construction argument, holds the block size and the sampling frequency
        name : string
            Name of the component, Standard visr.Component construction argument
        parent : visr.CompositeComponent
            Containing component if there is one, None if this is a top-level component of the signal flow.
        hoaOrder: int or None
            The maximum HOA order that can be reproduced. If None, the HOA order is deduced
            from the first dimension of the HOA filters (possibly contained in a SOFA file).
        sofaFile: string or NoneType
            A file in SOFA format containing the decoding filters. This expects the filters in the
            field 'Data.IR', dimensions (hoaOrder+1)**2 x 2 x irLength. If None, then the filters
            must be provided in 'decodingFilters' parameter.
        decodingFilters : numpy.ndarray or NoneType
            Alternative way to provide the HOA decoding filters.
        interpolationSteps: int, optional
           Number of samples to transition to new object positions after an update.
        headOrientation : array-like
            Head orientation in spherical coordinates (2- or 3-element vector or list). Either a static orientation (when no tracking is used),
            or the initial view direction
        headTracking: bool
            Whether dynamic head tracking is active.
        fftImplementation: string, optional
            The FFT library to be used in the filtering. THe default uses VISR's
            default implementation for the present platform.
        """
        if (decodingFilters is None) == (sofaFile is None):
            raise ValueError(
                "HoaObjectToBinauralRenderer: Either 'decodingFilters' or 'sofaFile' must be provided."
            )
        if sofaFile is None:
            filters = decodingFilters
        else:
            # pos and delays are not used here.
            [pos, filters, delays] = readSofaFile(sofaFile)

        if hoaOrder is None:
            numHoaCoeffs = filters.shape[0]
            orderP1 = int(np.floor(np.sqrt(numHoaCoeffs)))
            if orderP1**2 != numHoaCoeffs:
                raise ValueError(
                    "If hoaOrder is not given, the number of HOA filters must be a square number"
                )
            hoaOrder = orderP1 - 1
        else:
            numHoaCoeffs = (hoaOrder + 1)**2

        if filters.ndim != 3 or filters.shape[1] != 2 or filters.shape[
                0] < numHoaCoeffs:
            raise ValueError(
                "HoaObjectToBinauralRenderer: the filter data must be a 3D matrix where the second dimension is 2 and the first dimension is equal or larger than (hoaOrder+1)^2."
            )

        # Set default value for fading between interpolation
        if interpolationSteps is None:
            interpolationSteps = context.period

        super(HoaBinauralRenderer, self).__init__(context, name, parent)
        self.hoaSignalInput = visr.AudioInputFloat("audioIn", self,
                                                   numHoaCoeffs)
        self.binauralOutput = visr.AudioOutputFloat("audioOut", self, 2)

        filterMtx = np.concatenate(
            (filters[0:numHoaCoeffs, 0, :], filters[0:numHoaCoeffs, 1, :]))
        routings = rbbl.FilterRoutingList()
        for idx in range(0, numHoaCoeffs):
            routings.addRouting(idx, 0, idx, 1.0)
            routings.addRouting(idx, 1, idx + numHoaCoeffs, 1.0)

        self.binauralFilterBank = rcl.FirFilterMatrix(
            context,
            'binauralFilterBank',
            self,
            numberOfInputs=numHoaCoeffs,
            numberOfOutputs=2,
            filterLength=filters.shape[-1],
            maxFilters=2 * numHoaCoeffs,
            maxRoutings=2 * numHoaCoeffs,
            filters=filterMtx,
            routings=routings,
            controlInputs=rcl.FirFilterMatrix.ControlPortConfig.NoInputs,
            fftImplementation=fftImplementation)

        if headTracking or (headOrientation is not None):

            numMatrixCoeffs = ((hoaOrder + 1) * (2 * hoaOrder + 1) *
                               (2 * hoaOrder + 3)) // 3

            self.rotationCalculator = HoaRotationMatrixCalculator(
                context,
                "RotationCalculator",
                self,
                hoaOrder,
                dynamicOrientation=headTracking,
                initialOrientation=headOrientation)

            rotationMatrixRoutings = rbbl.SparseGainRoutingList()
            for oIdx in range(hoaOrder + 1):
                entryStart = (oIdx * (2 * oIdx - 1) * (2 * oIdx + 1)) // 3
                diagStart = oIdx**2
                for rowIdx in range(2 * oIdx + 1):
                    row = diagStart + rowIdx
                    colsPerRow = 2 * oIdx + 1
                    for colIdx in range(2 * oIdx + 1):
                        col = diagStart + colIdx
                        entryIdx = entryStart + rowIdx * colsPerRow + colIdx
                        rotationMatrixRoutings.addRouting(
                            entryIdx, row, col, 0.0)

            self.rotationMatrix = rcl.SparseGainMatrix(
                context,
                "rotationMatrix",
                self,
                numberOfInputs=numHoaCoeffs,
                numberOfOutputs=numHoaCoeffs,
                interpolationSteps=interpolationSteps,
                maxRoutingPoints=numMatrixCoeffs,
                initialRoutings=rotationMatrixRoutings,
                controlInputs=rcl.SparseGainMatrix.ControlPortConfig.Gain)
            self.audioConnection(self.hoaSignalInput,
                                 self.rotationMatrix.audioPort("in"))
            self.audioConnection(self.rotationMatrix.audioPort("out"),
                                 self.binauralFilterBank.audioPort("in"))
            self.parameterConnection(
                self.rotationCalculator.parameterPort("coefficients"),
                self.rotationMatrix.parameterPort("gainInput"))

            if headTracking:
                self.trackingInput = visr.ParameterInput(
                    "tracking", self, pml.ListenerPosition.staticType,
                    pml.DoubleBufferingProtocol.staticType,
                    pml.EmptyParameterConfig())
                self.parameterConnection(
                    self.trackingInput,
                    self.rotationCalculator.parameterPort("orientation"))
        else:
            self.audioConnection(self.hoaSignalInput,
                                 self.binauralFilterbank.audioPort("in"))

        self.audioConnection(self.binauralFilterBank.audioPort("out"),
                             self.binauralOutput)
    def __init__( self,
                 context, name, parent,
                 *,
                 numberOfObjects,
                 sofaFile = None,
                 hrirPositions = None,
                 hrirData = None,
                 hrirDelays = None,
                 headOrientation = None,
                 dynamicITD = False,
                 dynamicILD = False,
                 hrirInterpolation = False,
                 filterCrossfading = False,
                 fftImplementation = "default",
                 headTrackingReceiver = None,
                 headTrackingPositionalArguments = None,
                 headTrackingKeywordArguments = None,
                 sceneReceiveUdpPort = None
                 ):
        """
        Constructor.

        Parameters
        ----------
        context : visr.SignalFlowContext
            Standard visr.Component construction argument, holds the block size and the sampling frequency
        name : string
            Name of the component, Standard visr.Component construction argument
        parent : visr.CompositeComponent
            Containing component if there is one, None if this is a top-level component of the signal flow.
        numberOfObjects: int
            Maximum number of audio objects
        sofaFile: str, optional
            Optional SOFA for loading loaded the HRIR and associated data (HRIR measurement positions and delays)
            If not provided, the information must be provided by the hrirPositions and hrirData arguments.
        hrirPositions: numpy.ndarray, optional
            Optional way to provide the measurement grid for the BRIR listener view directions.
            If a SOFA file is provided, this is optional and overrides the listener view data
            in the file. Otherwise this argument is mandatory.
            Dimension #grid directions x (dimension of position argument)
        hrirData: numpy.ndarray, optional
            Optional way to provide the BRIR data.
            Dimension: #grid directions  x #ears (2) # x #loudspeakers x #ir length
        hrirDelays: numpy.ndarray, optional
            Optional BRIR delays. If a SOFA file is given, this  argument overrides
            a potential delay setting from the file. Otherwise, no extra delays
            are applied unless this option is provided.
            Dimension: #grid directions  x #ears(2) x # loudspeakers
        headOrientation: array-like, optional
            Head orientation in spherical coordinates (2- or 3-element vector or list).
            Either a static orientation (when no tracking is used), or the
            initial view direction
        dynamicITD: bool, optional
            Whether the ITD is applied separately. That requires preprocessed HRIR data
        dynamicILD: bool, optional
            Whether the ILD is computed and applied separately. At the moment this feature is not used (apart from applying the object gains)
        hrirInterpolation: bool, optional
            Whether the controller supports interpolation between neighbouring HRTF grid
            points. False means nearest neighbour (no interpolation), True
            enables barycentric interpolation.
        filterCrossfading: bool, optional
            Use a crossfading FIR filter matrix to avoid switching artifacts.
        fftImplementation: string, optional
            The FFT implementation to use. Default value enables VISR's default
            FFT library for the platform.
        headTrackingReceiver: class type, optional
            Class of the head tracking recveiver, None (default value) disables dynamic head tracking.
        headTrackingPositionalArguments: tuple optional
            Positional arguments passed to the constructor of the head tracking receiver object.
            Must be a tuple. If there is only a single argument, a trailing comma must be added.
        headTrackingKeywordArguments: dict, optional
            Keyword arguments passed to the constructor of the head tracking receiver. Must be a dictionary (dict)
        sceneReceiveUdpPort: int, optional
            A UDP port number where scene object metadata (in the S3A JSON format) is to be received).
            If not given (default), no network receiver is instantiated, and the object exposes a
            top-level parameter input port "objectVectorInput"
        """
        super( RealtimeDynamicHrirRenderer, self ).__init__( context, name, parent )
        self.objectSignalInput = visr.AudioInputFloat( "audioIn", self, numberOfObjects )
        self.binauralOutput = visr.AudioOutputFloat( "audioOut", self, 2 )

        enableTracking = (headTrackingReceiver is not None)

        # Handle loading of HRIR data from either a SOFA file or the the matrix arguments.
        if (hrirData is not None) == (sofaFile is not None):
            raise ValueError( "Exactly one of the arguments sofaFile and hrirData must be present." )
        if sofaFile is not None:
            [ sofaHrirPositions, hrirData, sofaHrirDelays ] = readSofaFile( sofaFile )
            # If hrirDelays is not provided as an argument, use the one retrieved from the SOFA file
            if hrirDelays is None:
                hrirDelays = sofaHrirDelays
            # Use the positions obtained from the SOFA file only if the argument is not set
            if hrirPositions is None:
                hrirPositions = sofaHrirPositions

        self.dynamicHrirRenderer = DynamicHrirRenderer( context, "DynamicBinauralRenderer", self,
                                                       numberOfObjects = numberOfObjects,
                                                       hrirPositions = hrirPositions,
                                                       hrirData = hrirData,
                                                       hrirDelays = hrirDelays,
                                                       headOrientation = headOrientation,
                                                       headTracking = enableTracking,
                                                       dynamicITD = dynamicITD,
                                                       dynamicILD = dynamicILD,
                                                       hrirInterpolation = hrirInterpolation,
                                                       filterCrossfading = filterCrossfading,
                                                       fftImplementation = fftImplementation
                                                       )

        if sceneReceiveUdpPort is None:
            self.objectVectorInput = visr.ParameterInput( "objectVector", self, pml.ObjectVector.staticType,
                                                         pml.DoubleBufferingProtocol.staticType,
                                                         pml.EmptyParameterConfig() )
            self.parameterConnection( self.objectVectorInput,
                                     self.dynamicHrirRenderer.parameterPort("objectVector"))

        else:
            self.sceneReceiver = rcl.UdpReceiver( context, "SceneReceiver", self,
                                                 port = int(sceneReceiveUdpPort) )
            self.sceneDecoder = rcl.SceneDecoder( context, "SceneDecoder", self )
            self.parameterConnection( self.sceneReceiver.parameterPort("messageOutput"),
                                 self.sceneDecoder.parameterPort("datagramInput") )
            self.parameterConnection( self.sceneDecoder.parameterPort( "objectVectorOutput"),
                                 self.dynamicHrirRenderer.parameterPort("objectVector"))
        if enableTracking:
            if headTrackingPositionalArguments == None:
                headTrackingPositionalArguments = ()
            if headTrackingKeywordArguments == None:
                headTrackingKeywordArguments = {}
            self.trackingDevice = headTrackingReceiver(context, "HeadTrackingReceiver", self,
                                                *headTrackingPositionalArguments,
                                                **headTrackingKeywordArguments )
            self.parameterConnection( self.trackingDevice.parameterPort("orientation"), self.dynamicHrirRenderer.parameterPort("tracking"))

        self.audioConnection(  self.objectSignalInput, self.dynamicHrirRenderer.audioPort("audioIn"))
        self.audioConnection( self.dynamicHrirRenderer.audioPort("audioOut"), self.binauralOutput)
sofaFile = 'bbcrdlr_systemD.sofa' # 4+5+0 setup
#sofaFile = 'bbcrdlr_systemE.sofa' # 4+5+1 setup
#sofaFile = 'bbcrdlr_systemF.sofa' # 3+7+0 setup
#sofaFile = 'bbcrdlr_systemG.sofa' # 4+9+0 setup
#sofaFile = 'bbcrdlr_systemH.sofa' # 9+10+3 setup
#sofaFile = 'bbcrdlr_reduced_onsets.sofa' # Full 32-loudspeaker surround setup

fullSofaPath = os.path.join( sofaDirectory, sofaFile )
if not os.path.exists( fullSofaPath ):
    sofaDir = os.path.split( fullSofaPath )[0]
    if not os.path.exists( sofaDir ):
        os.makedirs( sofaDir )
    urlretrieve( 'http://data.bbcarp.org.uk/bbcrd-brirs/sofa/' + sofaFile,
                fullSofaPath )

hrirPos, hrirData, hrirDelays = readSofaFile( fullSofaPath,
                                              truncationLength = BRIRtruncationLength )

# Safety check to ensure that the dynamicITD is only used with datasets that contain extracted initial delays.
if useDynamicITD and (hrirDelays is None or len(hrirDelays.shape) != 3 ):
    raise ValueError( "The option 'useDynamicITD' must be used only with BRIR datasets that provide a full delay dataset in 'Data.Delay'." )

# Crude check for 'horizontal-only' listener view directions
if np.max( np.abs(hrirPos[:,1])) < deg2rad( 1 ):
    hrirPos = hrirPos[ :, [0,2] ] # transform to polar coordinates

# Dimension of hrirData is #measurement positions x #ears x # lsp x ir length
numLoudspeakers = hrirData.shape[2]

context = visr.SignalFlowContext( period=blockSize, samplingFrequency=fs)

renderer = VirtualLoudspeakerRenderer( context, "VirtualLoudspeakerRenderer", None,
    def __init__(self,
                 context,
                 name,
                 parent,
                 numberOfObjects,
                 maxHoaOrder=None,
                 sofaFile=None,
                 decodingFilters=None,
                 interpolationSteps=None,
                 headOrientation=None,
                 headTracking=True,
                 objectChannelAllocation=False,
                 fftImplementation='default'):
        """
        Constructor.

        Parameters
        ----------
        context : visr.SignalFlowContext
            Standard visr.Component construction argument, holds the block size and the sampling frequency
        name : string
            Name of the component, Standard visr.Component construction argument
        parent : visr.CompositeComponent
            Containing component if there is one, None if this is a top-level component of the signal flow.
        numberOfObjects : int
            The number of audio objects to be rendered.
        maxHoaOrder: int or None
            The maximum HOA order that can be reproduced. If None, the HOA order is deduced
            from the first dimension of the HOA filters (possibly contained in a SOFA file).
        sofaFile: string or NoneType
        decodingFilters : numpy.ndarray or NoneType
            Alternative way to provide the HOA decoding filters.
        interpolationSteps: int
        headOrientation : array-like
            Head orientation in spherical coordinates (2- or 3-element vector or list). Either a static orientation (when no tracking is used),
            or the initial view direction
        headTracking: bool
            Whether dynamic head tracking is active.
        objectChannelAllocation: bool
            Whether the processing resources are allocated from a pool of resources
            (True), or whether fixed processing resources statically tied to the audio signal channels are used.
            Not implemented at the moment, so leave the default value (False).
        fftImplementation: string, optional
            The FFT library to be used in the filtering. THe default uses VISR's
            default implementation for the present platform.
        """
        if (decodingFilters is None) == (sofaFile is None):
            raise ValueError(
                "HoaObjectToBinauralRenderer: Either 'decodingFilters' or 'sofaFile' must be provided."
            )
        if sofaFile is None:
            filters = decodingFilters
        else:
            # pos and delays are not used here.
            [pos, filters, delays] = readSofaFile(sofaFile)

        if maxHoaOrder is None:
            numHoaCoeffs = filters.shape[0]
            orderP1 = int(np.floor(np.sqrt(numHoaCoeffs)))
            if orderP1**2 != numHoaCoeffs:
                raise ValueError(
                    "If maxHoaOrder is not given, the number of HOA filters must be a square number"
                )
            maxHoaOrder = orderP1 - 1
        else:
            numHoaCoeffs = (maxHoaOrder + 1)**2

        if filters.ndim != 3 or filters.shape[1] != 2 or filters.shape[
                0] < numHoaCoeffs:
            raise ValueError(
                "HoaObjectToBinauralRenderer: the filter data must be a 3D matrix where the second dimension is 2 and the first dimension is equal or larger than (maxHoaOrder+1)^2."
            )

        super(HoaObjectToBinauralRenderer,
              self).__init__(context, name, parent)
        self.objectSignalInput = visr.AudioInputFloat("audioIn", self,
                                                      numberOfObjects)
        self.binauralOutput = visr.AudioOutputFloat("audioOut", self, 2)
        self.objectVectorInput = visr.ParameterInput(
            "objectVector", self, pml.ObjectVector.staticType,
            pml.DoubleBufferingProtocol.staticType, pml.EmptyParameterConfig())

        if interpolationSteps is None:
            interpolationSteps = context.period

        self.objectEncoder = HoaObjectEncoder(
            context,
            'HoaEncoder',
            self,
            numberOfObjects=numberOfObjects,
            hoaOrder=maxHoaOrder,
            channelAllocation=objectChannelAllocation)

        self.parameterConnection(
            self.objectVectorInput,
            self.objectEncoder.parameterPort("objectVector"))

        self.encoderMatrix = rcl.GainMatrix(
            context,
            "encoderMatrix",
            self,
            numberOfInputs=numberOfObjects,
            numberOfOutputs=(maxHoaOrder + 1)**2,
            interpolationSteps=interpolationSteps,
            initialGains=0.0,
            controlInput=True)
        self.audioConnection(self.objectSignalInput,
                             self.encoderMatrix.audioPort("in"))

        filterMtx = np.concatenate(
            (filters[0:numHoaCoeffs, 0, :], filters[0:numHoaCoeffs, 1, :]))

        routings = rbbl.FilterRoutingList()
        for idx in range(0, numHoaCoeffs):
            routings.addRouting(idx, 0, idx, 1.0)
            routings.addRouting(idx, 1, idx + numHoaCoeffs, 1.0)

        self.binauralFilterBank = rcl.FirFilterMatrix(
            context,
            'binauralFilterBank',
            self,
            numberOfInputs=numHoaCoeffs,
            numberOfOutputs=2,
            filterLength=filters.shape[-1],
            maxFilters=2 * numHoaCoeffs,
            maxRoutings=2 * numHoaCoeffs,
            filters=filterMtx,
            routings=routings,
            controlInputs=rcl.FirFilterMatrix.ControlPortConfig.NoInputs,
            fftImplementation=fftImplementation)

        self.audioConnection(self.encoderMatrix.audioPort("out"),
                             self.binauralFilterBank.audioPort("in"))
        self.audioConnection(self.binauralFilterBank.audioPort("out"),
                             self.binauralOutput)

        if headTracking:
            self.trackingInput = visr.ParameterInput(
                "tracking", self, pml.ListenerPosition.staticType,
                pml.DoubleBufferingProtocol.staticType,
                pml.EmptyParameterConfig())
            self.coefficientRotator = HoaCoefficientRotation(
                context,
                'coefficientRotator',
                self,
                numberOfObjects=numberOfObjects,
                hoaOrder=maxHoaOrder)
            self.parameterConnection(
                self.trackingInput,
                self.coefficientRotator.parameterPort("tracking"))
            self.parameterConnection(
                self.objectEncoder.parameterPort("coefficientOutput"),
                self.coefficientRotator.parameterPort("coefficientInput"))
            self.parameterConnection(
                self.coefficientRotator.parameterPort("coefficientOutput"),
                self.encoderMatrix.parameterPort("gainInput"))
        else:
            self.parameterConnection(
                self.objectEncoder.parameterPort("coefficientOutput"),
                self.encoderMatrix.parameterPort("gainInput"))