#include "i2s_audio_media_player.h" #ifdef USE_ESP32_FRAMEWORK_ARDUINO #include "esphome/core/log.h" namespace esphome { namespace i2s_audio { static const char *const TAG = "audio"; void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) { if (call.get_media_url().has_value()) { if (this->audio_->isRunning()) this->audio_->stopSong(); this->high_freq_.start(); this->audio_->connecttohost(call.get_media_url().value().c_str()); this->state = media_player::MEDIA_PLAYER_STATE_PLAYING; } if (call.get_volume().has_value()) { this->volume = call.get_volume().value(); this->set_volume_(volume); this->unmute_(); } if (call.get_command().has_value()) { switch (call.get_command().value()) { case media_player::MEDIA_PLAYER_COMMAND_PLAY: if (!this->audio_->isRunning()) this->audio_->pauseResume(); this->state = media_player::MEDIA_PLAYER_STATE_PLAYING; break; case media_player::MEDIA_PLAYER_COMMAND_PAUSE: if (this->audio_->isRunning()) this->audio_->pauseResume(); this->state = media_player::MEDIA_PLAYER_STATE_PAUSED; break; case media_player::MEDIA_PLAYER_COMMAND_STOP: this->stop_(); break; case media_player::MEDIA_PLAYER_COMMAND_MUTE: this->mute_(); break; case media_player::MEDIA_PLAYER_COMMAND_UNMUTE: this->unmute_(); break; case media_player::MEDIA_PLAYER_COMMAND_TOGGLE: this->audio_->pauseResume(); if (this->audio_->isRunning()) { this->state = media_player::MEDIA_PLAYER_STATE_PLAYING; } else { this->state = media_player::MEDIA_PLAYER_STATE_PAUSED; } break; case media_player::MEDIA_PLAYER_COMMAND_VOLUME_UP: { float new_volume = this->volume + 0.1f; if (new_volume > 1.0f) new_volume = 1.0f; this->set_volume_(new_volume); this->unmute_(); break; } case media_player::MEDIA_PLAYER_COMMAND_VOLUME_DOWN: { float new_volume = this->volume - 0.1f; if (new_volume < 0.0f) new_volume = 0.0f; this->set_volume_(new_volume); this->unmute_(); break; } } } this->publish_state(); } void I2SAudioMediaPlayer::mute_() { if (this->mute_pin_ != nullptr) { this->mute_pin_->digital_write(true); } else { this->set_volume_(0.0f, false); } this->muted_ = true; } void I2SAudioMediaPlayer::unmute_() { if (this->mute_pin_ != nullptr) { this->mute_pin_->digital_write(false); } else { this->set_volume_(this->volume, false); } this->muted_ = false; } void I2SAudioMediaPlayer::set_volume_(float volume, bool publish) { this->audio_->setVolume(remap(volume, 0.0f, 1.0f, 0, 21)); if (publish) this->volume = volume; } void I2SAudioMediaPlayer::stop_() { if (this->audio_->isRunning()) this->audio_->stopSong(); this->high_freq_.stop(); this->state = media_player::MEDIA_PLAYER_STATE_IDLE; } void I2SAudioMediaPlayer::setup() { ESP_LOGCONFIG(TAG, "Setting up Audio..."); #if SOC_I2S_SUPPORTS_DAC if (this->internal_dac_mode_ != I2S_DAC_CHANNEL_DISABLE) { this->audio_ = make_unique