TestAudioSampleFormat.cpp (4233B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * 5 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 6 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #include <type_traits> 9 10 #include "AudioSampleFormat.h" 11 #include "gtest/gtest.h" 12 13 using namespace mozilla; 14 15 template <typename T> 16 constexpr T LowestSample() { 17 if constexpr (std::is_integral_v<T>) { 18 return std::numeric_limits<T>::lowest(); 19 } else { 20 return -1.0f; 21 } 22 } 23 24 // When converting a sample-type to another sample-type, this returns the 25 // maximum value possible in the destination format 26 template <typename Dest> 27 constexpr Dest HighestSample() { 28 if constexpr (std::is_integral_v<Dest>) { 29 return std::numeric_limits<Dest>::max(); 30 } else { 31 return +1.0f; 32 } 33 } 34 35 // When converting a sample-type to another sample-type, this returns the 36 // maximum value expected in the destination format 37 template <typename Dest, typename Source> 38 constexpr Dest HighestSampleExpected() { 39 // When converting small integer samples to large integer sample, the higher 40 // bound isn't reached because of positive / negative integer assymetry. 41 if constexpr (std::is_same_v<Source, uint8_t> && 42 std::is_same_v<Dest, int16_t>) { 43 return 32512; // INT16_MAX - 2 << 7 + 1 44 } else if constexpr (std::is_same_v<Source, uint8_t> && 45 std::is_same_v<Dest, int32_t>) { 46 return 2130706432; // INT32_MAX - (2 << 23) + 1 47 } else if constexpr (std::is_same_v<Source, int16_t> && 48 std::is_same_v<Dest, int32_t>) { 49 return 2147418112; // INT32_MAX - UINT16_MAX 50 } 51 52 if constexpr (std::is_integral_v<Dest>) { 53 return std::numeric_limits<Dest>::max(); 54 } else { 55 return +1.0f; 56 } 57 } 58 59 template <typename Source, typename Dest> 60 void TestSampleTypePair() { 61 std::cout << __PRETTY_FUNCTION__ << std::endl; 62 63 ASSERT_EQ(LowestSample<Dest>(), 64 ConvertAudioSample<Dest>(LowestSample<Source>())); 65 Dest expected = HighestSampleExpected<Dest, Source>(); 66 ASSERT_EQ(expected, ConvertAudioSample<Dest>(HighestSample<Source>())); 67 ASSERT_EQ(Bias<Dest>(), ConvertAudioSample<Dest>(Bias<Source>())); 68 } 69 70 template <typename T> 71 void TestSampleType24bits() { 72 std::cout << __PRETTY_FUNCTION__ << std::endl; 73 74 int32_t max_sample_24bits = (2 << 22) - 1; 75 int32_t min_sample_24bits = -(2 << 22); 76 int32_t silence_24bits = 0; 77 78 ASSERT_EQ(LowestSample<T>(), Int24ToAudioSample<T>(min_sample_24bits)); 79 ASSERT_EQ(Int24ToAudioSample<T>(min_sample_24bits), LowestSample<T>()); 80 if constexpr (std::is_same_v<T, int32_t>) { 81 // Quantization issue: 2147483392 + (2<<8 - 1) == INT32_MAX 82 // See comment on HighestSampleExpected above 83 const int32_t HIGHEST_FROM_24BITS = 2147483392; 84 ASSERT_EQ(HIGHEST_FROM_24BITS, Int24ToAudioSample<T>(max_sample_24bits)); 85 ASSERT_EQ(Int24ToAudioSample<T>(max_sample_24bits), HIGHEST_FROM_24BITS); 86 } else { 87 ASSERT_EQ(HighestSample<T>(), Int24ToAudioSample<T>(max_sample_24bits)); 88 ASSERT_EQ(Int24ToAudioSample<T>(max_sample_24bits), HighestSample<T>()); 89 } 90 ASSERT_EQ(Bias<T>(), Int24ToAudioSample<T>(silence_24bits)); 91 ASSERT_EQ(Int24ToAudioSample<T>(silence_24bits), Bias<T>()); 92 } 93 94 TEST(AudioSampleFormat, Boundaries) 95 { 96 TestSampleTypePair<uint8_t, uint8_t>(); 97 TestSampleTypePair<uint8_t, int16_t>(); 98 TestSampleTypePair<uint8_t, int32_t>(); 99 TestSampleTypePair<uint8_t, float>(); 100 TestSampleTypePair<int16_t, uint8_t>(); 101 TestSampleTypePair<int16_t, int16_t>(); 102 TestSampleTypePair<int16_t, int32_t>(); 103 TestSampleTypePair<int16_t, float>(); 104 TestSampleTypePair<int32_t, uint8_t>(); 105 TestSampleTypePair<int32_t, int16_t>(); 106 TestSampleTypePair<int32_t, int32_t>(); 107 TestSampleTypePair<int32_t, float>(); 108 TestSampleTypePair<float, uint8_t>(); 109 TestSampleTypePair<float, int16_t>(); 110 TestSampleTypePair<float, int32_t>(); 111 TestSampleTypePair<float, float>(); 112 113 // Separately test 24-bit audio stored in 32-bits integers. 114 TestSampleType24bits<uint8_t>(); 115 TestSampleType24bits<int16_t>(); 116 TestSampleType24bits<int32_t>(); 117 TestSampleType24bits<float>(); 118 }