ESPHome 2025.5.0
Loading...
Searching...
No Matches
audio_reader.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP_IDF
4
5#include "audio.h"
7
9
10#include "esp_err.h"
11
12#include <esp_http_client.h>
13
14namespace esphome {
15namespace audio {
16
17enum class AudioReaderState : uint8_t {
18 READING = 0, // More data is available to read
19 FINISHED, // All data has been read and transferred
20 FAILED, // Encountered an error
21};
22
24 /*
25 * @brief Class that facilitates reading a raw audio file.
26 * Files can be read from flash (stored in a AudioFile struct) or from an http source.
27 * The file data is sent to a ring buffer sink.
28 */
29 public:
33 AudioReader(size_t buffer_size) : buffer_size_(buffer_size) {}
35
39 esp_err_t add_sink(const std::weak_ptr<RingBuffer> &output_ring_buffer);
40
45 esp_err_t start(const std::string &uri, AudioFileType &file_type);
46
51 esp_err_t start(AudioFile *audio_file, AudioFileType &file_type);
52
56
57 protected:
59 static esp_err_t http_event_handler(esp_http_client_event_t *evt);
60
64 static AudioFileType get_audio_type(const char *content_type);
65
68
69 std::shared_ptr<RingBuffer> file_ring_buffer_;
70 std::unique_ptr<AudioSinkTransferBuffer> output_transfer_buffer_;
72
75
76 esp_http_client_handle_t client_{nullptr};
77
80 const uint8_t *file_current_{nullptr};
81};
82} // namespace audio
83} // namespace esphome
84
85#endif
AudioReaderState http_read_()
AudioReaderState file_read_()
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
const uint8_t * file_current_
static AudioFileType get_audio_type(const char *content_type)
Determines the audio file type from the http header's Content-Type key.
std::shared_ptr< RingBuffer > file_ring_buffer_
AudioReader(size_t buffer_size)
Constructs an AudioReader object.
AudioReaderState read()
Reads new file data from the source and sends to the ring buffer sink.
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
Monitors the http client events to attempt determining the file type from the Content-Type header.
esp_http_client_handle_t client_
esp_err_t add_sink(const std::weak_ptr< RingBuffer > &output_ring_buffer)
Adds a sink ring buffer for audio data.
esp_err_t start(const std::string &uri, AudioFileType &file_type)
Starts reading an audio file from an http source.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7