Ejemplo n.º 1
0
    def __init__(self,
                 ugens,
                 name=None,
                 optimize=True,
                 parameter_names=None,
                 **kwargs):
        import supriya.synthdefs
        import supriya.ugens

        ServerObjectProxy.__init__(self)
        compiler = supriya.synthdefs.SynthDefCompiler
        self._name = name
        ugens = list(copy.deepcopy(ugens))
        assert all(isinstance(_, supriya.ugens.UGen) for _ in ugens)
        ugens = self._cleanup_pv_chains(ugens)
        ugens = self._cleanup_local_bufs(ugens)
        if optimize:
            ugens = self._optimize_ugen_graph(ugens)
        ugens = self._sort_ugens_topologically(ugens)
        self._ugens = tuple(ugens)
        self._constants = self._collect_constants(self._ugens)
        self._control_ugens = self._collect_control_ugens(self._ugens)
        self._indexed_parameters = self._collect_indexed_parameters(
            self._control_ugens, parameter_names=parameter_names)
        self._compiled_ugen_graph = compiler.compile_ugen_graph(self)
Ejemplo n.º 2
0
 def _register_with_local_server(
     self,
     server=None,
 ):
     ServerObjectProxy.allocate(self, server=server)
     synthdef_name = self.actual_name
     self.server._synthdefs[synthdef_name] = self
Ejemplo n.º 3
0
 def __init__(
     self,
     bus_group_or_index=None,
     calculation_rate=CalculationRate.CONTROL,
 ):
     import supriya.realtime
     ServerObjectProxy.__init__(self)
     bus_group = None
     bus_id = None
     self._bus_id_was_set_manually = False
     if bus_group_or_index is not None:
         self._bus_id_was_set_manually = True
         if isinstance(bus_group_or_index, supriya.realtime.BusGroup):
             bus_group = bus_group_or_index
         elif isinstance(bus_group_or_index, int):
             bus_id = int(bus_group_or_index)
     self._bus_group = bus_group
     self._bus_id = bus_id
     if calculation_rate is None:
         calculation_rate = 'control'
     calculation_rate = CalculationRate.from_expr(calculation_rate)
     assert calculation_rate in (
         CalculationRate.AUDIO,
         CalculationRate.CONTROL,
     )
     self._calculation_rate = calculation_rate
Ejemplo n.º 4
0
 def __init__(
     self,
     bus_count=1,
     calculation_rate=None,
     bus_id=None,
     ):
     import supriya.realtime
     import supriya.synthdefs
     ServerObjectProxy.__init__(self)
     calculation_rate = supriya.synthdefs.CalculationRate.from_expr(
         calculation_rate)
     assert calculation_rate in (
         supriya.synthdefs.CalculationRate.AUDIO,
         supriya.synthdefs.CalculationRate.CONTROL,
         )
     self._calculation_rate = calculation_rate
     bus_count = int(bus_count)
     assert 0 < bus_count
     self._buses = tuple(
         supriya.realtime.Bus(
             bus_group_or_index=self,
             calculation_rate=self.calculation_rate,
             )
         for _ in range(bus_count)
         )
     assert isinstance(bus_id, (type(None), int))
     self._bus_id = bus_id
Ejemplo n.º 5
0
 def _allocate_buffer_id(self):
     if self.buffer_id is None:
         buffer_id = self.server.buffer_allocator.allocate(1)
         if buffer_id is None:
             ServerObjectProxy.free(self)
             raise ValueError
         self._buffer_id = buffer_id
Ejemplo n.º 6
0
 def free(self):
     import supriya.commands
     synthdef_name = self.actual_name
     del (self.server._synthdefs[synthdef_name])
     request = supriya.commands.SynthDefFreeRequest(synthdef=self, )
     if self.server.is_running:
         request.communicate(server=self.server)
     ServerObjectProxy.free(self)
Ejemplo n.º 7
0
 def __init__(self, buffer_count=1):
     import supriya.realtime
     ServerObjectProxy.__init__(self)
     self._buffer_id = None
     buffer_count = int(buffer_count)
     assert 0 < buffer_count
     self._buffers = tuple(
         supriya.realtime.Buffer(buffer_group_or_index=self)
         for _ in range(buffer_count))
Ejemplo n.º 8
0
 def free(self):
     if not self.is_allocated:
         raise supriya.exceptions.BusNotAllocated
     if not self._bus_id_was_set_manually:
         allocator = self._get_allocator(
             calculation_rate=self.calculation_rate, server=self.server)
         allocator.free(self.bus_id)
     self._bus_id = None
     ServerObjectProxy.free(self)
     return self
