diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-06-14 18:57:24 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-06-14 18:57:24 -0700 |
| commit | 720042f96454ae347b2fe85baa36cde0cf5ce17e (patch) | |
| tree | 4fb938bfdff579e222c968faf0f6f84a95fedec6 /shared-module/audioio | |
| parent | 618943d90a958d36d9b0e2206552100069b10ddb (diff) | |
| parent | fa814a32ce3a211bcec9b06ad488aa1dde1b669d (diff) | |
Merge pull request #930 from dhalbert/sd_card_audio_play
Fix playing audio from SD card
Diffstat (limited to 'shared-module/audioio')
| -rw-r--r-- | shared-module/audioio/RawSample.c | 12 | ||||
| -rw-r--r-- | shared-module/audioio/RawSample.h | 12 | ||||
| -rw-r--r-- | shared-module/audioio/WaveFile.c | 39 | ||||
| -rw-r--r-- | shared-module/audioio/WaveFile.h | 12 | ||||
| -rw-r--r-- | shared-module/audioio/__init__.h | 36 |
5 files changed, 82 insertions, 29 deletions
diff --git a/shared-module/audioio/RawSample.c b/shared-module/audioio/RawSample.c index e7059bf5a..6422e346b 100644 --- a/shared-module/audioio/RawSample.c +++ b/shared-module/audioio/RawSample.c @@ -66,18 +66,18 @@ void audioio_rawsample_reset_buffer(audioio_rawsample_obj_t* self, uint8_t channel) { } -bool audioio_rawsample_get_buffer(audioio_rawsample_obj_t* self, - bool single_channel, - uint8_t channel, - uint8_t** buffer, - uint32_t* buffer_length) { +audioio_get_buffer_result_t audioio_rawsample_get_buffer(audioio_rawsample_obj_t* self, + bool single_channel, + uint8_t channel, + uint8_t** buffer, + uint32_t* buffer_length) { *buffer_length = self->len; if (single_channel) { *buffer = self->buffer + (channel % self->channel_count) * (self->bits_per_sample / 8); } else { *buffer = self->buffer; } - return true; + return GET_BUFFER_DONE; } void audioio_rawsample_get_buffer_structure(audioio_rawsample_obj_t* self, bool single_channel, diff --git a/shared-module/audioio/RawSample.h b/shared-module/audioio/RawSample.h index 1604a1725..fe5283db4 100644 --- a/shared-module/audioio/RawSample.h +++ b/shared-module/audioio/RawSample.h @@ -29,6 +29,8 @@ #include "py/obj.h" +#include "shared-module/audioio/__init__.h" + typedef struct { mp_obj_base_t base; uint8_t* buffer; @@ -45,11 +47,11 @@ typedef struct { void audioio_rawsample_reset_buffer(audioio_rawsample_obj_t* self, bool single_channel, uint8_t channel); -bool audioio_rawsample_get_buffer(audioio_rawsample_obj_t* self, - bool single_channel, - uint8_t channel, - uint8_t** buffer, - uint32_t* buffer_length); // length in bytes +audioio_get_buffer_result_t audioio_rawsample_get_buffer(audioio_rawsample_obj_t* self, + bool single_channel, + uint8_t channel, + uint8_t** buffer, + uint32_t* buffer_length); // length in bytes void audioio_rawsample_get_buffer_structure(audioio_rawsample_obj_t* self, bool single_channel, bool* single_buffer, bool* samples_signed, uint32_t* max_buffer_length, uint8_t* spacing); diff --git a/shared-module/audioio/WaveFile.c b/shared-module/audioio/WaveFile.c index 99283937c..7247db137 100644 --- a/shared-module/audioio/WaveFile.c +++ b/shared-module/audioio/WaveFile.c @@ -29,6 +29,7 @@ #include <stdint.h> #include <string.h> +#include "py/mperrno.h" #include "py/runtime.h" #include "shared-module/audioio/WaveFile.h" @@ -50,20 +51,26 @@ void common_hal_audioio_wavefile_construct(audioio_wavefile_obj_t* self, uint8_t chunk_header[16]; f_rewind(&self->file->fp); UINT bytes_read; - f_read(&self->file->fp, chunk_header, 16, &bytes_read); + if (f_read(&self->file->fp, chunk_header, 16, &bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } if (bytes_read != 16 || memcmp(chunk_header, "RIFF", 4) != 0 || memcmp(chunk_header + 8, "WAVEfmt ", 8) != 0) { mp_raise_ValueError("Invalid wave file"); } uint32_t format_size; - f_read(&self->file->fp, &format_size, 4, &bytes_read); + if (f_read(&self->file->fp, &format_size, 4, &bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } if (bytes_read != 4 || format_size > sizeof(struct wave_format_chunk)) { mp_raise_ValueError("Invalid format chunk size"); } struct wave_format_chunk format; - f_read(&self->file->fp, &format, format_size, &bytes_read); + if (f_read(&self->file->fp, &format, format_size, &bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } if (bytes_read != format_size) { } @@ -83,14 +90,18 @@ void common_hal_audioio_wavefile_construct(audioio_wavefile_obj_t* self, // TODO(tannewt): Skip any extra chunks that occur before the data section. uint8_t data_tag[4]; - f_read(&self->file->fp, &data_tag, 4, &bytes_read); + if (f_read(&self->file->fp, &data_tag, 4, &bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } if (bytes_read != 4 || memcmp((uint8_t *) data_tag, "data", 4) != 0) { mp_raise_ValueError("Data chunk must follow fmt chunk"); } uint32_t data_length; - f_read(&self->file->fp, &data_length, 4, &bytes_read); + if (f_read(&self->file->fp, &data_length, 4, &bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } if (bytes_read != 4) { mp_raise_ValueError("Invalid file"); } @@ -152,11 +163,11 @@ void audioio_wavefile_reset_buffer(audioio_wavefile_obj_t* self, self->right_read_count = 0; } -bool audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, - bool single_channel, - uint8_t channel, - uint8_t** buffer, - uint32_t* buffer_length) { +audioio_get_buffer_result_t audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, + bool single_channel, + uint8_t channel, + uint8_t** buffer, + uint32_t* buffer_length) { if (!single_channel) { channel = 0; } @@ -171,7 +182,7 @@ bool audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, if (self->bytes_remaining == 0 && need_more_data) { *buffer = NULL; *buffer_length = 0; - return true; + return GET_BUFFER_DONE; } if (need_more_data) { @@ -185,7 +196,9 @@ bool audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, } else { *buffer = self->buffer; } - f_read(&self->file->fp, *buffer, num_bytes_to_load, &length_read); + if (f_read(&self->file->fp, *buffer, num_bytes_to_load, &length_read) != FR_OK) { + return GET_BUFFER_ERROR; + } *buffer_length = length_read; if (self->buffer_index % 2 == 1) { self->second_buffer_length = length_read; @@ -213,7 +226,7 @@ bool audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, *buffer = *buffer + self->bits_per_sample / 8; } - return self->bytes_remaining == 0; + return self->bytes_remaining == 0 ? GET_BUFFER_DONE : GET_BUFFER_MORE_DATA; } void audioio_wavefile_get_buffer_structure(audioio_wavefile_obj_t* self, bool single_channel, diff --git a/shared-module/audioio/WaveFile.h b/shared-module/audioio/WaveFile.h index 9698d3c93..75a46c03b 100644 --- a/shared-module/audioio/WaveFile.h +++ b/shared-module/audioio/WaveFile.h @@ -29,6 +29,8 @@ #include "py/obj.h" +#include "shared-module/audioio/__init__.h" + typedef struct { mp_obj_base_t base; uint8_t* buffer; @@ -56,11 +58,11 @@ typedef struct { void audioio_wavefile_reset_buffer(audioio_wavefile_obj_t* self, bool single_channel, uint8_t channel); -bool audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, - bool single_channel, - uint8_t channel, - uint8_t** buffer, - uint32_t* buffer_length); // length in bytes +audioio_get_buffer_result_t audioio_wavefile_get_buffer(audioio_wavefile_obj_t* self, + bool single_channel, + uint8_t channel, + uint8_t** buffer, + uint32_t* buffer_length); // length in bytes void audioio_wavefile_get_buffer_structure(audioio_wavefile_obj_t* self, bool single_channel, bool* single_buffer, bool* samples_signed, uint32_t* max_buffer_length, uint8_t* spacing); diff --git a/shared-module/audioio/__init__.h b/shared-module/audioio/__init__.h new file mode 100644 index 000000000..2491beb12 --- /dev/null +++ b/shared-module/audioio/__init__.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO__INIT__H +#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO__INIT__H + +typedef enum { + GET_BUFFER_DONE, // No more data to read + GET_BUFFER_MORE_DATA, // More data to read. + GET_BUFFER_ERROR, // Error while reading data. +} audioio_get_buffer_result_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO__INIT__H |
