Skip to content

Commit

Permalink
SUPRIYA-352: Add UGen Bitwise Operators (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
josiah-wolf-oberholtzer authored Jan 14, 2024
1 parent 6a12db6 commit 9f5a191
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
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

0 comments on commit 9f5a191

Please sign in to comment.