|
| 1 | + |
| 2 | +// SPDX-License-Identiefier: MIT |
| 3 | +// SPDX-CopyRightText: 2025 Badge.Team |
| 4 | + |
| 5 | +#include "asp/audio.h" |
| 6 | +#include <stdlib.h> |
| 7 | +#include "asp/err.h" |
| 8 | +#include "bsp/audio.h" |
| 9 | +#include "driver/i2s_common.h" |
| 10 | +#include "driver/i2s_types.h" |
| 11 | +#include "err.h" |
| 12 | + |
| 13 | +// Set audio sampling rate. |
| 14 | +asp_err_t asp_audio_set_rate(uint32_t rate_hz) { |
| 15 | + return asp_esp_err_conv(bsp_audio_set_rate(rate_hz)); |
| 16 | +} |
| 17 | + |
| 18 | +// Get current volume. |
| 19 | +asp_err_t asp_audio_get_volume(float* out_percentage) { |
| 20 | + return asp_esp_err_conv(bsp_audio_get_volume(out_percentage)); |
| 21 | +} |
| 22 | + |
| 23 | +// Set audio volume. |
| 24 | +asp_err_t asp_audio_set_volume(float percentage) { |
| 25 | + return asp_esp_err_conv(bsp_audio_set_volume(percentage)); |
| 26 | +} |
| 27 | + |
| 28 | +// Enable/disable the audio amplifier. |
| 29 | +asp_err_t asp_audio_set_amplifier(bool enabled) { |
| 30 | + return asp_esp_err_conv(bsp_audio_set_amplifier(enabled)); |
| 31 | +} |
| 32 | + |
| 33 | +// Stop the audio stream. |
| 34 | +asp_err_t asp_audio_stop() { |
| 35 | + i2s_chan_handle_t handle; |
| 36 | + ASP_RETURN_ON_ERR(asp_esp_err_conv(bsp_audio_get_i2s_handle(&handle))); |
| 37 | + if (!handle) { |
| 38 | + return ASP_ERR_FAIL; |
| 39 | + } |
| 40 | + return i2s_channel_disable(handle); |
| 41 | +} |
| 42 | + |
| 43 | +// Start the audio stream. |
| 44 | +asp_err_t asp_audio_start() { |
| 45 | + i2s_chan_handle_t handle; |
| 46 | + ASP_RETURN_ON_ERR(asp_esp_err_conv(bsp_audio_get_i2s_handle(&handle))); |
| 47 | + if (!handle) { |
| 48 | + return ASP_ERR_FAIL; |
| 49 | + } |
| 50 | + return i2s_channel_enable(handle); |
| 51 | +} |
| 52 | + |
| 53 | +// Write audio samples. |
| 54 | +asp_err_t asp_audio_write(void* samples, size_t samples_size, int64_t timeout_ms) { |
| 55 | + i2s_chan_handle_t handle; |
| 56 | + ASP_RETURN_ON_ERR(asp_esp_err_conv(bsp_audio_get_i2s_handle(&handle))); |
| 57 | + if (!handle) { |
| 58 | + return ASP_ERR_FAIL; |
| 59 | + } |
| 60 | + return asp_esp_err_conv(i2s_channel_write(handle, samples, samples_size, NULL, timeout_ms)); |
| 61 | +} |
0 commit comments