Ejemplo n.º 9
0
 def _register_with_local_server(self, server):
     ServerObjectProxy.allocate(self, server=server)
     allocator = self.server.buffer_allocator
     buffer_id = allocator.allocate(len(self))
     if buffer_id is None:
         ServerObjectProxy.free(self)
         raise ValueError
     self._buffer_id = buffer_id
     for buffer_ in self:
         buffer_._register_with_local_server()
     return buffer_id
Ejemplo n.º 10
0
 def free(self):
     import supriya.realtime
     if not self.is_allocated:
         return
     allocator = supriya.realtime.Bus._get_allocator(
         calculation_rate=self.calculation_rate,
         server=self.server,
         )
     allocator.free(self.bus_id)
     self._bus_id = None
     ServerObjectProxy.free(self)
Ejemplo n.º 11
0
 def free(self):
     if not self.is_allocated:
         return
     if not self._bus_id_was_set_manually:
         allocator = self._get_allocator(
             calculation_rate=self.calculation_rate,
             server=self.server,
         )
         allocator.free(self.bus_id)
     self._bus_id = None
     ServerObjectProxy.free(self)
Ejemplo n.º 12
0
 def _unregister_with_local_server(self):
     node_id = self.node_id
     if self.server is not None:
         if self._node_id in self._server._nodes:
             del (self._server._nodes[self._node_id])
         if self.node_id_is_permanent:
             self.server.node_id_allocator.free_permanent_node_id(
                 self.node_id, )
     self._node_id = None
     self._node_id_is_permanent = None
     ServerObjectProxy.free(self)
     return node_id
Ejemplo n.º 13
0
 def free(self) -> 'BufferGroup':
     """
     Frees all buffers in buffer group.
     """
     if not self.is_allocated:
         raise supriya.exceptions.BufferNotAllocated
     for buffer_ in self:
         buffer_.free()
     buffer_id = self.buffer_id
     self._buffer_id = None
     self.server.buffer_allocator.free(buffer_id)
     ServerObjectProxy.free(self)
     return self
Ejemplo n.º 14
0
    def allocate(self, server=None):
        import supriya.realtime

        if self.is_allocated:
            raise supriya.exceptions.BusAlreadyAllocated
        ServerObjectProxy.allocate(self, server=server)
        allocator = supriya.realtime.Bus._get_allocator(
            calculation_rate=self.calculation_rate, server=self.server)
        bus_id = allocator.allocate(len(self))
        if bus_id is None:
            ServerObjectProxy.free(self)
            raise ValueError
        self._bus_id = bus_id
        return self
Ejemplo n.º 15
0
    def free(self):
        """
        Frees all buffers in buffer group.

        Returns none.
        """
        if not self.is_allocated:
            return
        for buffer_ in self:
            buffer_.free()
        buffer_id = self.buffer_id
        self._buffer_id = None
        self.server.buffer_allocator.free(buffer_id)
        ServerObjectProxy.free(self)
Ejemplo n.º 16
0
 def free(self) -> "Buffer":
     """
     Frees buffer.
     """
     if not self.is_allocated:
         raise supriya.exceptions.BufferNotAllocated
     buffer_id = self._unregister_with_local_server()
     request = self._unregister_with_remote_server(buffer_id)
     if self.server.is_running:
         request.communicate(server=self.server, sync=False)
     if not self._buffer_id_was_set_manually:
         self.server.buffer_allocator.free(self.buffer_id)
     self._buffer_id = None
     ServerObjectProxy.free(self)
     return self
Ejemplo n.º 17
0
    def __init__(self, buffer_group_or_index=None):
        import supriya.realtime

        ServerObjectProxy.__init__(self)
        buffer_group = None
        buffer_id = None
        self._buffer_id_was_set_manually = False
        if buffer_group_or_index is not None:
            self._buffer_id_was_set_manually = True
            if isinstance(buffer_group_or_index, supriya.realtime.BufferGroup):
                buffer_group = buffer_group_or_index
            elif isinstance(buffer_group_or_index, int):
                buffer_id = int(buffer_group_or_index)
        self._buffer_group = buffer_group
        self._buffer_id = buffer_id
Ejemplo n.º 18
0
 def _register_with_local_server(self,
                                 node_id=None,
                                 node_id_is_permanent=False,
                                 server=None):
     id_allocator = server.node_id_allocator
     if node_id is None:
         if node_id_is_permanent:
             node_id = id_allocator.allocate_permanent_node_id()
         else:
             node_id = server.node_id_allocator.allocate_node_id()
     else:
         node_id = int(node_id)
     ServerObjectProxy.allocate(self, server=server)
     self._node_id = node_id
     self._node_id_is_permanent = bool(node_id_is_permanent)
     self._server._nodes[self._node_id] = self
     return node_id
