Skip to content

Commit 57eb925

Browse files
authored
Merge pull request #10797 from tekktrik/dev/fix-pdmin-record
Update audiobusio.PDMIn.record()
2 parents 47990e3 + e9b74b1 commit 57eb925

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

shared-bindings/audiobusio/PDMIn.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ static void check_for_deinit(audiobusio_pdmin_obj_t *self) {
155155
// Provided by context manager helper.
156156

157157

158-
//| def record(self, destination: WriteableBuffer, destination_length: int) -> None:
158+
//| def record(self, destination: WriteableBuffer, destination_length: int) -> int:
159159
//| """Records destination_length bytes of samples to destination. This is
160160
//| blocking.
161161
//|
162-
//| An IOError may be raised when the destination is too slow to record the
162+
//| An OSError may be raised when the destination is too slow to record the
163163
//| audio at the given rate. For internal flash, writing all 1s to the file
164164
//| before recording is recommended to speed up writes.
165165
//|
@@ -176,22 +176,22 @@ static mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destinat
176176
mp_buffer_info_t bufinfo;
177177
if (mp_obj_is_type(destination, &mp_type_fileio)) {
178178
mp_raise_NotImplementedError(MP_ERROR_TEXT("Cannot record to a file"));
179-
} else if (mp_get_buffer(destination, &bufinfo, MP_BUFFER_WRITE)) {
180-
if (bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL) < length) {
181-
mp_raise_ValueError(MP_ERROR_TEXT("Destination capacity is smaller than destination_length."));
182-
}
183-
uint8_t bit_depth = common_hal_audiobusio_pdmin_get_bit_depth(self);
184-
if (bufinfo.typecode != 'H' && bit_depth == 16) {
185-
mp_raise_ValueError(MP_ERROR_TEXT("destination buffer must be an array of type 'H' for bit_depth = 16"));
186-
} else if (bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE && bit_depth == 8) {
187-
mp_raise_ValueError(MP_ERROR_TEXT("destination buffer must be a bytearray or array of type 'B' for bit_depth = 8"));
188-
}
189-
// length is the buffer length in slots, not bytes.
190-
uint32_t length_written =
191-
common_hal_audiobusio_pdmin_record_to_buffer(self, bufinfo.buf, length);
192-
return MP_OBJ_NEW_SMALL_INT(length_written);
193179
}
194-
return mp_const_none;
180+
181+
mp_get_buffer_raise(destination, &bufinfo, MP_BUFFER_WRITE);
182+
if (bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL) < length) {
183+
mp_raise_ValueError(MP_ERROR_TEXT("Destination capacity is smaller than destination_length."));
184+
}
185+
uint8_t bit_depth = common_hal_audiobusio_pdmin_get_bit_depth(self);
186+
if (bufinfo.typecode != 'H' && bit_depth == 16) {
187+
mp_raise_ValueError(MP_ERROR_TEXT("destination buffer must be an array of type 'H' for bit_depth = 16"));
188+
} else if (bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE && bit_depth == 8) {
189+
mp_raise_ValueError(MP_ERROR_TEXT("destination buffer must be a bytearray or array of type 'B' for bit_depth = 8"));
190+
}
191+
// length is the buffer length in slots, not bytes.
192+
uint32_t length_written =
193+
common_hal_audiobusio_pdmin_record_to_buffer(self, bufinfo.buf, length);
194+
return MP_OBJ_NEW_SMALL_INT(length_written);
195195
}
196196
MP_DEFINE_CONST_FUN_OBJ_3(audiobusio_pdmin_record_obj, audiobusio_pdmin_obj_record);
197197

0 commit comments

Comments
 (0)