Use std::numbers constants more

Part of modernizing to C++20.

Also prefer C++ std::isfinite to C macro, see
  https://en.cppreference.com/w/c/numeric/math/isfinite
vs
  https://en.cppreference.com/w/cpp/numeric/math/isfinite

Bug: None
Change-Id: I41c3173984a5bb5356b6774af3794d1b05798d37
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/394244
Reviewed-by: Guido Urdaneta <guidou@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@meta.com>
Cr-Commit-Position: refs/heads/main@{#44877}
This commit is contained in:
Philipp Hancke 2025-06-03 13:33:29 -07:00 committed by WebRTC LUCI CQ
parent b62b0b2c11
commit de2bbb38ff
8 changed files with 51 additions and 53 deletions

View File

@ -11,18 +11,11 @@
#ifndef API_AUDIO_AUDIO_PROCESSING_H_
#define API_AUDIO_AUDIO_PROCESSING_H_
// MSVC++ requires this to be set before any other includes to get M_PI.
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <stddef.h> // size_t
#include <stdio.h> // FILE
#include <string.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <optional>
#include <string>

View File

@ -82,19 +82,17 @@
// Note: we're glossing over how the sub-sample handling works with
// `virtual_source_idx_`, etc.
// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES
#include "common_audio/resampler/sinc_resampler.h"
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <limits>
#include <numbers>
#include "rtc_base/checks.h"
#include "rtc_base/cpu_info.h"
#include "rtc_base/memory/aligned_malloc.h"
#include "rtc_base/system/arch.h"
namespace webrtc {
@ -210,14 +208,16 @@ void SincResampler::InitializeKernel() {
for (size_t i = 0; i < kKernelSize; ++i) {
const size_t idx = i + offset_idx * kKernelSize;
const float pre_sinc = static_cast<float>(
M_PI * (static_cast<int>(i) - static_cast<int>(kKernelSize / 2) -
subsample_offset));
std::numbers::pi *
(static_cast<int>(i) - static_cast<int>(kKernelSize / 2) -
subsample_offset));
kernel_pre_sinc_storage_[idx] = pre_sinc;
// Compute Blackman window, matching the offset of the sinc().
const float x = (i - subsample_offset) / kKernelSize;
const float window = static_cast<float>(kA0 - kA1 * cos(2.0 * M_PI * x) +
kA2 * cos(4.0 * M_PI * x));
const float window =
static_cast<float>(kA0 - kA1 * cos(2.0 * std::numbers::pi * x) +
kA2 * cos(4.0 * std::numbers::pi * x));
kernel_window_storage_[idx] = window;
// Compute the sinc with offset, then window the sinc() function and store

View File

@ -11,15 +11,16 @@
// Modified from the Chromium original:
// src/media/base/sinc_resampler_unittest.cc
// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES
#include "common_audio/resampler/sinc_resampler.h"
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <numbers>
#include <tuple>
#include "common_audio/resampler/sinusoidal_linear_chirp_source.h"
@ -261,7 +262,7 @@ TEST_P(SincResamplerTest, Resample) {
std::unique_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]);
memcpy(kernel.get(), resampler.get_kernel_for_testing(),
SincResampler::kKernelStorageSize);
resampler.SetRatio(M_PI);
resampler.SetRatio(std::numbers::pi_v<float>);
ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(),
SincResampler::kKernelStorageSize));
resampler.SetRatio(io_ratio);

View File

@ -8,12 +8,11 @@
* be found in the AUTHORS file in the root of the source tree.
*/
// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES
#include "common_audio/resampler/sinusoidal_linear_chirp_source.h"
#include <math.h>
#include <cmath>
#include <cstddef>
#include <numbers>
namespace webrtc {
@ -43,7 +42,8 @@ void SinusoidalLinearChirpSource::Run(size_t frames, float* destination) {
} else {
// Sinusoidal linear chirp.
double t = (current_index_ - delay_samples_) / sample_rate_;
destination[i] = sin(2 * M_PI * (kMinFrequency * t + (k_ / 2) * t * t));
destination[i] =
sin(2 * std::numbers::pi * (kMinFrequency * t + (k_ / 2) * t * t));
}
}
}

View File

