Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUPRIYA-352: Add UGen Bitwise Operators #354

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions supriya/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class BinaryOperator(IntEnumeration):
ADDITION = 0
AMCLIP = 40
ATAN2 = 22
BIT_AND = 14
BIT_OR = 15
BIT_XOR = 16
BITWISE_AND = 14
BITWISE_OR = 15
BITWISE_XOR = 16
CLIP2 = 42
DIFFERENCE_OF_SQUARES = 34 # a*a - b*b
EQUAL = 6
Expand Down
115 changes: 115 additions & 0 deletions supriya/ugens/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,45 @@ def __add__(
"""
return UGenMethodMixin._compute_binary_op(self, expr, BinaryOperator.ADDITION)

def __and__(
self, expr: Union[SupportsFloat, "UGenMethodMixin"]
) -> "UGenMethodMixin":
"""
Computes the bitwise AND of the UGen graph and `expr`.

.. container:: example

::

>>> ugen_graph = supriya.ugens.WhiteNoise.kr()
>>> expr = supriya.ugens.SinOsc.ar()
>>> result = ugen_graph & expr
>>> result
BinaryOpUGen.ar()

::

>>> supriya.graph(result) # doctest: +SKIP

::

>>> print(result)
synthdef:
name: 9a5b4d1212b6b7fe299c21a8b1e401cc
ugens:
- WhiteNoise.kr: null
- SinOsc.ar:
frequency: 440.0
phase: 0.0
- BinaryOpUGen(BITWISE_AND).ar:
left: WhiteNoise.kr[0]
right: SinOsc.ar[0]

"""
return UGenMethodMixin._compute_binary_op(
self, expr, BinaryOperator.BITWISE_AND
)

def __div__(
self, expr: Union[SupportsFloat, "UGenMethodMixin"]
) -> "UGenMethodMixin":
Expand Down Expand Up @@ -1341,6 +1380,43 @@ def __neg__(self) -> "UGenMethodMixin":
"""
return UGenMethodMixin._compute_unary_op(self, UnaryOperator.NEGATIVE)

def __or__(
self, expr: Union[SupportsFloat, "UGenMethodMixin"]
) -> "UGenMethodMixin":
"""
Computes the bitwise OR of the UGen graph and `expr`.

.. container:: example

::

>>> ugen_graph = supriya.ugens.WhiteNoise.kr()
>>> expr = supriya.ugens.SinOsc.ar()
>>> result = ugen_graph | expr
>>> result
BinaryOpUGen.ar()

::

>>> supriya.graph(result) # doctest: +SKIP

::

>>> print(result)
synthdef:
name: 333e2e7362f86138866f3f2a160f77dd
ugens:
- WhiteNoise.kr: null
- SinOsc.ar:
frequency: 440.0
phase: 0.0
- BinaryOpUGen(BITWISE_OR).ar:
left: WhiteNoise.kr[0]
right: SinOsc.ar[0]

"""
return UGenMethodMixin._compute_binary_op(self, expr, BinaryOperator.BITWISE_OR)

def __pow__(
self, expr: Union[SupportsFloat, "UGenMethodMixin"]
) -> "UGenMethodMixin":
Expand Down Expand Up @@ -2113,6 +2189,45 @@ def __sub__(
self, expr, BinaryOperator.SUBTRACTION
)

def __xor__(
self, expr: Union[SupportsFloat, "UGenMethodMixin"]
) -> "UGenMethodMixin":
"""
Computes the bitwise XOR of the UGen graph and `expr`.

.. container:: example

::

>>> ugen_graph = supriya.ugens.WhiteNoise.kr()
>>> expr = supriya.ugens.SinOsc.ar()
>>> result = ugen_graph ^ expr
>>> result
BinaryOpUGen.ar()

::

>>> supriya.graph(result) # doctest: +SKIP

::

>>> print(result)
synthdef:
name: 355f2c7fa510863b921bb8c28bc4a682
ugens:
- WhiteNoise.kr: null
- SinOsc.ar:
frequency: 440.0
phase: 0.0
- BinaryOpUGen(BITWISE_XOR).ar:
left: WhiteNoise.kr[0]
right: SinOsc.ar[0]

"""
return UGenMethodMixin._compute_binary_op(
self, expr, BinaryOperator.BITWISE_XOR
)

__truediv__ = __div__
__rtruediv__ = __rdiv__

Expand Down
Loading