77#include <stdint.h>
88
99#include "shared-bindings/audiofilters/Distortion.h"
10- #include "shared-module/audiofilters/Distortion.h"
1110
1211#include "shared/runtime/context_manager_helpers.h"
1312#include "py/binary.h"
13+ #include "py/enum.h"
1414#include "py/objproperty.h"
1515#include "py/runtime.h"
1616#include "shared-bindings/util.h"
1717#include "shared-module/synthio/block.h"
1818
19+ //| class DistortionMode:
20+ //| """The method of distortion used by the `audiofilters.Distortion` effect."""
21+ //|
22+ //| CLIP: DistortionMode
23+ //| """Digital distortion effect which cuts off peaks at the top and bottom of the waveform."""
24+ //|
25+ //| ATAN: DistortionMode
26+ //| """"""
27+ //|
28+ //| LOFI: DistortionMode
29+ //| """Low-resolution digital distortion effect (bit depth reduction). You can use it to emulate the sound of early digital audio devices."""
30+ //|
31+ //| OVERDRIVE: DistortionMode
32+ //| """Emulates the warm distortion produced by a field effect transistor, which is commonly used in solid-state musical instrument amplifiers. The `audiofilters.Distortion.drive` property has no effect in this mode."""
33+ //|
34+ //| WAVESHAPE: DistortionMode
35+ //| """Waveshaper distortions are used mainly by electronic musicians to achieve an extra-abrasive sound."""
36+ //|
37+
38+ MAKE_ENUM_VALUE (audiofilters_distortion_mode_type , distortion_mode , CLIP , DISTORTION_MODE_CLIP );
39+ MAKE_ENUM_VALUE (audiofilters_distortion_mode_type , distortion_mode , ATAN , DISTORTION_MODE_ATAN );
40+ MAKE_ENUM_VALUE (audiofilters_distortion_mode_type , distortion_mode , LOFI , DISTORTION_MODE_LOFI );
41+ MAKE_ENUM_VALUE (audiofilters_distortion_mode_type , distortion_mode , OVERDRIVE , DISTORTION_MODE_OVERDRIVE );
42+ MAKE_ENUM_VALUE (audiofilters_distortion_mode_type , distortion_mode , WAVESHAPE , DISTORTION_MODE_WAVESHAPE );
43+
44+ MAKE_ENUM_MAP (audiofilters_distortion_mode ) {
45+ MAKE_ENUM_MAP_ENTRY (distortion_mode , CLIP ),
46+ MAKE_ENUM_MAP_ENTRY (distortion_mode , ATAN ),
47+ MAKE_ENUM_MAP_ENTRY (distortion_mode , LOFI ),
48+ MAKE_ENUM_MAP_ENTRY (distortion_mode , OVERDRIVE ),
49+ MAKE_ENUM_MAP_ENTRY (distortion_mode , WAVESHAPE ),
50+ };
51+
52+ static MP_DEFINE_CONST_DICT (audiofilters_distortion_mode_locals_dict , audiofilters_distortion_mode_locals_table );
53+
54+ MAKE_PRINTER (audiofilters , audiofilters_distortion_mode );
55+
56+ MAKE_ENUM_TYPE (audiofilters , DistortionMode , audiofilters_distortion_mode );
57+
58+ static audiofilters_distortion_mode validate_distortion_mode (mp_obj_t obj , qstr arg_name ) {
59+ return cp_enum_value (& audiofilters_distortion_mode_type , obj , arg_name );
60+ }
61+
1962//| class Distortion:
2063//| """A Distortion effect"""
2164//|
71114//| synth.release(note)
72115//| time.sleep(5)"""
73116//| ...
117+
74118static mp_obj_t audiofilters_distortion_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
75119 enum { ARG_drive , ARG_pre_gain , ARG_post_gain , ARG_mode , ARG_mix , ARG_buffer_size , ARG_sample_rate , ARG_bits_per_sample , ARG_samples_signed , ARG_channel_count , };
76120 static const mp_arg_t allowed_args [] = {
77121 { MP_QSTR_drive , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_OBJ_NULL } },
78122 { MP_QSTR_pre_gain , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_OBJ_NULL } },
79123 { MP_QSTR_post_gain , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_OBJ_NULL } },
80- { MP_QSTR_mode , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_ROM_PTR (( void * ) & distortion_mode_CLIP_obj ) } },
124+ { MP_QSTR_mode , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_OBJ_NULL } },
81125 { MP_QSTR_mix , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = MP_OBJ_NULL } },
82126 { MP_QSTR_buffer_size , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 512 } },
83127 { MP_QSTR_sample_rate , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 8000 } },
@@ -96,11 +140,13 @@ static mp_obj_t audiofilters_distortion_make_new(const mp_obj_type_t *type, size
96140 mp_raise_ValueError (MP_ERROR_TEXT ("bits_per_sample must be 8 or 16" ));
97141 }
98142
99- audiofilters_distortion_mode_t mode = validate_distortion_mode (args [ARG_mode ].u_obj , MP_QSTR_mode );
143+ audiofilters_distortion_mode mode = DISTORTION_MODE_CLIP ;
144+ if (args [ARG_mode ].u_obj != MP_OBJ_NULL ) {
145+ mode = validate_distortion_mode (args [ARG_mode ].u_obj , MP_QSTR_mode );
146+ }
100147
101148 audiofilters_distortion_obj_t * self = mp_obj_malloc (audiofilters_distortion_obj_t , & audiofilters_distortion_type );
102149 common_hal_audiofilters_distortion_construct (self , args [ARG_drive ].u_obj , args [ARG_pre_gain ].u_obj , args [ARG_post_gain ].u_obj , mode , args [ARG_mix ].u_obj , args [ARG_buffer_size ].u_int , bits_per_sample , args [ARG_samples_signed ].u_bool , channel_count , sample_rate );
103-
104150 return MP_OBJ_FROM_PTR (self );
105151}
106152
@@ -236,7 +282,7 @@ static mp_obj_t audiofilters_distortion_obj_set_mode(size_t n_args, const mp_obj
236282 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
237283 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
238284
239- audiofilters_distortion_mode_t mode = validate_distortion_mode (args [ARG_mode ].u_obj , MP_QSTR_mode );
285+ audiofilters_distortion_mode mode = validate_distortion_mode (args [ARG_mode ].u_obj , MP_QSTR_mode );
240286 common_hal_audiofilters_distortion_set_mode (self , mode );
241287
242288 return mp_const_none ;
0 commit comments