Content-Length: 542088 | pFad | http://github.com/adafruit/circuitpython/commit/0410d2260125f5b97ddbc3fbb52db1d6ef81741c

3C Added `soft_clip` property to toggle between hard clipping (default) … · adafruit/circuitpython@0410d22 · GitHub
Skip to content

Commit 0410d22

Browse files
committedDec 11, 2024
Added soft_clip property to toggle between hard clipping (default) and soft clipping.
1 parent 222ce2c commit 0410d22

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed
 

Diff for: ‎shared-bindings/audiofilters/Distortion.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static audiofilters_distortion_mode validate_distortion_mode(mp_obj_t obj, qstr
6363
//| pre_gain: synthio.BlockInput = 0.0,
6464
//| post_gain: synthio.BlockInput = 0.0,
6565
//| mode: DistortionMode = DistortionMode.CLIP,
66+
//| soft_clip: bool = False,
6667
//| mix: synthio.BlockInput = 1.0,
6768
//| buffer_size: int = 512,
6869
//| sample_rate: int = 8000,
@@ -81,6 +82,7 @@ static audiofilters_distortion_mode validate_distortion_mode(mp_obj_t obj, qstr
8182
//| :param synthio.BlockInput pre_gain: Increases or decreases the volume before the effect, in decibels. Value can range from -60 to 60.
8283
//| :param synthio.BlockInput post_gain: Increases or decreases the volume after the effect, in decibels. Value can range from -80 to 24.
8384
//| :param DistortionMode mode: Distortion type.
85+
//| :param bool soft_clip: Whether or not to soft clip (True) or hard clip (False) the output.
8486
//| :param synthio.BlockInput mix: The mix as a ratio of the sample (0.0) to the effect (1.0).
8587
//| :param int buffer_size: The total size in bytes of each of the two playback buffers to use
8688
//| :param int sample_rate: The sample rate to be used
@@ -111,12 +113,13 @@ static audiofilters_distortion_mode validate_distortion_mode(mp_obj_t obj, qstr
111113
//| ...
112114

113115
static 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) {
114-
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, };
116+
enum { ARG_drive, ARG_pre_gain, ARG_post_gain, ARG_mode, ARG_soft_clip, ARG_mix, ARG_buffer_size, ARG_sample_rate, ARG_bits_per_sample, ARG_samples_signed, ARG_channel_count, };
115117
static const mp_arg_t allowed_args[] = {
116118
{ MP_QSTR_drive, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0)} },
117119
{ MP_QSTR_pre_gain, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0)} },
118120
{ MP_QSTR_post_gain, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0)} },
119121
{ MP_QSTR_mode, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
122+
{ MP_QSTR_soft_clip, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
120123
{ MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1)} },
121124
{ MP_QSTR_buffer_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 512} },
122125
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} },
@@ -141,7 +144,7 @@ static mp_obj_t audiofilters_distortion_make_new(const mp_obj_type_t *type, size
141144
}
142145

143146
audiofilters_distortion_obj_t *self = mp_obj_malloc(audiofilters_distortion_obj_t, &audiofilters_distortion_type);
144-
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);
147+
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_soft_clip].u_obj, args[ARG_mix].u_obj, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);
145148
return MP_OBJ_FROM_PTR(self);
146149
}
147150

@@ -257,6 +260,25 @@ MP_PROPERTY_GETSET(audiofilters_distortion_mode_obj,
257260
(mp_obj_t)&audiofilters_distortion_set_mode_obj);
258261

259262

263+
//| soft_clip: bool
264+
//| """Whether or not to soft clip (True) or hard clip (False) the output."""
265+
static mp_obj_t audiofilters_distortion_obj_get_soft_clip(mp_obj_t self_in) {
266+
return mp_obj_new_bool(common_hal_audiofilters_distortion_get_soft_clip(self_in));
267+
}
268+
MP_DEFINE_CONST_FUN_OBJ_1(audiofilters_distortion_get_soft_clip_obj, audiofilters_distortion_obj_get_soft_clip);
269+
270+
static mp_obj_t audiofilters_distortion_obj_set_soft_clip(mp_obj_t self_in, mp_obj_t soft_clip_in) {
271+
audiofilters_distortion_obj_t *self = MP_OBJ_TO_PTR(self_in);
272+
common_hal_audiofilters_distortion_set_soft_clip(self, mp_obj_is_true(soft_clip_in));
273+
return mp_const_none;
274+
}
275+
MP_DEFINE_CONST_FUN_OBJ_2(audiofilters_distortion_set_soft_clip_obj, audiofilters_distortion_obj_set_soft_clip);
276+
277+
MP_PROPERTY_GETSET(audiofilters_distortion_soft_clip_obj,
278+
(mp_obj_t)&audiofilters_distortion_get_soft_clip_obj,
279+
(mp_obj_t)&audiofilters_distortion_set_soft_clip_obj);
280+
281+
260282
//| mix: synthio.BlockInput
261283
//| """The rate the filtered signal mix between 0 and 1 where 0 is only sample and 1 is all effect."""
262284
static mp_obj_t audiofilters_distortion_obj_get_mix(mp_obj_t self_in) {
@@ -339,6 +361,7 @@ static const mp_rom_map_elem_t audiofilters_distortion_locals_dict_table[] = {
339361
{ MP_ROM_QSTR(MP_QSTR_pre_gain), MP_ROM_PTR(&audiofilters_distortion_pre_gain_obj) },
340362
{ MP_ROM_QSTR(MP_QSTR_post_gain), MP_ROM_PTR(&audiofilters_distortion_post_gain_obj) },
341363
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&audiofilters_distortion_mode_obj) },
364+
{ MP_ROM_QSTR(MP_QSTR_soft_clip), MP_ROM_PTR(&audiofilters_distortion_soft_clip_obj) },
342365
{ MP_ROM_QSTR(MP_QSTR_mix), MP_ROM_PTR(&audiofilters_distortion_mix_obj) },
343366
};
344367
static MP_DEFINE_CONST_DICT(audiofilters_distortion_locals_dict, audiofilters_distortion_locals_dict_table);

