|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Cooper Dalrymple |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include "shared-bindings/audiodelays/PitchShift.h" |
| 10 | +#include "shared-bindings/audiocore/__init__.h" |
| 11 | +#include "shared-module/audiodelays/PitchShift.h" |
| 12 | + |
| 13 | +#include "shared/runtime/context_manager_helpers.h" |
| 14 | +#include "py/binary.h" |
| 15 | +#include "py/objproperty.h" |
| 16 | +#include "py/runtime.h" |
| 17 | +#include "shared-bindings/util.h" |
| 18 | +#include "shared-module/synthio/block.h" |
| 19 | + |
| 20 | +//| class PitchShift: |
| 21 | +//| """A pitch shift effect""" |
| 22 | +//| |
| 23 | +//| def __init__( |
| 24 | +//| self, |
| 25 | +//| semitones: synthio.BlockInput = 0.0, |
| 26 | +//| mix: synthio.BlockInput = 1.0, |
| 27 | +//| window: int = 1024, |
| 28 | +//| overlap: int = 128, |
| 29 | +//| buffer_size: int = 512, |
| 30 | +//| sample_rate: int = 8000, |
| 31 | +//| bits_per_sample: int = 16, |
| 32 | +//| samples_signed: bool = True, |
| 33 | +//| channel_count: int = 1, |
| 34 | +//| ) -> None: |
| 35 | +//| """Create a pitch shift effect where the original sample play back is altered to change the |
| 36 | +//| the perceived pitch by a factor of semi-tones (1/12th of an octave). This effect will cause |
| 37 | +//| a slight delay in the output depending on the size of the window and overlap buffers. |
| 38 | +//| |
| 39 | +//| The mix parameter allows you to change how much of the unchanged sample passes through to |
| 40 | +//| the output to how much of the effect audio you hear as the output. |
| 41 | +//| |
| 42 | +//| :param synthio.BlockInput semitones: The amount of pitch shifting in semitones (1/12th of an octave) |
| 43 | +//| :param synthio.BlockInput mix: The mix as a ratio of the sample (0.0) to the effect (1.0) |
| 44 | +//| :param int window: The total size in bytes of the window buffer used alter the playback pitch |
| 45 | +//| :param int overlap: The total size in bytes of the overlap buffer used to prevent popping in the output. If set as 0, no overlap will be used. |
| 46 | +//| :param int buffer_size: The total size in bytes of each of the two playback buffers to use |
| 47 | +//| :param int sample_rate: The sample rate to be used |
| 48 | +//| :param int channel_count: The number of channels the source samples contain. 1 = mono; 2 = stereo. |
| 49 | +//| :param int bits_per_sample: The bits per sample of the effect |
| 50 | +//| :param bool samples_signed: Effect is signed (True) or unsigned (False) |
| 51 | +//| |
| 52 | +//| Shifting the pitch of a synth by 5 semitones:: |
| 53 | +//| |
| 54 | +//| import time |
| 55 | +//| import board |
| 56 | +//| import audiobusio |
| 57 | +//| import synthio |
| 58 | +//| import audiodelays |
| 59 | +//| |
| 60 | +//| audio = audiobusio.I2SOut(bit_clock=board.GP0, word_select=board.GP1, data=board.GP2) |
| 61 | +//| synth = synthio.Synthesizer(channel_count=1, sample_rate=44100) |
| 62 | +//| pitch_shift = audiodelays.PitchShift(semitones=5.0, mix=0.5, window=2048, overlap=256, buffer_size=1024, channel_count=1, sample_rate=44100) |
| 63 | +//| pitch_shift.play(synth) |
| 64 | +//| audio.play(pitch_shift) |
| 65 | +//| |
| 66 | +//| while True: |
| 67 | +//| for notenum in (60, 64, 67, 71): |
| 68 | +//| synth.press(notenum) |
| 69 | +//| time.sleep(0.25) |
| 70 | +//| synth.release_all()""" |
| 71 | +//| ... |
| 72 | +//| |
| 73 | +static mp_obj_t audiodelays_pitch_shift_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 74 | + enum { ARG_semitones, ARG_mix, ARG_window, ARG_overlap, ARG_buffer_size, ARG_sample_rate, ARG_bits_per_sample, ARG_samples_signed, ARG_channel_count, }; |
| 75 | + static const mp_arg_t allowed_args[] = { |
| 76 | + { MP_QSTR_semitones, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0)} }, |
| 77 | + { MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1)} }, |
| 78 | + { MP_QSTR_window, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1024} }, |
| 79 | + { MP_QSTR_overlap, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 128} }, |
| 80 | + { MP_QSTR_buffer_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 512} }, |
| 81 | + { MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} }, |
| 82 | + { MP_QSTR_bits_per_sample, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} }, |
| 83 | + { MP_QSTR_samples_signed, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} }, |
| 84 | + { MP_QSTR_channel_count, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1 } }, |
| 85 | + }; |
| 86 | + |
| 87 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 88 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 89 | + |
| 90 | + mp_int_t channel_count = mp_arg_validate_int_range(args[ARG_channel_count].u_int, 1, 2, MP_QSTR_channel_count); |
| 91 | + mp_int_t sample_rate = mp_arg_validate_int_min(args[ARG_sample_rate].u_int, 1, MP_QSTR_sample_rate); |
| 92 | + mp_int_t bits_per_sample = args[ARG_bits_per_sample].u_int; |
| 93 | + if (bits_per_sample != 8 && bits_per_sample != 16) { |
| 94 | + mp_raise_ValueError(MP_ERROR_TEXT("bits_per_sample must be 8 or 16")); |
| 95 | + } |
| 96 | + |
| 97 | + audiodelays_pitch_shift_obj_t *self = mp_obj_malloc(audiodelays_pitch_shift_obj_t, &audiodelays_pitch_shift_type); |
| 98 | + common_hal_audiodelays_pitch_shift_construct(self, args[ARG_semitones].u_obj, args[ARG_mix].u_obj, args[ARG_window].u_int, args[ARG_overlap].u_int, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate); |
| 99 | + |
| 100 | + return MP_OBJ_FROM_PTR(self); |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +//| def deinit(self) -> None: |
| 105 | +//| """Deinitialises the PitchShift.""" |
| 106 | +//| ... |
| 107 | +//| |
| 108 | +static mp_obj_t audiodelays_pitch_shift_deinit(mp_obj_t self_in) { |
| 109 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 110 | + common_hal_audiodelays_pitch_shift_deinit(self); |
| 111 | + return mp_const_none; |
| 112 | +} |
| 113 | +static MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_pitch_shift_deinit_obj, audiodelays_pitch_shift_deinit); |
| 114 | + |
| 115 | +static void check_for_deinit(audiodelays_pitch_shift_obj_t *self) { |
| 116 | + audiosample_check_for_deinit(&self->base); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +//| def __enter__(self) -> PitchShift: |
| 121 | +//| """No-op used by Context Managers.""" |
| 122 | +//| ... |
| 123 | +//| |
| 124 | +// Provided by context manager helper. |
| 125 | + |
| 126 | +//| def __exit__(self) -> None: |
| 127 | +//| """Automatically deinitializes when exiting a context. See |
| 128 | +//| :ref:`lifetime-and-contextmanagers` for more info.""" |
| 129 | +//| ... |
| 130 | +//| |
| 131 | +static mp_obj_t audiodelays_pitch_shift_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 132 | + (void)n_args; |
| 133 | + common_hal_audiodelays_pitch_shift_deinit(args[0]); |
| 134 | + return mp_const_none; |
| 135 | +} |
| 136 | +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiodelays_pitch_shift___exit___obj, 4, 4, audiodelays_pitch_shift_obj___exit__); |
| 137 | + |
| 138 | + |
| 139 | +//| semitones: synthio.BlockInput |
| 140 | +//| """The amount of pitch shifting in semitones (1/12th of an octave).""" |
| 141 | +//| |
| 142 | +static mp_obj_t audiodelays_pitch_shift_obj_get_semitones(mp_obj_t self_in) { |
| 143 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 144 | + return common_hal_audiodelays_pitch_shift_get_semitones(self); |
| 145 | +} |
| 146 | +MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_pitch_shift_get_semitones_obj, audiodelays_pitch_shift_obj_get_semitones); |
| 147 | + |
| 148 | +static mp_obj_t audiodelays_pitch_shift_obj_set_semitones(mp_obj_t self_in, mp_obj_t semitones_in) { |
| 149 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 150 | + common_hal_audiodelays_pitch_shift_set_semitones(self, semitones_in); |
| 151 | + return mp_const_none; |
| 152 | +} |
| 153 | +MP_DEFINE_CONST_FUN_OBJ_2(audiodelays_pitch_shift_set_semitones_obj, audiodelays_pitch_shift_obj_set_semitones); |
| 154 | + |
| 155 | +MP_PROPERTY_GETSET(audiodelays_pitch_shift_semitones_obj, |
| 156 | + (mp_obj_t)&audiodelays_pitch_shift_get_semitones_obj, |
| 157 | + (mp_obj_t)&audiodelays_pitch_shift_set_semitones_obj); |
| 158 | + |
| 159 | + |
| 160 | +//| mix: synthio.BlockInput |
| 161 | +//| """The output mix between 0 and 1 where 0 is only sample and 1 is all effect.""" |
| 162 | +static mp_obj_t audiodelays_pitch_shift_obj_get_mix(mp_obj_t self_in) { |
| 163 | + return common_hal_audiodelays_pitch_shift_get_mix(self_in); |
| 164 | +} |
| 165 | +MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_pitch_shift_get_mix_obj, audiodelays_pitch_shift_obj_get_mix); |
| 166 | + |
| 167 | +static mp_obj_t audiodelays_pitch_shift_obj_set_mix(mp_obj_t self_in, mp_obj_t mix_in) { |
| 168 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 169 | + common_hal_audiodelays_pitch_shift_set_mix(self, mix_in); |
| 170 | + return mp_const_none; |
| 171 | +} |
| 172 | +MP_DEFINE_CONST_FUN_OBJ_2(audiodelays_pitch_shift_set_mix_obj, audiodelays_pitch_shift_obj_set_mix); |
| 173 | + |
| 174 | +MP_PROPERTY_GETSET(audiodelays_pitch_shift_mix_obj, |
| 175 | + (mp_obj_t)&audiodelays_pitch_shift_get_mix_obj, |
| 176 | + (mp_obj_t)&audiodelays_pitch_shift_set_mix_obj); |
| 177 | + |
| 178 | + |
| 179 | +//| playing: bool |
| 180 | +//| """True when the effect is playing a sample. (read-only)""" |
| 181 | +//| |
| 182 | +static mp_obj_t audiodelays_pitch_shift_obj_get_playing(mp_obj_t self_in) { |
| 183 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 184 | + check_for_deinit(self); |
| 185 | + return mp_obj_new_bool(common_hal_audiodelays_pitch_shift_get_playing(self)); |
| 186 | +} |
| 187 | +MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_pitch_shift_get_playing_obj, audiodelays_pitch_shift_obj_get_playing); |
| 188 | + |
| 189 | +MP_PROPERTY_GETTER(audiodelays_pitch_shift_playing_obj, |
| 190 | + (mp_obj_t)&audiodelays_pitch_shift_get_playing_obj); |
| 191 | + |
| 192 | + |
| 193 | +//| def play(self, sample: circuitpython_typing.AudioSample, *, loop: bool = False) -> None: |
| 194 | +//| """Plays the sample once when loop=False and continuously when loop=True. |
| 195 | +//| Does not block. Use `playing` to block. |
| 196 | +//| |
| 197 | +//| The sample must match the encoding settings given in the constructor.""" |
| 198 | +//| ... |
| 199 | +//| |
| 200 | +static mp_obj_t audiodelays_pitch_shift_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 201 | + enum { ARG_sample, ARG_loop }; |
| 202 | + static const mp_arg_t allowed_args[] = { |
| 203 | + { MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED, {} }, |
| 204 | + { MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, |
| 205 | + }; |
| 206 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 207 | + check_for_deinit(self); |
| 208 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 209 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 210 | + |
| 211 | + mp_obj_t sample = args[ARG_sample].u_obj; |
| 212 | + common_hal_audiodelays_pitch_shift_play(self, sample, args[ARG_loop].u_bool); |
| 213 | + |
| 214 | + return mp_const_none; |
| 215 | +} |
| 216 | +MP_DEFINE_CONST_FUN_OBJ_KW(audiodelays_pitch_shift_play_obj, 1, audiodelays_pitch_shift_obj_play); |
| 217 | + |
| 218 | + |
| 219 | +//| def stop(self) -> None: |
| 220 | +//| """Stops playback of the sample.""" |
| 221 | +//| ... |
| 222 | +//| |
| 223 | +//| |
| 224 | +static mp_obj_t audiodelays_pitch_shift_obj_stop(mp_obj_t self_in) { |
| 225 | + audiodelays_pitch_shift_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 226 | + common_hal_audiodelays_pitch_shift_stop(self); |
| 227 | + return mp_const_none; |
| 228 | +} |
| 229 | +MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_pitch_shift_stop_obj, audiodelays_pitch_shift_obj_stop); |
| 230 | + |
| 231 | + |
| 232 | +static const mp_rom_map_elem_t audiodelays_pitch_shift_locals_dict_table[] = { |
| 233 | + // Methods |
| 234 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiodelays_pitch_shift_deinit_obj) }, |
| 235 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, |
| 236 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiodelays_pitch_shift___exit___obj) }, |
| 237 | + { MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiodelays_pitch_shift_play_obj) }, |
| 238 | + { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audiodelays_pitch_shift_stop_obj) }, |
| 239 | + |
| 240 | + // Properties |
| 241 | + { MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiodelays_pitch_shift_playing_obj) }, |
| 242 | + { MP_ROM_QSTR(MP_QSTR_semitones), MP_ROM_PTR(&audiodelays_pitch_shift_semitones_obj) }, |
| 243 | + { MP_ROM_QSTR(MP_QSTR_mix), MP_ROM_PTR(&audiodelays_pitch_shift_mix_obj) }, |
| 244 | + AUDIOSAMPLE_FIELDS, |
| 245 | +}; |
| 246 | +static MP_DEFINE_CONST_DICT(audiodelays_pitch_shift_locals_dict, audiodelays_pitch_shift_locals_dict_table); |
| 247 | + |
| 248 | +static const audiosample_p_t audiodelays_pitch_shift_proto = { |
| 249 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample) |
| 250 | + .reset_buffer = (audiosample_reset_buffer_fun)audiodelays_pitch_shift_reset_buffer, |
| 251 | + .get_buffer = (audiosample_get_buffer_fun)audiodelays_pitch_shift_get_buffer, |
| 252 | +}; |
| 253 | + |
| 254 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 255 | + audiodelays_pitch_shift_type, |
| 256 | + MP_QSTR_PitchShift, |
| 257 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 258 | + make_new, audiodelays_pitch_shift_make_new, |
| 259 | + locals_dict, &audiodelays_pitch_shift_locals_dict, |
| 260 | + protocol, &audiodelays_pitch_shift_proto |
| 261 | + ); |
0 commit comments