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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 (c) {}
  20. AudioChannelSet::AudioChannelSet (const Array<ChannelType>& c)
  21. {
  22. for (auto channel : c)
  23. addChannel (channel);
  24. }
  25. bool AudioChannelSet::operator== (const AudioChannelSet& other) const noexcept { return channels == other.channels; }
  26. bool AudioChannelSet::operator!= (const AudioChannelSet& other) const noexcept { return channels != other.channels; }
  27. bool AudioChannelSet::operator< (const AudioChannelSet& other) const noexcept { return channels < other.channels; }
  28. String AudioChannelSet::getChannelTypeName (AudioChannelSet::ChannelType type)
  29. {
  30. if (type >= discreteChannel0)
  31. return "Discrete " + String (type - discreteChannel0 + 1);
  32. switch (type)
  33. {
  34. case left: return NEEDS_TRANS("Left");
  35. case right: return NEEDS_TRANS("Right");
  36. case centre: return NEEDS_TRANS("Centre");
  37. case LFE: return NEEDS_TRANS("LFE");
  38. case leftSurround: return NEEDS_TRANS("Left Surround");
  39. case rightSurround: return NEEDS_TRANS("Right Surround");
  40. case leftCentre: return NEEDS_TRANS("Left Centre");
  41. case rightCentre: return NEEDS_TRANS("Right Centre");
  42. case centreSurround: return NEEDS_TRANS("Centre Surround");
  43. case leftSurroundRear: return NEEDS_TRANS("Left Surround Rear");
  44. case rightSurroundRear: return NEEDS_TRANS("Right Surround Rear");
  45. case topMiddle: return NEEDS_TRANS("Top Middle");
  46. case topFrontLeft: return NEEDS_TRANS("Top Front Left");
  47. case topFrontCentre: return NEEDS_TRANS("Top Front Centre");
  48. case topFrontRight: return NEEDS_TRANS("Top Front Right");
  49. case topRearLeft: return NEEDS_TRANS("Top Rear Left");
  50. case topRearCentre: return NEEDS_TRANS("Top Rear Centre");
  51. case topRearRight: return NEEDS_TRANS("Top Rear Right");
  52. case wideLeft: return NEEDS_TRANS("Wide Left");
  53. case wideRight: return NEEDS_TRANS("Wide Right");
  54. case LFE2: return NEEDS_TRANS("LFE 2");
  55. case leftSurroundSide: return NEEDS_TRANS("Left Surround Side");
  56. case rightSurroundSide: return NEEDS_TRANS("Right Surround Side");
  57. case ambisonicW: return NEEDS_TRANS("Ambisonic W");
  58. case ambisonicX: return NEEDS_TRANS("Ambisonic X");
  59. case ambisonicY: return NEEDS_TRANS("Ambisonic Y");
  60. case ambisonicZ: return NEEDS_TRANS("Ambisonic Z");
  61. case topSideLeft: return NEEDS_TRANS("Top Side Left");
  62. case topSideRight: return NEEDS_TRANS("Top Side Right");
  63. default: break;
  64. }
  65. return "Unknown";
  66. }
  67. String AudioChannelSet::getAbbreviatedChannelTypeName (AudioChannelSet::ChannelType type)
  68. {
  69. if (type >= discreteChannel0)
  70. return String (type - discreteChannel0 + 1);
  71. switch (type)
  72. {
  73. case left: return "L";
  74. case right: return "R";
  75. case centre: return "C";
  76. case LFE: return "Lfe";
  77. case leftSurround: return "Ls";
  78. case rightSurround: return "Rs";
  79. case leftCentre: return "Lc";
  80. case rightCentre: return "Rc";
  81. case centreSurround: return "Cs";
  82. case leftSurroundRear: return "Lrs";
  83. case rightSurroundRear: return "Rrs";
  84. case topMiddle: return "Tm";
  85. case topFrontLeft: return "Tfl";
  86. case topFrontCentre: return "Tfc";
  87. case topFrontRight: return "Tfr";
  88. case topRearLeft: return "Trl";
  89. case topRearCentre: return "Trc";
  90. case topRearRight: return "Trr";
  91. case wideLeft: return "Wl";
  92. case wideRight: return "Wr";
  93. case LFE2: return "Lfe2";
  94. case leftSurroundSide: return "Lss";
  95. case rightSurroundSide: return "Rss";
  96. case ambisonicW: return "W";
  97. case ambisonicX: return "X";
  98. case ambisonicY: return "Y";
  99. case ambisonicZ: return "Z";
  100. case topSideLeft: return "Tsl";
  101. case topSideRight: return "Tsr";
  102. default: break;
  103. }
  104. return {};
  105. }
  106. AudioChannelSet::ChannelType AudioChannelSet::getChannelTypeFromAbbreviation (const String& abbr)
  107. {
  108. if (abbr.length() > 0 && (abbr[0] >= '0' && abbr[0] <= '9'))
  109. return static_cast<AudioChannelSet::ChannelType> (static_cast<int> (discreteChannel0)
  110. + abbr.getIntValue() + 1);
  111. if (abbr == "L") return left;
  112. if (abbr == "R") return right;
  113. if (abbr == "C") return centre;
  114. if (abbr == "Lfe") return LFE;
  115. if (abbr == "Ls") return leftSurround;
  116. if (abbr == "Rs") return rightSurround;
  117. if (abbr == "Lc") return leftCentre;
  118. if (abbr == "Rc") return rightCentre;
  119. if (abbr == "Cs") return centreSurround;
  120. if (abbr == "Lrs") return leftSurroundRear;
  121. if (abbr == "Rrs") return rightSurroundRear;
  122. if (abbr == "Tm") return topMiddle;
  123. if (abbr == "Tfl") return topFrontLeft;
  124. if (abbr == "Tfc") return topFrontCentre;
  125. if (abbr == "Tfr") return topFrontRight;
  126. if (abbr == "Trl") return topRearLeft;
  127. if (abbr == "Trc") return topRearCentre;
  128. if (abbr == "Trr") return topRearRight;
  129. if (abbr == "Wl") return wideLeft;
  130. if (abbr == "Wr") return wideRight;
  131. if (abbr == "Lfe2") return LFE2;
  132. if (abbr == "Lss") return leftSurroundSide;
  133. if (abbr == "Rss") return rightSurroundSide;
  134. if (abbr == "W") return ambisonicW;
  135. if (abbr == "X") return ambisonicX;
  136. if (abbr == "Y") return ambisonicY;
  137. if (abbr == "Z") return ambisonicZ;
  138. if (abbr == "Tsl") return topSideLeft;
  139. if (abbr == "Tsr") return topSideRight;
  140. return unknown;
  141. }
  142. String AudioChannelSet::getSpeakerArrangementAsString() const
  143. {
  144. StringArray speakerTypes;
  145. for (auto& speaker : getChannelTypes())
  146. {
  147. auto name = getAbbreviatedChannelTypeName (speaker);
  148. if (name.isNotEmpty())
  149. speakerTypes.add (name);
  150. }
  151. return speakerTypes.joinIntoString (" ");
  152. }
  153. AudioChannelSet AudioChannelSet::fromAbbreviatedString (const String& str)
  154. {
  155. AudioChannelSet set;
  156. for (auto& abbr : StringArray::fromTokens (str, true))
  157. {
  158. auto type = getChannelTypeFromAbbreviation (abbr);
  159. if (type != unknown)
  160. set.addChannel (type);
  161. }
  162. return set;
  163. }
  164. String AudioChannelSet::getDescription() const
  165. {
  166. if (isDiscreteLayout()) return "Discrete #" + String (size());
  167. if (*this == disabled()) return "Disabled";
  168. if (*this == mono()) return "Mono";
  169. if (*this == stereo()) return "Stereo";
  170. if (*this == createLCR()) return "LCR";
  171. if (*this == createLRS()) return "LRS";
  172. if (*this == createLCRS()) return "LCRS";
  173. if (*this == create5point0()) return "5.0 Surround";
  174. if (*this == create5point1()) return "5.1 Surround";
  175. if (*this == create6point0()) return "6.0 Surround";
  176. if (*this == create6point1()) return "6.1 Surround";
  177. if (*this == create6point0Music()) return "6.0 (Music) Surround";
  178. if (*this == create6point1Music()) return "6.1 (Music) Surround";
  179. if (*this == create7point0()) return "7.0 Surround";
  180. if (*this == create7point1()) return "7.1 Surround";
  181. if (*this == create7point0SDDS()) return "7.0 Surround SDDS";
  182. if (*this == create7point1SDDS()) return "7.1 Surround SDDS";
  183. if (*this == create7point0point2()) return "7.0.2 Surround";
  184. if (*this == create7point1point2()) return "7.1.2 Surround";
  185. if (*this == quadraphonic()) return "Quadraphonic";
  186. if (*this == pentagonal()) return "Pentagonal";
  187. if (*this == hexagonal()) return "Hexagonal";
  188. if (*this == octagonal()) return "Octagonal";
  189. if (*this == ambisonic()) return "Ambisonic";
  190. return "Unknown";
  191. }
  192. bool AudioChannelSet::isDiscreteLayout() const noexcept
  193. {
  194. for (auto& speaker : getChannelTypes())
  195. if (speaker <= topSideRight)
  196. return false;
  197. return true;
  198. }
  199. int AudioChannelSet::size() const noexcept
  200. {
  201. return channels.countNumberOfSetBits();
  202. }
  203. AudioChannelSet::ChannelType AudioChannelSet::getTypeOfChannel (int index) const noexcept
  204. {
  205. int bit = channels.findNextSetBit(0);
  206. for (int i = 0; i < index && bit >= 0; ++i)
  207. bit = channels.findNextSetBit (bit + 1);
  208. return static_cast<ChannelType> (bit);
  209. }
  210. int AudioChannelSet::getChannelIndexForType (AudioChannelSet::ChannelType type) const noexcept
  211. {
  212. int idx = 0;
  213. for (int bit = channels.findNextSetBit (0); bit >= 0; bit = channels.findNextSetBit (bit + 1))
  214. {
  215. if (static_cast<ChannelType> (bit) == type)
  216. return idx;
  217. idx++;
  218. }
  219. return -1;
  220. }
  221. Array<AudioChannelSet::ChannelType> AudioChannelSet::getChannelTypes() const
  222. {
  223. Array<ChannelType> result;
  224. for (int bit = channels.findNextSetBit(0); bit >= 0; bit = channels.findNextSetBit (bit + 1))
  225. result.add (static_cast<ChannelType> (bit));
  226. return result;
  227. }
  228. void AudioChannelSet::addChannel (ChannelType newChannel)
  229. {
  230. const int bit = static_cast<int> (newChannel);
  231. jassert (bit >= 0 && bit < 1024);
  232. channels.setBit (bit);
  233. }
  234. void AudioChannelSet::removeChannel (ChannelType newChannel)
  235. {
  236. const int bit = static_cast<int> (newChannel);
  237. jassert (bit >= 0 && bit < 1024);
  238. channels.clearBit (bit);
  239. }
  240. AudioChannelSet AudioChannelSet::disabled() { return {}; }
  241. AudioChannelSet AudioChannelSet::mono() { return AudioChannelSet (1u << centre); }
  242. AudioChannelSet AudioChannelSet::stereo() { return AudioChannelSet ((1u << left) | (1u << right)); }
  243. AudioChannelSet AudioChannelSet::createLCR() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre)); }
  244. AudioChannelSet AudioChannelSet::createLRS() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << surround)); }
  245. AudioChannelSet AudioChannelSet::createLCRS() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << surround)); }
  246. AudioChannelSet AudioChannelSet::create5point0() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurround) | (1u << rightSurround)); }
  247. AudioChannelSet AudioChannelSet::create5point1() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << LFE) | (1u << leftSurround) | (1u << rightSurround)); }
  248. AudioChannelSet AudioChannelSet::create6point0() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurround) | (1u << rightSurround) | (1u << centreSurround)); }
  249. AudioChannelSet AudioChannelSet::create6point1() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << LFE) | (1u << leftSurround) | (1u << rightSurround) | (1u << centreSurround)); }
  250. AudioChannelSet AudioChannelSet::create6point0Music() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << leftSurround) | (1u << rightSurround) | (1u << leftSurroundSide) | (1u << rightSurroundSide)); }
  251. AudioChannelSet AudioChannelSet::create6point1Music() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << LFE) | (1u << leftSurround) | (1u << rightSurround) | (1u << leftSurroundSide) | (1u << rightSurroundSide)); }
  252. AudioChannelSet AudioChannelSet::create7point0() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurroundSide) | (1u << rightSurroundSide) | (1u << leftSurroundRear) | (1u << rightSurroundRear)); }
  253. AudioChannelSet AudioChannelSet::create7point0SDDS() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurround) | (1u << rightSurround) | (1u << leftCentre) | (1u << rightCentre)); }
  254. AudioChannelSet AudioChannelSet::create7point1() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << LFE) | (1u << leftSurroundSide) | (1u << rightSurroundSide) | (1u << leftSurroundRear) | (1u << rightSurroundRear)); }
  255. AudioChannelSet AudioChannelSet::create7point1SDDS() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << LFE) | (1u << leftSurround) | (1u << rightSurround) | (1u << leftCentre) | (1u << rightCentre)); }
  256. AudioChannelSet AudioChannelSet::ambisonic() { return AudioChannelSet ((1u << ambisonicW) | (1u << ambisonicX) | (1u << ambisonicY) | (1u << ambisonicZ)); }
  257. AudioChannelSet AudioChannelSet::quadraphonic() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << leftSurround) | (1u << rightSurround)); }
  258. AudioChannelSet AudioChannelSet::pentagonal() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurroundRear) | (1u << rightSurroundRear)); }
  259. AudioChannelSet AudioChannelSet::hexagonal() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << centreSurround) | (1u << leftSurroundRear) | (1u << rightSurroundRear)); }
  260. AudioChannelSet AudioChannelSet::octagonal() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurround) | (1u << rightSurround) | (1u << centreSurround) | (1u << wideLeft) | (1u << wideRight)); }
  261. AudioChannelSet AudioChannelSet::create7point0point2() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << leftSurroundSide) | (1u << rightSurroundSide) | (1u << leftSurroundRear) | (1u << rightSurroundRear) | (1u << topSideLeft) | (1u << topSideRight)); }
  262. AudioChannelSet AudioChannelSet::create7point1point2() { return AudioChannelSet ((1u << left) | (1u << right) | (1u << centre) | (1u << LFE) | (1u << leftSurroundSide) | (1u << rightSurroundSide) | (1u << leftSurroundRear) | (1u << rightSurroundRear) | (1u << topSideLeft) | (1u << topSideRight)); }
  263. AudioChannelSet AudioChannelSet::discreteChannels (int numChannels)
  264. {
  265. AudioChannelSet s;
  266. s.channels.setRange (discreteChannel0, numChannels, true);
  267. return s;
  268. }
  269. AudioChannelSet AudioChannelSet::canonicalChannelSet (int numChannels)
  270. {
  271. if (numChannels == 1) return AudioChannelSet::mono();
  272. if (numChannels == 2) return AudioChannelSet::stereo();
  273. if (numChannels == 3) return AudioChannelSet::createLCR();
  274. if (numChannels == 4) return AudioChannelSet::quadraphonic();
  275. if (numChannels == 5) return AudioChannelSet::create5point0();
  276. if (numChannels == 6) return AudioChannelSet::create5point1();
  277. if (numChannels == 7) return AudioChannelSet::create7point0();
  278. if (numChannels == 8) return AudioChannelSet::create7point1();
  279. return discreteChannels (numChannels);
  280. }
  281. AudioChannelSet AudioChannelSet::namedChannelSet (int numChannels)
  282. {
  283. if (numChannels == 1) return AudioChannelSet::mono();
  284. if (numChannels == 2) return AudioChannelSet::stereo();
  285. if (numChannels == 3) return AudioChannelSet::createLCR();
  286. if (numChannels == 4) return AudioChannelSet::quadraphonic();
  287. if (numChannels == 5) return AudioChannelSet::create5point0();
  288. if (numChannels == 6) return AudioChannelSet::create5point1();
  289. if (numChannels == 7) return AudioChannelSet::create7point0();
  290. if (numChannels == 8) return AudioChannelSet::create7point1();
  291. return {};
  292. }
  293. Array<AudioChannelSet> AudioChannelSet::channelSetsWithNumberOfChannels (int numChannels)
  294. {
  295. Array<AudioChannelSet> retval;
  296. if (numChannels != 0)
  297. {
  298. retval.add (AudioChannelSet::discreteChannels (numChannels));
  299. if (numChannels == 1)
  300. {
  301. retval.add (AudioChannelSet::mono());
  302. }
  303. else if (numChannels == 2)
  304. {
  305. retval.add (AudioChannelSet::stereo());
  306. }
  307. else if (numChannels == 3)
  308. {
  309. retval.add (AudioChannelSet::createLCR());
  310. retval.add (AudioChannelSet::createLRS());
  311. }
  312. else if (numChannels == 4)
  313. {
  314. retval.add (AudioChannelSet::quadraphonic());
  315. retval.add (AudioChannelSet::createLCRS());
  316. retval.add (AudioChannelSet::ambisonic());
  317. }
  318. else if (numChannels == 5)
  319. {
  320. retval.add (AudioChannelSet::create5point0());
  321. retval.add (AudioChannelSet::pentagonal());
  322. }
  323. else if (numChannels == 6)
  324. {
  325. retval.add (AudioChannelSet::create5point1());
  326. retval.add (AudioChannelSet::create6point0());
  327. retval.add (AudioChannelSet::create6point0Music());
  328. retval.add (AudioChannelSet::hexagonal());
  329. }
  330. else if (numChannels == 7)
  331. {
  332. retval.add (AudioChannelSet::create7point0());
  333. retval.add (AudioChannelSet::create7point0SDDS());
  334. retval.add (AudioChannelSet::create6point1());
  335. retval.add (AudioChannelSet::create6point1Music());
  336. }
  337. else if (numChannels == 8)
  338. {
  339. retval.add (AudioChannelSet::create7point1());
  340. retval.add (AudioChannelSet::create7point1SDDS());
  341. retval.add (AudioChannelSet::octagonal());
  342. }
  343. }
  344. return retval;
  345. }
  346. AudioChannelSet JUCE_CALLTYPE AudioChannelSet::channelSetWithChannels (const Array<ChannelType>& channelArray)
  347. {
  348. AudioChannelSet set;
  349. for (auto ch : channelArray)
  350. {
  351. jassert (! set.channels[static_cast<int> (ch)]);
  352. set.addChannel (ch);
  353. }
  354. return set;
  355. }
  356. //==============================================================================
  357. AudioChannelSet JUCE_CALLTYPE AudioChannelSet::fromWaveChannelMask (int32 dwChannelMask)
  358. {
  359. return AudioChannelSet (static_cast<uint32> ((dwChannelMask & ((1 << 18) - 1)) << 1));
  360. }
  361. int32 AudioChannelSet::getWaveChannelMask() const noexcept
  362. {
  363. if (channels.getHighestBit() > topRearRight)
  364. return -1;
  365. return (channels.toInteger() >> 1);
  366. }
  367. } // namespace juce