Skip to content

Commit 16575fc

Browse files
committed
Added Distortion and DistortionMode classes to audiofilters module based on Godot's AudioEffectDistortion class.
1 parent 38779cd commit 16575fc

File tree

8 files changed

+914
-0
lines changed

8 files changed

+914
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ $(filter $(SRC_PATTERNS), \
570570
_bleio/ScanEntry.c \
571571
_eve/__init__.c \
572572
__future__/__init__.c \
573+
audiofilters/DistortionMode.c \
573574
camera/ImageFormat.c \
574575
canio/Match.c \
575576
codeop/__init__.c \
@@ -626,6 +627,7 @@ SRC_SHARED_MODULE_ALL = \
626627
audiodelays/Echo.c \
627628
audiodelays/__init__.c \
628629
audiofilters/Filter.c \
630+
audiofilters/Distortion.c \
629631
audiofilters/__init__.c \
630632
audioio/__init__.c \
631633
audiomixer/Mixer.c \

shared-bindings/audiofilters/Distortion.c

Lines changed: 362 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include "shared-bindings/audiofilters/DistortionMode.h"
10+
#include "shared-module/audiofilters/Distortion.h"
11+
12+
extern const mp_obj_type_t audiofilters_distortion_type;
13+
14+
void common_hal_audiofilters_distortion_construct(audiofilters_distortion_obj_t *self,
15+
mp_obj_t drive, mp_obj_t pre_gain, mp_obj_t post_gain,
16+
audiofilters_distortion_mode_t mode, mp_obj_t mix,
17+
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed,
18+
uint8_t channel_count, uint32_t sample_rate);
19+
20+
void common_hal_audiofilters_distortion_deinit(audiofilters_distortion_obj_t *self);
21+
bool common_hal_audiofilters_distortion_deinited(audiofilters_distortion_obj_t *self);
22+
23+
uint32_t common_hal_audiofilters_distortion_get_sample_rate(audiofilters_distortion_obj_t *self);
24+
uint8_t common_hal_audiofilters_distortion_get_channel_count(audiofilters_distortion_obj_t *self);
25+
uint8_t common_hal_audiofilters_distortion_get_bits_per_sample(audiofilters_distortion_obj_t *self);
26+
27+
mp_obj_t common_hal_audiofilters_distortion_get_drive(audiofilters_distortion_obj_t *self);
28+
void common_hal_audiofilters_distortion_set_drive(audiofilters_distortion_obj_t *self, mp_obj_t arg);
29+
30+
mp_obj_t common_hal_audiofilters_distortion_get_pre_gain(audiofilters_distortion_obj_t *self);
31+
void common_hal_audiofilters_distortion_set_pre_gain(audiofilters_distortion_obj_t *self, mp_obj_t arg);
32+
33+
mp_obj_t common_hal_audiofilters_distortion_get_post_gain(audiofilters_distortion_obj_t *self);
34+
void common_hal_audiofilters_distortion_set_post_gain(audiofilters_distortion_obj_t *self, mp_obj_t arg);
35+
36+
audiofilters_distortion_mode_t common_hal_audiofilters_distortion_get_mode(audiofilters_distortion_obj_t *self);
37+
void common_hal_audiofilters_distortion_set_mode(audiofilters_distortion_obj_t *self, audiofilters_distortion_mode_t mode);
38+
39+
mp_obj_t common_hal_audiofilters_distortion_get_mix(audiofilters_distortion_obj_t *self);
40+
void common_hal_audiofilters_distortion_set_mix(audiofilters_distortion_obj_t *self, mp_obj_t arg);
41+
42+
bool common_hal_audiofilters_distortion_get_playing(audiofilters_distortion_obj_t *self);
43+
void common_hal_audiofilters_distortion_play(audiofilters_distortion_obj_t *self, mp_obj_t sample, bool loop);
44+
void common_hal_audiofilters_distortion_stop(audiofilters_distortion_obj_t *self);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "py/enum.h"
8+
9+
#include "shared-bindings/audiofilters/DistortionMode.h"
10+
11+
MAKE_ENUM_VALUE(audiofilters_distortion_mode_type, distortion_mode, CLIP, DISTORTION_MODE_CLIP);
12+
MAKE_ENUM_VALUE(audiofilters_distortion_mode_type, distortion_mode, ATAN, DISTORTION_MODE_ATAN);
13+
MAKE_ENUM_VALUE(audiofilters_distortion_mode_type, distortion_mode, LOFI, DISTORTION_MODE_LOFI);
14+
MAKE_ENUM_VALUE(audiofilters_distortion_mode_type, distortion_mode, OVERDRIVE, DISTORTION_MODE_OVERDRIVE);
15+
MAKE_ENUM_VALUE(audiofilters_distortion_mode_type, distortion_mode, WAVESHAPE, DISTORTION_MODE_WAVESHAPE);
16+
17+
//| class DistortionMode:
18+
//| """The method of distortion used by the `audiofilters.Distortion` effect."""
19+
//|
20+
//| CLIP: DistortionMode
21+
//| """Digital distortion effect which cuts off peaks at the top and bottom of the waveform."""
22+
//|
23+
//| ATAN: DistortionMode
24+
//| """"""
25+
//|
26+
//| LOFI: DistortionMode
27+
//| """Low-resolution digital distortion effect (bit depth reduction). You can use it to emulate the sound of early digital audio devices."""
28+
//|
29+
//| OVERDRIVE: DistortionMode
30+
//| """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."""
31+
//|
32+
//| WAVESHAPE: DistortionMode
33+
//| """Waveshaper distortions are used mainly by electronic musicians to achieve an extra-abrasive sound."""
34+
//|
35+
MAKE_ENUM_MAP(audiofilters_distortion_mode) {
36+
MAKE_ENUM_MAP_ENTRY(distortion_mode, CLIP),
37+
MAKE_ENUM_MAP_ENTRY(distortion_mode, ATAN),
38+
MAKE_ENUM_MAP_ENTRY(distortion_mode, LOFI),
39+
MAKE_ENUM_MAP_ENTRY(distortion_mode, OVERDRIVE),
40+
MAKE_ENUM_MAP_ENTRY(distortion_mode, WAVESHAPE),
41+
};
42+
static MP_DEFINE_CONST_DICT(audiofilters_distortion_mode_locals_dict, audiofilters_distortion_mode_locals_table);
43+
44+
MAKE_PRINTER(audiofilters, audiofilters_distortion_mode);
45+
46+
MAKE_ENUM_TYPE(audiofilters, DistortionMode, audiofilters_distortion_mode);
47+
48+
audiofilters_distortion_mode_t validate_distortion_mode(mp_obj_t obj, qstr arg_name) {
49+
return cp_enum_value(&audiofilters_distortion_mode_type, obj, arg_name);
50+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include "py/enum.h"
10+
11+
typedef enum audiofilters_distortion_mode_e {
12+
DISTORTION_MODE_CLIP,
13+
DISTORTION_MODE_ATAN,
14+
DISTORTION_MODE_LOFI,
15+
DISTORTION_MODE_OVERDRIVE,
16+
DISTORTION_MODE_WAVESHAPE,
17+
} audiofilters_distortion_mode_t;
18+
19+
extern const cp_enum_obj_t distortion_mode_CLIP_obj;
20+
extern const mp_obj_type_t audiofilters_distortion_mode_type;
21+
22+
extern audiofilters_distortion_mode_t validate_distortion_mode(mp_obj_t obj, qstr arg_name);

shared-bindings/audiofilters/__init__.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "py/runtime.h"
1111

1212
#include "shared-bindings/audiofilters/__init__.h"
13+
#include "shared-bindings/audiofilters/DistortionMode.h"
14+
#include "shared-bindings/audiofilters/Distortion.h"
1315
#include "shared-bindings/audiofilters/Filter.h"
1416

1517
//| """Support for audio filter effects
@@ -21,6 +23,10 @@
2123
static const mp_rom_map_elem_t audiofilters_module_globals_table[] = {
2224
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audiofilters) },
2325
{ MP_ROM_QSTR(MP_QSTR_Filter), MP_ROM_PTR(&audiofilters_filter_type) },
26+
{ MP_ROM_QSTR(MP_QSTR_Distortion), MP_ROM_PTR(&audiofilters_distortion_type) },
27+
28+
// Enum-like Classes.
29+
{ MP_ROM_QSTR(MP_QSTR_DistortionMode), MP_ROM_PTR(&audiofilters_distortion_mode_type) },
2430
};
2531

2632
static MP_DEFINE_CONST_DICT(audiofilters_module_globals, audiofilters_module_globals_table);

0 commit comments

Comments
 (0)