ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
capture_time.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <algorithm>
6#include <cstdint>
7
8namespace esphome::i2s_audio {
9
23inline int64_t dma_capture_time_us(uint32_t bytes_received, uint32_t drops, uint32_t drops_before_read,
24 int64_t receive_time_us, uint32_t bytes_read_before, uint32_t bytes_read,
25 uint32_t dma_desc_num, uint32_t dma_buffer_bytes,
26 const audio::AudioStreamInfo &stream_info) {
27 // A drop between the two snapshots may have removed buffers newer than this read's first frame, which the count
28 // back would skip, so the capture time is unknown. It is also unknown if no buffer was recorded because the
29 // callbacks failed to register.
30 if ((drops != drops_before_read) || (receive_time_us == 0)) {
31 return 0;
32 }
33 // Count back from the newest DMA buffer to this read's first frame. At most dma_desc_num buffers follow the read: a
34 // full queue plus one the driver has recorded but not queued yet.
35 const uint32_t bytes_queued = bytes_received - drops * dma_buffer_bytes;
36 const uint32_t bytes_after_first =
37 std::min<uint32_t>(bytes_queued - bytes_read_before, bytes_read + dma_desc_num * dma_buffer_bytes);
38 const uint32_t frames_after_first = stream_info.bytes_to_frames(bytes_after_first);
39 return receive_time_us - static_cast<int64_t>(frames_after_first) * 1000000 / stream_info.get_sample_rate();
40}
41
42} // namespace esphome::i2s_audio
uint32_t bytes_to_frames(size_t bytes) const
Convert bytes to frames.
Definition audio.h:43
uint32_t get_sample_rate() const
Definition audio.h:30
int64_t dma_capture_time_us(uint32_t bytes_received, uint32_t drops, uint32_t drops_before_read, int64_t receive_time_us, uint32_t bytes_read_before, uint32_t bytes_read, uint32_t dma_desc_num, uint32_t dma_buffer_bytes, const audio::AudioStreamInfo &stream_info)
Returns when the first frame of a read was captured, counted back from the newest DMA buffer,...
static void uint32_t