diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index 9196577f69a..c965684f9b0 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -43,6 +43,7 @@ SRC_BITMAP := \ shared-bindings/audiofilters/Filter.c \ shared-bindings/audiofilters/Phaser.c \ shared-bindings/audiofilters/__init__.c \ + shared-bindings/audiotap/LevelReader.c \ shared-bindings/audiotap/Tap.c \ shared-bindings/audiotap/__init__.c \ shared-bindings/audiofreeverb/Freeverb.c \ @@ -95,6 +96,7 @@ SRC_BITMAP := \ shared-module/audiofilters/Filter.c \ shared-module/audiofilters/Phaser.c \ shared-module/audiofilters/__init__.c \ + shared-module/audiotap/LevelReader.c \ shared-module/audiotap/Tap.c \ shared-module/audiotap/__init__.c \ shared-module/audiofreeverb/Freeverb.c \ diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 1c6be24f5ab..e92e8722b4c 100755 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -792,6 +792,7 @@ SRC_SHARED_MODULE_ALL = \ audiospeed/Resampler.c \ audiospeed/SpeedChanger.c \ audiospeed/__init__.c \ + audiotap/LevelReader.c \ audiotap/Tap.c \ audiotap/__init__.c \ audiodelays/Echo.c \ diff --git a/shared-bindings/audiofilewriter/AudioFileWriter.c b/shared-bindings/audiofilewriter/AudioFileWriter.c index f3d66d4b69f..ea43a847feb 100644 --- a/shared-bindings/audiofilewriter/AudioFileWriter.c +++ b/shared-bindings/audiofilewriter/AudioFileWriter.c @@ -120,14 +120,15 @@ //| samples_signed=mic_format.samples_signed, //| max_packet_size=mic_format.max_packet_size, //| ) -//| # The tap only measures the levels, so it keeps no audio. +//| # The tap is only there to measure the levels, so it keeps no audio. //| tap = audiotap.Tap(capacity=0) +//| levels = audiotap.LevelReader(tap) //| # No file: fetch buffers through the tap at the microphone's rate and drop them. //| sink = AudioFileWriter(None) //| sink.play(tap.play(mic)) //| //| while True: -//| print(f"peak {tap.peak:.3f} rms {tap.rms:.3f}") +//| print(f"peak {levels.peak:.3f} rms {levels.rms:.3f}") //| time.sleep(0.5) //| """ //| ... diff --git a/shared-bindings/audiotap/LevelReader.c b/shared-bindings/audiotap/LevelReader.c new file mode 100644 index 00000000000..ff856b4adc1 --- /dev/null +++ b/shared-bindings/audiotap/LevelReader.c @@ -0,0 +1,133 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include + +#include "shared-bindings/audiotap/LevelReader.h" +#include "shared-bindings/audiotap/Tap.h" + +#include "shared/runtime/context_manager_helpers.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/util.h" + +//| class LevelReader: +//| """Measures the peak and RMS levels of the audio that passes through a `Tap`""" +//| +//| def __init__(self, tap: Tap) -> None: +//| """Create a LevelReader that measures the audio passing through ``tap``. +//| +//| The levels are measured on all of the audio that passes through the +//| tap, whether or not it is read with `Tap.readinto`, so a tap made +//| with a ``capacity`` of 0 can be used for levels alone. +//| +//| A tap can have one LevelReader at a time. `deinit` it to detach it +//| from the tap. +//| +//| :param Tap tap: The tap whose audio is measured. +//| +//| """ +//| ... +//| +static mp_obj_t audiotap_levelreader_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_tap, }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_tap, MP_ARG_OBJ | MP_ARG_REQUIRED, {} }, + }; + + 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); + + audiotap_tap_obj_t *tap = MP_OBJ_TO_PTR(mp_arg_validate_type(args[ARG_tap].u_obj, &audiotap_tap_type, MP_QSTR_tap)); + + audiotap_levelreader_obj_t *self = mp_obj_malloc(audiotap_levelreader_obj_t, &audiotap_levelreader_type); + common_hal_audiotap_levelreader_construct(self, tap); + + return MP_OBJ_FROM_PTR(self); +} + +//| def deinit(self) -> None: +//| """Detaches the LevelReader from its tap, which stops measuring levels.""" +//| ... +//| +static mp_obj_t audiotap_levelreader_deinit(mp_obj_t self_in) { + audiotap_levelreader_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_audiotap_levelreader_deinit(self); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(audiotap_levelreader_deinit_obj, audiotap_levelreader_deinit); + +static void check_for_deinit(audiotap_levelreader_obj_t *self) { + if (common_hal_audiotap_levelreader_deinited(self)) { + raise_deinited_error(); + } +} + +//| def __enter__(self) -> LevelReader: +//| """No-op used by Context Managers.""" +//| ... +//| +// Provided by context manager helper. + +//| def __exit__(self) -> None: +//| """Automatically deinitializes when exiting a context. See +//| :ref:`lifetime-and-contextmanagers` for more info.""" +//| ... +//| +// Provided by context manager helper. + +//| peak: float +//| """The largest magnitude of the audio that passed through the tap since +//| this was last read, from 0.0 to 1.0 of full scale. Reading this starts a +//| new measurement. 0.0 when no audio passed through. The channels of +//| stereo audio are measured together. (read-only)""" +//| +static mp_obj_t audiotap_levelreader_obj_get_peak(mp_obj_t self_in) { + audiotap_levelreader_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + return mp_obj_new_float(common_hal_audiotap_levelreader_get_peak(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(audiotap_levelreader_get_peak_obj, audiotap_levelreader_obj_get_peak); + +MP_PROPERTY_GETTER(audiotap_levelreader_peak_obj, + (mp_obj_t)&audiotap_levelreader_get_peak_obj); + +//| rms: float +//| """The RMS level of the audio that passed through the tap since this was +//| last read, from 0.0 to 1.0 of full scale. Reading this starts a new +//| measurement, separate from `peak`'s. 0.0 when no audio passed through. +//| The channels of stereo audio are measured together. (read-only)""" +//| +//| +static mp_obj_t audiotap_levelreader_obj_get_rms(mp_obj_t self_in) { + audiotap_levelreader_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + return mp_obj_new_float(common_hal_audiotap_levelreader_get_rms(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(audiotap_levelreader_get_rms_obj, audiotap_levelreader_obj_get_rms); + +MP_PROPERTY_GETTER(audiotap_levelreader_rms_obj, + (mp_obj_t)&audiotap_levelreader_get_rms_obj); + +static const mp_rom_map_elem_t audiotap_levelreader_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiotap_levelreader_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_peak), MP_ROM_PTR(&audiotap_levelreader_peak_obj) }, + { MP_ROM_QSTR(MP_QSTR_rms), MP_ROM_PTR(&audiotap_levelreader_rms_obj) }, +}; +static MP_DEFINE_CONST_DICT(audiotap_levelreader_locals_dict, audiotap_levelreader_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + audiotap_levelreader_type, + MP_QSTR_LevelReader, + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, + make_new, audiotap_levelreader_make_new, + locals_dict, &audiotap_levelreader_locals_dict + ); diff --git a/shared-bindings/audiotap/LevelReader.h b/shared-bindings/audiotap/LevelReader.h new file mode 100644 index 00000000000..2a81daa3a4c --- /dev/null +++ b/shared-bindings/audiotap/LevelReader.h @@ -0,0 +1,18 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "shared-module/audiotap/LevelReader.h" + +extern const mp_obj_type_t audiotap_levelreader_type; + +void common_hal_audiotap_levelreader_construct(audiotap_levelreader_obj_t *self, audiotap_tap_obj_t *tap); +void common_hal_audiotap_levelreader_deinit(audiotap_levelreader_obj_t *self); +bool common_hal_audiotap_levelreader_deinited(audiotap_levelreader_obj_t *self); + +mp_float_t common_hal_audiotap_levelreader_get_peak(audiotap_levelreader_obj_t *self); +mp_float_t common_hal_audiotap_levelreader_get_rms(audiotap_levelreader_obj_t *self); diff --git a/shared-bindings/audiotap/Tap.c b/shared-bindings/audiotap/Tap.c index 5e6fb16caf4..5f40994b3dc 100644 --- a/shared-bindings/audiotap/Tap.c +++ b/shared-bindings/audiotap/Tap.c @@ -24,10 +24,10 @@ //| plays the tap gets the sample's audio unchanged. //| //| As the audio passes through, the tap keeps up to ``capacity`` seconds -//| of it in a buffer, which `readinto` copies out of without waiting, and -//| measures its `peak` and `rms` levels. The program can look at -//| the audio this way, at the point in the chain where the tap is, -//| while it plays. +//| of it in a buffer, which `readinto` copies out of without waiting. +//| The program can look at the audio this way, at the point in the +//| chain where the tap is, while it plays. To measure its peak and RMS +//| levels as well, attach a `LevelReader` to the tap. //| //| The tap takes its sample rate, channel count, bits per sample and //| signedness from the first sample it plays, and allocates the buffer @@ -45,8 +45,8 @@ //| never affected. //| //| :param float capacity: How many seconds of audio are kept for -//| `readinto`. 0 keeps none, for a tap that only measures `peak` and -//| `rms`. +//| `readinto`. 0 keeps none, for a tap that is only measured by a +//| `LevelReader`. //| //| Showing the level of a USB microphone while it is heard through the //| DVI output, and keeping the audio for a spectrum display. The @@ -94,6 +94,7 @@ //| ) //| # Keep a quarter second, more than arrives between reads of the loop below. //| tap = audiotap.Tap(capacity=0.25) +//| levels = audiotap.LevelReader(tap) //| mixer = audiomixer.Mixer(voice_count=1, sample_rate=48000, channel_count=mic.channel_count) //| supervisor.runtime.audio.play(mixer) //| mixer.voice[0].play(audiospeed.Resampler(tap.play(mic))) @@ -116,8 +117,8 @@ //| # Read the audio that is waiting. A spectrum display would use these samples. //| while tap.available >= len(samples): //| tap.readinto(samples) -//| peak = tap.peak -//| rms = tap.rms +//| peak = levels.peak +//| rms = levels.rms //| peak_bar.width = max(1, int(peak * display.width)) //| rms_bar.width = max(1, int(rms * display.width)) //| print(f"peak {peak:.3f} rms {rms:.3f}") @@ -249,38 +250,6 @@ static mp_obj_t audiotap_tap_obj_clear(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(audiotap_tap_clear_obj, audiotap_tap_obj_clear); -//| peak: float -//| """The largest magnitude of the audio that passed through since this was -//| last read, from 0.0 to 1.0 of full scale. Reading this starts a new -//| measurement. 0.0 when no audio passed through. The channels of stereo -//| audio are measured together. (read-only)""" -//| -static mp_obj_t audiotap_tap_obj_get_peak(mp_obj_t self_in) { - audiotap_tap_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return mp_obj_new_float(common_hal_audiotap_tap_get_peak(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(audiotap_tap_get_peak_obj, audiotap_tap_obj_get_peak); - -MP_PROPERTY_GETTER(audiotap_tap_peak_obj, - (mp_obj_t)&audiotap_tap_get_peak_obj); - -//| rms: float -//| """The RMS level of the audio that passed through since this was last -//| read, from 0.0 to 1.0 of full scale. Reading this starts a new -//| measurement, separate from `peak`'s. 0.0 when no audio passed through. -//| The channels of stereo audio are measured together. (read-only)""" -//| -static mp_obj_t audiotap_tap_obj_get_rms(mp_obj_t self_in) { - audiotap_tap_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return mp_obj_new_float(common_hal_audiotap_tap_get_rms(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(audiotap_tap_get_rms_obj, audiotap_tap_obj_get_rms); - -MP_PROPERTY_GETTER(audiotap_tap_rms_obj, - (mp_obj_t)&audiotap_tap_get_rms_obj); - //| def play(self, sample: circuitpython_typing.AudioSample, *, loop: bool = False) -> Tap: //| """Plays the sample once when loop=False and continuously when loop=True. //| Does not block. Use `playing` to block. @@ -337,8 +306,6 @@ static const mp_rom_map_elem_t audiotap_tap_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiotap_tap_playing_obj) }, { MP_ROM_QSTR(MP_QSTR_available), MP_ROM_PTR(&audiotap_tap_available_obj) }, { MP_ROM_QSTR(MP_QSTR_overflow), MP_ROM_PTR(&audiotap_tap_overflow_obj) }, - { MP_ROM_QSTR(MP_QSTR_peak), MP_ROM_PTR(&audiotap_tap_peak_obj) }, - { MP_ROM_QSTR(MP_QSTR_rms), MP_ROM_PTR(&audiotap_tap_rms_obj) }, AUDIOSAMPLE_FIELDS, }; static MP_DEFINE_CONST_DICT(audiotap_tap_locals_dict, audiotap_tap_locals_dict_table); diff --git a/shared-bindings/audiotap/Tap.h b/shared-bindings/audiotap/Tap.h index 0b4d99a83c4..1d7b440417a 100644 --- a/shared-bindings/audiotap/Tap.h +++ b/shared-bindings/audiotap/Tap.h @@ -23,5 +23,3 @@ uint32_t common_hal_audiotap_tap_get_available(audiotap_tap_obj_t *self); uint32_t common_hal_audiotap_tap_readinto(audiotap_tap_obj_t *self, uint8_t *buffer, uint32_t length); void common_hal_audiotap_tap_clear(audiotap_tap_obj_t *self); bool common_hal_audiotap_tap_get_overflow(audiotap_tap_obj_t *self); -mp_float_t common_hal_audiotap_tap_get_peak(audiotap_tap_obj_t *self); -mp_float_t common_hal_audiotap_tap_get_rms(audiotap_tap_obj_t *self); diff --git a/shared-bindings/audiotap/__init__.c b/shared-bindings/audiotap/__init__.c index 06278eaf2d7..617f1139766 100644 --- a/shared-bindings/audiotap/__init__.c +++ b/shared-bindings/audiotap/__init__.c @@ -9,17 +9,20 @@ #include "py/obj.h" #include "py/runtime.h" +#include "shared-bindings/audiotap/LevelReader.h" #include "shared-bindings/audiotap/Tap.h" //| """Look at audio as it plays //| //| The `audiotap` module contains `Tap`, which goes into an audio chain like //| the effects in `audiofilters` and `audiodelays`. Audio passes through it -//| unchanged, and the program can read a copy of it and its levels, for meters, -//| waveforms, spectrum analysis or recording, while it plays.""" +//| unchanged, and the program can read a copy of it, for waveforms, spectrum +//| analysis or recording, while it plays. A `LevelReader` attached to a tap +//| measures the peak and RMS levels of its audio, for meters.""" static const mp_rom_map_elem_t audiotap_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audiotap) }, + { MP_ROM_QSTR(MP_QSTR_LevelReader), MP_ROM_PTR(&audiotap_levelreader_type) }, { MP_ROM_QSTR(MP_QSTR_Tap), MP_ROM_PTR(&audiotap_tap_type) }, }; diff --git a/shared-module/audiotap/LevelReader.c b/shared-module/audiotap/LevelReader.c new file mode 100644 index 00000000000..64e4b1bedcd --- /dev/null +++ b/shared-module/audiotap/LevelReader.c @@ -0,0 +1,113 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#include "shared-bindings/audiotap/LevelReader.h" +#include "shared-bindings/audiocore/__init__.h" + +#include +#include + +#include "py/runtime.h" + +void common_hal_audiotap_levelreader_construct(audiotap_levelreader_obj_t *self, audiotap_tap_obj_t *tap) { + audiosample_check_for_deinit(&tap->base); + if (tap->levels != NULL) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q in use"), MP_QSTR_tap); + } + self->tap = tap; + self->peak = 0; + self->peak_reset = false; + self->sum_squares = 0; + self->level_count = 0; + self->rms_reset = false; + // The levels must start from 0 before the tap's writer can see them. + __sync_synchronize(); + tap->levels = self; +} + +bool common_hal_audiotap_levelreader_deinited(audiotap_levelreader_obj_t *self) { + return self->tap == NULL; +} + +void common_hal_audiotap_levelreader_deinit(audiotap_levelreader_obj_t *self) { + audiotap_tap_obj_t *tap = self->tap; + if (tap == NULL) { + return; + } + // The tap stops measuring. If its writer is measuring right now, it + // finishes with this object, which is still allocated, and no harm done. + if (tap->levels == self) { + tap->levels = NULL; + } + self->tap = NULL; +} + +// The sample at i, as a signed 16 bit value. +static int32_t sample_at(const audiosample_base_t *format, const uint8_t *data, uint32_t i) { + if (MP_LIKELY(format->bits_per_sample == 16)) { + // data is the source's 16 bit buffer, advanced only in whole samples, + // so it is aligned. + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" + uint16_t raw = ((const uint16_t *)data)[i]; + #pragma GCC diagnostic pop + return format->samples_signed ? (int16_t)raw : (int16_t)(raw ^ 0x8000); + } + uint8_t raw = data[i]; + return (format->samples_signed ? (int8_t)raw : (int8_t)(raw ^ 0x80)) * 256; +} + +void audiotap_levelreader_measure(audiotap_levelreader_obj_t *self, + const audiosample_base_t *format, const uint8_t *data, uint32_t count) { + if (self->peak_reset) { + self->peak = 0; + self->peak_reset = false; + } + if (self->rms_reset) { + self->sum_squares = 0; + self->level_count = 0; + self->rms_reset = false; + } + uint32_t peak = self->peak; + uint64_t sum = 0; + for (uint32_t i = 0; i < count; i++) { + int32_t value = sample_at(format, data, i); + uint32_t magnitude = value < 0 ? -value : value; + if (magnitude > peak) { + peak = magnitude; + } + sum += (uint64_t)(value * value); + } + self->peak = peak; + self->sum_squares += (float)sum / (32768.0f * 32768.0f); + self->level_count += count; +} + +static void check_tap(audiotap_levelreader_obj_t *self) { + audiosample_check_for_deinit(&self->tap->base); +} + +mp_float_t common_hal_audiotap_levelreader_get_peak(audiotap_levelreader_obj_t *self) { + check_tap(self); + if (self->peak_reset) { + // No audio passed since the last read. + return 0; + } + mp_float_t peak = (mp_float_t)self->peak / MICROPY_FLOAT_CONST(32768.0); + self->peak_reset = true; + return peak; +} + +mp_float_t common_hal_audiotap_levelreader_get_rms(audiotap_levelreader_obj_t *self) { + check_tap(self); + if (self->rms_reset) { + // No audio passed since the last read. + return 0; + } + uint32_t count = self->level_count; + float sum_squares = self->sum_squares; + self->rms_reset = true; + return count > 0 ? MICROPY_FLOAT_C_FUN(sqrt)((mp_float_t)sum_squares / count) : 0; +} diff --git a/shared-module/audiotap/LevelReader.h b/shared-module/audiotap/LevelReader.h new file mode 100644 index 00000000000..6fc5de3853d --- /dev/null +++ b/shared-module/audiotap/LevelReader.h @@ -0,0 +1,32 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#pragma once + +#include "py/obj.h" + +#include "shared-module/audiotap/Tap.h" + +extern const mp_obj_type_t audiotap_levelreader_type; + +typedef struct audiotap_levelreader_obj { + mp_obj_base_t base; + + // The tap whose audio is measured. NULL once deinitialized. + audiotap_tap_obj_t *tap; + + // Levels of the audio since each was last read. Reading one asks for its + // reset, and the tap's writer does it, so that only the writer changes them. + volatile uint32_t peak; // largest magnitude, 0 to 32768 + volatile bool peak_reset; + volatile float sum_squares; // of samples scaled to -1.0 to 1.0 + volatile uint32_t level_count; // samples summed + volatile bool rms_reset; +} audiotap_levelreader_obj_t; + +// Called by the tap for each run of samples that passes through it, by +// whatever plays the tap. It may be an interrupt, so this never allocates. +void audiotap_levelreader_measure(audiotap_levelreader_obj_t *self, + const audiosample_base_t *format, const uint8_t *data, uint32_t count); diff --git a/shared-module/audiotap/Tap.c b/shared-module/audiotap/Tap.c index 279fa4131f3..1ecaed5ddfb 100644 --- a/shared-module/audiotap/Tap.c +++ b/shared-module/audiotap/Tap.c @@ -4,9 +4,9 @@ // // SPDX-License-Identifier: MIT #include "shared-bindings/audiotap/Tap.h" +#include "shared-module/audiotap/LevelReader.h" #include "shared-bindings/audiocore/__init__.h" -#include #include #include @@ -39,11 +39,7 @@ void common_hal_audiotap_tap_construct(audiotap_tap_obj_t *self, mp_float_t capa self->read_pos = 0; self->overflow = false; - self->peak = 0; - self->peak_reset = false; - self->sum_squares = 0; - self->level_count = 0; - self->rms_reset = false; + self->levels = NULL; } void common_hal_audiotap_tap_deinit(audiotap_tap_obj_t *self) { @@ -52,6 +48,7 @@ void common_hal_audiotap_tap_deinit(audiotap_tap_obj_t *self) { self->silence = NULL; self->ring = NULL; self->capacity = 0; + self->levels = NULL; } void audiotap_tap_reset_buffer(audiotap_tap_obj_t *self, @@ -155,51 +152,18 @@ static uint32_t ring_advance(uint32_t capacity, uint32_t pos, uint32_t count) { return pos >= 2 * capacity ? pos - 2 * capacity : pos; } -// The sample at i, as a signed 16 bit value. -static int32_t sample_at(audiotap_tap_obj_t *self, const uint8_t *data, uint32_t i) { - if (MP_LIKELY(self->base.bits_per_sample == 16)) { - // data is the source's 16 bit buffer, or the silence allocated on the - // heap, advanced only in whole samples, so it is aligned. - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wcast-align" - uint16_t raw = ((const uint16_t *)data)[i]; - #pragma GCC diagnostic pop - return self->base.samples_signed ? (int16_t)raw : (int16_t)(raw ^ 0x8000); - } - uint8_t raw = data[i]; - return (self->base.samples_signed ? (int8_t)raw : (int8_t)(raw ^ 0x80)) * 256; -} - // Called by whatever plays the tap, for each run of samples from the source. // It may be an interrupt, so it never allocates and changes only write_pos and -// the levels. +// the attached LevelReader's levels. static void tap_capture(audiotap_tap_obj_t *self, const uint8_t *data, uint32_t count) { if (count == 0) { return; } - if (self->peak_reset) { - self->peak = 0; - self->peak_reset = false; - } - if (self->rms_reset) { - self->sum_squares = 0; - self->level_count = 0; - self->rms_reset = false; - } - uint32_t peak = self->peak; - uint64_t sum = 0; - for (uint32_t i = 0; i < count; i++) { - int32_t value = sample_at(self, data, i); - uint32_t magnitude = value < 0 ? -value : value; - if (magnitude > peak) { - peak = magnitude; - } - sum += (uint64_t)(value * value); + audiotap_levelreader_obj_t *levels = self->levels; + if (levels != NULL) { + audiotap_levelreader_measure(levels, &self->base, data, count); } - self->peak = peak; - self->sum_squares += (float)sum / (32768.0f * 32768.0f); - self->level_count += count; if (self->ring == NULL) { return; @@ -269,27 +233,6 @@ bool common_hal_audiotap_tap_get_overflow(audiotap_tap_obj_t *self) { return overflow; } -mp_float_t common_hal_audiotap_tap_get_peak(audiotap_tap_obj_t *self) { - if (self->peak_reset) { - // No audio passed since the last read. - return 0; - } - mp_float_t peak = (mp_float_t)self->peak / MICROPY_FLOAT_CONST(32768.0); - self->peak_reset = true; - return peak; -} - -mp_float_t common_hal_audiotap_tap_get_rms(audiotap_tap_obj_t *self) { - if (self->rms_reset) { - // No audio passed since the last read. - return 0; - } - uint32_t count = self->level_count; - float sum_squares = self->sum_squares; - self->rms_reset = true; - return count > 0 ? MICROPY_FLOAT_C_FUN(sqrt)((mp_float_t)sum_squares / count) : 0; -} - audioio_get_buffer_result_t audiotap_tap_get_buffer(audiotap_tap_obj_t *self, bool single_channel_output, uint8_t channel, uint8_t **buffer, uint32_t *buffer_length) { (void)single_channel_output; diff --git a/shared-module/audiotap/Tap.h b/shared-module/audiotap/Tap.h index 2961a968685..acad4dda132 100644 --- a/shared-module/audiotap/Tap.h +++ b/shared-module/audiotap/Tap.h @@ -11,6 +11,8 @@ extern const mp_obj_type_t audiotap_tap_type; +struct audiotap_levelreader_obj; + typedef struct { audiosample_base_t base; @@ -41,13 +43,8 @@ typedef struct { // Audio was dropped because the ring was full. Cleared when read. volatile bool overflow; - // Levels of the audio since each was last read. Reading one asks for its - // reset, and the writer does it, so that only the writer changes them. - volatile uint32_t peak; // largest magnitude, 0 to 32768 - volatile bool peak_reset; - volatile float sum_squares; // of samples scaled to -1.0 to 1.0 - volatile uint32_t level_count; // samples summed - volatile bool rms_reset; + // Measures the audio that passes through, when a LevelReader is attached. + struct audiotap_levelreader_obj *volatile levels; } audiotap_tap_obj_t; void audiotap_tap_reset_buffer(audiotap_tap_obj_t *self, diff --git a/shared-module/audiotap/__init__.c b/shared-module/audiotap/__init__.c index 4ab4bccdc69..c10944ee211 100644 --- a/shared-module/audiotap/__init__.c +++ b/shared-module/audiotap/__init__.c @@ -4,4 +4,5 @@ // // SPDX-License-Identifier: MIT -// Nothing to do here. The module's only state is in its Tap objects. +// Nothing to do here. The module's only state is in its Tap and LevelReader +// objects. diff --git a/tests/circuitpython/audiotap_levelreader.py b/tests/circuitpython/audiotap_levelreader.py new file mode 100644 index 00000000000..ccd14e2d9dd --- /dev/null +++ b/tests/circuitpython/audiotap_levelreader.py @@ -0,0 +1,85 @@ +import array +from audiocore import get_buffer, RawSample +from audiotap import LevelReader, Tap + +ramp = array.array("h", range(-300, 300, 3)) # 200 samples +source = RawSample(ramp, sample_rate=8000) + + +def output(sample, times=1): + # Stands in for an audio output, which pulls the audio through the tap + # and plays it. Returns what it played. + out = [] + for _ in range(times): + result, buffer = get_buffer(sample) + out.extend(buffer) + return out + + +# Before any audio passes, the levels are 0. +tap = Tap(capacity=0.125) +levels = LevelReader(tap) +print("idle", levels.peak, levels.rms) + +# Peak and RMS are measured separately, each since it was last read. +tap.play(source, loop=True) +output(tap, 4) +print("peak", round(levels.peak, 4), levels.peak) +output(tap) +print("rms", round(levels.rms, 4), levels.rms) +print("peak again", round(levels.peak, 4)) + +# Measuring does not take audio from readinto. +copy = array.array("h", [0] * 1000) +print("read", tap.readinto(copy), list(copy[:200]) == list(ramp)) + +# A tap has one LevelReader at a time. +try: + LevelReader(tap) +except ValueError as error: + print("second", error) + +# deinit detaches it, and audio that passes after that is not measured. +levels.deinit() +levels.deinit() +try: + levels.peak +except ValueError as error: + print("deinit", error) +output(tap) +with LevelReader(tap) as again: + print("not measured", again.peak, again.rms) + output(tap) + print("measured", round(again.peak, 4)) +print("detached by exit", LevelReader(tap).peak) + +# Levels only: a tap that keeps no audio. +meter = Tap(capacity=0) +meter_levels = LevelReader(meter) +meter.play(RawSample(array.array("h", [16384, -32768] * 16), sample_rate=8000), loop=True) +output(meter) +print("meter", meter.available, meter_levels.peak, meter_levels.rms) + +# 8 bit unsigned levels: 0x80 is silence. +eight = Tap(capacity=0.002) +eight_levels = LevelReader(eight) +eight.play(RawSample(array.array("B", [0x80, 0xC0] * 8), sample_rate=8000), loop=True) +output(eight) +print("8 bit", eight.bits_per_sample, eight_levels.peak, eight_levels.rms) + +# Only a Tap can be measured. +try: + LevelReader(source) +except TypeError as error: + print("not a tap", error) + +# A deinitialized tap can't be measured. +meter.deinit() +try: + meter_levels.peak +except ValueError as error: + print("tap deinit", error) +try: + LevelReader(meter) +except ValueError as error: + print("new on deinit tap", error) diff --git a/tests/circuitpython/audiotap_levelreader.py.exp b/tests/circuitpython/audiotap_levelreader.py.exp new file mode 100644 index 00000000000..9b8dad1fef4 --- /dev/null +++ b/tests/circuitpython/audiotap_levelreader.py.exp @@ -0,0 +1,15 @@ +idle 0.0 0.0 +peak 0.0092 0.0 +rms 0.0053 0.0 +peak again 0.0092 +read 1000 True +second tap in use +deinit Object has been deinitialized and can no longer be used. Create a new object. +not measured 0.0 0.0 +measured 0.0092 +detached by exit 0.0 +meter 0 1.0 0.7905694150420949 +8 bit 8 0.5 0.3535533905932738 +not a tap tap must be of type Tap, not RawSample +tap deinit Object has been deinitialized and can no longer be used. Create a new object. +new on deinit tap Object has been deinitialized and can no longer be used. Create a new object. diff --git a/tests/circuitpython/audiotap_tap.py b/tests/circuitpython/audiotap_tap.py index f5797bc3f18..8631b9353df 100644 --- a/tests/circuitpython/audiotap_tap.py +++ b/tests/circuitpython/audiotap_tap.py @@ -19,7 +19,7 @@ def output(sample, times=1): # Before a sample is played, the tap has no format and keeps nothing. tap = Tap(capacity=0.125) -print("idle", tap.playing, tap.available, tap.peak, tap.rms) +print("idle", tap.playing, tap.available) # The tap takes the format of the first sample it plays and keeps 125 ms of it. tap.play(source, loop=True) @@ -33,11 +33,8 @@ def output(sample, times=1): print("read", tap.readinto(copy), list(copy[:800]) == played, tap.available) print("read empty", tap.readinto(copy)) -# Peak and RMS are measured separately, each since it was last read. -print("peak", round(tap.peak, 4), tap.peak) -output(tap) -print("rms", round(tap.rms, 4), tap.rms) -print("peak again", round(tap.peak, 4)) +# Levels are measured by a LevelReader (audiotap_levelreader.py), not the tap. +print("no levels", hasattr(tap, "peak"), hasattr(tap, "rms")) # Later samples must match the first one's format. try: @@ -69,11 +66,10 @@ def output(sample, times=1): part = array.array("h", [7] * 8) print("slice", small.readinto(memoryview(part)[2:5]), list(part)) -# Levels only. -meter = Tap(capacity=0) -meter.play(RawSample(array.array("h", [16384, -32768] * 16), sample_rate=8000), loop=True) -output(meter) -print("meter", meter.available, meter.readinto(copy), meter.peak, meter.rms) +# A tap that keeps nothing still passes audio through. +nothing = Tap(capacity=0) +nothing.play(source, loop=True) +print("keeps nothing", output(nothing) == list(ramp), nothing.available, nothing.readinto(copy)) # Stereo reads whole frames. stereo = Tap(capacity=0.004) # 32 frames @@ -82,12 +78,6 @@ def output(sample, times=1): odd = array.array("h", [0] * 5) print("stereo", stereo.channel_count, stereo.readinto(odd), list(odd)) -# 8 bit unsigned levels: 0x80 is silence. -eight = Tap(capacity=0.002) -eight.play(RawSample(array.array("B", [0x80, 0xC0] * 8), sample_rate=8000), loop=True) -output(eight) -print("8 bit", eight.bits_per_sample, eight.peak, eight.rms) - # In a chain, and the end of a sample that does not loop: then the tap plays silence. once = Tap() chain = Filter(buffer_size=128, sample_rate=8000) diff --git a/tests/circuitpython/audiotap_tap.py.exp b/tests/circuitpython/audiotap_tap.py.exp index 142f8b87d1d..2150262b1ee 100644 --- a/tests/circuitpython/audiotap_tap.py.exp +++ b/tests/circuitpython/audiotap_tap.py.exp @@ -1,12 +1,10 @@ -idle False 0 0.0 0.0 +idle False 0 playing True 8000 1 16 played 800 [-300, -297, -294] True available 800 read 800 True 0 read empty 0 -peak 0.0092 0.0 -rms 0.0053 0.0 -peak again 0.0092 +no levels False False mismatch The sample's sample_rate does not match pieces [100, 100, 50] True full 80 True False @@ -14,9 +12,8 @@ kept oldest 80 True cleared 0 bytearray 5 75 slice 3 [7, 7, -285, -282, -279, 7, 7, 7] -meter 0 0 1.0 0.7905694150420949 +keeps nothing True 0 0 stereo 2 4 [0, 1, 2, 3, 0] -8 bit 8 0.5 0.3535533905932738 ended False 200 silence {0} 200 deinit Object has been deinitialized and can no longer be used. Create a new object.