Skip to content

Added mono to stereo conversion and panning to audiomixer.Mixer #10529

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions shared-bindings/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ MP_PROPERTY_GETSET(audiomixer_mixervoice_level_obj,
(mp_obj_t)&audiomixer_mixervoice_get_level_obj,
(mp_obj_t)&audiomixer_mixervoice_set_level_obj);

//| panning: synthio.BlockInput
//| """Defines the channel(s) in which the voice appears, as a floating point number between
//| -1 and 1. If your board does not support synthio, this property will only accept a float
//| value. This property is ignored if ``audiomixer.Mixer.channel_count=1``.
//|
//| -1 is left channel only, 0 is both channels, and 1 is right channel. For fractional values,
//| the note plays at full amplitude in one channel and partial amplitude in the other channel.
//| For instance -.5 plays at full amplitude in the left channel and 1/2 amplitude in the right
//| channel."""
static mp_obj_t audiomixer_mixervoice_obj_get_panning(mp_obj_t self_in) {
return common_hal_audiomixer_mixervoice_get_panning(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(audiomixer_mixervoice_get_panning_obj, audiomixer_mixervoice_obj_get_panning);

static mp_obj_t audiomixer_mixervoice_obj_set_panning(mp_obj_t self_in, mp_obj_t panning_in) {
audiomixer_mixervoice_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiomixer_mixervoice_set_panning(self, panning_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audiomixer_mixervoice_set_panning_obj, audiomixer_mixervoice_obj_set_panning);

MP_PROPERTY_GETSET(audiomixer_mixervoice_panning_obj,
(mp_obj_t)&audiomixer_mixervoice_get_panning_obj,
(mp_obj_t)&audiomixer_mixervoice_set_panning_obj);

//| loop: bool
//| """Get or set the loop status of the currently playing sample."""
static mp_obj_t audiomixer_mixervoice_obj_get_loop(mp_obj_t self_in) {
Expand Down Expand Up @@ -158,6 +183,7 @@ static const mp_rom_map_elem_t audiomixer_mixervoice_locals_dict_table[] = {
// Properties
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiomixer_mixervoice_playing_obj) },
{ MP_ROM_QSTR(MP_QSTR_level), MP_ROM_PTR(&audiomixer_mixervoice_level_obj) },
{ MP_ROM_QSTR(MP_QSTR_panning), MP_ROM_PTR(&audiomixer_mixervoice_panning_obj) },
{ MP_ROM_QSTR(MP_QSTR_loop), MP_ROM_PTR(&audiomixer_mixervoice_loop_obj) },
};
static MP_DEFINE_CONST_DICT(audiomixer_mixervoice_locals_dict, audiomixer_mixervoice_locals_dict_table);
Expand Down
2 changes: 2 additions & 0 deletions shared-bindings/audiomixer/MixerVoice.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void common_hal_audiomixer_mixervoice_stop(audiomixer_mixervoice_obj_t *self);
void common_hal_audiomixer_mixervoice_end(audiomixer_mixervoice_obj_t *self);
mp_obj_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self);
void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_obj_t gain);
mp_obj_t common_hal_audiomixer_mixervoice_get_panning(audiomixer_mixervoice_obj_t *self);
void common_hal_audiomixer_mixervoice_set_panning(audiomixer_mixervoice_obj_t *self, mp_obj_t value);

bool common_hal_audiomixer_mixervoice_get_playing(audiomixer_mixervoice_obj_t *self);

Expand Down
4 changes: 2 additions & 2 deletions shared-module/audiocore/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ void audiosample_convert_s16s_u8s(uint8_t *buffer_out, const int16_t *buffer_in,
}
}

void audiosample_must_match(audiosample_base_t *self, mp_obj_t other_in) {
void audiosample_must_match(audiosample_base_t *self, mp_obj_t other_in, bool allow_mono_to_stereo) {
const audiosample_base_t *other = audiosample_check(other_in);
if (other->sample_rate != self->sample_rate) {
mp_raise_ValueError_varg(MP_ERROR_TEXT("The sample's %q does not match"), MP_QSTR_sample_rate);
}
if (other->channel_count != self->channel_count) {
if ((!allow_mono_to_stereo || (allow_mono_to_stereo && self->channel_count != 2)) && other->channel_count != self->channel_count) {
mp_raise_ValueError_varg(MP_ERROR_TEXT("The sample's %q does not match"), MP_QSTR_channel_count);
}
if (other->bits_per_sample != self->bits_per_sample) {
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiocore/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static inline void audiosample_get_buffer_structure_checked(mp_obj_t self_in, bo
audiosample_get_buffer_structure(audiosample_check(self_in), single_channel_output, single_buffer, samples_signed, max_buffer_length, spacing);
}

void audiosample_must_match(audiosample_base_t *self, mp_obj_t other);
void audiosample_must_match(audiosample_base_t *self, mp_obj_t other, bool allow_mono_to_stereo);

void audiosample_convert_u8m_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
void audiosample_convert_u8s_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiodelays/Chorus.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool common_hal_audiodelays_chorus_get_playing(audiodelays_chorus_obj_t *self) {
}

void common_hal_audiodelays_chorus_play(audiodelays_chorus_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiodelays/Echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ bool common_hal_audiodelays_echo_get_playing(audiodelays_echo_obj_t *self) {
}

void common_hal_audiodelays_echo_play(audiodelays_echo_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiodelays/MultiTapDelay.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ bool common_hal_audiodelays_multi_tap_delay_get_playing(audiodelays_multi_tap_de
}

void common_hal_audiodelays_multi_tap_delay_play(audiodelays_multi_tap_delay_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiodelays/PitchShift.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bool common_hal_audiodelays_pitch_shift_get_playing(audiodelays_pitch_shift_obj_
}

void common_hal_audiodelays_pitch_shift_play(audiodelays_pitch_shift_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiofilters/Distortion.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ bool common_hal_audiofilters_distortion_get_playing(audiofilters_distortion_obj_
}

void common_hal_audiofilters_distortion_play(audiofilters_distortion_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiofilters/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bool common_hal_audiofilters_filter_get_playing(audiofilters_filter_obj_t *self)
}

void common_hal_audiofilters_filter_play(audiofilters_filter_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiofilters/Phaser.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool common_hal_audiofilters_phaser_get_playing(audiofilters_phaser_obj_t *self)
}

void common_hal_audiofilters_phaser_play(audiofilters_phaser_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion shared-module/audiofreeverb/Freeverb.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ bool common_hal_audiofreeverb_freeverb_get_playing(audiofreeverb_freeverb_obj_t
}

void common_hal_audiofreeverb_freeverb_play(audiofreeverb_freeverb_obj_t *self, mp_obj_t sample, bool loop) {
audiosample_must_match(&self->base, sample);
audiosample_must_match(&self->base, sample, false);

self->sample = sample;
self->loop = loop;
Expand Down
Loading
Loading