|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2026 Tod Kurt |
| 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/audiotools/SpeedChanger.h" |
| 13 | +#include "shared-bindings/audiocore/__init__.h" |
| 14 | +#include "shared-bindings/util.h" |
| 15 | +#include "shared-module/audiotools/SpeedChanger.h" |
| 16 | + |
| 17 | +// Convert a Python float to 16.16 fixed-point rate |
| 18 | +static uint32_t rate_to_fp(mp_obj_t rate_obj) { |
| 19 | + mp_float_t rate = mp_arg_validate_obj_float_range(rate_obj, 0.001, 1000.0, MP_QSTR_rate); |
| 20 | + return (uint32_t)(rate * (1 << 16)); |
| 21 | +} |
| 22 | + |
| 23 | +// Convert 16.16 fixed-point rate to Python float |
| 24 | +static mp_obj_t fp_to_rate(uint32_t rate_fp) { |
| 25 | + return mp_obj_new_float((mp_float_t)rate_fp / (1 << 16)); |
| 26 | +} |
| 27 | + |
| 28 | +//| class SpeedChanger: |
| 29 | +//| """Wraps an audio sample to play it back at a different speed. |
| 30 | +//| |
| 31 | +//| Uses nearest-neighbor resampling with a fixed-point phase accumulator |
| 32 | +//| for CPU-efficient variable-speed playback.""" |
| 33 | +//| |
| 34 | +//| def __init__(self, source: audiosample, rate: float = 1.0) -> None: |
| 35 | +//| """Create a SpeedChanger that wraps ``source``. |
| 36 | +//| |
| 37 | +//| :param audiosample source: The audio source to resample. |
| 38 | +//| :param float rate: Playback speed multiplier. 1.0 = normal, 2.0 = double speed, |
| 39 | +//| 0.5 = half speed. Must be positive. |
| 40 | +//| |
| 41 | +//| Playing a wave file at 1.5x speed:: |
| 42 | +//| |
| 43 | +//| import board |
| 44 | +//| import audiocore |
| 45 | +//| import audiotools |
| 46 | +//| import audioio |
| 47 | +//| |
| 48 | +//| wav = audiocore.WaveFile("drum.wav") |
| 49 | +//| fast = audiotools.SpeedChanger(wav, rate=1.5) |
| 50 | +//| audio = audioio.AudioOut(board.A0) |
| 51 | +//| audio.play(fast) |
| 52 | +//| |
| 53 | +//| # Change speed during playback: |
| 54 | +//| fast.rate = 2.0 # double speed |
| 55 | +//| fast.rate = 0.5 # half speed |
| 56 | +//| """ |
| 57 | +//| ... |
| 58 | +//| |
| 59 | +static mp_obj_t audiotools_speedchanger_make_new(const mp_obj_type_t *type, |
| 60 | + size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 61 | + enum { ARG_source, ARG_rate }; |
| 62 | + static const mp_arg_t allowed_args[] = { |
| 63 | + { MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 64 | + { MP_QSTR_rate, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 65 | + }; |
| 66 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 67 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 68 | + |
| 69 | + // Validate source implements audiosample protocol |
| 70 | + mp_obj_t source = args[ARG_source].u_obj; |
| 71 | + audiosample_check(source); |
| 72 | + |
| 73 | + uint32_t rate_fp = 1 << 16; // default 1.0 |
| 74 | + if (args[ARG_rate].u_obj != mp_const_none) { |
| 75 | + rate_fp = rate_to_fp(args[ARG_rate].u_obj); |
| 76 | + } |
| 77 | + |
| 78 | + audiotools_speedchanger_obj_t *self = mp_obj_malloc(audiotools_speedchanger_obj_t, &audiotools_speedchanger_type); |
| 79 | + common_hal_audiotools_speedchanger_construct(self, source, rate_fp); |
| 80 | + return MP_OBJ_FROM_PTR(self); |
| 81 | +} |
| 82 | + |
| 83 | +//| def deinit(self) -> None: |
| 84 | +//| """Deinitialises the SpeedChanger and releases all memory resources for reuse.""" |
| 85 | +//| ... |
| 86 | +//| |
| 87 | +static mp_obj_t audiotools_speedchanger_deinit(mp_obj_t self_in) { |
| 88 | + audiotools_speedchanger_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 89 | + common_hal_audiotools_speedchanger_deinit(self); |
| 90 | + return mp_const_none; |
| 91 | +} |
| 92 | +static MP_DEFINE_CONST_FUN_OBJ_1(audiotools_speedchanger_deinit_obj, audiotools_speedchanger_deinit); |
| 93 | + |
| 94 | +//| rate: float |
| 95 | +//| """Playback speed multiplier. Can be changed during playback.""" |
| 96 | +//| |
| 97 | +static mp_obj_t audiotools_speedchanger_obj_get_rate(mp_obj_t self_in) { |
| 98 | + audiotools_speedchanger_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 99 | + audiosample_check_for_deinit(&self->base); |
| 100 | + return fp_to_rate(common_hal_audiotools_speedchanger_get_rate(self)); |
| 101 | +} |
| 102 | +MP_DEFINE_CONST_FUN_OBJ_1(audiotools_speedchanger_get_rate_obj, audiotools_speedchanger_obj_get_rate); |
| 103 | + |
| 104 | +static mp_obj_t audiotools_speedchanger_obj_set_rate(mp_obj_t self_in, mp_obj_t rate_obj) { |
| 105 | + audiotools_speedchanger_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 106 | + audiosample_check_for_deinit(&self->base); |
| 107 | + common_hal_audiotools_speedchanger_set_rate(self, rate_to_fp(rate_obj)); |
| 108 | + return mp_const_none; |
| 109 | +} |
| 110 | +MP_DEFINE_CONST_FUN_OBJ_2(audiotools_speedchanger_set_rate_obj, audiotools_speedchanger_obj_set_rate); |
| 111 | + |
| 112 | +MP_PROPERTY_GETSET(audiotools_speedchanger_rate_obj, |
| 113 | + (mp_obj_t)&audiotools_speedchanger_get_rate_obj, |
| 114 | + (mp_obj_t)&audiotools_speedchanger_set_rate_obj); |
| 115 | + |
| 116 | +static const mp_rom_map_elem_t audiotools_speedchanger_locals_dict_table[] = { |
| 117 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiotools_speedchanger_deinit_obj) }, |
| 118 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, |
| 119 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) }, |
| 120 | + { MP_ROM_QSTR(MP_QSTR_rate), MP_ROM_PTR(&audiotools_speedchanger_rate_obj) }, |
| 121 | + AUDIOSAMPLE_FIELDS, |
| 122 | +}; |
| 123 | +static MP_DEFINE_CONST_DICT(audiotools_speedchanger_locals_dict, audiotools_speedchanger_locals_dict_table); |
| 124 | + |
| 125 | +static const audiosample_p_t audiotools_speedchanger_proto = { |
| 126 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample) |
| 127 | + .reset_buffer = (audiosample_reset_buffer_fun)audiotools_speedchanger_reset_buffer, |
| 128 | + .get_buffer = (audiosample_get_buffer_fun)audiotools_speedchanger_get_buffer, |
| 129 | +}; |
| 130 | + |
| 131 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 132 | + audiotools_speedchanger_type, |
| 133 | + MP_QSTR_SpeedChanger, |
| 134 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 135 | + make_new, audiotools_speedchanger_make_new, |
| 136 | + locals_dict, &audiotools_speedchanger_locals_dict, |
| 137 | + protocol, &audiotools_speedchanger_proto |
| 138 | + ); |
0 commit comments