diff --git a/esphome/components/audio/audio.cpp b/esphome/components/audio/audio.cpp index 3eaed7954a..7602f2128c 100644 --- a/esphome/components/audio/audio.cpp +++ b/esphome/components/audio/audio.cpp @@ -5,11 +5,11 @@ namespace audio { /* *************** AudioListener **************** */ -AudioStreamer *AudioListener::start(AudioStreamInfo &info) { +AudioStreamer *AudioListener::start(const AudioStreamInfo &audio_stream_info) { if (current_streamer_ != nullptr) { return nullptr; } - if (this->starting(info)) { + if (this->starting(audio_stream_info)) { this->current_streamer_ = new AudioStreamer(); this->current_streamer_->set_parent(this); } @@ -19,7 +19,7 @@ AudioStreamer *AudioListener::start(AudioStreamInfo &info) { AudioStreamer *AudioListener::start() { AudioStreamInfo info; this->get_default_audio_stream_info(info); - this->start(info); + return this->start(info); } bool AudioListener::can_stream(AudioStreamer *streamer) { @@ -38,8 +38,8 @@ AudioStreamer::~AudioStreamer() { bool AudioStreamer::is_running() { return (this->parent_ == nullptr) ? false : this->parent_->can_stream(this); } -size_t AudioStreamer::stream(const uint8_t *data, const size_t size, TickType_t ticks_to_wait) { - if (!this->is_running(this)) +size_t AudioStreamer::stream(const uint8_t *data, size_t size, TickType_t ticks_to_wait) { + if (!this->is_running()) return 0; return this->parent_->streaming(data, size, ticks_to_wait); } diff --git a/esphome/components/audio/audio.h b/esphome/components/audio/audio.h index eaa1eeb4af..3d5788ab0c 100644 --- a/esphome/components/audio/audio.h +++ b/esphome/components/audio/audio.h @@ -39,7 +39,7 @@ class AudioStreamer : public Parented { /// @param length The length of the audio data in bytes. /// @return The number of bytes that were actually written to the speaker's internal buffer. - size_t stream(const uint8_t *data, const size_t size, TickType_t ticks_to_wait = 0); + size_t stream(const uint8_t *data, size_t size, TickType_t ticks_to_wait = 0); bool is_running(); }; @@ -54,11 +54,7 @@ class AudioListener { AudioStreamer *start(const AudioStreamInfo &audio_stream_info); AudioStreamer *start(); - void stop() { - if (this->current_streamer_ != nullptr) { - delete this->current_streamer_; - } - } + void stop() { delete this->current_streamer_; } virtual bool can_stream(AudioStreamer *streamer); @@ -69,6 +65,7 @@ class AudioListener { virtual void get_default_audio_stream_info(AudioStreamInfo &audio_stream_info) {} protected: + friend class AudioStreamer; virtual bool starting(const AudioStreamInfo &audio_stream_info) = 0; virtual size_t streaming(const uint8_t *data, size_t size, TickType_t ticks_to_wait) = 0; virtual void stopping(){};