From 79923db52ff81ea1514cce0958ce08060e21b0c1 Mon Sep 17 00:00:00 2001 From: Semion Sidorenko Date: Fri, 3 Nov 2023 17:23:27 +0100 Subject: [PATCH] Add keepdot and filtdot operators --- .../sardine/filter_functions.md | 32 ++++++++++++++++++- .../sequences/sardine_parser/funclib.py | 6 ++++ .../sequences/sardine_parser/tree_calc.py | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/sardine_doc/src/pattern_languages/sardine/filter_functions.md b/docs/sardine_doc/src/pattern_languages/sardine/filter_functions.md index 73e4d6d7..e07a3d0d 100644 --- a/docs/sardine_doc/src/pattern_languages/sardine/filter_functions.md +++ b/docs/sardine_doc/src/pattern_languages/sardine/filter_functions.md @@ -2,7 +2,7 @@ There is a group of functions used for filtering: removing events, filtering values, etc... These functions are very low-level but they can be really interesting for specific use cases. -## mask +## mask Apply a boolean mask on values from the collection. True values will return the value itself, others will return a silence. This function is used by others for many different operations. @@ -43,3 +43,33 @@ This function can be used to filter some values from a pattern. Imagine generati # Removing 2 and 4 from the generated ramp Pa * d('hat', p=.25, speed='(filter [0:10] [2 4])') ``` + + +## filtdot + +This function can be used to filter out some values from a pattern, replacing them with silences (.). + +**Arguments:** +- **collection:** the collection you would like to filter. +- **filter:** a list containing the elements to filter + +**Example:** +```python +# Replacing 2 and 4 By silences +Pa * d('hat', p=.25, speed='(filtdot [0:10] [2 4])') # Equivalent to speed='0 1 . 3 . 5 6 7 8 9 10' +``` + + +## keepdot + +This function does the opposite of filtdot: only the values that are in the given filter are kept. + +**Arguments:** +- **collection:** the collection you would like to filter. +- **filter:** a list containing the elements to keep + +**Example:** +```python +# Replacing 2 and 4 By silences +Pa * d('hat', p=.25, speed='(keepdot [0:10] [2 4])') # Equivalent to speed='. . 2 . 4 . . . . . .' +``` diff --git a/sardine_core/sequences/sardine_parser/funclib.py b/sardine_core/sequences/sardine_parser/funclib.py index 96a47e11..ffad5a2e 100644 --- a/sardine_core/sequences/sardine_parser/funclib.py +++ b/sardine_core/sequences/sardine_parser/funclib.py @@ -505,6 +505,12 @@ def notdot(self, collection: list) -> list: """Invert a rhythm pattern (1 -> ., . -> 1)""" return [1 if x == None else None for x in collection] + def filtdot(self, collection: list, filter_: list) -> list: + return [x if x not in filter_ else None for x in collection] + + def keepdot(self, collection: list, keep_: list) -> list: + return [x if x in keep_ else None for x in collection] + def clamp( self, collection: list, diff --git a/sardine_core/sequences/sardine_parser/tree_calc.py b/sardine_core/sequences/sardine_parser/tree_calc.py index 98e0b2d5..186be044 100644 --- a/sardine_core/sequences/sardine_parser/tree_calc.py +++ b/sardine_core/sequences/sardine_parser/tree_calc.py @@ -571,6 +571,8 @@ def function_call(self, func_name, *args): "neu": self.library.negative_euclidian_rhythm, "mask": self.library.mask, "notdot": self.library.notdot, + "filtdot": self.library.filtdot, + "keepdot": self.library.keepdot, "euclid": self.library.euclidian_to_number, "numclid": self.library.euclidian_to_number, "e": self.library.euclidian_to_number,