Skip to content

Commit 46ebae1

Browse files
committed
Remove separate DistortionMode code files.
1 parent 16575fc commit 46ebae1

File tree

8 files changed

+78
-97
lines changed

8 files changed

+78
-97
lines changed

py/circuitpy_defns.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ $(filter $(SRC_PATTERNS), \
570570
_bleio/ScanEntry.c \
571571
_eve/__init__.c \
572572
__future__/__init__.c \
573-
audiofilters/DistortionMode.c \
574573
camera/ImageFormat.c \
575574
canio/Match.c \
576575
codeop/__init__.c \

shared-bindings/audiofilters/Distortion.c

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,58 @@
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
//|
@@ -71,13 +114,14 @@
71114
//| synth.release(note)
72115
//| time.sleep(5)"""
73116
//| ...
117+
74118
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) {
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;

shared-bindings/audiofilters/Distortion.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
#pragma once
88

9-
#include "shared-bindings/audiofilters/DistortionMode.h"
109
#include "shared-module/audiofilters/Distortion.h"
1110

1211
extern const mp_obj_type_t audiofilters_distortion_type;
12+
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_t mode, mp_obj_t mix,
16+
audiofilters_distortion_mode mode, 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

@@ -33,8 +33,8 @@ void common_hal_audiofilters_distortion_set_pre_gain(audiofilters_distortion_obj
3333
mp_obj_t common_hal_audiofilters_distortion_get_post_gain(audiofilters_distortion_obj_t *self);
3434
void common_hal_audiofilters_distortion_set_post_gain(audiofilters_distortion_obj_t *self, mp_obj_t arg);
3535

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);
36+
audiofilters_distortion_mode 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 mode);
3838

3939
mp_obj_t common_hal_audiofilters_distortion_get_mix(audiofilters_distortion_obj_t *self);
4040
void common_hal_audiofilters_distortion_set_mix(audiofilters_distortion_obj_t *self, mp_obj_t arg);

shared-bindings/audiofilters/DistortionMode.c

Lines changed: 0 additions & 50 deletions
This file was deleted.

shared-bindings/audiofilters/DistortionMode.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

shared-bindings/audiofilters/__init__.c

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

1212
#include "shared-bindings/audiofilters/__init__.h"
13-
#include "shared-bindings/audiofilters/DistortionMode.h"
1413
#include "shared-bindings/audiofilters/Distortion.h"
1514
#include "shared-bindings/audiofilters/Filter.h"
1615

shared-module/audiofilters/Distortion.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
// SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
44
//
55
// SPDX-License-Identifier: MIT
6-
#include "shared-bindings/audiofilters/Distortion.h"
76

87
#include <stdint.h>
8+
#include "py/obj.h"
99
#include "py/runtime.h"
1010
#include <math.h>
11+
#include "shared-bindings/audiofilters/Distortion.h"
12+
#include "shared-module/audiofilters/Distortion.h"
1113

1214
/**
1315
* Based on Godot's AudioEffectDistortion
@@ -16,9 +18,10 @@
1618
*/
1719

1820
void common_hal_audiofilters_distortion_construct(audiofilters_distortion_obj_t *self,
19-
mp_obj_t drive, mp_obj_t pre_gain, mp_obj_t post_gain, audiofilters_distortion_mode_t mode, mp_obj_t mix,
20-
uint32_t buffer_size, uint8_t bits_per_sample,
21-
bool samples_signed, uint8_t channel_count, uint32_t sample_rate) {
21+
mp_obj_t drive, mp_obj_t pre_gain, mp_obj_t post_gain,
22+
audiofilters_distortion_mode mode, mp_obj_t mix,
23+
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed,
24+
uint8_t channel_count, uint32_t sample_rate) {
2225

2326
// Basic settings every effect and audio sample has
2427
// These are the effects values, not the source sample(s)
@@ -125,11 +128,11 @@ void common_hal_audiofilters_distortion_set_post_gain(audiofilters_distortion_ob
125128
synthio_block_assign_slot(arg, &self->post_gain, MP_QSTR_post_gain);
126129
}
127130

128-
audiofilters_distortion_mode_t common_hal_audiofilters_distortion_get_mode(audiofilters_distortion_obj_t *self) {
131+
audiofilters_distortion_mode common_hal_audiofilters_distortion_get_mode(audiofilters_distortion_obj_t *self) {
129132
return self->mode;
130133
}
131134

132-
void common_hal_audiofilters_distortion_set_mode(audiofilters_distortion_obj_t *self, audiofilters_distortion_mode_t arg) {
135+
void common_hal_audiofilters_distortion_set_mode(audiofilters_distortion_obj_t *self, audiofilters_distortion_mode arg) {
133136
self->mode = arg;
134137
}
135138

shared-module/audiofilters/Distortion.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88
#include "py/obj.h"
99

10+
#include "shared-bindings/audiofilters/Distortion.h"
1011
#include "shared-module/audiocore/__init__.h"
1112
#include "shared-module/synthio/block.h"
12-
#include "shared-bindings/audiofilters/DistortionMode.h"
13+
14+
typedef enum {
15+
DISTORTION_MODE_CLIP,
16+
DISTORTION_MODE_ATAN,
17+
DISTORTION_MODE_LOFI,
18+
DISTORTION_MODE_OVERDRIVE,
19+
DISTORTION_MODE_WAVESHAPE,
20+
} audiofilters_distortion_mode;
1321

1422
extern const mp_obj_type_t audiofilters_distortion_type;
1523

@@ -18,7 +26,7 @@ typedef struct {
1826
synthio_block_slot_t drive;
1927
synthio_block_slot_t pre_gain;
2028
synthio_block_slot_t post_gain;
21-
audiofilters_distortion_mode_t mode;
29+
audiofilters_distortion_mode mode;
2230
synthio_block_slot_t mix;
2331

2432
uint8_t bits_per_sample;
@@ -44,11 +52,9 @@ void audiofilters_distortion_reset_buffer(audiofilters_distortion_obj_t *self,
4452
uint8_t channel);
4553

4654
audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_distortion_obj_t *self,
47-
bool single_channel_output,
48-
uint8_t channel,
49-
uint8_t **buffer,
50-
uint32_t *buffer_length); // length in bytes
55+
bool single_channel_output, uint8_t channel,
56+
uint8_t **buffer, uint32_t *buffer_length);
5157

52-
void audiofilters_distortion_get_buffer_structure(audiofilters_distortion_obj_t *self, bool single_channel_output,
53-
bool *single_buffer, bool *samples_signed,
58+
void audiofilters_distortion_get_buffer_structure(audiofilters_distortion_obj_t *self,
59+
bool single_channel_output, bool *single_buffer, bool *samples_signed,
5460
uint32_t *max_buffer_length, uint8_t *spacing);

0 commit comments

Comments
 (0)