Diff for: ‎shared-bindings/audiofilters/Distortion.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern const mp_obj_type_t audiofilters_distortion_mode_type;
1313

1414
void common_hal_audiofilters_distortion_construct(audiofilters_distortion_obj_t *self,
1515
mp_obj_t drive, mp_obj_t pre_gain, mp_obj_t post_gain,
16-
audiofilters_distortion_mode mode, mp_obj_t mix,
16+
audiofilters_distortion_mode mode, bool soft_clip, mp_obj_t mix,
1717
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed,
1818
uint8_t channel_count, uint32_t sample_rate);
1919

@@ -36,6 +36,9 @@ void common_hal_audiofilters_distortion_set_post_gain(audiofilters_distortion_ob
3636
audiofilters_distortion_mode common_hal_audiofilters_distortion_get_mode(audiofilters_distortion_obj_t *self);
3737
void common_hal_audiofilters_distortion_set_mode(audiofilters_distortion_obj_t *self, audiofilters_distortion_mode mode);
3838

39+
bool common_hal_audiofilters_distortion_get_soft_clip(audiofilters_distortion_obj_t *self);
40+
void common_hal_audiofilters_distortion_set_soft_clip(audiofilters_distortion_obj_t *self, bool soft_clip);
41+
3942
mp_obj_t common_hal_audiofilters_distortion_get_mix(audiofilters_distortion_obj_t *self);
4043
void common_hal_audiofilters_distortion_set_mix(audiofilters_distortion_obj_t *self, mp_obj_t arg);
4144

Diff for: ‎shared-module/audiofilters/Distortion.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
void common_hal_audiofilters_distortion_construct(audiofilters_distortion_obj_t *self,
2121
mp_obj_t drive, mp_obj_t pre_gain, mp_obj_t post_gain,
22-
audiofilters_distortion_mode mode, mp_obj_t mix,
22+
audiofilters_distortion_mode mode, bool soft_clip, mp_obj_t mix,
2323
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed,
2424
uint8_t channel_count, uint32_t sample_rate) {
2525

@@ -68,6 +68,7 @@ void common_hal_audiofilters_distortion_construct(audiofilters_distortion_obj_t
6868
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
6969

7070
self->mode = mode;
71+
self->soft_clip = soft_clip;
7172
}
7273

7374
bool common_hal_audiofilters_distortion_deinited(audiofilters_distortion_obj_t *self) {
@@ -117,6 +118,14 @@ void common_hal_audiofilters_distortion_set_mode(audiofilters_distortion_obj_t *
117118
self->mode = arg;
118119
}
119120

121+
bool common_hal_audiofilters_distortion_get_soft_clip(audiofilters_distortion_obj_t *self) {
122+
return self->soft_clip;
123+
}
124+
125+
void common_hal_audiofilters_distortion_set_soft_clip(audiofilters_distortion_obj_t *self, bool soft_clip) {
126+
self->soft_clip = soft_clip;
127+
}
128+
120129
mp_obj_t common_hal_audiofilters_distortion_get_mix(audiofilters_distortion_obj_t *self) {
121130
return self->mix.obj;
122131
}

Diff for: ‎shared-module/audiofilters/Distortion.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef struct {
2626
synthio_block_slot_t pre_gain;
2727
synthio_block_slot_t post_gain;
2828
audiofilters_distortion_mode mode;
29+
bool soft_clip;
2930
synthio_block_slot_t mix;
3031

3132
uint8_t bits_per_sample;

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/adafruit/circuitpython/commit/0410d2260125f5b97ddbc3fbb52db1d6ef81741c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy