-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathESPAudio.h
More file actions
205 lines (179 loc) · 6.31 KB
/
ESPAudio.h
File metadata and controls
205 lines (179 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "esphome.h"
#if defined(ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
#include "AudioOutputI2S.h"
#define aAudioOutput() AudioOutputI2S(0, AudioOutputI2S::INTERNAL_DAC)
#else
#include "AudioOutputI2SNoDAC.h"
#define aAudioOutput() AudioOutputI2SNoDAC()
#endif
#if defined(SDCARD)
#include "SD.h"
#include "AudioFileSourceSD.h"
#define aFS SD
#define aFS_STR "SD"
using aAudioFileSource = AudioFileSourceSD;
#else
#include "LittleFS.h"
#include "AudioFileSourceLittleFS.h"
#define aFS LittleFS
#define aFS_STR "LittleFS"
using aAudioFileSource = AudioFileSourceLittleFS;
#endif
#include "AudioGeneratorMP3.h"
#include "AudioGeneratorWAV.h"
#include "AudioFileSourceHTTPStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioFileSourcePROGMEM.h"
#define DEBUGa(format, ...) ESP_LOGD("audio", format, ##__VA_ARGS__)
#define get_audio_component(constructor) static_cast<ESPAudio *> \
(const_cast<custom_component::CustomComponentConstructor *>(&constructor)->get_component(0))
#define playFile(audioComponent, fname) get_audio_component(audioComponent)->play(fname)
#define playData(audioComponent, data) get_audio_component(audioComponent)->play_data(data, sizeof(data), ESPAudio::MP3)
#define stopFile(audioComponent) get_audio_component(audioComponent)->stop()
#define isPlaying(audioComponent) get_audio_component(audioComponent)->is_playing()
#define I2SO_DATA 2
class ESPAudio : public Component {
public:
enum GeneratorType {WAV, MP3, UNK};
void setup() override {
this->fs_initialized_ = aFS.begin();
}
void loop() override {
if (this->gen_ && this->gen_->isRunning()) {
if (!this->gen_->loop()) {
this->stop();
}
}
}
void stop() {
if (this->gen_) {
this->gen_->stop(); // also stops output (out) and file (src)
}
delete this->gen_;
this->gen_ = nullptr;
delete this->out_;
this->out_ = nullptr;
delete this->buf_;
this->buf_ = nullptr;
delete this->src_;
this->src_ = nullptr;
#ifdef ESP8266
pinMode(I2SO_DATA, OUTPUT);
#endif
DEBUGa("Stopped playing");
}
bool play_file(const char *filename, GeneratorType type) {
this->stop();
return this->play_(this->open_file_(filename), type);
}
bool play_stream(const char *url, GeneratorType type) {
this->stop();
return this->play_(this->open_stream_(url), type);
}
bool play_data(const uint8_t *data, size_t size, GeneratorType type) {
this->stop();
return this->play_(this->open_data_(data, size), type);
}
void play(const char *filename) {
auto type = this->get_file_type_(filename);
bool play_result = false;
if (type == MP3) {
play_result = this->is_stream_(filename) ?
this->play_stream(filename, type) :
this->play_file(filename, type);
} else if (type == WAV) {
play_result = this->is_stream_(filename) ?
this->play_stream(filename, type) :
this->play_file(filename, type);
} else {
DEBUGa("Unknown audio file type %s", filename);
}
if (play_result) {
DEBUGa("Started playing audio file %s", filename);
} else {
DEBUGa("Failed to play audio file %s", filename);
this->stop();
}
}
bool is_playing() {
return this->gen_ && this->gen_->isRunning();
}
protected:
AudioOutput *out_{};
AudioGenerator *gen_{};
AudioFileSource *src_{};
AudioFileSourceBuffer *buf_{};
bool fs_initialized_{};
AudioFileSource *open_file_(const char *filename) {
if (!this->fs_initialized_) {
// try mount again
this->fs_initialized_ = aFS.begin();
if (!this->fs_initialized_) {
DEBUGa (aFS_STR " is not initialized");
return nullptr;
}
}
this->src_ = new aAudioFileSource();
if (!this->src_->open(filename)) {
DEBUGa("Can't open file %s", filename);
return nullptr;
}
return this->src_;
}
AudioFileSource *open_stream_(const char *url) {
this->src_ = new AudioFileSourceHTTPStream();
if (!this->src_->open(url)) {
DEBUGa("Can't open http stream %s", url);
return nullptr;
}
this->buf_ = new AudioFileSourceBuffer(this->src_, 2048);
return this->buf_;
}
AudioFileSource *open_data_(const uint8_t *data, size_t size) {
this->src_ = new AudioFileSourcePROGMEM(data, size);
if (!this->src_->isOpen()) {
DEBUGa("Can't read data array");
return nullptr;
}
return this->src_;
}
bool play_(AudioFileSource *src, GeneratorType type) {
if (!src) {
return false;
}
if (type == WAV) {
this->gen_ = new AudioGeneratorWAV();
} else if (type == MP3) {
this->gen_ = new AudioGeneratorMP3();
} else {
return false;
}
this->out_ = new aAudioOutput();
return this->gen_->begin(src, this->out_);
}
const GeneratorType get_file_type_(const char *filename) {
const char *dot = strrchr(filename, '.');
if (!dot) {
return UNK;
}
dot++;
if ((dot[0] == 'w' || dot[0] == 'W') &&
(dot[1] == 'a' || dot[1] == 'A') &&
(dot[2] == 'v' || dot[2] == 'V') &&
(dot[3] == 0)) {
return WAV;
}
if ((dot[0] == 'm' || dot[0] == 'M') &&
(dot[1] == 'p' || dot[1] == 'P') &&
(dot[2] == '3') &&
(dot[3] == 0)) {
return MP3;
}
return UNK;
}
bool is_stream_(const char *filename) {
const char *http = strstr(filename, "http://");
const char *https = strstr(filename, "https://");
return (http && (http - filename) == 0) || (https && (https - filename) == 0);
}
};