|
| 1 | +#ifndef _MEMS_AUDIO_H_ |
| 2 | +#define _MEMS_AUDIO_H_ |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | +#include <stddef.h> |
| 6 | + |
| 7 | +#ifdef __cplusplus |
| 8 | +extern "C" { |
| 9 | +#endif |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * @brief How many milliseconds of audio can fit in the audio buffer(s). |
| 14 | + * Interrupts for recieved data fire at half this duration / twice the frequency. |
| 15 | + */ |
| 16 | +#ifndef MEMS_AUDIO_MS_BUFFER |
| 17 | +#define MEMS_AUDIO_MS_BUFFER (1) |
| 18 | +#endif |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief The number of bits per sample of the PCM output |
| 23 | + */ |
| 24 | +#define PCM_OUT_RESOLUTION 16 |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief The output frequency of PCM samples in Hz. |
| 28 | + */ |
| 29 | +#define PCM_OUT_SAMPLING_FREQUENCY 16000 |
| 30 | + |
| 31 | +/** |
| 32 | + * @brief type for describing error conditions. |
| 33 | + */ |
| 34 | +typedef int32_t mems_audio_err_t; |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief The datatype that holds an output PCM sample. |
| 38 | + */ |
| 39 | +typedef int16_t pcm_sample_t; |
| 40 | +_Static_assert(PCM_OUT_RESOLUTION==16, "Output PCM resolution must be 16-bits"); |
| 41 | + |
| 42 | + |
| 43 | +typedef enum { |
| 44 | + MEMS_AUDIO_OK = 0, |
| 45 | + MEMS_AUDIO_ERROR_ALREADY_INITIALIZED = -1, |
| 46 | + MEMS_AUDIO_ERROR_NOT_INITIALIZED = -2 |
| 47 | +} mems_audio_err_enum_t; |
| 48 | + |
| 49 | +#define IS_MEMS_AUDIO_ERROR(e) (e) |
| 50 | +#define CHECK_MEMS_AUDIO_ERROR(e) { if (IS_MEMS_AUDIO_ERROR(e)) return e; } |
| 51 | +#define CHECK_MEMS_AUDIO_INITIALIZED(x) { if (!x) return MEMS_AUDIO_ERROR_NOT_INITIALIZED; } |
| 52 | + |
| 53 | +typedef struct MemsAudio_t MemsAudio; |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Callback informing that PCM samples are available for processing. |
| 57 | + */ |
| 58 | +typedef void (*pcm_data_available_t)(MemsAudio* audio, pcm_sample_t* pcmSamples, size_t pcmLength); |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief MemsAudio manages the filter, buffers and callbacks used to capture PDM audio samples and convert to PCM. |
| 62 | + * |
| 63 | + */ |
| 64 | +typedef struct MemsAudio_t { |
| 65 | + |
| 66 | + /** |
| 67 | + * @brief The buffer to store PCM audio samples |
| 68 | + */ |
| 69 | + volatile pcm_sample_t* volatile pcmOutputBuffer; |
| 70 | + |
| 71 | + /** |
| 72 | + * @brief The length of the PCM buffer. SHould be at least MEMS_AUDIO_PCM_BUFFER_LENGTH |
| 73 | + */ |
| 74 | + volatile size_t pcmOutputBufferLength; |
| 75 | + |
| 76 | + /** |
| 77 | + * @brief Optional callback for when PCM data is available. |
| 78 | + */ |
| 79 | + pcm_data_available_t pcm_data_available; |
| 80 | + |
| 81 | + void* audioImpl; |
| 82 | + void* userData; |
| 83 | +} MemsAudio; |
| 84 | + |
| 85 | + |
| 86 | +mems_audio_err_t mems_audio_init(MemsAudio* audio); |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Uninitializes the MemsAudio instance. |
| 90 | + * |
| 91 | + * @param audio |
| 92 | + * @return mems_audio_err_t |
| 93 | + */ |
| 94 | +mems_audio_err_t mems_audio_uninit(MemsAudio* audio); |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Asynchronously records audio. |
| 98 | + * |
| 99 | + * @param audio |
| 100 | + * @param pdmBuffer |
| 101 | + * @param pdmBufferLength |
| 102 | + * @return mems_audio_err_t |
| 103 | + */ |
| 104 | +mems_audio_err_t mems_audio_record(MemsAudio* audio); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Pause recording audio. |
| 108 | + */ |
| 109 | +mems_audio_err_t mems_audio_pause(MemsAudio* audio); |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief Resume recording audio. |
| 113 | + * |
| 114 | + * @param audio |
| 115 | + * @return mems_audio_err_t |
| 116 | + */ |
| 117 | +mems_audio_err_t mems_audio_resume(MemsAudio* audio); |
| 118 | + |
| 119 | +/** |
| 120 | + * @brief Stop recording audio and |
| 121 | + * |
| 122 | + * @param audio |
| 123 | + * @return mems_audio_err_t |
| 124 | + */ |
| 125 | +mems_audio_err_t mems_audio_stop(MemsAudio* audio); |
| 126 | + |
| 127 | +#ifdef __cplusplus |
| 128 | +} |
| 129 | +#endif |
| 130 | + |
| 131 | + |
| 132 | +#endif // _MEMS_AUDIO_H_ |
0 commit comments