ESPHome  2024.4.1
rtttl.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #ifdef USE_OUTPUT
8 #endif
9 
10 #ifdef USE_SPEAKER
12 #endif
13 
14 namespace esphome {
15 namespace rtttl {
16 
17 #ifdef USE_SPEAKER
18 static const size_t SAMPLE_BUFFER_SIZE = 512;
19 
20 struct SpeakerSample {
21  int16_t left{0};
22  int16_t right{0};
23 };
24 #endif
25 
26 class Rtttl : public Component {
27  public:
28 #ifdef USE_OUTPUT
29  void set_output(output::FloatOutput *output) { this->output_ = output; }
30 #endif
31 #ifdef USE_SPEAKER
32  void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
33 #endif
34  void set_gain(float gain) {
35  if (gain < 0.1f)
36  gain = 0.1f;
37  if (gain > 1.0f)
38  gain = 1.0f;
39  this->gain_ = gain;
40  }
41  void play(std::string rtttl);
42  void stop();
43  void dump_config() override;
44 
45  bool is_playing() { return this->note_duration_ != 0; }
46  void loop() override;
47 
48  void add_on_finished_playback_callback(std::function<void()> callback) {
49  this->on_finished_playback_callback_.add(std::move(callback));
50  }
51 
52  protected:
53  inline uint8_t get_integer_() {
54  uint8_t ret = 0;
55  while (isdigit(this->rtttl_[this->position_])) {
56  ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
57  }
58  return ret;
59  }
60 
61  std::string rtttl_{""};
62  size_t position_{0};
63  uint16_t wholenote_;
65  uint16_t default_octave_;
66  uint32_t last_note_;
67  uint16_t note_duration_;
68 
69  uint32_t output_freq_;
70  float gain_{0.6f};
71 
72 #ifdef USE_OUTPUT
74 #endif
75 
76  void play_output_();
77 
78 #ifdef USE_SPEAKER
79  speaker::Speaker *speaker_{nullptr};
80  int sample_rate_{16000};
81  int samples_per_wave_{0};
82  int samples_sent_{0};
83  int samples_count_{0};
84  int samples_gap_{0};
85 
86 #endif
87 
89 };
90 
91 template<typename... Ts> class PlayAction : public Action<Ts...> {
92  public:
93  PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
94  TEMPLATABLE_VALUE(std::string, value)
95 
96  void play(Ts... x) override { this->rtttl_->play(this->value_.value(x...)); }
97 
98  protected:
100 };
101 
102 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
103  public:
104  void play(Ts... x) override { this->parent_->stop(); }
105 };
106 
107 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
108  public:
109  bool check(Ts... x) override { return this->parent_->is_playing(); }
110 };
111 
113  public:
114  explicit FinishedPlaybackTrigger(Rtttl *parent) {
115  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
116  }
117 };
118 
119 } // namespace rtttl
120 } // namespace esphome
uint16_t wholenote_
Definition: rtttl.h:63
void loop()
void set_gain(float gain)
Definition: rtttl.h:34
void play(Ts... x) override
Definition: rtttl.h:104
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
uint16_t x
Definition: tt21100.cpp:17
CallbackManager< void()> on_finished_playback_callback_
Definition: rtttl.h:88
uint32_t output_freq_
Definition: rtttl.h:69
bool check(Ts... x) override
Definition: rtttl.h:109
uint8_t get_integer_()
Definition: rtttl.h:53
Base class for all automation conditions.
Definition: automation.h:74
uint16_t note_duration_
Definition: rtttl.h:67
uint16_t default_duration_
Definition: rtttl.h:64
void set_speaker(speaker::Speaker *speaker)
Definition: rtttl.h:32
void set_output(output::FloatOutput *output)
Definition: rtttl.h:29
output::FloatOutput * output_
Definition: rtttl.h:73
PlayAction(Rtttl *rtttl)
Definition: rtttl.h:93
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition: rtttl.h:94
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: rtttl.h:48
bool is_playing()
Definition: rtttl.h:45
uint16_t default_octave_
Definition: rtttl.h:65
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
FinishedPlaybackTrigger(Rtttl *parent)
Definition: rtttl.h:114
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515
uint32_t last_note_
Definition: rtttl.h:66