1
0

gen_audio_channel_conversion.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <stdio.h>
  19. /*
  20. Built with:
  21. gcc -o genchancvt build-scripts/gen_audio_channel_conversion.c -lm && ./genchancvt > src/audio/SDL_audio_channel_converters.h
  22. */
  23. #define NUM_CHANNELS 8
  24. static const char *layout_names[NUM_CHANNELS] = {
  25. "Mono", "Stereo", "2.1", "Quad", "4.1", "5.1", "6.1", "7.1"
  26. };
  27. static const char *channel_names[NUM_CHANNELS][NUM_CHANNELS] = {
  28. /* mono */ { "FC" },
  29. /* stereo */ { "FL", "FR" },
  30. /* 2.1 */ { "FL", "FR", "LFE" },
  31. /* quad */ { "FL", "FR", "BL", "BR" },
  32. /* 4.1 */ { "FL", "FR", "LFE", "BL", "BR" },
  33. /* 5.1 */ { "FL", "FR", "FC", "LFE", "BL", "BR" },
  34. /* 6.1 */ { "FL", "FR", "FC", "LFE", "BC", "SL", "SR" },
  35. /* 7.1 */ { "FL", "FR", "FC", "LFE", "BL", "BR", "SL", "SR" },
  36. };
  37. /*
  38. * This table is from FAudio:
  39. *
  40. * https://raw.githubusercontent.com/FNA-XNA/FAudio/master/src/matrix_defaults.inl
  41. */
  42. static const float channel_conversion_matrix[8][8][64] = {
  43. {
  44. /* 1 x 1 */
  45. { 1.000000000f },
  46. /* 1 x 2 */
  47. { 1.000000000f, 1.000000000f },
  48. /* 1 x 3 */
  49. { 1.000000000f, 1.000000000f, 0.000000000f },
  50. /* 1 x 4 */
  51. { 1.000000000f, 1.000000000f, 0.000000000f, 0.000000000f },
  52. /* 1 x 5 */
  53. { 1.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  54. /* 1 x 6 */
  55. { 1.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  56. /* 1 x 7 */
  57. { 1.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  58. /* 1 x 8 */
  59. { 1.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f }
  60. },
  61. {
  62. /* 2 x 1 */
  63. { 0.500000000f, 0.500000000f },
  64. /* 2 x 2 */
  65. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  66. /* 2 x 3 */
  67. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f },
  68. /* 2 x 4 */
  69. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  70. /* 2 x 5 */
  71. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  72. /* 2 x 6 */
  73. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  74. /* 2 x 7 */
  75. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  76. /* 2 x 8 */
  77. { 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f }
  78. },
  79. {
  80. /* 3 x 1 */
  81. { 0.333333343f, 0.333333343f, 0.333333343f },
  82. /* 3 x 2 */
  83. { 0.800000012f, 0.000000000f, 0.200000003f, 0.000000000f, 0.800000012f, 0.200000003f },
  84. /* 3 x 3 */
  85. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  86. /* 3 x 4 */
  87. { 0.888888896f, 0.000000000f, 0.111111112f, 0.000000000f, 0.888888896f, 0.111111112f, 0.000000000f, 0.000000000f, 0.111111112f, 0.000000000f, 0.000000000f, 0.111111112f },
  88. /* 3 x 5 */
  89. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  90. /* 3 x 6 */
  91. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  92. /* 3 x 7 */
  93. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  94. /* 3 x 8 */
  95. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f }
  96. },
  97. {
  98. /* 4 x 1 */
  99. { 0.250000000f, 0.250000000f, 0.250000000f, 0.250000000f },
  100. /* 4 x 2 */
  101. { 0.421000004f, 0.000000000f, 0.358999997f, 0.219999999f, 0.000000000f, 0.421000004f, 0.219999999f, 0.358999997f },
  102. /* 4 x 3 */
  103. { 0.421000004f, 0.000000000f, 0.358999997f, 0.219999999f, 0.000000000f, 0.421000004f, 0.219999999f, 0.358999997f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  104. /* 4 x 4 */
  105. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  106. /* 4 x 5 */
  107. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  108. /* 4 x 6 */
  109. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  110. /* 4 x 7 */
  111. { 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.500000000f, 0.500000000f, 0.000000000f, 0.000000000f, 0.796000004f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.796000004f },
  112. /* 4 x 8 */
  113. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f }
  114. },
  115. {
  116. /* 5 x 1 */
  117. { 0.200000003f, 0.200000003f, 0.200000003f, 0.200000003f, 0.200000003f },
  118. /* 5 x 2 */
  119. { 0.374222219f, 0.000000000f, 0.111111112f, 0.319111109f, 0.195555553f, 0.000000000f, 0.374222219f, 0.111111112f, 0.195555553f, 0.319111109f },
  120. /* 5 x 3 */
  121. { 0.421000004f, 0.000000000f, 0.000000000f, 0.358999997f, 0.219999999f, 0.000000000f, 0.421000004f, 0.000000000f, 0.219999999f, 0.358999997f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f },
  122. /* 5 x 4 */
  123. { 0.941176474f, 0.000000000f, 0.058823530f, 0.000000000f, 0.000000000f, 0.000000000f, 0.941176474f, 0.058823530f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.058823530f, 0.941176474f, 0.000000000f, 0.000000000f, 0.000000000f, 0.058823530f, 0.000000000f, 0.941176474f },
  124. /* 5 x 5 */
  125. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  126. /* 5 x 6 */
  127. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  128. /* 5 x 7 */
  129. { 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.500000000f, 0.500000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.796000004f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.796000004f },
  130. /* 5 x 8 */
  131. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f }
  132. },
  133. {
  134. /* 6 x 1 */
  135. { 0.166666672f, 0.166666672f, 0.166666672f, 0.166666672f, 0.166666672f, 0.166666672f },
  136. /* 6 x 2 */
  137. { 0.294545442f, 0.000000000f, 0.208181813f, 0.090909094f, 0.251818180f, 0.154545456f, 0.000000000f, 0.294545442f, 0.208181813f, 0.090909094f, 0.154545456f, 0.251818180f },
  138. /* 6 x 3 */
  139. { 0.324000001f, 0.000000000f, 0.229000002f, 0.000000000f, 0.277000010f, 0.170000002f, 0.000000000f, 0.324000001f, 0.229000002f, 0.000000000f, 0.170000002f, 0.277000010f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f },
  140. /* 6 x 4 */
  141. { 0.558095276f, 0.000000000f, 0.394285709f, 0.047619049f, 0.000000000f, 0.000000000f, 0.000000000f, 0.558095276f, 0.394285709f, 0.047619049f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.047619049f, 0.558095276f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.047619049f, 0.000000000f, 0.558095276f },
  142. /* 6 x 5 */
  143. { 0.586000025f, 0.000000000f, 0.414000005f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.586000025f, 0.414000005f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.586000025f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.586000025f },
  144. /* 6 x 6 */
  145. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  146. /* 6 x 7 */
  147. { 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.939999998f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.500000000f, 0.500000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.796000004f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.796000004f },
  148. /* 6 x 8 */
  149. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f }
  150. },
  151. {
  152. /* 7 x 1 */
  153. { 0.143142849f, 0.143142849f, 0.143142849f, 0.142857149f, 0.143142849f, 0.143142849f, 0.143142849f },
  154. /* 7 x 2 */
  155. { 0.247384623f, 0.000000000f, 0.174461529f, 0.076923080f, 0.174461529f, 0.226153851f, 0.100615382f, 0.000000000f, 0.247384623f, 0.174461529f, 0.076923080f, 0.174461529f, 0.100615382f, 0.226153851f },
  156. /* 7 x 3 */
  157. { 0.268000007f, 0.000000000f, 0.188999996f, 0.000000000f, 0.188999996f, 0.245000005f, 0.108999997f, 0.000000000f, 0.268000007f, 0.188999996f, 0.000000000f, 0.188999996f, 0.108999997f, 0.245000005f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  158. /* 7 x 4 */
  159. { 0.463679999f, 0.000000000f, 0.327360004f, 0.040000003f, 0.000000000f, 0.168960005f, 0.000000000f, 0.000000000f, 0.463679999f, 0.327360004f, 0.040000003f, 0.000000000f, 0.000000000f, 0.168960005f, 0.000000000f, 0.000000000f, 0.000000000f, 0.040000003f, 0.327360004f, 0.431039989f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.040000003f, 0.327360004f, 0.000000000f, 0.431039989f },
  160. /* 7 x 5 */
  161. { 0.483000010f, 0.000000000f, 0.340999991f, 0.000000000f, 0.000000000f, 0.175999999f, 0.000000000f, 0.000000000f, 0.483000010f, 0.340999991f, 0.000000000f, 0.000000000f, 0.000000000f, 0.175999999f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.340999991f, 0.449000001f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.340999991f, 0.000000000f, 0.449000001f },
  162. /* 7 x 6 */
  163. { 0.611000001f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.223000005f, 0.000000000f, 0.000000000f, 0.611000001f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.223000005f, 0.000000000f, 0.000000000f, 0.611000001f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.432000011f, 0.568000019f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.432000011f, 0.000000000f, 0.568000019f },
  164. /* 7 x 7 */
  165. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f },
  166. /* 7 x 8 */
  167. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.707000017f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.707000017f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f }
  168. },
  169. {
  170. /* 8 x 1 */
  171. { 0.125125006f, 0.125125006f, 0.125125006f, 0.125000000f, 0.125125006f, 0.125125006f, 0.125125006f, 0.125125006f },
  172. /* 8 x 2 */
  173. { 0.211866662f, 0.000000000f, 0.150266662f, 0.066666670f, 0.181066677f, 0.111066669f, 0.194133341f, 0.085866667f, 0.000000000f, 0.211866662f, 0.150266662f, 0.066666670f, 0.111066669f, 0.181066677f, 0.085866667f, 0.194133341f },
  174. /* 8 x 3 */
  175. { 0.226999998f, 0.000000000f, 0.160999998f, 0.000000000f, 0.194000006f, 0.119000003f, 0.208000004f, 0.092000000f, 0.000000000f, 0.226999998f, 0.160999998f, 0.000000000f, 0.119000003f, 0.194000006f, 0.092000000f, 0.208000004f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f },
  176. /* 8 x 4 */
  177. { 0.466344833f, 0.000000000f, 0.329241365f, 0.034482758f, 0.000000000f, 0.000000000f, 0.169931039f, 0.000000000f, 0.000000000f, 0.466344833f, 0.329241365f, 0.034482758f, 0.000000000f, 0.000000000f, 0.000000000f, 0.169931039f, 0.000000000f, 0.000000000f, 0.000000000f, 0.034482758f, 0.466344833f, 0.000000000f, 0.433517247f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.034482758f, 0.000000000f, 0.466344833f, 0.000000000f, 0.433517247f },
  178. /* 8 x 5 */
  179. { 0.483000010f, 0.000000000f, 0.340999991f, 0.000000000f, 0.000000000f, 0.000000000f, 0.175999999f, 0.000000000f, 0.000000000f, 0.483000010f, 0.340999991f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.175999999f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.483000010f, 0.000000000f, 0.449000001f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.483000010f, 0.000000000f, 0.449000001f },
  180. /* 8 x 6 */
  181. { 0.518000007f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.188999996f, 0.000000000f, 0.000000000f, 0.518000007f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.188999996f, 0.000000000f, 0.000000000f, 0.518000007f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.518000007f, 0.000000000f, 0.481999993f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.518000007f, 0.000000000f, 0.481999993f },
  182. /* 8 x 7 */
  183. { 0.541000009f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.541000009f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.541000009f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.287999988f, 0.287999988f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.458999991f, 0.000000000f, 0.541000009f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.458999991f, 0.000000000f, 0.541000009f },
  184. /* 8 x 8 */
  185. { 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f }
  186. }
  187. };
  188. static char *remove_dots(const char *str) /* this is NOT robust. */
  189. {
  190. static char retval1[32];
  191. static char retval2[32];
  192. static int idx = 0;
  193. char *retval = (idx++ & 1) ? retval1 : retval2;
  194. char *ptr = retval;
  195. while (*str) {
  196. if (*str != '.') {
  197. *(ptr++) = *str;
  198. }
  199. str++;
  200. }
  201. *ptr = '\0';
  202. return retval;
  203. }
  204. static char *lowercase(const char *str) /* this is NOT robust. */
  205. {
  206. static char retval1[32];
  207. static char retval2[32];
  208. static int idx = 0;
  209. char *retval = (idx++ & 1) ? retval1 : retval2;
  210. char *ptr = retval;
  211. while (*str) {
  212. const char ch = *(str++);
  213. *(ptr++) = ((ch >= 'A') && (ch <= 'Z')) ? (ch - ('A' - 'a')) : ch;
  214. }
  215. *ptr = '\0';
  216. return retval;
  217. }
  218. static void write_converter(const int fromchans, const int tochans)
  219. {
  220. const char *fromstr = layout_names[fromchans-1];
  221. const char *tostr = layout_names[tochans-1];
  222. const float *cvtmatrix = channel_conversion_matrix[fromchans-1][tochans-1];
  223. const float *fptr;
  224. const int convert_backwards = (tochans > fromchans);
  225. int input_channel_used[NUM_CHANNELS];
  226. int i, j;
  227. if (tochans == fromchans) {
  228. return; /* nothing to convert, don't generate a converter. */
  229. }
  230. for (i = 0; i < fromchans; i++) {
  231. input_channel_used[i] = 0;
  232. }
  233. fptr = cvtmatrix;
  234. for (j = 0; j < tochans; j++) {
  235. for (i = 0; i < fromchans; i++) {
  236. #if 0
  237. printf("to=%d, from=%d, coeff=%f\n", j, i, *fptr);
  238. #endif
  239. if (*(fptr++) != 0.0f) {
  240. input_channel_used[i]++;
  241. }
  242. }
  243. }
  244. printf("static void SDL_Convert%sTo%s(float *dst, const float *src, int num_frames)\n{\n", remove_dots(fromstr), remove_dots(tostr));
  245. printf(" int i;\n"
  246. "\n"
  247. " LOG_DEBUG_AUDIO_CONVERT(\"%s\", \"%s\");\n"
  248. "\n", lowercase(fromstr), lowercase(tostr));
  249. if (convert_backwards) { /* must convert backwards when growing the output in-place. */
  250. printf(" // convert backwards, since output is growing in-place.\n");
  251. printf(" src += (num_frames-1)");
  252. if (fromchans != 1) {
  253. printf(" * %d", fromchans);
  254. }
  255. printf(";\n");
  256. printf(" dst += (num_frames-1)");
  257. if (tochans != 1) {
  258. printf(" * %d", tochans);
  259. }
  260. printf(";\n");
  261. printf(" for (i = num_frames; i; i--, ");
  262. if (fromchans == 1) {
  263. printf("src--");
  264. } else {
  265. printf("src -= %d", fromchans);
  266. }
  267. printf(", ");
  268. if (tochans == 1) {
  269. printf("dst--");
  270. } else {
  271. printf("dst -= %d", tochans);
  272. }
  273. printf(") {\n");
  274. fptr = cvtmatrix;
  275. for (i = 0; i < fromchans; i++) {
  276. if (input_channel_used[i] > 1) { /* don't read it from src more than once. */
  277. printf(" const float src%s = src[%d];\n", channel_names[fromchans-1][i], i);
  278. }
  279. }
  280. for (j = tochans - 1; j >= 0; j--) {
  281. int has_input = 0;
  282. fptr = cvtmatrix + (fromchans * j);
  283. printf(" dst[%d] /* %s */ =", j, channel_names[tochans-1][j]);
  284. for (i = fromchans - 1; i >= 0; i--) {
  285. const float coefficient = fptr[i];
  286. char srcname[32];
  287. if (coefficient == 0.0f) {
  288. continue;
  289. } else if (input_channel_used[i] > 1) {
  290. snprintf(srcname, sizeof (srcname), "src%s", channel_names[fromchans-1][i]);
  291. } else {
  292. snprintf(srcname, sizeof (srcname), "src[%d]", i);
  293. }
  294. if (has_input) {
  295. printf(" +");
  296. }
  297. has_input = 1;
  298. if (coefficient == 1.0f) {
  299. printf(" %s", srcname);
  300. } else {
  301. printf(" (%s * %.9ff)", srcname, coefficient);
  302. }
  303. }
  304. if (!has_input) {
  305. printf(" 0.0f");
  306. }
  307. printf(";\n");
  308. }
  309. printf(" }\n");
  310. } else {
  311. printf(" for (i = num_frames; i; i--, ");
  312. if (fromchans == 1) {
  313. printf("src++");
  314. } else {
  315. printf("src += %d", fromchans);
  316. }
  317. printf(", ");
  318. if (tochans == 1) {
  319. printf("dst++");
  320. } else {
  321. printf("dst += %d", tochans);
  322. }
  323. printf(") {\n");
  324. fptr = cvtmatrix;
  325. for (i = 0; i < fromchans; i++) {
  326. if (input_channel_used[i] > 1) { /* don't read it from src more than once. */
  327. printf(" const float src%s = src[%d];\n", channel_names[fromchans-1][i], i);
  328. }
  329. }
  330. for (j = 0; j < tochans; j++) {
  331. int has_input = 0;
  332. fptr = cvtmatrix + (fromchans * j);
  333. printf(" dst[%d] /* %s */ =", j, channel_names[tochans-1][j]);
  334. for (i = 0; i < fromchans; i++) {
  335. const float coefficient = fptr[i];
  336. char srcname[32];
  337. if (coefficient == 0.0f) {
  338. continue;
  339. } else if (input_channel_used[i] > 1) {
  340. snprintf(srcname, sizeof (srcname), "src%s", channel_names[fromchans-1][i]);
  341. } else {
  342. snprintf(srcname, sizeof (srcname), "src[%d]", i);
  343. }
  344. if (has_input) {
  345. printf(" +");
  346. }
  347. has_input = 1;
  348. if (coefficient == 1.0f) {
  349. printf(" %s", srcname);
  350. } else {
  351. printf(" (%s * %.9ff)", srcname, coefficient);
  352. }
  353. }
  354. if (!has_input) {
  355. printf(" 0.0f");
  356. }
  357. printf(";\n");
  358. }
  359. printf(" }\n");
  360. }
  361. printf("\n}\n\n");
  362. }
  363. int main(void)
  364. {
  365. int ini, outi;
  366. printf(
  367. "/*\n"
  368. " Simple DirectMedia Layer\n"
  369. " Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>\n"
  370. "\n"
  371. " This software is provided 'as-is', without any express or implied\n"
  372. " warranty. In no event will the authors be held liable for any damages\n"
  373. " arising from the use of this software.\n"
  374. "\n"
  375. " Permission is granted to anyone to use this software for any purpose,\n"
  376. " including commercial applications, and to alter it and redistribute it\n"
  377. " freely, subject to the following restrictions:\n"
  378. "\n"
  379. " 1. The origin of this software must not be misrepresented; you must not\n"
  380. " claim that you wrote the original software. If you use this software\n"
  381. " in a product, an acknowledgment in the product documentation would be\n"
  382. " appreciated but is not required.\n"
  383. " 2. Altered source versions must be plainly marked as such, and must not be\n"
  384. " misrepresented as being the original software.\n"
  385. " 3. This notice may not be removed or altered from any source distribution.\n"
  386. "*/\n"
  387. "\n"
  388. "// DO NOT EDIT, THIS FILE WAS GENERATED BY build-scripts/gen_audio_channel_conversion.c\n"
  389. "\n"
  390. "\n"
  391. "typedef void (*SDL_AudioChannelConverter)(float *dst, const float *src, int num_frames);\n"
  392. "\n"
  393. );
  394. for (ini = 1; ini <= NUM_CHANNELS; ini++) {
  395. for (outi = 1; outi <= NUM_CHANNELS; outi++) {
  396. write_converter(ini, outi);
  397. }
  398. }
  399. printf("static const SDL_AudioChannelConverter channel_converters[%d][%d] = { /* [from][to] */\n", NUM_CHANNELS, NUM_CHANNELS);
  400. for (ini = 1; ini <= NUM_CHANNELS; ini++) {
  401. const char *comma = "";
  402. printf(" {");
  403. for (outi = 1; outi <= NUM_CHANNELS; outi++) {
  404. const char *fromstr = layout_names[ini-1];
  405. const char *tostr = layout_names[outi-1];
  406. if (ini == outi) {
  407. printf("%s NULL", comma);
  408. } else {
  409. printf("%s SDL_Convert%sTo%s", comma, remove_dots(fromstr), remove_dots(tostr));
  410. }
  411. comma = ",";
  412. }
  413. printf(" }%s\n", (ini == NUM_CHANNELS) ? "" : ",");
  414. }
  415. printf("};\n\n");
  416. return 0;
  417. }