|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2026 Cooper Dalrymple |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include "shared/runtime/context_manager_helpers.h" |
| 10 | +#include "py/objproperty.h" |
| 11 | +#include "py/runtime.h" |
| 12 | +#include "shared-bindings/audiospeed/Resampler.h" |
| 13 | +#include "shared-bindings/audiocore/__init__.h" |
| 14 | +#include "shared-bindings/util.h" |
| 15 | +#include "shared-module/audiospeed/Resampler.h" |
| 16 | + |
| 17 | +//| class Resampler: |
| 18 | +//| """Wraps an audio sample to match it to the destination sample rate.""" |
| 19 | +//| |
| 20 | +//| def __init__(self) -> None: |
| 21 | +//| """Create a Resampler that wraps ``source``. |
| 22 | +//| |
| 23 | +//| :param audiosample source: The audio source to resample. |
| 24 | +//| |
| 25 | +//| Playing a wave file through a mixer with half the sample rate:: |
| 26 | +//| |
| 27 | +//| import board |
| 28 | +//| import audiocore |
| 29 | +//| import audiomixer |
| 30 | +//| import audiospeed |
| 31 | +//| import audioio |
| 32 | +//| |
| 33 | +//| wav = audiocore.WaveFile("sample.wav") |
| 34 | +//| resampler = audiospeed.Resampler(wav) |
| 35 | +//| mixer = audiomixer.Mixer( |
| 36 | +//| channel_count=wav.channel_count, |
| 37 | +//| bits_per_sample=wav.bits_per_sample, |
| 38 | +//| sample_rate=wav.sample_rate // 2, |
| 39 | +//| ) |
| 40 | +//| audio = audioio.AudioOut(board.A0) |
| 41 | +//| audio.play(mixer) |
| 42 | +//| mixer.play(resampler) |
| 43 | +//| """ |
| 44 | +//| ... |
| 45 | +//| |
| 46 | +static mp_obj_t audiospeed_resampler_make_new(const mp_obj_type_t *type, |
| 47 | + size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 48 | + enum { ARG_source }; |
| 49 | + static const mp_arg_t allowed_args[] = { |
| 50 | + { MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 51 | + }; |
| 52 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 53 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 54 | + |
| 55 | + // Validate source implements audiosample protocol |
| 56 | + mp_obj_t source = args[ARG_source].u_obj; |
| 57 | + audiosample_check(source); |
| 58 | + |
| 59 | + audiospeed_resampler_obj_t *self = mp_obj_malloc(audiospeed_resampler_obj_t, &audiospeed_resampler_type); |
| 60 | + common_hal_audiospeed_resampler_construct(self, source); |
| 61 | + return MP_OBJ_FROM_PTR(self); |
| 62 | +} |
| 63 | + |
| 64 | +//| def deinit(self) -> None: |
| 65 | +//| """Deinitialises the Resampler and releases all memory resources for reuse.""" |
| 66 | +//| ... |
| 67 | +//| |
| 68 | +static mp_obj_t audiospeed_resampler_deinit(mp_obj_t self_in) { |
| 69 | + audiospeed_resampler_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 70 | + common_hal_audiospeed_resampler_deinit(self); |
| 71 | + return mp_const_none; |
| 72 | +} |
| 73 | +static MP_DEFINE_CONST_FUN_OBJ_1(audiospeed_resampler_deinit_obj, audiospeed_resampler_deinit); |
| 74 | + |
| 75 | +//| rate: float |
| 76 | +//| """Playback speed multiplier.""" |
| 77 | +//| |
| 78 | +static mp_obj_t audiospeed_resampler_obj_get_rate(mp_obj_t self_in) { |
| 79 | + audiospeed_resampler_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 80 | + audiosample_check_for_deinit(&self->base.base); |
| 81 | + return common_hal_audiospeed_resampler_get_rate(self); |
| 82 | +} |
| 83 | +MP_DEFINE_CONST_FUN_OBJ_1(audiospeed_resampler_get_rate_obj, audiospeed_resampler_obj_get_rate); |
| 84 | + |
| 85 | +MP_PROPERTY_GETTER(audiospeed_resampler_rate_obj, |
| 86 | + (mp_obj_t)&audiospeed_resampler_get_rate_obj); |
| 87 | + |
| 88 | +static const mp_rom_map_elem_t audiospeed_resampler_locals_dict_table[] = { |
| 89 | + // Methods |
| 90 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiospeed_resampler_deinit_obj) }, |
| 91 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, |
| 92 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) }, |
| 93 | + |
| 94 | + // Properties |
| 95 | + { MP_ROM_QSTR(MP_QSTR_rate), MP_ROM_PTR(&audiospeed_resampler_rate_obj) }, |
| 96 | + AUDIOSAMPLE_FIELDS, |
| 97 | +}; |
| 98 | +static MP_DEFINE_CONST_DICT(audiospeed_resampler_locals_dict, audiospeed_resampler_locals_dict_table); |
| 99 | + |
| 100 | +static const audiosample_p_t audiospeed_resampler_proto = { |
| 101 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample) |
| 102 | + .reset_buffer = (audiosample_reset_buffer_fun)audiospeed_resampler_reset_buffer, |
| 103 | + .get_buffer = (audiosample_get_buffer_fun)audiospeed_resampler_get_buffer, |
| 104 | +}; |
| 105 | + |
| 106 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 107 | + audiospeed_resampler_type, |
| 108 | + MP_QSTR_Resampler, |
| 109 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 110 | + make_new, audiospeed_resampler_make_new, |
| 111 | + locals_dict, &audiospeed_resampler_locals_dict, |
| 112 | + protocol, &audiospeed_resampler_proto |
| 113 | + ); |
0 commit comments