mirror of
https://github.com/esphome/esphome.git
synced 2026-10-19 12:42:50 +00:00
[audio] Compute frames_to_microseconds() in 64 bits (#21089)
This commit is contained in:
@@ -23,7 +23,8 @@ AudioStreamInfo::AudioStreamInfo(uint8_t bits_per_sample, uint8_t channels, uint
|
||||
}
|
||||
|
||||
uint32_t AudioStreamInfo::frames_to_microseconds(uint32_t frames) const {
|
||||
return (frames * 1000000 + (this->sample_rate_ >> 1)) / this->sample_rate_;
|
||||
// In 64 bits: frames * 1000000 overflows 32 bits above 4294 frames, about 90 ms at 48 kHz.
|
||||
return (static_cast<uint64_t>(frames) * 1000000 + (this->sample_rate_ >> 1)) / this->sample_rate_;
|
||||
}
|
||||
|
||||
uint32_t AudioStreamInfo::frames_to_milliseconds_with_remainder(uint32_t *total_frames) const {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "esphome/components/audio/audio.h"
|
||||
|
||||
namespace esphome::audio::testing {
|
||||
|
||||
TEST(AudioStreamInfo, FramesToMicroseconds) {
|
||||
AudioStreamInfo info(16, 2, 48000);
|
||||
EXPECT_EQ(info.frames_to_microseconds(48), 1000U);
|
||||
EXPECT_EQ(info.frames_to_microseconds(1), 21U); // 20.83 us, rounded
|
||||
}
|
||||
|
||||
TEST(AudioStreamInfo, FramesToMicrosecondsAboveFourThousandFrames) {
|
||||
// frames * 1000000 no longer fits 32 bits above 4294 frames, about 90 ms at 48 kHz.
|
||||
AudioStreamInfo info(24, 2, 48000);
|
||||
EXPECT_EQ(info.frames_to_microseconds(4800), 100000U);
|
||||
EXPECT_EQ(info.frames_to_microseconds(48000), 1000000U);
|
||||
}
|
||||
|
||||
} // namespace esphome::audio::testing
|
||||
Reference in New Issue
Block a user