summaryrefslogtreecommitdiff
path: root/src/drivers/bluetooth.cpp
diff options
context:
space:
mode:
authorcooljqln <cooljqln@noreply.codeberg.org>2024-05-09 06:24:14 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2024-05-09 06:24:14 +0000
commitb720ba42a0f7645e1f6c3925e92971778c309e94 (patch)
tree03be2c6fbc1496e48d465f2ec3510d11ab4d73d9 /src/drivers/bluetooth.cpp
parent344a46d0664eb75d232eacea91a4957a25e071f6 (diff)
parent690c64c151777121afc2b39868f73b0fe666458e (diff)
downloadtangara-fw-b720ba42a0f7645e1f6c3925e92971778c309e94.tar.gz
Merge pull request 'Factor gapless handling out of audio_fsm and into the audio pipeline' (#69) from jqln/audio-pipeline into main
Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/69 Reviewed-by: ailurux <ailurux@noreply.codeberg.org>
Diffstat (limited to 'src/drivers/bluetooth.cpp')
-rw-r--r--src/drivers/bluetooth.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp
index 4caffae7..fcb764f6 100644
--- a/src/drivers/bluetooth.cpp
+++ b/src/drivers/bluetooth.cpp
@@ -38,6 +38,7 @@ namespace drivers {
DRAM_ATTR static StreamBufferHandle_t sStream = nullptr;
DRAM_ATTR static std::atomic<float> sVolumeFactor = 1.f;
+DRAM_ATTR static std::atomic<uint32_t> sSamplesUsed = 0;
static tasks::WorkerPool* sBgWorker;
@@ -47,8 +48,8 @@ auto gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t* param) -> void {
bluetooth::events::internal::Gap{.type = event, .param = param});
}
-auto avrcp_cb(esp_avrc_ct_cb_event_t event,
- esp_avrc_ct_cb_param_t* param) -> void {
+auto avrcp_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t* param)
+ -> void {
esp_avrc_ct_cb_param_t copy = *param;
sBgWorker->Dispatch<void>([=]() {
auto lock = bluetooth::BluetoothState::lock();
@@ -73,6 +74,13 @@ IRAM_ATTR auto a2dp_data_cb(uint8_t* buf, int32_t buf_size) -> int32_t {
}
size_t bytes_received = xStreamBufferReceive(stream, buf, buf_size, 0);
+ size_t samples_received = bytes_received / 2;
+ if (UINT32_MAX - sSamplesUsed < samples_received) {
+ sSamplesUsed = samples_received - (UINT32_MAX - sSamplesUsed);
+ } else {
+ sSamplesUsed += samples_received;
+ }
+
// Apply software volume scaling.
int16_t* samples = reinterpret_cast<int16_t*>(buf);
float factor = sVolumeFactor.load();
@@ -165,6 +173,10 @@ auto Bluetooth::SetVolumeFactor(float f) -> void {
sVolumeFactor = f;
}
+auto Bluetooth::SamplesUsed() -> uint32_t {
+ return sSamplesUsed;
+}
+
auto Bluetooth::SetEventHandler(std::function<void(bluetooth::Event)> cb)
-> void {
auto lock = bluetooth::BluetoothState::lock();