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