77#include <stdint.h>
88
99#include "py/obj.h"
10+ #include "py/objproperty.h"
1011#include "py/gc.h"
1112#include "py/runtime.h"
1213
1314#include "shared-bindings/audiocore/__init__.h"
1415#include "shared-bindings/audiocore/RawSample.h"
1516#include "shared-bindings/audiocore/WaveFile.h"
17+ #include "shared-bindings/util.h"
1618// #include "shared-bindings/audiomixer/Mixer.h"
1719
1820//| """Support for audio samples"""
@@ -24,15 +26,18 @@ static mp_obj_t audiocore_get_buffer(mp_obj_t sample_in) {
2426 uint32_t buffer_length = 0 ;
2527 audioio_get_buffer_result_t gbr = audiosample_get_buffer (sample_in , false, 0 , & buffer , & buffer_length );
2628
29+ // audiosample_get_buffer checked that we're a sample so this is a safe cast
30+ audiosample_base_t * sample = MP_OBJ_TO_PTR (sample_in );
31+
2732 mp_obj_t result [2 ] = {mp_obj_new_int_from_uint (gbr ), mp_const_none };
2833
2934 if (gbr != GET_BUFFER_ERROR ) {
3035 bool single_buffer , samples_signed ;
3136 uint32_t max_buffer_length ;
3237 uint8_t spacing ;
3338
34- uint8_t bits_per_sample = audiosample_bits_per_sample ( sample_in );
35- audiosample_get_buffer_structure (sample_in , false, & single_buffer , & samples_signed , & max_buffer_length , & spacing );
39+ uint8_t bits_per_sample = audiosample_get_bits_per_sample ( sample );
40+ audiosample_get_buffer_structure (sample , false, & single_buffer , & samples_signed , & max_buffer_length , & spacing );
3641 // copies the data because the gc semantics of get_buffer are unclear
3742 void * result_buf = m_malloc (buffer_length );
3843 memcpy (result_buf , buffer , buffer_length );
@@ -55,7 +60,7 @@ static mp_obj_t audiocore_get_structure(mp_obj_t sample_in) {
5560 uint32_t max_buffer_length ;
5661 uint8_t spacing ;
5762
58- audiosample_get_buffer_structure (sample_in , false, & single_buffer , & samples_signed , & max_buffer_length , & spacing );
63+ audiosample_get_buffer_structure_checked (sample_in , false, & single_buffer , & samples_signed , & max_buffer_length , & spacing );
5964 mp_obj_t result [4 ] = {
6065 mp_obj_new_int_from_uint (single_buffer ),
6166 mp_obj_new_int_from_uint (samples_signed ),
@@ -92,4 +97,61 @@ const mp_obj_module_t audiocore_module = {
9297 .globals = (mp_obj_dict_t * )& audiocore_module_globals ,
9398};
9499
100+ bool audiosample_deinited (const audiosample_base_t * self ) {
101+ return self -> channel_count == 0 ;
102+ }
103+
104+ void audiosample_check_for_deinit (const audiosample_base_t * self ) {
105+ if (audiosample_deinited (self )) {
106+ raise_deinited_error ();
107+ }
108+ }
109+
110+ void audiosample_mark_deinit (audiosample_base_t * self ) {
111+ self -> channel_count = 0 ;
112+ }
113+
114+ // common implementation of channel_count property for audio samples
115+ static mp_obj_t audiosample_obj_get_channel_count (mp_obj_t self_in ) {
116+ audiosample_base_t * self = MP_OBJ_TO_PTR (self_in );
117+ audiosample_check_for_deinit (self );
118+ return MP_OBJ_NEW_SMALL_INT (audiosample_get_channel_count (self ));
119+ }
120+ MP_DEFINE_CONST_FUN_OBJ_1 (audiosample_get_channel_count_obj , audiosample_obj_get_channel_count );
121+
122+ MP_PROPERTY_GETTER (audiosample_channel_count_obj ,
123+ (mp_obj_t )& audiosample_get_channel_count_obj );
124+
125+
126+ // common implementation of bits_per_sample property for audio samples
127+ static mp_obj_t audiosample_obj_get_bits_per_sample (mp_obj_t self_in ) {
128+ audiosample_base_t * self = MP_OBJ_TO_PTR (self_in );
129+ audiosample_check_for_deinit (self );
130+ return MP_OBJ_NEW_SMALL_INT (audiosample_get_bits_per_sample (self ));
131+ }
132+ MP_DEFINE_CONST_FUN_OBJ_1 (audiosample_get_bits_per_sample_obj , audiosample_obj_get_bits_per_sample );
133+
134+ MP_PROPERTY_GETTER (audiosample_bits_per_sample_obj ,
135+ (mp_obj_t )& audiosample_get_bits_per_sample_obj );
136+
137+ // common implementation of sample_rate property for audio samples
138+ static mp_obj_t audiosample_obj_get_sample_rate (mp_obj_t self_in ) {
139+ audiosample_base_t * self = MP_OBJ_TO_PTR (self_in );
140+ audiosample_check_for_deinit (self );
141+ return MP_OBJ_NEW_SMALL_INT (audiosample_get_sample_rate (audiosample_check (self_in )));
142+ }
143+ MP_DEFINE_CONST_FUN_OBJ_1 (audiosample_get_sample_rate_obj , audiosample_obj_get_sample_rate );
144+
145+ static mp_obj_t audiosample_obj_set_sample_rate (mp_obj_t self_in , mp_obj_t sample_rate ) {
146+ audiosample_base_t * self = MP_OBJ_TO_PTR (self_in );
147+ audiosample_check_for_deinit (self );
148+ audiosample_set_sample_rate (audiosample_check (self_in ), mp_obj_get_int (sample_rate ));
149+ return mp_const_none ;
150+ }
151+ MP_DEFINE_CONST_FUN_OBJ_2 (audiosample_set_sample_rate_obj , audiosample_obj_set_sample_rate );
152+
153+ MP_PROPERTY_GETSET (audiosample_sample_rate_obj ,
154+ (mp_obj_t )& audiosample_get_sample_rate_obj ,
155+ (mp_obj_t )& audiosample_set_sample_rate_obj );
156+
95157MP_REGISTER_MODULE (MP_QSTR_audiocore , audiocore_module );
0 commit comments