|
27 | 27 | #include <stdint.h>
|
28 | 28 |
|
29 | 29 | #include "lib/utils/context_manager_helpers.h"
|
| 30 | +#include "py/binary.h" |
30 | 31 | #include "py/objproperty.h"
|
31 | 32 | #include "py/runtime.h"
|
32 | 33 | #include "shared-bindings/microcontroller/Pin.h"
|
|
62 | 63 | //|
|
63 | 64 | //| # Generate one period of sine wav.
|
64 | 65 | //| length = 8000 // 440
|
65 |
| -//| b = array.array("H", [0] * length) |
| 66 | +//| sine_wave = array.array("H", [0] * length) |
66 | 67 | //| for i in range(length):
|
67 |
| -//| b[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15) |
| 68 | +//| sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15) |
68 | 69 | //|
|
69 |
| -//| sample = audioio.AudioOut(board.SPEAKER, sin_wave) |
| 70 | +//| sample = audioio.AudioOut(board.SPEAKER, sine_wave) |
70 | 71 | //| sample.play(loop=True)
|
71 | 72 | //| time.sleep(1)
|
72 | 73 | //| sample.stop()
|
@@ -106,12 +107,15 @@ STATIC mp_obj_t audioio_audioout_make_new(const mp_obj_type_t *type, size_t n_ar
|
106 | 107 | if (MP_OBJ_IS_TYPE(args[1], &fatfs_type_fileio)) {
|
107 | 108 | common_hal_audioio_audioout_construct_from_file(self, pin, MP_OBJ_TO_PTR(args[1]));
|
108 | 109 | } else if (mp_get_buffer(args[1], &bufinfo, MP_BUFFER_READ)) {
|
109 |
| - if (bufinfo.len % 2 == 1) { |
110 |
| - mp_raise_ValueError("sample_source must be an even number of bytes (two per sample)"); |
| 110 | + uint8_t bytes_per_sample = 1; |
| 111 | + if (bufinfo.typecode == 'H') { |
| 112 | + bytes_per_sample = 2; |
| 113 | + } else if (bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { |
| 114 | + mp_raise_ValueError("sample_source buffer must be a bytearray or array of type 'H' or 'B'"); |
111 | 115 | }
|
112 |
| - common_hal_audioio_audioout_construct_from_buffer(self, pin, ((uint16_t*)bufinfo.buf), bufinfo.len / 2); |
| 116 | + common_hal_audioio_audioout_construct_from_buffer(self, pin, ((uint16_t*)bufinfo.buf), bufinfo.len, bytes_per_sample); |
113 | 117 | } else {
|
114 |
| - mp_raise_TypeError("sample_source must be a file or bytes-like object."); |
| 118 | + mp_raise_TypeError("sample_source must be a file or bytes-like object"); |
115 | 119 | }
|
116 | 120 |
|
117 | 121 | return MP_OBJ_FROM_PTR(self);
|
|
0 commit comments