ESPHome 2026.3.0
Loading...
Searching...
No Matches
audio.cpp
Go to the documentation of this file.
1#include "audio.h"
2
4
5#include <cstring>
6
7namespace esphome {
8namespace audio {
9
10// Euclidean's algorithm for finding the greatest common divisor
11static uint32_t gcd(uint32_t a, uint32_t b) {
12 while (b != 0) {
13 uint32_t t = b;
14 b = a % b;
15 a = t;
16 }
17 return a;
18}
19
20AudioStreamInfo::AudioStreamInfo(uint8_t bits_per_sample, uint8_t channels, uint32_t sample_rate)
21 : bits_per_sample_(bits_per_sample), channels_(channels), sample_rate_(sample_rate) {
22 this->ms_sample_rate_gcd_ = gcd(1000, this->sample_rate_);
23 this->bytes_per_sample_ = (this->bits_per_sample_ + 7) / 8;
24}
25
27 return (frames * 1000000 + (this->sample_rate_ >> 1)) / this->sample_rate_;
28}
29
31 uint32_t unprocessable_frames = *total_frames % (this->sample_rate_ / this->ms_sample_rate_gcd_);
32 uint32_t frames_for_ms_calculation = *total_frames - unprocessable_frames;
33
34 uint32_t playback_ms = (frames_for_ms_calculation * 1000) / this->sample_rate_;
35 *total_frames = unprocessable_frames;
36 return playback_ms;
37}
38
40 return (this->bits_per_sample_ == rhs.get_bits_per_sample()) && (this->channels_ == rhs.get_channels()) &&
41 (this->sample_rate_ == rhs.get_sample_rate());
42}
43
45 switch (file_type) {
46#ifdef USE_AUDIO_FLAC_SUPPORT
48 return "FLAC";
49#endif
50#ifdef USE_AUDIO_MP3_SUPPORT
52 return "MP3";
53#endif
54#ifdef USE_AUDIO_OPUS_SUPPORT
56 return "OPUS";
57#endif
59 return "WAV";
60 default:
61 return "unknown";
62 }
63}
64
65AudioFileType detect_audio_file_type(const char *content_type, const char *url) {
66 // Try Content-Type header first
67 if (content_type != nullptr && content_type[0] != '\0') {
68#ifdef USE_AUDIO_MP3_SUPPORT
69 if (strcasecmp(content_type, "mp3") == 0 || strcasecmp(content_type, "audio/mp3") == 0 ||
70 strcasecmp(content_type, "audio/mpeg") == 0) {
71 return AudioFileType::MP3;
72 }
73#endif
74 if (strcasecmp(content_type, "audio/wav") == 0) {
75 return AudioFileType::WAV;
76 }
77#ifdef USE_AUDIO_FLAC_SUPPORT
78 if (strcasecmp(content_type, "audio/flac") == 0 || strcasecmp(content_type, "audio/x-flac") == 0) {
80 }
81#endif
82#ifdef USE_AUDIO_OPUS_SUPPORT
83 // Match "audio/ogg" with a codecs parameter containing "opus"
84 // Valid forms: audio/ogg;codecs=opus, audio/ogg; codecs="opus", etc.
85 // Plain "audio/ogg" without opus is not matched (almost always Ogg Vorbis)
86 if (strncasecmp(content_type, "audio/ogg", 9) == 0 && strcasestr(content_type + 9, "opus") != nullptr) {
88 }
89#endif
90 }
91
92 // Fallback to URL extension
93 if (url != nullptr && url[0] != '\0') {
94 if (str_endswith_ignore_case(url, ".wav")) {
95 return AudioFileType::WAV;
96 }
97#ifdef USE_AUDIO_MP3_SUPPORT
98 if (str_endswith_ignore_case(url, ".mp3")) {
99 return AudioFileType::MP3;
100 }
101#endif
102#ifdef USE_AUDIO_FLAC_SUPPORT
103 if (str_endswith_ignore_case(url, ".flac")) {
104 return AudioFileType::FLAC;
105 }
106#endif
107#ifdef USE_AUDIO_OPUS_SUPPORT
108 if (str_endswith_ignore_case(url, ".opus")) {
109 return AudioFileType::OPUS;
110 }
111#endif
112 }
113
114 return AudioFileType::NONE;
115}
116
117void scale_audio_samples(const int16_t *audio_samples, int16_t *output_buffer, int16_t scale_factor,
118 size_t samples_to_scale) {
119 // Note the assembly dsps_mulc function has audio glitches if the input and output buffers are the same.
120 for (size_t i = 0; i < samples_to_scale; i++) {
121 int32_t acc = (int32_t) audio_samples[i] * (int32_t) scale_factor;
122 output_buffer[i] = (int16_t) (acc >> 15);
123 }
124}
125
126} // namespace audio
127} // namespace esphome
uint8_t get_bits_per_sample() const
Definition audio.h:28
uint32_t frames_to_microseconds(uint32_t frames) const
Computes the duration, in microseconds, the given amount of frames represents.
Definition audio.cpp:26
uint8_t get_channels() const
Definition audio.h:29
uint32_t frames_to_milliseconds_with_remainder(uint32_t *frames) const
Computes the duration, in milliseconds, the given amount of frames represents.
Definition audio.cpp:30
bool operator==(const AudioStreamInfo &rhs) const
Definition audio.cpp:39
uint32_t get_sample_rate() const
Definition audio.h:30
void scale_audio_samples(const int16_t *audio_samples, int16_t *output_buffer, int16_t scale_factor, size_t samples_to_scale)
Scales Q15 fixed point audio samples.
Definition audio.cpp:117
const char * audio_file_type_to_string(AudioFileType file_type)
Helper function to convert file type to a const char string.
Definition audio.cpp:44
AudioFileType detect_audio_file_type(const char *content_type, const char *url)
Detect audio file type from a Content-Type header value and/or URL extension.
Definition audio.cpp:65
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool str_endswith_ignore_case(const char *str, size_t str_len, const char *suffix, size_t suffix_len)
Case-insensitive check if string ends with suffix (no heap allocation).
Definition helpers.cpp:179
static void uint32_t