stereo.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-FileCopyrightText: 2012-2026 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Which side of a stereo pair a port symbol names, and what the other side is called.
  4. //
  5. // ponytail: naming heuristic. LV2 declares real pairs through the port-groups extension
  6. // and this repo's backend already reads them (utils_lilv.cpp -> PluginPort.group), but
  7. // that field does not reach the plugin JSON, no released mod-desktop ships a libmod_utils
  8. // that has it, and most plugins declare no groups anyway. Swap the body of this function
  9. // for a port.group comparison once the field is in the browser.
  10. var STEREO_OTHER_SIDE = {
  11. 'l': 'r',
  12. 'r': 'l',
  13. 'left': 'right',
  14. 'right': 'left',
  15. }
  16. // Recase `word` the way `like` is cased: "R" for "L", "Right" for "Left", else lower
  17. function stereoRecase(word, like) {
  18. if (like === like.toUpperCase()) {
  19. return word.toUpperCase()
  20. }
  21. if (like[0] === like[0].toUpperCase()) {
  22. return word[0].toUpperCase() + word.slice(1)
  23. }
  24. return word
  25. }
  26. // Given one port symbol, returns the symbol its stereo partner would have, and whether
  27. // this symbol is the left/odd half of the pair (the half that draws the merged cable).
  28. // Returns null for symbols that name no side at all, such as a mono "in".
  29. //
  30. // The returned symbol is a guess: the caller decides it is a real pair by checking that
  31. // a port of the same type and direction actually goes by that name.
  32. function stereoCounterpart(symbol) {
  33. // Deliberately permissive: "signal" is read as a left channel and guesses "signar".
  34. // Nothing is named that, so the caller's existence check throws the guess away. An
  35. // earlier version demanded a separator or a case change to rule such words out, and
  36. // that cost real pairs -- inl/inr, outl/outr, inputleft/inputright, OUT1L/OUT1R --
  37. // because plenty of plugins name their ports in one flat case.
  38. var m = /^(.*?)([_-]?)(left|right|l|r)$/i.exec(symbol)
  39. if (m !== null) {
  40. var side = m[3].toLowerCase()
  41. return {
  42. symbol: m[1] + m[2] + stereoRecase(STEREO_OTHER_SIDE[side], m[3]),
  43. first: side[0] === 'l',
  44. }
  45. }
  46. // Numbered channels pair up as (1,2), (3,4), ... so a four-out plugin gets two
  47. // pairs rather than one pair and two strays
  48. m = /^(.*?)([_-]?)(\d+)$/.exec(symbol)
  49. if (m !== null) {
  50. var n = parseInt(m[3], 10)
  51. if (n < 1) {
  52. return null
  53. }
  54. var partner = String(n % 2 === 1 ? n + 1 : n - 1)
  55. while (partner.length < m[3].length) {
  56. partner = '0' + partner // keep any zero padding: out_01 -> out_02
  57. }
  58. return {
  59. symbol: m[1] + m[2] + partner,
  60. first: n % 2 === 1,
  61. }
  62. }
  63. return null
  64. }