Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio: reduce code size #10036

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ports/atmel-samd/audio_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
}


if (audiosample_bits_per_sample(sample) == 16) {
if (audiosample_get_bits_per_sample(sample) == 16) {
dma->beat_size = 2;
dma->bytes_per_sample = 2;
} else {
Expand All @@ -262,7 +262,7 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
}
}
// Transfer both channels at once.
if (!single_channel_output && audiosample_channel_count(sample) == 2) {
if (!single_channel_output && audiosample_get_channel_count(sample) == 2) {
dma->beat_size *= 2;
}

Expand Down
5 changes: 1 addition & 4 deletions ports/atmel-samd/audio_dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "py/obj.h"
#include "shared-module/audiocore/RawSample.h"
#include "shared-module/audiocore/WaveFile.h"
#include "shared-module/audiocore/__init__.h"
#include "supervisor/background_callback.h"

typedef struct {
Expand Down Expand Up @@ -40,10 +41,6 @@ typedef enum {
AUDIO_DMA_MEMORY_ERROR,
} audio_dma_result;

uint32_t audiosample_sample_rate(mp_obj_t sample_obj);
uint8_t audiosample_bits_per_sample(mp_obj_t sample_obj);
uint8_t audiosample_channel_count(mp_obj_t sample_obj);

void audio_dma_init(audio_dma_t *dma);
void audio_dma_reset(void);

Expand Down
8 changes: 4 additions & 4 deletions ports/atmel-samd/common-hal/audiobusio/I2SOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
mp_raise_RuntimeError(MP_ERROR_TEXT("Clock unit in use"));
}
#endif
uint8_t bits_per_sample = audiosample_bits_per_sample(sample);
uint8_t bits_per_sample = audiosample_get_bits_per_sample(sample);
// We always output stereo so output twice as many bits.
uint16_t bits_per_sample_output = bits_per_sample * 2;
uint16_t divisor = 48000000 / (bits_per_sample_output * audiosample_sample_rate(sample));
uint16_t divisor = 48000000 / (bits_per_sample_output * audiosample_get_sample_rate(sample));
// Find a free GCLK to generate the MCLK signal.
uint8_t gclk = find_free_gclk(divisor);
if (gclk > GCLK_GEN_NUM) {
Expand All @@ -235,7 +235,7 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
} else {
clkctrl |= I2S_CLKCTRL_FSOUTINV | I2S_CLKCTRL_BITDELAY_I2S;
}
uint8_t channel_count = audiosample_channel_count(sample);
uint8_t channel_count = audiosample_get_channel_count(sample);
if (channel_count > 2) {
mp_raise_ValueError(MP_ERROR_TEXT("Too many channels in sample"));
}
Expand All @@ -245,7 +245,7 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
#ifdef SAM_D5X_E5X
uint32_t serctrl = (self->clock_unit << I2S_RXCTRL_CLKSEL_Pos) | I2S_TXCTRL_TXSAME_SAME;
#endif
if (audiosample_channel_count(sample) == 1) {
if (audiosample_get_channel_count(sample) == 1) {
serctrl |= SERCTRL(MONO_MONO);
} else {
serctrl |= SERCTRL(MONO_STEREO);
Expand Down
8 changes: 4 additions & 4 deletions ports/atmel-samd/common-hal/audioio/AudioOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t *self,
common_hal_audioio_audioout_stop(self);
}
audio_dma_result result = AUDIO_DMA_OK;
uint32_t sample_rate = audiosample_sample_rate(sample);
uint32_t sample_rate = audiosample_get_sample_rate(sample);
#ifdef SAMD21
const uint32_t max_sample_rate = 350000;
#endif
Expand Down Expand Up @@ -364,12 +364,12 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t *self,
right_channel_reg = (uint32_t)&DAC->DATABUF[0].reg;
}

size_t num_channels = audiosample_channel_count(sample);
size_t num_channels = audiosample_get_channel_count(sample);

if (num_channels == 2 &&
// Are DAC channels sequential?
left_channel_reg + 2 == right_channel_reg &&
audiosample_bits_per_sample(sample) == 16) {
audiosample_get_bits_per_sample(sample) == 16) {
result = audio_dma_setup_playback(&self->left_dma, sample, loop, false, 0,
false /* output unsigned */,
left_channel_reg,
Expand Down Expand Up @@ -403,7 +403,7 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t *self,
}
}
Tc *timer = tc_insts[self->tc_index];
set_timer_frequency(timer, audiosample_sample_rate(sample));
set_timer_frequency(timer, audiosample_get_sample_rate(sample));
timer->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_RETRIGGER;
while (timer->COUNT16.STATUS.bit.STOP == 1) {
}
Expand Down
6 changes: 3 additions & 3 deletions ports/espressif/common-hal/audiobusio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {
port_i2s_pause(self);
self->sample = sample;
self->loop = loop;
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
self->channel_count = audiosample_channel_count(sample);
self->bytes_per_sample = audiosample_get_bits_per_sample(sample) / 8;
self->channel_count = audiosample_get_channel_count(sample);
bool single_buffer;
bool samples_signed;
uint32_t max_buffer_length;
Expand All @@ -164,7 +164,7 @@ void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {

audiosample_reset_buffer(self->sample, false, 0);

uint32_t sample_rate = audiosample_sample_rate(sample);
uint32_t sample_rate = audiosample_get_sample_rate(sample);
i2s_std_clk_config_t clk_config = I2S_STD_CLK_DEFAULT_CONFIG(sample_rate);
CHECK_ESP_RESULT(i2s_channel_reconfig_std_clock(self->handle, &clk_config));

Expand Down
6 changes: 3 additions & 3 deletions ports/espressif/common-hal/audioio/AudioOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,16 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t *self,

self->sample = sample;
self->looping = loop;
freq_hz = audiosample_sample_rate(self->sample);
freq_hz = audiosample_get_sample_rate(self->sample);

if (freq_hz != self->freq_hz) {
common_hal_audioio_audioout_deinit(self);
self->freq_hz = freq_hz;
audioout_init(self);
}

samples_size = audiosample_bits_per_sample(self->sample);
channel_count = audiosample_channel_count(self->sample);
samples_size = audiosample_get_bits_per_sample(self->sample);
channel_count = audiosample_get_channel_count(self->sample);
audiosample_get_buffer_structure(self->sample, false,
&_single_buffer, &samples_signed,
&_max_buffer_length, &_spacing);
Expand Down
3 changes: 0 additions & 3 deletions ports/espressif/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,3 @@ USB_NUM_IN_ENDPOINTS = 5

# Usually lots of flash space available
CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1

CIRCUITPY_AUDIOMP3 ?= 1
CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR ?= 1
6 changes: 3 additions & 3 deletions ports/mimxrt10xx/common-hal/audiobusio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ static void set_sai_clocking_for_sample_rate(uint32_t sample_rate) {
void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {
self->sample = sample;
self->loop = loop;
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
self->channel_count = audiosample_channel_count(sample);
self->bytes_per_sample = audiosample_get_bits_per_sample(sample) / 8;
self->channel_count = audiosample_get_channel_count(sample);
int instance = SAI_GetInstance(self->peripheral);
i2s_playing |= (1 << instance);
uint32_t sample_rate = audiosample_sample_rate(sample);
uint32_t sample_rate = audiosample_get_sample_rate(sample);
if (sample_rate != self->sample_rate) {
if (__builtin_popcount(i2s_playing) <= 1) {
// as this is the first/only i2s instance playing audio, we can
Expand Down
4 changes: 2 additions & 2 deletions ports/nordic/common-hal/audiobusio/I2SOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,

self->sample = sample;
self->loop = loop;
uint32_t sample_rate = audiosample_sample_rate(sample);
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
uint32_t sample_rate = audiosample_get_sample_rate(sample);
self->bytes_per_sample = audiosample_get_bits_per_sample(sample) / 8;

uint32_t max_buffer_length;
bool single_buffer, samples_signed;
Expand Down
6 changes: 3 additions & 3 deletions ports/nordic/common-hal/audiopwmio/PWMAudioOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
self->sample = sample;
self->loop = loop;

uint32_t sample_rate = audiosample_sample_rate(sample);
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
uint32_t sample_rate = audiosample_get_sample_rate(sample);
self->bytes_per_sample = audiosample_get_bits_per_sample(sample) / 8;

uint32_t max_buffer_length;
uint8_t spacing;
audiosample_get_buffer_structure(sample, /* single channel */ false,
&self->single_buffer, &self->signed_to_unsigned, &max_buffer_length,
&spacing);
self->sample_channel_count = audiosample_channel_count(sample);
self->sample_channel_count = audiosample_get_channel_count(sample);

mp_arg_validate_length_max(max_buffer_length, UINT16_MAX, MP_QSTR_buffer);

Expand Down
4 changes: 2 additions & 2 deletions ports/raspberrypi/audio_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ audio_dma_result audio_dma_setup_playback(
dma->output_signed = output_signed;
dma->sample_spacing = 1;
dma->output_resolution = output_resolution;
dma->sample_resolution = audiosample_bits_per_sample(sample);
dma->sample_resolution = audiosample_get_bits_per_sample(sample);
dma->output_register_address = output_register_address;
dma->swap_channel = swap_channel;

Expand Down Expand Up @@ -250,7 +250,7 @@ audio_dma_result audio_dma_setup_playback(
dma->output_size = 1;
}
// Transfer both channels at once.
if (!single_channel_output && audiosample_channel_count(sample) == 2) {
if (!single_channel_output && audiosample_get_channel_count(sample) == 2) {
dma->output_size *= 2;
}
enum dma_channel_transfer_size dma_size = DMA_SIZE_8;
Expand Down
6 changes: 3 additions & 3 deletions ports/raspberrypi/common-hal/audiobusio/I2SOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
common_hal_audiobusio_i2sout_stop(self);
}

uint8_t bits_per_sample = audiosample_bits_per_sample(sample);
uint8_t bits_per_sample = audiosample_get_bits_per_sample(sample);
// Make sure we transmit a minimum of 16 bits.
// TODO: Maybe we need an intermediate object to upsample instead. This is
// only needed for some I2S devices that expect at least 8.
Expand All @@ -242,8 +242,8 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
// We always output stereo so output twice as many bits.
uint16_t bits_per_sample_output = bits_per_sample * 2;
size_t clocks_per_bit = 6;
uint32_t frequency = bits_per_sample_output * audiosample_sample_rate(sample);
uint8_t channel_count = audiosample_channel_count(sample);
uint32_t frequency = bits_per_sample_output * audiosample_get_sample_rate(sample);
uint8_t channel_count = audiosample_get_channel_count(sample);
if (channel_count > 2) {
mp_raise_ValueError(MP_ERROR_TEXT("Too many channels in sample."));
}
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
// to trigger the DMA. Each has a 16 bit fractional divisor system clock * X / Y where X and Y
// are 16-bit.

uint32_t sample_rate = audiosample_sample_rate(sample);
uint32_t sample_rate = audiosample_get_sample_rate(sample);

uint32_t system_clock = common_hal_mcu_processor_get_frequency();
uint32_t best_denominator;
Expand Down
6 changes: 3 additions & 3 deletions ports/stm/common-hal/audiopwmio/PWMAudioOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,16 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
self->sample = sample;
self->loop = loop;

uint32_t sample_rate = audiosample_sample_rate(sample);
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
uint32_t sample_rate = audiosample_get_sample_rate(sample);
self->bytes_per_sample = audiosample_get_bits_per_sample(sample) / 8;

uint32_t max_buffer_length;
uint8_t spacing;
bool single_buffer;
bool samples_signed;
audiosample_get_buffer_structure(sample, /* single channel */ false,
&single_buffer, &samples_signed, &max_buffer_length, &spacing);
self->sample_channel_count = audiosample_channel_count(sample);
self->sample_channel_count = audiosample_get_channel_count(sample);
self->sample_offset = (samples_signed ? 0x8000 : 0) - self->quiescent_value;

free_buffers(self);
Expand Down
1 change: 0 additions & 1 deletion ports/unix/variants/coverage/mpconfigvariant.mk
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ CFLAGS += \
-DCIRCUITPY_AUDIOFILTERS=1 \
-DCIRCUITPY_AUDIOMIXER=1 \
-DCIRCUITPY_AUDIOMP3=1 \
-DCIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR=0 \
-DCIRCUITPY_AUDIOCORE_DEBUG=1 \
-DCIRCUITPY_BITMAPTOOLS=1 \
-DCIRCUITPY_CODEOP=1 \
Expand Down
31 changes: 2 additions & 29 deletions shared-bindings/audiocore/RawSample.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "py/runtime.h"
#include "shared-bindings/util.h"
#include "shared-bindings/audiocore/RawSample.h"
#include "shared-bindings/audiocore/__init__.h"

//| class RawSample:
//| """A raw audio sample buffer in memory"""
Expand Down Expand Up @@ -120,12 +121,6 @@ static mp_obj_t audioio_rawsample_deinit(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(audioio_rawsample_deinit_obj, audioio_rawsample_deinit);

static void check_for_deinit(audioio_rawsample_obj_t *self) {
if (common_hal_audioio_rawsample_deinited(self)) {
raise_deinited_error();
}
}

//| def __enter__(self) -> RawSample:
//| """No-op used by Context Managers."""
//| ...
Expand All @@ -151,24 +146,6 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_rawsample___exit___obj, 4, 4,
//| change it."""
//|
//|
static mp_obj_t audioio_rawsample_obj_get_sample_rate(mp_obj_t self_in) {
audioio_rawsample_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_audioio_rawsample_get_sample_rate(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(audioio_rawsample_get_sample_rate_obj, audioio_rawsample_obj_get_sample_rate);

static mp_obj_t audioio_rawsample_obj_set_sample_rate(mp_obj_t self_in, mp_obj_t sample_rate) {
audioio_rawsample_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_audioio_rawsample_set_sample_rate(self, mp_obj_get_int(sample_rate));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audioio_rawsample_set_sample_rate_obj, audioio_rawsample_obj_set_sample_rate);

MP_PROPERTY_GETSET(audioio_rawsample_sample_rate_obj,
(mp_obj_t)&audioio_rawsample_get_sample_rate_obj,
(mp_obj_t)&audioio_rawsample_set_sample_rate_obj);

static const mp_rom_map_elem_t audioio_rawsample_locals_dict_table[] = {
// Methods
Expand All @@ -177,18 +154,14 @@ static const mp_rom_map_elem_t audioio_rawsample_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_rawsample___exit___obj) },

// Properties
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&audioio_rawsample_sample_rate_obj) },
AUDIOSAMPLE_FIELDS,
};
static MP_DEFINE_CONST_DICT(audioio_rawsample_locals_dict, audioio_rawsample_locals_dict_table);

static const audiosample_p_t audioio_rawsample_proto = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample)
.sample_rate = (audiosample_sample_rate_fun)common_hal_audioio_rawsample_get_sample_rate,
.bits_per_sample = (audiosample_bits_per_sample_fun)common_hal_audioio_rawsample_get_bits_per_sample,
.channel_count = (audiosample_channel_count_fun)common_hal_audioio_rawsample_get_channel_count,
.reset_buffer = (audiosample_reset_buffer_fun)audioio_rawsample_reset_buffer,
.get_buffer = (audiosample_get_buffer_fun)audioio_rawsample_get_buffer,
.get_buffer_structure = (audiosample_get_buffer_structure_fun)audioio_rawsample_get_buffer_structure,
};

MP_DEFINE_CONST_OBJ_TYPE(
Expand Down
Loading