7
7
#include <stdint.h>
8
8
9
9
#include "py/obj.h"
10
+ #include "py/objproperty.h"
10
11
#include "py/gc.h"
11
12
#include "py/runtime.h"
12
13
13
14
#include "shared-bindings/audiocore/__init__.h"
14
15
#include "shared-bindings/audiocore/RawSample.h"
15
16
#include "shared-bindings/audiocore/WaveFile.h"
17
+ #include "shared-bindings/util.h"
16
18
// #include "shared-bindings/audiomixer/Mixer.h"
17
19
18
20
//| """Support for audio samples"""
@@ -24,15 +26,18 @@ static mp_obj_t audiocore_get_buffer(mp_obj_t sample_in) {
24
26
uint32_t buffer_length = 0 ;
25
27
audioio_get_buffer_result_t gbr = audiosample_get_buffer (sample_in , false, 0 , & buffer , & buffer_length );
26
28
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
+
27
32
mp_obj_t result [2 ] = {mp_obj_new_int_from_uint (gbr ), mp_const_none };
28
33
29
34
if (gbr != GET_BUFFER_ERROR ) {
30
35
bool single_buffer , samples_signed ;
31
36
uint32_t max_buffer_length ;
32
37
uint8_t spacing ;
33
38
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 );
36
41
// copies the data because the gc semantics of get_buffer are unclear
37
42
void * result_buf = m_malloc (buffer_length );
38
43
memcpy (result_buf , buffer , buffer_length );
@@ -55,7 +60,7 @@ static mp_obj_t audiocore_get_structure(mp_obj_t sample_in) {
55
60
uint32_t max_buffer_length ;
56
61
uint8_t spacing ;
57
62
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 );
59
64
mp_obj_t result [4 ] = {
60
65
mp_obj_new_int_from_uint (single_buffer ),
61
66
mp_obj_new_int_from_uint (samples_signed ),
@@ -92,4 +97,61 @@ const mp_obj_module_t audiocore_module = {
92
97
.globals = (mp_obj_dict_t * )& audiocore_module_globals ,
93
98
};
94
99
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
+
95
157
MP_REGISTER_MODULE (MP_QSTR_audiocore , audiocore_module );
0 commit comments