audiosample: convert to use a protocol
This eases addition of new sample sources, since the manual virtual function dispatch functions are just calls via a protocol
This commit is contained in:
parent
238e121236
commit
feb8eb935b
|
@ -180,9 +180,20 @@ STATIC const mp_rom_map_elem_t audioio_rawsample_locals_dict_table[] = {
|
|||
};
|
||||
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,
|
||||
};
|
||||
|
||||
const mp_obj_type_t audioio_rawsample_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_RawSample,
|
||||
.make_new = audioio_rawsample_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&audioio_rawsample_locals_dict,
|
||||
.protocol = &audioio_rawsample_proto,
|
||||
};
|
||||
|
|
|
@ -39,6 +39,8 @@ void common_hal_audioio_rawsample_construct(audioio_rawsample_obj_t* self,
|
|||
void common_hal_audioio_rawsample_deinit(audioio_rawsample_obj_t* self);
|
||||
bool common_hal_audioio_rawsample_deinited(audioio_rawsample_obj_t* self);
|
||||
uint32_t common_hal_audioio_rawsample_get_sample_rate(audioio_rawsample_obj_t* self);
|
||||
uint8_t common_hal_audioio_rawsample_get_bits_per_sample(audioio_rawsample_obj_t* self);
|
||||
uint8_t common_hal_audioio_rawsample_get_channel_count(audioio_rawsample_obj_t* self);
|
||||
void common_hal_audioio_rawsample_set_sample_rate(audioio_rawsample_obj_t* self, uint32_t sample_rate);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
|
||||
|
|
|
@ -206,9 +206,21 @@ STATIC const mp_rom_map_elem_t audioio_wavefile_locals_dict_table[] = {
|
|||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(audioio_wavefile_locals_dict, audioio_wavefile_locals_dict_table);
|
||||
|
||||
STATIC const audiosample_p_t audioio_wavefile_proto = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample)
|
||||
.sample_rate = (audiosample_sample_rate_fun)common_hal_audioio_wavefile_get_sample_rate,
|
||||
.bits_per_sample = (audiosample_bits_per_sample_fun)common_hal_audioio_wavefile_get_bits_per_sample,
|
||||
.channel_count = (audiosample_channel_count_fun)common_hal_audioio_wavefile_get_channel_count,
|
||||
.reset_buffer = (audiosample_reset_buffer_fun)audioio_wavefile_reset_buffer,
|
||||
.get_buffer = (audiosample_get_buffer_fun)audioio_wavefile_get_buffer,
|
||||
.get_buffer_structure = (audiosample_get_buffer_structure_fun)audioio_wavefile_get_buffer_structure,
|
||||
};
|
||||
|
||||
|
||||
const mp_obj_type_t audioio_wavefile_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_WaveFile,
|
||||
.make_new = audioio_wavefile_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&audioio_wavefile_locals_dict,
|
||||
.protocol = &audioio_wavefile_proto,
|
||||
};
|
||||
|
|
|
@ -291,9 +291,20 @@ STATIC const mp_rom_map_elem_t audiomixer_mixer_locals_dict_table[] = {
|
|||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(audiomixer_mixer_locals_dict, audiomixer_mixer_locals_dict_table);
|
||||
|
||||
STATIC const audiosample_p_t audiomixer_mixer_proto = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample)
|
||||
.sample_rate = (audiosample_sample_rate_fun)common_hal_audiomixer_mixer_get_sample_rate,
|
||||
.bits_per_sample = (audiosample_bits_per_sample_fun)common_hal_audiomixer_mixer_get_bits_per_sample,
|
||||
.channel_count = (audiosample_channel_count_fun)common_hal_audiomixer_mixer_get_channel_count,
|
||||
.reset_buffer = (audiosample_reset_buffer_fun)audiomixer_mixer_reset_buffer,
|
||||
.get_buffer = (audiosample_get_buffer_fun)audiomixer_mixer_get_buffer,
|
||||
.get_buffer_structure = (audiosample_get_buffer_structure_fun)audiomixer_mixer_get_buffer_structure,
|
||||
};
|
||||
|
||||
const mp_obj_type_t audiomixer_mixer_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Mixer,
|
||||
.make_new = audiomixer_mixer_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&audiomixer_mixer_locals_dict,
|
||||
.protocol = &audiomixer_mixer_proto,
|
||||
};
|
||||
|
|
|
@ -47,5 +47,7 @@ bool common_hal_audiomixer_mixer_deinited(audiomixer_mixer_obj_t* self);
|
|||
|
||||
bool common_hal_audiomixer_mixer_get_playing(audiomixer_mixer_obj_t* self);
|
||||
uint32_t common_hal_audiomixer_mixer_get_sample_rate(audiomixer_mixer_obj_t* self);
|
||||
uint8_t common_hal_audiomixer_mixer_get_channel_count(audiomixer_mixer_obj_t* self);
|
||||
uint8_t common_hal_audiomixer_mixer_get_bits_per_sample(audiomixer_mixer_obj_t* self);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOMIXER_MIXER_H
|
||||
|
|
|
@ -60,6 +60,12 @@ void common_hal_audioio_rawsample_set_sample_rate(audioio_rawsample_obj_t* self,
|
|||
uint32_t sample_rate) {
|
||||
self->sample_rate = sample_rate;
|
||||
}
|
||||
uint8_t common_hal_audioio_rawsample_get_bits_per_sample(audioio_rawsample_obj_t* self) {
|
||||
return self->bits_per_sample;
|
||||
}
|
||||
uint8_t common_hal_audioio_rawsample_get_channel_count(audioio_rawsample_obj_t* self) {
|
||||
return self->channel_count;
|
||||
}
|
||||
|
||||
void audioio_rawsample_reset_buffer(audioio_rawsample_obj_t* self,
|
||||
bool single_channel,
|
||||
|
|
|
@ -36,103 +36,37 @@
|
|||
#include "shared-module/audiomixer/Mixer.h"
|
||||
|
||||
uint32_t audiosample_sample_rate(mp_obj_t sample_obj) {
|
||||
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
|
||||
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
|
||||
return sample->sample_rate;
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
|
||||
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
return file->sample_rate;
|
||||
#if CIRCUITPY_AUDIOMIXER
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
|
||||
audiomixer_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
|
||||
return mixer->sample_rate;
|
||||
#endif
|
||||
}
|
||||
return 16000;
|
||||
const audiosample_p_t *proto = mp_proto_get_or_throw(MP_QSTR_protocol_audiosample, sample_obj);
|
||||
return proto->sample_rate(MP_OBJ_TO_PTR(sample_obj));
|
||||
}
|
||||
|
||||
uint8_t audiosample_bits_per_sample(mp_obj_t sample_obj) {
|
||||
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
|
||||
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
|
||||
return sample->bits_per_sample;
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
|
||||
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
return file->bits_per_sample;
|
||||
#if CIRCUITPY_AUDIOMIXER
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
|
||||
audiomixer_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
|
||||
return mixer->bits_per_sample;
|
||||
#endif
|
||||
}
|
||||
return 8;
|
||||
const audiosample_p_t *proto = mp_proto_get_or_throw(MP_QSTR_protocol_audiosample, sample_obj);
|
||||
return proto->bits_per_sample(MP_OBJ_TO_PTR(sample_obj));
|
||||
}
|
||||
|
||||
uint8_t audiosample_channel_count(mp_obj_t sample_obj) {
|
||||
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
|
||||
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
|
||||
return sample->channel_count;
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
|
||||
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
return file->channel_count;
|
||||
#if CIRCUITPY_AUDIOMIXER
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
|
||||
audiomixer_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
|
||||
return mixer->channel_count;
|
||||
#endif
|
||||
}
|
||||
return 1;
|
||||
const audiosample_p_t *proto = mp_proto_get_or_throw(MP_QSTR_protocol_audiosample, sample_obj);
|
||||
return proto->channel_count(MP_OBJ_TO_PTR(sample_obj));
|
||||
}
|
||||
|
||||
void audiosample_reset_buffer(mp_obj_t sample_obj, bool single_channel, uint8_t audio_channel) {
|
||||
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
|
||||
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
|
||||
audioio_rawsample_reset_buffer(sample, single_channel, audio_channel);
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
|
||||
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
audioio_wavefile_reset_buffer(file, single_channel, audio_channel);
|
||||
#if CIRCUITPY_AUDIOMIXER
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
|
||||
audiomixer_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
audiomixer_mixer_reset_buffer(file, single_channel, audio_channel);
|
||||
#endif
|
||||
}
|
||||
const audiosample_p_t *proto = mp_proto_get_or_throw(MP_QSTR_protocol_audiosample, sample_obj);
|
||||
proto->reset_buffer(MP_OBJ_TO_PTR(sample_obj));
|
||||
}
|
||||
|
||||
audioio_get_buffer_result_t audiosample_get_buffer(mp_obj_t sample_obj,
|
||||
bool single_channel,
|
||||
uint8_t channel,
|
||||
uint8_t** buffer, uint32_t* buffer_length) {
|
||||
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
|
||||
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
|
||||
return audioio_rawsample_get_buffer(sample, single_channel, channel, buffer, buffer_length);
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
|
||||
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
return audioio_wavefile_get_buffer(file, single_channel, channel, buffer, buffer_length);
|
||||
#if CIRCUITPY_AUDIOMIXER
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
|
||||
audiomixer_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
return audiomixer_mixer_get_buffer(file, single_channel, channel, buffer, buffer_length);
|
||||
#endif
|
||||
}
|
||||
return GET_BUFFER_DONE;
|
||||
const audiosample_p_t *proto = mp_proto_get_or_throw(MP_QSTR_protocol_audiosample, sample_obj);
|
||||
return proto->get_buffer(MP_OBJ_TO_PTR(sample_obj), single_channel, channel, buffer, buffer_length);
|
||||
}
|
||||
|
||||
void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
|
||||
bool* single_buffer, bool* samples_signed,
|
||||
uint32_t* max_buffer_length, uint8_t* spacing) {
|
||||
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
|
||||
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
|
||||
audioio_rawsample_get_buffer_structure(sample, single_channel, single_buffer,
|
||||
samples_signed, max_buffer_length, spacing);
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
|
||||
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
audioio_wavefile_get_buffer_structure(file, single_channel, single_buffer, samples_signed,
|
||||
max_buffer_length, spacing);
|
||||
#if CIRCUITPY_AUDIOMIXER
|
||||
} else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
|
||||
audiomixer_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
|
||||
audiomixer_mixer_get_buffer_structure(file, single_channel, single_buffer, samples_signed,
|
||||
max_buffer_length, spacing);
|
||||
#endif
|
||||
}
|
||||
const audiosample_p_t *proto = mp_proto_get_or_throw(MP_QSTR_protocol_audiosample, sample_obj);
|
||||
proto->get_buffer_structure(MP_OBJ_TO_PTR(sample_obj), single_channel, single_buffer,
|
||||
samples_signed, max_buffer_length, spacing);
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/proto.h"
|
||||
|
||||
typedef enum {
|
||||
GET_BUFFER_DONE, // No more data to read
|
||||
|
@ -38,15 +39,37 @@ typedef enum {
|
|||
GET_BUFFER_ERROR, // Error while reading data.
|
||||
} audioio_get_buffer_result_t;
|
||||
|
||||
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 audiosample_reset_buffer(mp_obj_t sample_obj, bool single_channel, uint8_t audio_channel);
|
||||
audioio_get_buffer_result_t audiosample_get_buffer(mp_obj_t sample_obj,
|
||||
typedef uint32_t (*audiosample_sample_rate_fun)(void* sample_obj);
|
||||
typedef uint8_t (*audiosample_bits_per_sample_fun)(void* sample_obj);
|
||||
typedef uint8_t (*audiosample_channel_count_fun)(void* sample_obj);
|
||||
typedef void (*audiosample_reset_buffer_fun)(void* sample_obj);
|
||||
typedef audioio_get_buffer_result_t (*audiosample_get_buffer_fun)(void* sample_obj,
|
||||
bool single_channel, uint8_t channel, uint8_t** buffer,
|
||||
uint32_t* buffer_length);
|
||||
typedef void (*audiosample_get_buffer_structure_fun)(void* sample_obj,
|
||||
bool single_channel, bool* single_buffer,
|
||||
bool* samples_signed, uint32_t *max_buffer_length,
|
||||
uint8_t* spacing);
|
||||
|
||||
typedef struct _audiosample_p_t {
|
||||
MP_PROTOCOL_HEAD // MP_QSTR_protocol_audiosample
|
||||
audiosample_sample_rate_fun sample_rate;
|
||||
audiosample_bits_per_sample_fun bits_per_sample;
|
||||
audiosample_channel_count_fun channel_count;
|
||||
audiosample_reset_buffer_fun reset_buffer;
|
||||
audiosample_get_buffer_fun get_buffer;
|
||||
audiosample_get_buffer_structure_fun get_buffer_structure;
|
||||
} audiosample_p_t;
|
||||
|
||||
uint32_t audiosample_sample_rate(void* sample_obj);
|
||||
uint8_t audiosample_bits_per_sample(void* sample_obj);
|
||||
uint8_t audiosample_channel_count(void* sample_obj);
|
||||
void audiosample_reset_buffer(void* sample_obj, bool single_channel, uint8_t audio_channel);
|
||||
audioio_get_buffer_result_t audiosample_get_buffer(void* sample_obj,
|
||||
bool single_channel,
|
||||
uint8_t channel,
|
||||
uint8_t** buffer, uint32_t* buffer_length);
|
||||
void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
|
||||
void audiosample_get_buffer_structure(void* sample_obj, bool single_channel,
|
||||
bool* single_buffer, bool* samples_signed,
|
||||
uint32_t* max_buffer_length, uint8_t* spacing);
|
||||
|
||||
|
|
|
@ -76,6 +76,14 @@ uint32_t common_hal_audiomixer_mixer_get_sample_rate(audiomixer_mixer_obj_t* sel
|
|||
return self->sample_rate;
|
||||
}
|
||||
|
||||
uint8_t common_hal_audiomixer_mixer_get_channel_count(audiomixer_mixer_obj_t* self) {
|
||||
return self->channel_count;
|
||||
}
|
||||
|
||||
uint8_t common_hal_audiomixer_mixer_get_bits_per_sample(audiomixer_mixer_obj_t* self) {
|
||||
return self->bits_per_sample;
|
||||
}
|
||||
|
||||
bool common_hal_audiomixer_mixer_get_playing(audiomixer_mixer_obj_t* self) {
|
||||
for (uint8_t v = 0; v < self->voice_count; v++) {
|
||||
if (common_hal_audiomixer_mixervoice_get_playing(MP_OBJ_TO_PTR(self->voice[v]))) {
|
||||
|
|
Loading…
Reference in New Issue