@ -8,15 +8,19 @@
* be found in the AUTHORS file in the root of the source tree.
*/
// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES
#include "common_audio/wav_file.h"
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <limits>
#include <numbers>
#include <string>
#include "common_audio/wav_header.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.h"
@ -135,12 +139,13 @@ TEST(WavWriterTest, LargeFile) {
const double t =
static_cast<double>(i) / (kNumChannels * kSampleRate);
const double x = std::numeric_limits<int16_t>::max() *
std::sin(t * kToneHz * 2 * M_PI);
samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
std::sin(t * kToneHz * 2 * std::numbers::pi);
samples[i] = std::pow(std::sin(t * 2 * 2 * std::numbers::pi), 10) * x;
samples[i + 1] =
std::pow(std::cos(t * 2 * 2 * std::numbers::pi), 10) * x;
// See https://issues.webrtc.org/issues/379973428
RTC_CHECK(isfinite(samples[i]));
RTC_CHECK(isfinite(samples[i + 1]));
RTC_CHECK(std::isfinite(samples[i]));
RTC_CHECK(std::isfinite(samples[i + 1]));
}
{
WavWriter w(outfile, kSampleRate, kNumChannels, wav_format);
@ -177,7 +182,7 @@ TEST(WavWriterTest, LargeFile) {
EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
for (size_t i = 0; i < kNumSamples; ++i) {
EXPECT_NEAR(samples[i], read_samples[i], 1);
if (!isfinite(samples[i])) {
if (!std::isfinite(samples[i])) {
// See https://issues.webrtc.org/issues/379973428
RTC_LOG(LS_ERROR)
<< "samples[" << i << "] is not finite. "
@ -192,7 +197,7 @@ TEST(WavWriterTest, LargeFile) {
EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
for (size_t i = 0; i < kNumSamples; ++i) {
EXPECT_NEAR(samples[i], static_cast<float>(read_samples[i]), 1);
if (!isfinite(samples[i])) {
if (!std::isfinite(samples[i])) {
// See https://issues.webrtc.org/issues/379973428
RTC_LOG(LS_ERROR)
<< "samples[" << i << "] is not finite. "

View File

@ -8,12 +8,11 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#define _USE_MATH_DEFINES
#include "common_audio/window_generator.h"
#include <cmath>
#include <complex>
#include <cstddef>
#include <numbers>
#include "rtc_base/checks.h"

View File

@ -14,6 +14,7 @@
#include <algorithm>
#include <cmath>
#include <memory>
#include <numbers>
#include <vector>
#include "api/array_view.h"
@ -91,7 +92,8 @@ TEST(PostFilterTest, Tone19p8kHzSignalAttenuation48k) {
constexpr int num_frames = sample_rate_hz * 10 / 1000; // 10ms;
constexpr double tone_frequency = 19800; // Hz
const double phase_increment = tone_frequency * 2.0 * M_PI / sample_rate_hz;
const double phase_increment =
tone_frequency * 2.0 * std::numbers::pi / sample_rate_hz;
double phase = 0.0;
std::vector<float> audio_input(num_frames);
@ -132,7 +134,8 @@ TEST(PostFilterTest, Tone17kHzSignalNoAttenuation48k) {
constexpr int num_frames = sample_rate_hz * 10 / 1000; // 10ms;
constexpr double tone_frequency = 16800; // Hz
const double phase_increment = tone_frequency * 2.0 * M_PI / sample_rate_hz;
const double phase_increment =
tone_frequency * 2.0 * std::numbers::pi / sample_rate_hz;
double phase = 0.0;
std::vector<float> audio_input(num_frames);

View File

@ -33,17 +33,13 @@
// cases in which there are wrong offsets leading to self cross-talk (which is
// rejected).
// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <map>
#include <memory>
#include <numbers>
#include <optional>
#include <string>
#include <utility>
@ -108,7 +104,8 @@ std::unique_ptr<MockWavReaderFactory> CreateMockWavReaderFactory() {
void CreateSineWavFile(absl::string_view filepath,
const MockWavReaderFactory::Params& params,
float frequency_hz = 440.0f) {
const double phase_step = 2 * M_PI * frequency_hz / params.sample_rate;
const double phase_step =
2 * std::numbers::pi * frequency_hz / params.sample_rate;
double phase = 0.0;
std::vector<int16_t> samples(params.num_samples);
for (size_t i = 0; i < params.num_samples; ++i) {