/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "AudioSampleFormat.h" #include "gtest/gtest.h" #include using namespace mozilla; template constexpr T LowestSample() { if constexpr (std::is_integral_v) { return std::numeric_limits::lowest(); } else { return -1.0f; } } // When converting a sample-type to another sample-type, this returns the // maximum value possible in the destination format template constexpr Dest HighestSample() { if constexpr (std::is_integral_v) { return std::numeric_limits::max(); } else { return +1.0f; } } // When converting a sample-type to another sample-type, this returns the // maximum value expected in the destination format template constexpr Dest HighestSampleExpected() { // When converting small integer samples to large integer sample, the higher // bound isn't reached because of positive / negative integer assymetry. if constexpr (std::is_same_v && std::is_same_v) { return 32512; // INT16_MAX - 2 << 7 + 1 } else if constexpr (std::is_same_v && std::is_same_v) { return 2130706432; // INT32_MAX - (2 << 23) + 1 } else if constexpr (std::is_same_v && std::is_same_v) { return 2147418112; // INT32_MAX - UINT16_MAX } if constexpr (std::is_integral_v) { return std::numeric_limits::max(); } else { return +1.0f; } } template void TestSampleTypePair() { std::cout << __PRETTY_FUNCTION__ << std::endl; ASSERT_EQ(LowestSample(), ConvertAudioSample(LowestSample())); Dest expected = HighestSampleExpected(); ASSERT_EQ(expected, ConvertAudioSample(HighestSample())); ASSERT_EQ(Bias(), ConvertAudioSample(Bias())); } template void TestSampleType24bits() { std::cout << __PRETTY_FUNCTION__ << std::endl; int32_t max_sample_24bits = (2 << 22) - 1; int32_t min_sample_24bits = -(2 << 22); int32_t silence_24bits = 0; ASSERT_EQ(LowestSample(), Int24ToAudioSample(min_sample_24bits)); ASSERT_EQ(Int24ToAudioSample(min_sample_24bits), LowestSample()); if constexpr (std::is_same_v) { // Quantization issue: 2147483392 + (2<<8 - 1) == INT32_MAX // See comment on HighestSampleExpected above const int32_t HIGHEST_FROM_24BITS = 2147483392; ASSERT_EQ(HIGHEST_FROM_24BITS, Int24ToAudioSample(max_sample_24bits)); ASSERT_EQ(Int24ToAudioSample(max_sample_24bits), HIGHEST_FROM_24BITS); } else { ASSERT_EQ(HighestSample(), Int24ToAudioSample(max_sample_24bits)); ASSERT_EQ(Int24ToAudioSample(max_sample_24bits), HighestSample()); } ASSERT_EQ(Bias(), Int24ToAudioSample(silence_24bits)); ASSERT_EQ(Int24ToAudioSample(silence_24bits), Bias()); } TEST(AudioSampleFormat, Boundaries) { TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); TestSampleTypePair(); // Separately test 24-bit audio stored in 32-bits integers. TestSampleType24bits(); TestSampleType24bits(); TestSampleType24bits(); TestSampleType24bits(); }