Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

audio-convert.cpp 6.3 KiB

pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
pirms 11 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <lol/lib/doctest>
  2. #include <lol/audio/stream>
  3. TEST_CASE("sample conversion: float|double ←→ float|double")
  4. {
  5. auto cv1 = lol::audio::sample::convert<float, float>;
  6. CHECK(cv1(-1.0f) == -1.0f);
  7. CHECK(cv1(-0.5f) == -0.5f);
  8. CHECK(cv1( 0.0f) == 0.0f);
  9. CHECK(cv1( 0.5f) == 0.5f);
  10. CHECK(cv1( 1.0f) == 1.0f);
  11. auto cv2 = lol::audio::sample::convert<float, double>;
  12. CHECK(cv2(-1.0f) == -1.0);
  13. CHECK(cv2(-0.5f) == -0.5);
  14. CHECK(cv2( 0.0f) == 0.0);
  15. CHECK(cv2( 0.5f) == 0.5);
  16. CHECK(cv2( 1.0f) == 1.0);
  17. auto cv3 = lol::audio::sample::convert<double, float>;
  18. CHECK(cv3(-1.0) == -1.0f);
  19. CHECK(cv3(-0.5) == -0.5f);
  20. CHECK(cv3( 0.0) == 0.0f);
  21. CHECK(cv3( 0.5) == 0.5f);
  22. CHECK(cv3( 1.0) == 1.0f);
  23. auto cv4 = lol::audio::sample::convert<double, double>;
  24. CHECK(cv4(-1.0) == -1.0);
  25. CHECK(cv4(-0.5) == -0.5);
  26. CHECK(cv4( 0.0) == 0.0);
  27. CHECK(cv4( 0.5) == 0.5);
  28. CHECK(cv4( 1.0) == 1.0);
  29. }
  30. TEST_CASE("sample conversion: float → int8|uint8")
  31. {
  32. auto cv1 = lol::audio::sample::convert<float, int8_t>;
  33. CHECK(cv1(-1.5f) == -0x80);
  34. CHECK(cv1(-1.0f) == -0x80);
  35. CHECK(cv1(-0.5f) == -0x40);
  36. CHECK(cv1( 0.0f) == 0x00);
  37. CHECK(cv1( 0.5f) == 0x40);
  38. CHECK(cv1( 1.0f) == 0x7f);
  39. CHECK(cv1( 1.5f) == 0x7f);
  40. auto cv2 = lol::audio::sample::convert<float, uint8_t>;
  41. CHECK(cv2(-1.5f) == 0x00);
  42. CHECK(cv2(-1.0f) == 0x00);
  43. CHECK(cv2(-0.5f) == 0x40);
  44. CHECK(cv2( 0.0f) == 0x80);
  45. CHECK(cv2( 0.5f) == 0xc0);
  46. CHECK(cv2( 1.0f) == 0xff);
  47. CHECK(cv2( 1.5f) == 0xff);
  48. }
  49. TEST_CASE("sample conversion: int8|uint8 → float")
  50. {
  51. auto cv1 = lol::audio::sample::convert<int8_t, float>;
  52. CHECK(cv1(-0x80) == -1.0f);
  53. CHECK(cv1( 0x7f) == 1.0f);
  54. for (int n = -0x80; n < 0x7f; ++n)
  55. {
  56. CAPTURE(n);
  57. CHECK(cv1(n) < cv1(n + 1));
  58. }
  59. auto cv2 = lol::audio::sample::convert<uint8_t, float>;
  60. CHECK(cv2(0x00) == -1.0f);
  61. CHECK(cv2(0xff) == 1.0f);
  62. for (int n = 0x00; n < 0xff; ++n)
  63. {
  64. CAPTURE(n);
  65. CHECK(cv2(n) < cv2(n + 1));
  66. }
  67. }
  68. TEST_CASE("sample conversion: float → int16|uint16")
  69. {
  70. auto cv1 = lol::audio::sample::convert<float, int16_t>;
  71. CHECK(cv1(-1.5f) == -0x8000);
  72. CHECK(cv1(-1.0f) == -0x8000);
  73. CHECK(cv1(-0.5f) == -0x4000);
  74. CHECK(cv1( 0.0f) == 0x0000);
  75. CHECK(cv1( 0.5f) == 0x4000);
  76. CHECK(cv1( 1.0f) == 0x7fff);
  77. CHECK(cv1( 1.5f) == 0x7fff);
  78. auto cv2 = lol::audio::sample::convert<float, uint16_t>;
  79. CHECK(cv2(-1.5f) == 0x0000);
  80. CHECK(cv2(-1.0f) == 0x0000);
  81. CHECK(cv2(-0.5f) == 0x4000);
  82. CHECK(cv2( 0.0f) == 0x8000);
  83. CHECK(cv2( 0.5f) == 0xc000);
  84. CHECK(cv2( 1.0f) == 0xffff);
  85. CHECK(cv2( 1.5f) == 0xffff);
  86. }
  87. TEST_CASE("sample conversion: int16|uint16 → float")
  88. {
  89. auto cv1 = lol::audio::sample::convert<int16_t, float>;
  90. CHECK(cv1(-0x8000) == -1.0f);
  91. CHECK(cv1( 0x7fff) == 1.0f);
  92. for (int n = -0x8000; n < 0x7fff; ++n)
  93. {
  94. CAPTURE(n);
  95. CHECK(cv1(n) < cv1(n + 1));
  96. }
  97. auto cv2 = lol::audio::sample::convert<uint16_t, float>;
  98. CHECK(cv2(0x0000) == -1.0f);
  99. CHECK(cv2(0xffff) == 1.0f);
  100. for (int n = 0x0000; n < 0xffff; ++n)
  101. {
  102. CAPTURE(n);
  103. CHECK(cv2(n) < cv2(n + 1));
  104. }
  105. }
  106. TEST_CASE("sample conversion: int8 ←→ uint8")
  107. {
  108. auto cv1 = lol::audio::sample::convert<int8_t, uint8_t>;
  109. for (int n = -0x80; n <= 0x7f; ++n)
  110. {
  111. CAPTURE(n);
  112. CHECK(cv1(n) == n + 0x80);
  113. }
  114. auto cv2 = lol::audio::sample::convert<uint8_t, int8_t>;
  115. for (int n = 0x00; n <= 0xff; ++n)
  116. {
  117. CAPTURE(n);
  118. CHECK(cv2(n) == n - 0x80);
  119. }
  120. }
  121. TEST_CASE("sample conversion: int16 ←→ uint16")
  122. {
  123. auto cv1 = lol::audio::sample::convert<int16_t, uint16_t>;
  124. for (int n = -0x8000; n <= 0x7fff; ++n)
  125. {
  126. CAPTURE(n);
  127. CHECK(cv1(n) == n + 0x8000);
  128. }
  129. auto cv2 = lol::audio::sample::convert<uint16_t, int16_t>;
  130. for (int n = 0x0000; n <= 0xffff; ++n)
  131. {
  132. CAPTURE(n);
  133. CHECK(cv2(n) == n - 0x8000);
  134. }
  135. }
  136. TEST_CASE("sample conversion: int16|uint16 → int8|uint8")
  137. {
  138. auto cv1 = lol::audio::sample::convert<int16_t, int8_t>;
  139. CHECK(cv1(-0x8000) == -0x80);
  140. CHECK(cv1(-0x4000) == -0x40);
  141. CHECK(cv1(-0x0080) == -0x01);
  142. CHECK(cv1(-0x0001) == -0x01);
  143. CHECK(cv1( 0x0000) == 0x00);
  144. CHECK(cv1( 0x00ff) == 0x00);
  145. CHECK(cv1( 0x3fff) == 0x3f);
  146. CHECK(cv1( 0x7fff) == 0x7f);
  147. auto cv2 = lol::audio::sample::convert<uint16_t, int8_t>;
  148. CHECK(cv2(0x0000) == -0x80);
  149. CHECK(cv2(0x4000) == -0x40);
  150. CHECK(cv2(0x7f80) == -0x01);
  151. CHECK(cv2(0x7fff) == -0x01);
  152. CHECK(cv2(0x8000) == 0x00);
  153. CHECK(cv2(0x80ff) == 0x00);
  154. CHECK(cv2(0xbfff) == 0x3f);
  155. CHECK(cv2(0xffff) == 0x7f);
  156. auto cv3 = lol::audio::sample::convert<int16_t, uint8_t>;
  157. CHECK(cv3(-0x8000) == 0x00);
  158. CHECK(cv3(-0x4000) == 0x40);
  159. CHECK(cv3(-0x0080) == 0x7f);
  160. CHECK(cv3(-0x0001) == 0x7f);
  161. CHECK(cv3( 0x0000) == 0x80);
  162. CHECK(cv3( 0x00ff) == 0x80);
  163. CHECK(cv3( 0x3fff) == 0xbf);
  164. CHECK(cv3( 0x7fff) == 0xff);
  165. auto cv4 = lol::audio::sample::convert<uint16_t, uint8_t>;
  166. CHECK(cv4(0x0000) == 0x00);
  167. CHECK(cv4(0x4000) == 0x40);
  168. CHECK(cv4(0x7f80) == 0x7f);
  169. CHECK(cv4(0x7fff) == 0x7f);
  170. CHECK(cv4(0x8000) == 0x80);
  171. CHECK(cv4(0x80ff) == 0x80);
  172. CHECK(cv4(0xbfff) == 0xbf);
  173. CHECK(cv4(0xffff) == 0xff);
  174. }
  175. TEST_CASE("sample conversion: uint8 → int16|uint16")
  176. {
  177. auto cv1 = lol::audio::sample::convert<uint8_t, int16_t>;
  178. for (int n = 0x00; n <= 0xff; ++n)
  179. {
  180. CAPTURE(n);
  181. CHECK(cv1(n) == n * 0x101 - 0x8000);
  182. }
  183. auto cv2 = lol::audio::sample::convert<uint8_t, uint16_t>;
  184. for (int n = 0x00; n <= 0xff; ++n)
  185. {
  186. CAPTURE(n);
  187. CHECK(cv2(n) == n * 0x101);
  188. }
  189. }
  190. TEST_CASE("sample conversion: int8 → float → int8")
  191. {
  192. auto cv1 = lol::audio::sample::convert<int8_t, float>;
  193. auto cv2 = lol::audio::sample::convert<float, int8_t>;
  194. for (int n = -0x80; n <= 0x7f; ++n)
  195. {
  196. CAPTURE(n);
  197. CHECK(cv2(cv1(n)) == n);
  198. }
  199. }
  200. TEST_CASE("sample conversion: uint8 → float → uint8")
  201. {
  202. auto cv1 = lol::audio::sample::convert<uint8_t, float>;
  203. auto cv2 = lol::audio::sample::convert<float, uint8_t>;
  204. for (int n = 0x00; n <= 0xff; ++n)
  205. {
  206. CAPTURE(n);
  207. CHECK(cv2(cv1(n)) == n);
  208. }
  209. }