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