Ejemplo n.º 19
0
 def allocate(self, server=None, sync=False):
     if self.bus_group is not None:
         return
     if self.is_allocated:
         raise supriya.exceptions.BusAlreadyAllocated
     ServerObjectProxy.allocate(self, server=server)
     if self.bus_id is None:
         allocator = self._get_allocator(
             calculation_rate=self.calculation_rate, server=self.server)
         bus_id = allocator.allocate(1)
         if bus_id is None:
             ServerObjectProxy.free(self)
             raise ValueError
         self._bus_id = bus_id
     if sync:
         self.server.sync()
     return self
Ejemplo n.º 20
0
    def free(self):
        """
        Frees buffer.

        Returns none.
        """
        if not self.is_allocated:
            return
        buffer_id = self._unregister_with_local_server()
        request = self._unregister_with_remote_server(buffer_id)
        if self.server.is_running:
            request.communicate(
                server=self.server,
                sync=False,
            )
        if not self._buffer_id_was_set_manually:
            self.server.buffer_allocator.free(self.buffer_id)
        self._buffer_id = None
        ServerObjectProxy.free(self)
Ejemplo n.º 21
0
 def allocate(
     self,
     server=None,
     sync=False,
     ):
     import supriya.realtime
     if self.is_allocated:
         return
     ServerObjectProxy.allocate(self, server=server)
     allocator = supriya.realtime.Bus._get_allocator(
         calculation_rate=self.calculation_rate,
         server=self.server,
         )
     bus_id = allocator.allocate(len(self))
     if bus_id is None:
         ServerObjectProxy.free(self)
         raise ValueError
     self._bus_id = bus_id
     if sync:
         self.server.sync()
     return self
Ejemplo n.º 22
0
 def _register_with_local_server(
     self,
     node_id_is_permanent=False,
     server=None,
     ):
     if server is None or not server.is_running:
         raise ValueError(self)
     if self.server is not None or self in server._nodes:
         return
     id_allocator = server.node_id_allocator
     if node_id_is_permanent:
         node_id = id_allocator.allocate_permanent_node_id()
     else:
         node_id = server.node_id_allocator.allocate_node_id()
     if node_id is None:
         raise ValueError(self)
     elif node_id in server._nodes:
         raise ValueError(self)
     ServerObjectProxy.allocate(self, server=server)
     self._node_id = node_id
     self._node_id_is_permanent = bool(node_id_is_permanent)
     self._server._nodes[self._node_id] = self
     return node_id
Ejemplo n.º 23
0
 def __init__(self, name=None, node_id_is_permanent=False):
     ServerObjectProxy.__init__(self)
     UniqueTreeNode.__init__(self, name=name)
     self._is_paused = False
     self._node_id = None
     self._node_id_is_permanent = bool(node_id_is_permanent)
Ejemplo n.º 24
0
    def allocate(
        self,
        channel_count=1,
        frame_count=1,
        server=None,
        sync=True,
    ):
        """
        Allocates buffer on `server`.

        ::

            >>> server = supriya.realtime.Server().boot()
            >>> buffer_one = supriya.realtime.Buffer().allocate()
            >>> buffer_one.query()
            BufferInfoResponse(
                buffer_id=0,
                channel_count=1,
                frame_count=1,
                sample_rate=44100.0,
                )

        ::

            >>> buffer_two = supriya.realtime.Buffer().allocate(
            ...     frame_count=16,
            ...     )
            >>> buffer_two.query()
            BufferInfoResponse(
                buffer_id=1,
                channel_count=1,
                frame_count=16,
                sample_rate=44100.0,
                )

        ::

            >>> buffer_three = supriya.realtime.Buffer().allocate(
            ...     channel_count=2,
            ...     frame_count=32,
            ...     )
            >>> buffer_three.query()
            BufferInfoResponse(
                buffer_id=2,
                channel_count=2,
                frame_count=32,
                sample_rate=44100.0,
                )

        ::

            >>> buffer_one.free()
            >>> buffer_two.free()
            >>> buffer_three.free()

        Returns buffer.
        """
        if self.buffer_group is not None:
            return
        if self.is_allocated:
            return
        try:
            ServerObjectProxy.allocate(
                self,
                server=server,
            )
            channel_count = int(channel_count)
            frame_count = int(frame_count)
            assert 0 < channel_count
            assert 0 < frame_count
            self._allocate_buffer_id()
            self._register_with_local_server()
            request = self._register_with_remote_server(
                channel_count=channel_count,
                frame_count=frame_count,
            )
            request.communicate(
                server=self.server,
                sync=sync,
            )
        except:
            self.free()
        return self
