Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_AudioChannelSet.cpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. AudioChannelSet::AudioChannelSet (uint32 c) : channels (static_cast<int64> (c))
  20. {
  21. }
  22. AudioChannelSet::AudioChannelSet (const std::initializer_list<ChannelType>& c)
  23. {
  24. for (auto channel : c)
  25. addChannel (channel);
  26. }
  27. bool AudioChannelSet::operator== (const AudioChannelSet& other) const noexcept { return channels == other.channels; }
  28. bool AudioChannelSet::operator!= (const AudioChannelSet& other) const noexcept { return channels != other.channels; }
  29. bool AudioChannelSet::operator< (const AudioChannelSet& other) const noexcept { return channels < other.channels; }
  30. String AudioChannelSet::getChannelTypeName (AudioChannelSet::ChannelType type)
  31. {
  32. if (type >= discreteChannel0)
  33. return "Discrete " + String (type - discreteChannel0 + 1);
  34. switch (type)
  35. {
  36. case left: return NEEDS_TRANS("Left");
  37. case right: return NEEDS_TRANS("Right");
  38. case centre: return NEEDS_TRANS("Centre");
  39. case LFE: return NEEDS_TRANS("LFE");
  40. case leftSurround: return NEEDS_TRANS("Left Surround");
  41. case rightSurround: return NEEDS_TRANS("Right Surround");
  42. case leftCentre: return NEEDS_TRANS("Left Centre");
  43. case rightCentre: return NEEDS_TRANS("Right Centre");
  44. case centreSurround: return NEEDS_TRANS("Centre Surround");
  45. case leftSurroundRear: return NEEDS_TRANS("Left Surround Rear");
  46. case rightSurroundRear: return NEEDS_TRANS("Right Surround Rear");
  47. case topMiddle: return NEEDS_TRANS("Top Middle");
  48. case topFrontLeft: return NEEDS_TRANS("Top Front Left");
  49. case topFrontCentre: return NEEDS_TRANS("Top Front Centre");
  50. case topFrontRight: return NEEDS_TRANS("Top Front Right");
  51. case topRearLeft: return NEEDS_TRANS("Top Rear Left");
  52. case topRearCentre: return NEEDS_TRANS("Top Rear Centre");
  53. case topRearRight: return NEEDS_TRANS("Top Rear Right");
  54. case wideLeft: return NEEDS_TRANS("Wide Left");
  55. case wideRight: return NEEDS_TRANS("Wide Right");
  56. case LFE2: return NEEDS_TRANS("LFE 2");
  57. case leftSurroundSide: return NEEDS_TRANS("Left Surround Side");
  58. case rightSurroundSide: return NEEDS_TRANS("Right Surround Side");
  59. case ambisonicW: return NEEDS_TRANS("Ambisonic W");
  60. case ambisonicX: return NEEDS_TRANS("Ambisonic X");
  61. case ambisonicY: return NEEDS_TRANS("Ambisonic Y");
  62. case ambisonicZ: return NEEDS_TRANS("Ambisonic Z");
  63. case topSideLeft: return NEEDS_TRANS("Top Side Left");
  64. case topSideRight: return NEEDS_TRANS("Top Side Right");
  65. case ambisonicACN4: return NEEDS_TRANS("Ambisonic 4");
  66. case ambisonicACN5: return NEEDS_TRANS("Ambisonic 5");
  67. case ambisonicACN6: return NEEDS_TRANS("Ambisonic 6");
  68. case ambisonicACN7: return NEEDS_TRANS("Ambisonic 7");
  69. case ambisonicACN8: return NEEDS_TRANS("Ambisonic 8");
  70. case ambisonicACN9: return NEEDS_TRANS("Ambisonic 9");
  71. case ambisonicACN10: return NEEDS_TRANS("Ambisonic 10");
  72. case ambisonicACN11: return NEEDS_TRANS("Ambisonic 11");
  73. case ambisonicACN12: return NEEDS_TRANS("Ambisonic 12");
  74. case ambisonicACN13: return NEEDS_TRANS("Ambisonic 13");
  75. case ambisonicACN14: return NEEDS_TRANS("Ambisonic 14");
  76. case ambisonicACN15: return NEEDS_TRANS("Ambisonic 15");
  77. case ambisonicACN16: return NEEDS_TRANS("Ambisonic 16");
  78. case ambisonicACN17: return NEEDS_TRANS("Ambisonic 17");
  79. case ambisonicACN18: return NEEDS_TRANS("Ambisonic 18");
  80. case ambisonicACN19: return NEEDS_TRANS("Ambisonic 19");
  81. case ambisonicACN20: return NEEDS_TRANS("Ambisonic 20");
  82. case ambisonicACN21: return NEEDS_TRANS("Ambisonic 21");
  83. case ambisonicACN22: return NEEDS_TRANS("Ambisonic 22");
  84. case ambisonicACN23: return NEEDS_TRANS("Ambisonic 23");
  85. case ambisonicACN24: return NEEDS_TRANS("Ambisonic 24");
  86. case ambisonicACN25: return NEEDS_TRANS("Ambisonic 25");
  87. case ambisonicACN26: return NEEDS_TRANS("Ambisonic 26");
  88. case ambisonicACN27: return NEEDS_TRANS("Ambisonic 27");
  89. case ambisonicACN28: return NEEDS_TRANS("Ambisonic 28");
  90. case ambisonicACN29: return NEEDS_TRANS("Ambisonic 29");
  91. case ambisonicACN30: return NEEDS_TRANS("Ambisonic 30");
  92. case ambisonicACN31: return NEEDS_TRANS("Ambisonic 31");
  93. case ambisonicACN32: return NEEDS_TRANS("Ambisonic 32");
  94. case ambisonicACN33: return NEEDS_TRANS("Ambisonic 33");
  95. case ambisonicACN34: return NEEDS_TRANS("Ambisonic 34");
  96. case ambisonicACN35: return NEEDS_TRANS("Ambisonic 35");
  97. case bottomFrontLeft: return NEEDS_TRANS("Bottom Front Left");
  98. case bottomFrontCentre: return NEEDS_TRANS("Bottom Front Centre");
  99. case bottomFrontRight: return NEEDS_TRANS("Bottom Front Right");
  100. case proximityLeft: return NEEDS_TRANS("Proximity Left");
  101. case proximityRight: return NEEDS_TRANS("Proximity Right");
  102. case bottomSideLeft: return NEEDS_TRANS("Bottom Side Left");
  103. case bottomSideRight: return NEEDS_TRANS("Bottom Side Right");
  104. case bottomRearLeft: return NEEDS_TRANS("Bottom Rear Left");
  105. case bottomRearCentre: return NEEDS_TRANS("Bottom Rear Centre");
  106. case bottomRearRight: return NEEDS_TRANS("Bottom Rear Right");
  107. case discreteChannel0:
  108. case unknown:
  109. default: break;
  110. }
  111. return "Unknown";
  112. }
  113. String AudioChannelSet::getAbbreviatedChannelTypeName (AudioChannelSet::ChannelType type)
  114. {
  115. if (type >= discreteChannel0)
  116. return String (type - discreteChannel0 + 1);
  117. switch (type)
  118. {
  119. case left: return "L";
  120. case right: return "R";
  121. case centre: return "C";
  122. case LFE: return "Lfe";
  123. case leftSurround: return "Ls";
  124. case rightSurround: return "Rs";
  125. case leftCentre: return "Lc";
  126. case rightCentre: return "Rc";
  127. case centreSurround: return "Cs";
  128. case leftSurroundRear: return "Lrs";
  129. case rightSurroundRear: return "Rrs";
  130. case topMiddle: return "Tm";
  131. case topFrontLeft: return "Tfl";
  132. case topFrontCentre: return "Tfc";
  133. case topFrontRight: return "Tfr";
  134. case topRearLeft: return "Trl";
  135. case topRearCentre: return "Trc";
  136. case topRearRight: return "Trr";
  137. case wideLeft: return "Wl";
  138. case wideRight: return "Wr";
  139. case LFE2: return "Lfe2";
  140. case leftSurroundSide: return "Lss";
  141. case rightSurroundSide: return "Rss";
  142. case ambisonicACN0: return "ACN0";
  143. case ambisonicACN1: return "ACN1";
  144. case ambisonicACN2: return "ACN2";
  145. case ambisonicACN3: return "ACN3";
  146. case ambisonicACN4: return "ACN4";
  147. case ambisonicACN5: return "ACN5";
  148. case ambisonicACN6: return "ACN6";
  149. case ambisonicACN7: return "ACN7";
  150. case ambisonicACN8: return "ACN8";
  151. case ambisonicACN9: return "ACN9";
  152. case ambisonicACN10: return "ACN10";
  153. case ambisonicACN11: return "ACN11";
  154. case ambisonicACN12: return "ACN12";
  155. case ambisonicACN13: return "ACN13";
  156. case ambisonicACN14: return "ACN14";
  157. case ambisonicACN15: return "ACN15";
  158. case ambisonicACN16: return "ACN16";
  159. case ambisonicACN17: return "ACN17";
  160. case ambisonicACN18: return "ACN18";
  161. case ambisonicACN19: return "ACN19";
  162. case ambisonicACN20: return "ACN20";
  163. case ambisonicACN21: return "ACN21";
  164. case ambisonicACN22: return "ACN22";
  165. case ambisonicACN23: return "ACN23";
  166. case ambisonicACN24: return "ACN24";
  167. case ambisonicACN25: return "ACN25";
  168. case ambisonicACN26: return "ACN26";
  169. case ambisonicACN27: return "ACN27";
  170. case ambisonicACN28: return "ACN28";
  171. case ambisonicACN29: return "ACN29";
  172. case ambisonicACN30: return "ACN30";
  173. case ambisonicACN31: return "ACN31";
  174. case ambisonicACN32: return "ACN32";
  175. case ambisonicACN33: return "ACN33";
  176. case ambisonicACN34: return "ACN34";
  177. case ambisonicACN35: return "ACN35";
  178. case topSideLeft: return "Tsl";
  179. case topSideRight: return "Tsr";
  180. case bottomFrontLeft: return "Bfl";
  181. case bottomFrontCentre: return "Bfc";
  182. case bottomFrontRight: return "Bfr";
  183. case proximityLeft: return "Pl";
  184. case proximityRight: return "Pr";
  185. case bottomSideLeft: return "Bsl";
  186. case bottomSideRight: return "Bsr";
  187. case bottomRearLeft: return "Brl";
  188. case bottomRearCentre: return "Brc";
  189. case bottomRearRight: return "Brr";
  190. case discreteChannel0:
  191. case unknown:
  192. default: break;
  193. }
  194. if (type >= ambisonicACN4 && type <= ambisonicACN35)
  195. return "ACN" + String (type - ambisonicACN4 + 4);
  196. return {};
  197. }
  198. AudioChannelSet::ChannelType AudioChannelSet::getChannelTypeFromAbbreviation (const String& abbr)
  199. {
  200. if (abbr.length() > 0 && (abbr[0] >= '0' && abbr[0] <= '9'))
  201. return static_cast<AudioChannelSet::ChannelType> (static_cast<int> (discreteChannel0)
  202. + abbr.getIntValue() - 1);
  203. if (abbr == "L") return left;
  204. if (abbr == "R") return right;
  205. if (abbr == "C") return centre;
  206. if (abbr == "Lfe") return LFE;
  207. if (abbr == "Ls") return leftSurround;
  208. if (abbr == "Rs") return rightSurround;
  209. if (abbr == "Lc") return leftCentre;
  210. if (abbr == "Rc") return rightCentre;
  211. if (abbr == "Cs") return centreSurround;
  212. if (abbr == "Lrs") return leftSurroundRear;
  213. if (abbr == "Rrs") return rightSurroundRear;
  214. if (abbr == "Tm") return topMiddle;
  215. if (abbr == "Tfl") return topFrontLeft;
  216. if (abbr == "Tfc") return topFrontCentre;
  217. if (abbr == "Tfr") return topFrontRight;
  218. if (abbr == "Trl") return topRearLeft;
  219. if (abbr == "Trc") return topRearCentre;
  220. if (abbr == "Trr") return topRearRight;
  221. if (abbr == "Wl") return wideLeft;
  222. if (abbr == "Wr") return wideRight;
  223. if (abbr == "Lfe2") return LFE2;
  224. if (abbr == "Lss") return leftSurroundSide;
  225. if (abbr == "Rss") return rightSurroundSide;
  226. if (abbr == "W") return ambisonicW;
  227. if (abbr == "X") return ambisonicX;
  228. if (abbr == "Y") return ambisonicY;
  229. if (abbr == "Z") return ambisonicZ;
  230. if (abbr == "ACN0") return ambisonicACN0;
  231. if (abbr == "ACN1") return ambisonicACN1;
  232. if (abbr == "ACN2") return ambisonicACN2;
  233. if (abbr == "ACN3") return ambisonicACN3;
  234. if (abbr == "ACN4") return ambisonicACN4;
  235. if (abbr == "ACN5") return ambisonicACN5;
  236. if (abbr == "ACN6") return ambisonicACN6;
  237. if (abbr == "ACN7") return ambisonicACN7;
  238. if (abbr == "ACN8") return ambisonicACN8;
  239. if (abbr == "ACN9") return ambisonicACN9;
  240. if (abbr == "ACN10") return ambisonicACN10;
  241. if (abbr == "ACN11") return ambisonicACN11;
  242. if (abbr == "ACN12") return ambisonicACN12;
  243. if (abbr == "ACN13") return ambisonicACN13;
  244. if (abbr == "ACN14") return ambisonicACN14;
  245. if (abbr == "ACN15") return ambisonicACN15;
  246. if (abbr == "ACN16") return ambisonicACN16;
  247. if (abbr == "ACN17") return ambisonicACN17;
  248. if (abbr == "ACN18") return ambisonicACN18;
  249. if (abbr == "ACN19") return ambisonicACN19;
  250. if (abbr == "ACN20") return ambisonicACN20;
  251. if (abbr == "ACN21") return ambisonicACN21;
  252. if (abbr == "ACN22") return ambisonicACN22;
  253. if (abbr == "ACN23") return ambisonicACN23;
  254. if (abbr == "ACN24") return ambisonicACN24;
  255. if (abbr == "ACN25") return ambisonicACN25;
  256. if (abbr == "ACN26") return ambisonicACN26;
  257. if (abbr == "ACN27") return ambisonicACN27;
  258. if (abbr == "ACN28") return ambisonicACN28;
  259. if (abbr == "ACN29") return ambisonicACN29;
  260. if (abbr == "ACN30") return ambisonicACN30;
  261. if (abbr == "ACN31") return ambisonicACN31;
  262. if (abbr == "ACN32") return ambisonicACN32;
  263. if (abbr == "ACN33") return ambisonicACN33;
  264. if (abbr == "ACN34") return ambisonicACN34;
  265. if (abbr == "ACN35") return ambisonicACN35;
  266. if (abbr == "Tsl") return topSideLeft;
  267. if (abbr == "Tsr") return topSideRight;
  268. if (abbr == "Bfl") return bottomFrontLeft;
  269. if (abbr == "Bfc") return bottomFrontCentre;
  270. if (abbr == "Bfr") return bottomFrontRight;
  271. if (abbr == "Bsl") return bottomSideLeft;
  272. if (abbr == "Bsr") return bottomSideRight;
  273. if (abbr == "Brl") return bottomRearLeft;
  274. if (abbr == "Brc") return bottomRearCentre;
  275. if (abbr == "Brr") return bottomRearRight;
  276. return unknown;
  277. }
  278. String AudioChannelSet::getSpeakerArrangementAsString() const
  279. {
  280. StringArray speakerTypes;
  281. for (auto& speaker : getChannelTypes())
  282. {
  283. auto name = getAbbreviatedChannelTypeName (speaker);
  284. if (name.isNotEmpty())
  285. speakerTypes.add (name);
  286. }
  287. return speakerTypes.joinIntoString (" ");
  288. }
  289. AudioChannelSet AudioChannelSet::fromAbbreviatedString (const String& str)
  290. {
  291. AudioChannelSet set;
  292. for (auto& abbr : StringArray::fromTokens (str, true))
  293. {
  294. auto type = getChannelTypeFromAbbreviation (abbr);
  295. if (type != unknown)
  296. set.addChannel (type);
  297. }
  298. return set;
  299. }
  300. String AudioChannelSet::getDescription() const
  301. {
  302. if (isDiscreteLayout()) return "Discrete #" + String (size());
  303. if (*this == disabled()) return "Disabled";
  304. if (*this == mono()) return "Mono";
  305. if (*this == stereo()) return "Stereo";
  306. if (*this == createLCR()) return "LCR";
  307. if (*this == createLRS()) return "LRS";
  308. if (*this == createLCRS()) return "LCRS";
  309. if (*this == create5point0()) return "5.0 Surround";
  310. if (*this == create5point1()) return "5.1 Surround";
  311. if (*this == create5point1point2()) return "5.1.2 Surround";
  312. if (*this == create5point1point4()) return "5.1.4 Surround";
  313. if (*this == create6point0()) return "6.0 Surround";
  314. if (*this == create6point1()) return "6.1 Surround";
  315. if (*this == create6point0Music()) return "6.0 (Music) Surround";
  316. if (*this == create6point1Music()) return "6.1 (Music) Surround";
  317. if (*this == create7point0()) return "7.0 Surround";
  318. if (*this == create7point1()) return "7.1 Surround";
  319. if (*this == create7point0SDDS()) return "7.0 Surround SDDS";
  320. if (*this == create7point1SDDS()) return "7.1 Surround SDDS";
  321. if (*this == create7point0point2()) return "7.0.2 Surround";
  322. if (*this == create7point0point4()) return "7.0.4 Surround";
  323. if (*this == create7point1point2()) return "7.1.2 Surround";
  324. if (*this == create7point1point4()) return "7.1.4 Surround";
  325. if (*this == create7point1point6()) return "7.1.6 Surround";
  326. if (*this == create9point1point6()) return "9.1.6 Surround";
  327. if (*this == quadraphonic()) return "Quadraphonic";
  328. if (*this == pentagonal()) return "Pentagonal";
  329. if (*this == hexagonal()) return "Hexagonal";
  330. if (*this == octagonal()) return "Octagonal";
  331. // ambisonics
  332. {
  333. auto order = getAmbisonicOrder();
  334. if (order >= 0)
  335. {
  336. String suffix;
  337. switch (order)
  338. {
  339. case 1: suffix = "st"; break;
  340. case 2: suffix = "nd"; break;
  341. case 3: suffix = "rd"; break;
  342. default: suffix = "th"; break;
  343. }
  344. return String (order) + suffix + " Order Ambisonics";
  345. }
  346. }
  347. return "Unknown";
  348. }
  349. bool AudioChannelSet::isDiscreteLayout() const noexcept
  350. {
  351. for (auto& speaker : getChannelTypes())
  352. if (speaker <= ambisonicACN35)
  353. return false;
  354. return true;
  355. }
  356. int AudioChannelSet::size() const noexcept
  357. {
  358. return channels.countNumberOfSetBits();
  359. }
  360. AudioChannelSet::ChannelType AudioChannelSet::getTypeOfChannel (int index) const noexcept
  361. {
  362. int bit = channels.findNextSetBit(0);
  363. for (int i = 0; i < index && bit >= 0; ++i)
  364. bit = channels.findNextSetBit (bit + 1);
  365. return static_cast<ChannelType> (bit);
  366. }
  367. int AudioChannelSet::getChannelIndexForType (AudioChannelSet::ChannelType type) const noexcept
  368. {
  369. int idx = 0;
  370. for (int bit = channels.findNextSetBit (0); bit >= 0; bit = channels.findNextSetBit (bit + 1))
  371. {
  372. if (static_cast<ChannelType> (bit) == type)
  373. return idx;
  374. idx++;
  375. }
  376. return -1;
  377. }
  378. Array<AudioChannelSet::ChannelType> AudioChannelSet::getChannelTypes() const
  379. {
  380. Array<ChannelType> result;
  381. for (int bit = channels.findNextSetBit(0); bit >= 0; bit = channels.findNextSetBit (bit + 1))
  382. result.add (static_cast<ChannelType> (bit));
  383. return result;
  384. }
  385. void AudioChannelSet::addChannel (ChannelType newChannel)
  386. {
  387. const int bit = static_cast<int> (newChannel);
  388. jassert (bit >= 0 && bit < 1024);
  389. channels.setBit (bit);
  390. }
  391. void AudioChannelSet::removeChannel (ChannelType newChannel)
  392. {
  393. const int bit = static_cast<int> (newChannel);
  394. jassert (bit >= 0 && bit < 1024);
  395. channels.clearBit (bit);
  396. }
  397. AudioChannelSet AudioChannelSet::disabled() { return {}; }
  398. AudioChannelSet AudioChannelSet::mono() { return AudioChannelSet ({ centre }); }
  399. AudioChannelSet AudioChannelSet::stereo() { return AudioChannelSet ({ left, right }); }
  400. AudioChannelSet AudioChannelSet::createLCR() { return AudioChannelSet ({ left, right, centre }); }
  401. AudioChannelSet AudioChannelSet::createLRS() { return AudioChannelSet ({ left, right, surround }); }
  402. AudioChannelSet AudioChannelSet::createLCRS() { return AudioChannelSet ({ left, right, centre, surround }); }
  403. AudioChannelSet AudioChannelSet::create5point0() { return AudioChannelSet ({ left, right, centre, leftSurround, rightSurround }); }
  404. AudioChannelSet AudioChannelSet::create5point1() { return AudioChannelSet ({ left, right, centre, LFE, leftSurround, rightSurround }); }
  405. AudioChannelSet AudioChannelSet::create6point0() { return AudioChannelSet ({ left, right, centre, leftSurround, rightSurround, centreSurround }); }
  406. AudioChannelSet AudioChannelSet::create6point1() { return AudioChannelSet ({ left, right, centre, LFE, leftSurround, rightSurround, centreSurround }); }
  407. AudioChannelSet AudioChannelSet::create6point0Music() { return AudioChannelSet ({ left, right, leftSurround, rightSurround, leftSurroundSide, rightSurroundSide }); }
  408. AudioChannelSet AudioChannelSet::create6point1Music() { return AudioChannelSet ({ left, right, LFE, leftSurround, rightSurround, leftSurroundSide, rightSurroundSide }); }
  409. AudioChannelSet AudioChannelSet::create7point0() { return AudioChannelSet ({ left, right, centre, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear }); }
  410. AudioChannelSet AudioChannelSet::create7point0SDDS() { return AudioChannelSet ({ left, right, centre, leftSurround, rightSurround, leftCentre, rightCentre }); }
  411. AudioChannelSet AudioChannelSet::create7point1() { return AudioChannelSet ({ left, right, centre, LFE, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear }); }
  412. AudioChannelSet AudioChannelSet::create7point1SDDS() { return AudioChannelSet ({ left, right, centre, LFE, leftSurround, rightSurround, leftCentre, rightCentre }); }
  413. AudioChannelSet AudioChannelSet::quadraphonic() { return AudioChannelSet ({ left, right, leftSurround, rightSurround }); }
  414. AudioChannelSet AudioChannelSet::pentagonal() { return AudioChannelSet ({ left, right, centre, leftSurroundRear, rightSurroundRear }); }
  415. AudioChannelSet AudioChannelSet::hexagonal() { return AudioChannelSet ({ left, right, centre, centreSurround, leftSurroundRear, rightSurroundRear }); }
  416. AudioChannelSet AudioChannelSet::octagonal() { return AudioChannelSet ({ left, right, centre, leftSurround, rightSurround, centreSurround, wideLeft, wideRight }); }
  417. AudioChannelSet AudioChannelSet::create5point1point2() { return AudioChannelSet ({ left, right, centre, LFE, leftSurround, rightSurround, topSideLeft, topSideRight }); }
  418. AudioChannelSet AudioChannelSet::create5point1point4() { return AudioChannelSet ({ left, right, centre, LFE, leftSurround, rightSurround, topFrontLeft, topFrontRight, topRearLeft, topRearRight }); }
  419. AudioChannelSet AudioChannelSet::create7point0point2() { return AudioChannelSet ({ left, right, centre, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear, topSideLeft, topSideRight }); }
  420. AudioChannelSet AudioChannelSet::create7point1point2() { return AudioChannelSet ({ left, right, centre, LFE, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear, topSideLeft, topSideRight }); }
  421. AudioChannelSet AudioChannelSet::create7point0point4() { return AudioChannelSet ({ left, right, centre, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear, topFrontLeft, topFrontRight, topRearLeft, topRearRight }); }
  422. AudioChannelSet AudioChannelSet::create7point1point4() { return AudioChannelSet ({ left, right, centre, LFE, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear, topFrontLeft, topFrontRight, topRearLeft, topRearRight }); }
  423. AudioChannelSet AudioChannelSet::create7point1point6() { return AudioChannelSet ({ left, right, centre, LFE, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear, topFrontLeft, topFrontRight, topSideLeft, topSideRight, topRearLeft, topRearRight }); }
  424. AudioChannelSet AudioChannelSet::create9point1point6() { return AudioChannelSet ({ left, right, centre, LFE, leftSurroundSide, rightSurroundSide, leftSurroundRear, rightSurroundRear, wideLeft, wideRight, topFrontLeft, topFrontRight, topSideLeft, topSideRight, topRearLeft, topRearRight }); }
  425. AudioChannelSet AudioChannelSet::ambisonic (int order)
  426. {
  427. jassert (isPositiveAndBelow (order, 6));
  428. if (order == 0)
  429. return AudioChannelSet ((uint32) (1 << ambisonicACN0));
  430. AudioChannelSet set ((1u << ambisonicACN0) | (1u << ambisonicACN1) | (1u << ambisonicACN2) | (1u << ambisonicACN3));
  431. auto numAmbisonicChannels = (order + 1) * (order + 1);
  432. set.channels.setRange (ambisonicACN4, numAmbisonicChannels - 4, true);
  433. return set;
  434. }
  435. int AudioChannelSet::getAmbisonicOrder() const
  436. {
  437. auto ambisonicOrder = getAmbisonicOrderForNumChannels (size());
  438. if (ambisonicOrder >= 0)
  439. return (*this == ambisonic (ambisonicOrder) ? ambisonicOrder : -1);
  440. return -1;
  441. }
  442. AudioChannelSet AudioChannelSet::discreteChannels (int numChannels)
  443. {
  444. AudioChannelSet s;
  445. s.channels.setRange (discreteChannel0, numChannels, true);
  446. return s;
  447. }
  448. AudioChannelSet AudioChannelSet::canonicalChannelSet (int numChannels)
  449. {
  450. if (numChannels == 1) return AudioChannelSet::mono();
  451. if (numChannels == 2) return AudioChannelSet::stereo();
  452. if (numChannels == 3) return AudioChannelSet::createLCR();
  453. if (numChannels == 4) return AudioChannelSet::quadraphonic();
  454. if (numChannels == 5) return AudioChannelSet::create5point0();
  455. if (numChannels == 6) return AudioChannelSet::create5point1();
  456. if (numChannels == 7) return AudioChannelSet::create7point0();
  457. if (numChannels == 8) return AudioChannelSet::create7point1();
  458. return discreteChannels (numChannels);
  459. }
  460. AudioChannelSet AudioChannelSet::namedChannelSet (int numChannels)
  461. {
  462. if (numChannels == 1) return AudioChannelSet::mono();
  463. if (numChannels == 2) return AudioChannelSet::stereo();
  464. if (numChannels == 3) return AudioChannelSet::createLCR();
  465. if (numChannels == 4) return AudioChannelSet::quadraphonic();
  466. if (numChannels == 5) return AudioChannelSet::create5point0();
  467. if (numChannels == 6) return AudioChannelSet::create5point1();
  468. if (numChannels == 7) return AudioChannelSet::create7point0();
  469. if (numChannels == 8) return AudioChannelSet::create7point1();
  470. return {};
  471. }
  472. Array<AudioChannelSet> AudioChannelSet::channelSetsWithNumberOfChannels (int numChannels)
  473. {
  474. Array<AudioChannelSet> retval;
  475. if (numChannels != 0)
  476. {
  477. retval.add (AudioChannelSet::discreteChannels (numChannels));
  478. retval.addArray ([numChannels]() -> Array<AudioChannelSet>
  479. {
  480. switch (numChannels)
  481. {
  482. case 1:
  483. return { AudioChannelSet::mono() };
  484. case 2:
  485. return { AudioChannelSet::stereo() };
  486. case 3:
  487. return { AudioChannelSet::createLCR(),
  488. AudioChannelSet::createLRS() };
  489. case 4:
  490. return { AudioChannelSet::quadraphonic(),
  491. AudioChannelSet::createLCRS() };
  492. case 5:
  493. return { AudioChannelSet::create5point0(),
  494. AudioChannelSet::pentagonal() };
  495. case 6:
  496. return { AudioChannelSet::create5point1(),
  497. AudioChannelSet::create6point0(),
  498. AudioChannelSet::create6point0Music(),
  499. AudioChannelSet::hexagonal() };
  500. case 7:
  501. return { AudioChannelSet::create7point0(),
  502. AudioChannelSet::create7point0SDDS(),
  503. AudioChannelSet::create6point1(),
  504. AudioChannelSet::create6point1Music() };
  505. case 8:
  506. return { AudioChannelSet::create7point1(),
  507. AudioChannelSet::create7point1SDDS(),
  508. AudioChannelSet::octagonal(),
  509. AudioChannelSet::create5point1point2() };
  510. case 9:
  511. return { AudioChannelSet::create7point0point2() };
  512. case 10:
  513. return { AudioChannelSet::create5point1point4(),
  514. AudioChannelSet::create7point1point2() };
  515. case 11:
  516. return { AudioChannelSet::create7point0point4() };
  517. case 12:
  518. return { AudioChannelSet::create7point1point4() };
  519. case 14:
  520. return { AudioChannelSet::create7point1point6() };
  521. case 16:
  522. return { AudioChannelSet::create9point1point6() };
  523. }
  524. return {};
  525. }());
  526. auto order = getAmbisonicOrderForNumChannels (numChannels);
  527. if (order >= 0)
  528. retval.add (AudioChannelSet::ambisonic (order));
  529. }
  530. return retval;
  531. }
  532. AudioChannelSet JUCE_CALLTYPE AudioChannelSet::channelSetWithChannels (const Array<ChannelType>& channelArray)
  533. {
  534. AudioChannelSet set;
  535. for (auto ch : channelArray)
  536. {
  537. jassert (! set.channels[static_cast<int> (ch)]);
  538. set.addChannel (ch);
  539. }
  540. return set;
  541. }
  542. //==============================================================================
  543. AudioChannelSet JUCE_CALLTYPE AudioChannelSet::fromWaveChannelMask (int32 dwChannelMask)
  544. {
  545. return AudioChannelSet (static_cast<uint32> ((dwChannelMask & ((1 << 18) - 1)) << 1));
  546. }
  547. int32 AudioChannelSet::getWaveChannelMask() const noexcept
  548. {
  549. if (channels.getHighestBit() > topRearRight)
  550. return -1;
  551. return (channels.toInteger() >> 1);
  552. }
  553. //==============================================================================
  554. int JUCE_CALLTYPE AudioChannelSet::getAmbisonicOrderForNumChannels (int numChannels)
  555. {
  556. auto sqrtMinusOne = std::sqrt (static_cast<float> (numChannels)) - 1.0f;
  557. auto ambisonicOrder = jmax (0, static_cast<int> (std::floor (sqrtMinusOne)));
  558. if (ambisonicOrder > 5)
  559. return -1;
  560. return (static_cast<float> (ambisonicOrder) == sqrtMinusOne ? ambisonicOrder : -1);
  561. }
  562. //==============================================================================
  563. //==============================================================================
  564. #if JUCE_UNIT_TESTS
  565. class AudioChannelSetUnitTest : public UnitTest
  566. {
  567. public:
  568. AudioChannelSetUnitTest()
  569. : UnitTest ("AudioChannelSetUnitTest", UnitTestCategories::audio)
  570. {}
  571. void runTest() override
  572. {
  573. auto max = AudioChannelSet::maxChannelsOfNamedLayout;
  574. beginTest ("maxChannelsOfNamedLayout is non-discrete");
  575. expect (AudioChannelSet::channelSetsWithNumberOfChannels (max).size() >= 2);
  576. beginTest ("channelSetsWithNumberOfChannels returns correct speaker count");
  577. {
  578. for (auto ch = 1; ch <= max; ++ch)
  579. {
  580. auto channelSets = AudioChannelSet::channelSetsWithNumberOfChannels (ch);
  581. for (auto set : channelSets)
  582. expect (set.size() == ch);
  583. }
  584. }
  585. beginTest ("Ambisonics");
  586. {
  587. uint64 mask = 0;
  588. mask |= (1ull << AudioChannelSet::ambisonicACN0);
  589. checkAmbisonic (mask, 0, "0th Order Ambisonics");
  590. mask |= (1ull << AudioChannelSet::ambisonicACN1) | (1ull << AudioChannelSet::ambisonicACN2) | (1ull << AudioChannelSet::ambisonicACN3);
  591. checkAmbisonic (mask, 1, "1st Order Ambisonics");
  592. mask |= (1ull << AudioChannelSet::ambisonicACN4) | (1ull << AudioChannelSet::ambisonicACN5) | (1ull << AudioChannelSet::ambisonicACN6)
  593. | (1ull << AudioChannelSet::ambisonicACN7) | (1ull << AudioChannelSet::ambisonicACN8);
  594. checkAmbisonic (mask, 2, "2nd Order Ambisonics");
  595. mask |= (1ull << AudioChannelSet::ambisonicACN9) | (1ull << AudioChannelSet::ambisonicACN10) | (1ull << AudioChannelSet::ambisonicACN11)
  596. | (1ull << AudioChannelSet::ambisonicACN12) | (1ull << AudioChannelSet::ambisonicACN13) | (1ull << AudioChannelSet::ambisonicACN14)
  597. | (1ull << AudioChannelSet::ambisonicACN15);
  598. checkAmbisonic (mask, 3, "3rd Order Ambisonics");
  599. mask |= (1ull << AudioChannelSet::ambisonicACN16) | (1ull << AudioChannelSet::ambisonicACN17) | (1ull << AudioChannelSet::ambisonicACN18)
  600. | (1ull << AudioChannelSet::ambisonicACN19) | (1ull << AudioChannelSet::ambisonicACN20) | (1ull << AudioChannelSet::ambisonicACN21)
  601. | (1ull << AudioChannelSet::ambisonicACN22) | (1ull << AudioChannelSet::ambisonicACN23) | (1ull << AudioChannelSet::ambisonicACN24);
  602. checkAmbisonic (mask, 4, "4th Order Ambisonics");
  603. mask |= (1ull << AudioChannelSet::ambisonicACN25) | (1ull << AudioChannelSet::ambisonicACN26) | (1ull << AudioChannelSet::ambisonicACN27)
  604. | (1ull << AudioChannelSet::ambisonicACN28) | (1ull << AudioChannelSet::ambisonicACN29) | (1ull << AudioChannelSet::ambisonicACN30)
  605. | (1ull << AudioChannelSet::ambisonicACN31) | (1ull << AudioChannelSet::ambisonicACN32) | (1ull << AudioChannelSet::ambisonicACN33)
  606. | (1ull << AudioChannelSet::ambisonicACN34) | (1ull << AudioChannelSet::ambisonicACN35);
  607. checkAmbisonic (mask, 5, "5th Order Ambisonics");
  608. }
  609. }
  610. private:
  611. void checkAmbisonic (uint64 mask, int order, const char* layoutName)
  612. {
  613. auto expected = AudioChannelSet::ambisonic (order);
  614. auto numChannels = expected.size();
  615. expect (numChannels == BigInteger ((int64) mask).countNumberOfSetBits());
  616. expect (channelSetFromMask (mask) == expected);
  617. expect (order == expected.getAmbisonicOrder());
  618. expect (expected.getDescription() == layoutName);
  619. auto layouts = AudioChannelSet::channelSetsWithNumberOfChannels (numChannels);
  620. expect (layouts.contains (expected));
  621. for (auto layout : layouts)
  622. expect (layout.getAmbisonicOrder() == (layout == expected ? order : -1));
  623. }
  624. static AudioChannelSet channelSetFromMask (uint64 mask)
  625. {
  626. Array<AudioChannelSet::ChannelType> channels;
  627. for (int bit = 0; bit <= 62; ++bit)
  628. if ((mask & (1ull << bit)) != 0)
  629. channels.add (static_cast<AudioChannelSet::ChannelType> (bit));
  630. return AudioChannelSet::channelSetWithChannels (channels);
  631. }
  632. };
  633. static AudioChannelSetUnitTest audioChannelSetUnitTest;
  634. #endif
  635. } // namespace juce