|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2022 Matthew McGowan for Blues Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef _MEMS_AUDIO_H_ |
| 26 | +#define _MEMS_AUDIO_H_ |
| 27 | + |
| 28 | +#include <stdint.h> |
| 29 | +#include <stddef.h> |
| 30 | + |
| 31 | +#ifdef __cplusplus |
| 32 | +extern "C" { |
| 33 | +#endif |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief How many milliseconds of audio can fit in the audio buffer(s). |
| 38 | + * Interrupts for recieved data fire at half this duration / twice the frequency. |
| 39 | + */ |
| 40 | +#ifndef MEMS_AUDIO_MS_BUFFER |
| 41 | +#define MEMS_AUDIO_MS_BUFFER (1) |
| 42 | +#endif |
| 43 | + |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief The number of bits per sample of the PCM output |
| 47 | + */ |
| 48 | +#define PCM_OUT_RESOLUTION 16 |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief The output frequency of PCM samples in Hz. |
| 52 | + */ |
| 53 | +#define PCM_OUT_SAMPLING_FREQUENCY 16000 |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief type for describing error conditions. |
| 57 | + */ |
| 58 | +typedef int32_t mems_audio_err_t; |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief The datatype that holds an output PCM sample. |
| 62 | + */ |
| 63 | +typedef int16_t pcm_sample_t; |
| 64 | +_Static_assert(PCM_OUT_RESOLUTION==16, "Output PCM resolution must be 16-bits"); |
| 65 | + |
| 66 | + |
| 67 | +typedef enum { |
| 68 | + MEMS_AUDIO_OK = 0, |
| 69 | + MEMS_AUDIO_ERROR_ALREADY_INITIALIZED = -1, |
| 70 | + MEMS_AUDIO_ERROR_NOT_INITIALIZED = -2 |
| 71 | +} mems_audio_err_enum_t; |
| 72 | + |
| 73 | +#define IS_MEMS_AUDIO_ERROR(e) (e) |
| 74 | +#define CHECK_MEMS_AUDIO_ERROR(e) { if (IS_MEMS_AUDIO_ERROR(e)) return e; } |
| 75 | +#define CHECK_MEMS_AUDIO_INITIALIZED(x) { if (!x) return MEMS_AUDIO_ERROR_NOT_INITIALIZED; } |
| 76 | + |
| 77 | +typedef struct MemsAudio_t MemsAudio; |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Callback informing that PCM samples are available for processing. |
| 81 | + */ |
| 82 | +typedef void (*pcm_data_available_t)(MemsAudio* audio, pcm_sample_t* pcmSamples, size_t pcmLength); |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief MemsAudio manages the filter, buffers and callbacks used to capture PDM audio samples and convert to PCM. |
| 86 | + * |
| 87 | + */ |
| 88 | +typedef struct MemsAudio_t { |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief The buffer to store PCM audio samples |
| 92 | + */ |
| 93 | + volatile pcm_sample_t* volatile pcmOutputBuffer; |
| 94 | + |
| 95 | + /** |
| 96 | + * @brief The length of the PCM buffer. SHould be at least MEMS_AUDIO_PCM_BUFFER_LENGTH |
| 97 | + */ |
| 98 | + volatile size_t pcmOutputBufferLength; |
| 99 | + |
| 100 | + /** |
| 101 | + * @brief Optional callback for when PCM data is available. |
| 102 | + */ |
| 103 | + pcm_data_available_t pcm_data_available; |
| 104 | + |
| 105 | + void* audioImpl; |
| 106 | + void* userData; |
| 107 | +} MemsAudio; |
| 108 | + |
| 109 | + |
| 110 | +mems_audio_err_t mems_audio_init(MemsAudio* audio); |
| 111 | + |
| 112 | +/** |
| 113 | + * @brief Uninitializes the MemsAudio instance. |
| 114 | + * |
| 115 | + * @param audio |
| 116 | + * @return mems_audio_err_t |
| 117 | + */ |
| 118 | +mems_audio_err_t mems_audio_uninit(MemsAudio* audio); |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Asynchronously records audio. |
| 122 | + * |
| 123 | + * @param audio |
| 124 | + * @param pdmBuffer |
| 125 | + * @param pdmBufferLength |
| 126 | + * @return mems_audio_err_t |
| 127 | + */ |
| 128 | +mems_audio_err_t mems_audio_record(MemsAudio* audio); |
| 129 | + |
| 130 | +/** |
| 131 | + * @brief Pause recording audio. |
| 132 | + */ |
| 133 | +mems_audio_err_t mems_audio_pause(MemsAudio* audio); |
| 134 | + |
| 135 | +/** |
| 136 | + * @brief Resume recording audio. |
| 137 | + * |
| 138 | + * @param audio |
| 139 | + * @return mems_audio_err_t |
| 140 | + */ |
| 141 | +mems_audio_err_t mems_audio_resume(MemsAudio* audio); |
| 142 | + |
| 143 | +/** |
| 144 | + * @brief Stop recording audio and |
| 145 | + * |
| 146 | + * @param audio |
| 147 | + * @return mems_audio_err_t |
| 148 | + */ |
| 149 | +mems_audio_err_t mems_audio_stop(MemsAudio* audio); |
| 150 | + |
| 151 | +#ifdef __cplusplus |
| 152 | +} |
| 153 | +#endif |
| 154 | + |
| 155 | + |
| 156 | +#endif // _MEMS_AUDIO_H_ |
0 commit comments