Ejemplo n.º 25
0
    def allocate(self, channel_count=1, frame_count=1, server=None, sync=True):
        """
        Allocates buffer on `server`.

        ::

            >>> server = supriya.realtime.Server().boot()
            >>> buffer_one = supriya.realtime.Buffer().allocate()
            >>> buffer_one.query()
            BufferInfoResponse(
                items=(
                    Item(buffer_id=0, frame_count=1, channel_count=1, sample_rate=44100.0),
                    ),
                )

        ::

            >>> buffer_two = supriya.realtime.Buffer().allocate(
            ...     frame_count=16,
            ...     )
            >>> buffer_two.query()
            BufferInfoResponse(
                items=(
                    Item(buffer_id=1, frame_count=16, channel_count=1, sample_rate=44100.0),
                    ),
                )

        ::

            >>> buffer_three = supriya.realtime.Buffer().allocate(
            ...     channel_count=2,
            ...     frame_count=32,
            ...     )
            >>> buffer_three.query()
            BufferInfoResponse(
                items=(
                    Item(buffer_id=2, frame_count=32, channel_count=2, sample_rate=44100.0),
                    ),
                )

        ::

            >>> buffer_three.allocate()
            Traceback (most recent call last):
            ...
            supriya.exceptions.BufferAlreadyAllocated

        ::

            >>> buffer_one = buffer_one.free()
            >>> buffer_two = buffer_two.free()
            >>> buffer_three = buffer_three.free()

        Returns buffer.
        """
        if self.is_allocated:
            raise supriya.exceptions.BufferAlreadyAllocated
        try:
            ServerObjectProxy.allocate(self, server=server)
            channel_count = int(channel_count)
            frame_count = int(frame_count)
            assert 0 < channel_count
            assert 0 < frame_count
            self._allocate_buffer_id()
            self._register_with_local_server()
            request = self._register_with_remote_server(
                channel_count=channel_count, frame_count=frame_count)
            request.communicate(server=self.server, sync=sync)
        except Exception:
            self.free()
        return self
Ejemplo n.º 26
0
    def allocate_from_file(
        self,
        file_path,
        channel_indices=None,
        callback=None,
        frame_count=None,
        server=None,
        starting_frame=None,
        sync=True,
    ):
        """
        Allocates buffer on `server` with contents read from `file_path`.

        ::

            >>> server = supriya.realtime.Server().boot()
            >>> buffer_one = supriya.realtime.Buffer().allocate_from_file(
            ...     supriya.system.Assets['audio/pulse_44100sr_16bit_octo.wav'],
            ...     )
            >>> buffer_one.query()
            BufferInfoResponse(
                items=(
                    Item(buffer_id=0, frame_count=8, channel_count=8, sample_rate=44100.0),
                    ),
                )

        ::

            >>> buffer_two = supriya.realtime.Buffer().allocate_from_file(
            ...     supriya.system.Assets['audio/pulse_44100sr_16bit_octo.wav'],
            ...     channel_indices=(3, 4),
            ...     frame_count=4,
            ...     starting_frame=1,
            ...     sync=True,
            ...     )
            >>> buffer_two.query()
            BufferInfoResponse(
                items=(
                    Item(buffer_id=1, frame_count=4, channel_count=2, sample_rate=44100.0),
                    ),
                )

        ::

            >>> for frame_id in range(buffer_two.frame_count):
            ...     buffer_two.get_frame(frame_id).as_dict()
            ...
            OrderedDict([(0, (0.0, 0.0))])
            OrderedDict([(2, (0.0, 0.0))])
            OrderedDict([(4, (0.999969482421875, 0.0))])
            OrderedDict([(6, (0.0, 0.999969482421875))])

        ::

            >>> buffer_two.allocate()
            Traceback (most recent call last):
            ...
            supriya.exceptions.BufferAlreadyAllocated

        ::

            >>> buffer_one = buffer_one.free()
            >>> buffer_two = buffer_two.free()

        Returns buffer.
        """
        if self.is_allocated:
            raise supriya.exceptions.BufferAlreadyAllocated
        try:
            ServerObjectProxy.allocate(self, server=server)
            self._allocate_buffer_id()
            self._register_with_local_server()
            request = self._register_with_remote_server(
                frame_count=frame_count,
                channel_indices=channel_indices,
                file_path=file_path,
                starting_frame=starting_frame,
            )
            request.communicate(server=self.server, sync=sync)
        except Exception:
            ServerObjectProxy.allocate(self, server=server)
        return self