Skip to content

Commit 3e5c96f

Browse files
committed
Audio API, update to PAX 2
1 parent 02bf529 commit 3e5c96f

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
idf_component_register(
22
SRCS
3+
"src/audio.c"
34
"src/device.c"
45
"src/display.c"
56
"src/err.c"

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
dependencies:
22
badgeteam/badge-bsp: ">=0.3.2"
3-
robotman2412/pax-gfx: "^1.1.2"
3+
robotman2412/pax-gfx: ">=2.0.1"

include/asp/audio.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
// SPDX-License-Identiefier: MIT
3+
// SPDX-CopyRightText: 2025 Badge.Team
4+
5+
#pragma once
6+
7+
#include <stdbool.h>
8+
#include <stddef.h>
9+
#include <stdint.h>
10+
#include "asp/err.h"
11+
12+
// Set audio sampling rate.
13+
asp_err_t asp_audio_set_rate(uint32_t rate_hz);
14+
// Get current volume.
15+
asp_err_t asp_audio_get_volume(float* out_percentage);
16+
// Set audio volume.
17+
asp_err_t asp_audio_set_volume(float percentage);
18+
// Enable/disable the audio amplifier.
19+
asp_err_t asp_audio_set_amplifier(bool enabled);
20+
// Stop the audio stream.
21+
asp_err_t asp_audio_stop();
22+
// Start the audio stream.
23+
asp_err_t asp_audio_start();
24+
// Write audio samples.
25+
asp_err_t asp_audio_write(void* samples, size_t samples_size, int64_t timeout_ms);

src/audio.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)