utils.py 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. import os
  5. from ctypes import *
  6. from mod import get_unique_name
  7. from sys import platform
  8. # ------------------------------------------------------------------------------------------------------------
  9. # Convert a ctypes c_char_p into a python string
  10. def charPtrToString(charPtr):
  11. if not charPtr:
  12. return ""
  13. if isinstance(charPtr, str):
  14. return charPtr
  15. return charPtr.decode("utf-8", errors="ignore")
  16. # ------------------------------------------------------------------------------------------------------------
  17. # Convert a ctypes POINTER(c_char_p) into a python string list
  18. def charPtrPtrToStringList(charPtrPtr):
  19. if not charPtrPtr:
  20. return []
  21. i = 0
  22. charPtr = charPtrPtr[0]
  23. strList = []
  24. while charPtr:
  25. strList.append(charPtr.decode("utf-8", errors="ignore"))
  26. i += 1
  27. charPtr = charPtrPtr[i]
  28. return strList
  29. # ------------------------------------------------------------------------------------------------------------
  30. # Convert a ctypes POINTER(c_<num>) into a python number list
  31. def numPtrToList(numPtr):
  32. if not numPtr:
  33. return []
  34. i = 0
  35. num = numPtr[0] #.value
  36. numList = []
  37. while num not in (0, 0.0):
  38. numList.append(num)
  39. i += 1
  40. num = numPtr[i] #.value
  41. return numList
  42. # ------------------------------------------------------------------------------------------------------------
  43. def structPtrToList(structPtr):
  44. if not structPtr:
  45. return []
  46. i = 0
  47. ret = []
  48. struct = structPtr[0]
  49. while struct.valid:
  50. ret.append(structToDict(struct))
  51. i += 1
  52. struct = structPtr[i]
  53. return ret
  54. def structPtrPtrToList(structPtr):
  55. if not structPtr:
  56. return []
  57. i = 0
  58. ret = []
  59. struct = structPtr[0]
  60. while struct:
  61. ret.append(structToDict(struct.contents))
  62. i += 1
  63. struct = structPtr[i]
  64. return ret
  65. # ------------------------------------------------------------------------------------------------------------
  66. # Convert a ctypes value into a python one
  67. c_int_types = (c_int, c_int8, c_int16, c_int32, c_int64, c_uint, c_uint8, c_uint16, c_uint32, c_uint64, c_long, c_longlong)
  68. c_float_types = (c_float, c_double, c_longdouble)
  69. c_intp_types = tuple(POINTER(i) for i in c_int_types)
  70. c_floatp_types = tuple(POINTER(i) for i in c_float_types)
  71. c_struct_types = () # redefined below
  72. c_structp_types = () # redefined below
  73. c_structpp_types = () # redefined below
  74. c_union_types = ()
  75. def toPythonType(value, attr):
  76. #if value is None:
  77. #return ""
  78. if isinstance(value, (bool, int, float)):
  79. return value
  80. if isinstance(value, bytes):
  81. return charPtrToString(value)
  82. if isinstance(value, c_intp_types) or isinstance(value, c_floatp_types):
  83. return numPtrToList(value)
  84. if isinstance(value, POINTER(c_char_p)):
  85. return charPtrPtrToStringList(value)
  86. if isinstance(value, c_struct_types):
  87. return structToDict(value)
  88. if isinstance(value, c_structp_types):
  89. return structPtrToList(value)
  90. if isinstance(value, c_structpp_types):
  91. return structPtrPtrToList(value)
  92. if isinstance(value, c_union_types):
  93. return unionToDict(value)
  94. print("..............", attr, ".....................", value, ":", type(value))
  95. return value
  96. # ------------------------------------------------------------------------------------------------------------
  97. # Convert a ctypes struct into a python dict
  98. def structToDict(struct):
  99. return dict((attr, toPythonType(getattr(struct, attr), attr)) for attr, value in struct._fields_)
  100. def unionToDict(struct):
  101. if isinstance(struct, PluginParameterRanges):
  102. if struct.type == b'f':
  103. return structToDict(struct.f)
  104. if struct.type == b'l':
  105. return structToDict(struct.l)
  106. if struct.type == b's':
  107. return {
  108. 'minimum': "",
  109. 'maximum': "",
  110. 'default': charPtrToString(struct.s),
  111. }
  112. return None
  113. # ------------------------------------------------------------------------------------------------------------
  114. if platform == 'win32':
  115. ext = "dll"
  116. else:
  117. ext = "so"
  118. tryPath1 = os.path.join(os.path.dirname(__file__), "libmod_utils." + ext)
  119. tryPath2 = os.path.join(os.path.dirname(__file__), "..", "lib", "libmod_utils." + ext)
  120. tryPath3 = os.path.join(os.path.dirname(__file__), "..", "utils", "libmod_utils." + ext)
  121. if os.path.exists(tryPath1):
  122. utils = cdll.LoadLibrary(tryPath1)
  123. elif os.path.exists(tryPath2):
  124. utils = cdll.LoadLibrary(tryPath2)
  125. else:
  126. utils = cdll.LoadLibrary(tryPath3)
  127. # PluginLicenseType
  128. kPluginLicenseNonCommercial = 0
  129. kPluginLicenseTrial = -1
  130. kPluginLicensePaid = 1
  131. # PluginIOType
  132. kPluginIONull = 0
  133. kPluginIOAudioMono = 1
  134. kPluginIOAudioStereo = 2
  135. kPluginIOInstrument = 3
  136. kPluginIOMIDI = 4
  137. # PedalboardInfoType
  138. kPedalboardInfoUserOnly = 0
  139. kPedalboardInfoFactoryOnly = 1
  140. kPedalboardInfoBoth = 2
  141. class PluginAuthor(Structure):
  142. _fields_ = [
  143. ("name", c_char_p),
  144. ("homepage", c_char_p),
  145. ("email", c_char_p),
  146. ]
  147. class PluginGUIPort(Structure):
  148. _fields_ = [
  149. ("valid", c_bool),
  150. ("index", c_uint),
  151. ("name", c_char_p),
  152. ("symbol", c_char_p),
  153. ]
  154. class PluginGUI(Structure):
  155. _fields_ = [
  156. ("resourcesDirectory", c_char_p),
  157. ("iconTemplate", c_char_p),
  158. ("settingsTemplate", c_char_p),
  159. ("javascript", c_char_p),
  160. ("stylesheet", c_char_p),
  161. ("screenshot", c_char_p),
  162. ("thumbnail", c_char_p),
  163. ("discussionURL", c_char_p),
  164. ("documentation", c_char_p),
  165. ("brand", c_char_p),
  166. ("label", c_char_p),
  167. ("model", c_char_p),
  168. ("panel", c_char_p),
  169. ("color", c_char_p),
  170. ("knob", c_char_p),
  171. ("ports", POINTER(PluginGUIPort)),
  172. ("monitoredOutputs", POINTER(c_char_p)),
  173. ]
  174. class PluginGUI_Mini(Structure):
  175. _fields_ = [
  176. ("resourcesDirectory", c_char_p),
  177. ("screenshot", c_char_p),
  178. ("thumbnail", c_char_p),
  179. ]
  180. class PluginPortGroup(Structure):
  181. _fields_ = [
  182. ("valid", c_bool),
  183. ("uri", c_char_p),
  184. ("symbol", c_char_p),
  185. ("name", c_char_p),
  186. ("index", c_uint),
  187. ]
  188. class PluginPortRanges(Structure):
  189. _fields_ = [
  190. ("minimum", c_float),
  191. ("maximum", c_float),
  192. ("default", c_float),
  193. ]
  194. class PluginPortUnits(Structure):
  195. _fields_ = [
  196. ("label", c_char_p),
  197. ("render", c_char_p),
  198. ("symbol", c_char_p),
  199. ("_custom", c_bool), # internal, do not use
  200. ]
  201. class PluginPortScalePoint(Structure):
  202. _fields_ = [
  203. ("valid", c_bool),
  204. ("value", c_float),
  205. ("label", c_char_p),
  206. ]
  207. class PluginPort(Structure):
  208. _fields_ = [
  209. ("valid", c_bool),
  210. ("index", c_uint),
  211. ("name", c_char_p),
  212. ("symbol", c_char_p),
  213. ("ranges", PluginPortRanges),
  214. ("units", PluginPortUnits),
  215. ("comment", c_char_p),
  216. ("designation", c_char_p),
  217. ("group", c_char_p),
  218. ("properties", POINTER(c_char_p)),
  219. ("rangeSteps", c_int),
  220. ("scalePoints", POINTER(PluginPortScalePoint)),
  221. ("shortName", c_char_p),
  222. ]
  223. class PluginPortsI(Structure):
  224. _fields_ = [
  225. ("input", POINTER(PluginPort)),
  226. ("output", POINTER(PluginPort)),
  227. ]
  228. class PluginPorts(Structure):
  229. _fields_ = [
  230. ("audio", PluginPortsI),
  231. ("control", PluginPortsI),
  232. ("cv", PluginPortsI),
  233. ("midi", PluginPortsI),
  234. ]
  235. class PluginLongParameterRanges(Structure):
  236. _fields_ = [
  237. ("minimum", c_int64),
  238. ("maximum", c_int64),
  239. ("default", c_int64),
  240. ]
  241. class _PluginParameterRangesU(Union):
  242. _fields_ = [
  243. ("f", PluginPortRanges),
  244. ("l", PluginLongParameterRanges),
  245. ("s", c_char_p),
  246. ]
  247. class PluginParameterRanges(Structure):
  248. _anonymous_ = ("u",)
  249. _fields_ = [
  250. ("type", c_char),
  251. ("u", _PluginParameterRangesU),
  252. ]
  253. class PluginParameter(Structure):
  254. _fields_ = [
  255. ("valid", c_bool),
  256. ("readable", c_bool),
  257. ("writable", c_bool),
  258. ("uri", c_char_p),
  259. ("label", c_char_p),
  260. ("type", c_char_p),
  261. ("ranges", PluginParameterRanges),
  262. ("units", PluginPortUnits),
  263. ("comment", c_char_p),
  264. ("shortName", c_char_p),
  265. ("fileTypes", POINTER(c_char_p)),
  266. ("supportedExtensions", POINTER(c_char_p)),
  267. ]
  268. class PluginPreset(Structure):
  269. _fields_ = [
  270. ("valid", c_bool),
  271. ("uri", c_char_p),
  272. ("label", c_char_p),
  273. ("path", c_char_p),
  274. ]
  275. class PluginInfo(Structure):
  276. _fields_ = [
  277. ("valid", c_bool),
  278. ("uri", c_char_p),
  279. ("name", c_char_p),
  280. ("binary", c_char_p),
  281. ("brand", c_char_p),
  282. ("label", c_char_p),
  283. ("license", c_char_p),
  284. ("comment", c_char_p),
  285. ("buildEnvironment", c_char_p),
  286. ("category", POINTER(c_char_p)),
  287. ("microVersion", c_int),
  288. ("minorVersion", c_int),
  289. ("release", c_int),
  290. ("builder", c_int),
  291. ("licensed", c_int),
  292. ("iotype", c_int),
  293. ("hasExternalUI", c_bool),
  294. ("version", c_char_p),
  295. ("stability", c_char_p),
  296. ("author", PluginAuthor),
  297. ("bundles", POINTER(c_char_p)),
  298. ("gui", PluginGUI),
  299. ("ports", PluginPorts),
  300. ("portGroups", POINTER(PluginPortGroup)),
  301. ("parameters", POINTER(PluginParameter)),
  302. ("presets", POINTER(PluginPreset)),
  303. ]
  304. # a subset of PluginInfo
  305. class NonCachedPluginInfo(Structure):
  306. _fields_ = [
  307. ("licensed", c_int),
  308. ("presets", POINTER(PluginPreset)),
  309. ]
  310. class PluginInfo_Mini(Structure):
  311. _fields_ = [
  312. ("uri", c_char_p),
  313. ("name", c_char_p),
  314. ("brand", c_char_p),
  315. ("label", c_char_p),
  316. ("comment", c_char_p),
  317. ("buildEnvironment", c_char_p),
  318. ("category", POINTER(c_char_p)),
  319. ("microVersion", c_int),
  320. ("minorVersion", c_int),
  321. ("release", c_int),
  322. ("builder", c_int),
  323. ("licensed", c_int),
  324. ("iotype", c_int),
  325. ("gui", PluginGUI_Mini),
  326. ]
  327. class PluginInfo_Essentials(Structure):
  328. _fields_ = [
  329. ("controlInputs", POINTER(PluginPort)),
  330. ("monitoredOutputs", POINTER(c_char_p)),
  331. ("parameters", POINTER(PluginParameter)),
  332. ("buildEnvironment", c_char_p),
  333. ("microVersion", c_int),
  334. ("minorVersion", c_int),
  335. ("release", c_int),
  336. ("builder", c_int),
  337. ]
  338. class PerformancePluginInfo(Structure):
  339. _fields_ = [
  340. ("visible", c_bool),
  341. ("index", c_int),
  342. ]
  343. class PedalboardMidiControl(Structure):
  344. _fields_ = [
  345. ("channel", c_int8),
  346. ("control", c_uint8),
  347. # ranges added in v1.2, flag needed for old format compatibility
  348. ("hasRanges", c_bool),
  349. ("minimum", c_float),
  350. ("maximum", c_float),
  351. ]
  352. class PedalboardPluginPort(Structure):
  353. _fields_ = [
  354. ("valid", c_bool),
  355. ("symbol", c_char_p),
  356. ("value", c_float),
  357. ("midiCC", PedalboardMidiControl),
  358. ]
  359. class PedalboardPlugin(Structure):
  360. _fields_ = [
  361. ("valid", c_bool),
  362. ("bypassed", c_bool),
  363. ("instanceNumber", c_int),
  364. ("instance", c_char_p),
  365. ("uri", c_char_p),
  366. ("bypassCC", PedalboardMidiControl),
  367. ("x", c_float),
  368. ("y", c_float),
  369. ("ports", POINTER(PedalboardPluginPort)),
  370. ("preset", c_char_p),
  371. ("label", c_char_p),
  372. ("performance", PerformancePluginInfo)
  373. ]
  374. class PedalboardConnection(Structure):
  375. _fields_ = [
  376. ("valid", c_bool),
  377. ("source", c_char_p),
  378. ("target", c_char_p),
  379. ]
  380. class PedalboardHardwareMidiPort(Structure):
  381. _fields_ = [
  382. ("valid", c_bool),
  383. ("symbol", c_char_p),
  384. ("name", c_char_p),
  385. ]
  386. class PedalboardHardware(Structure):
  387. _fields_ = [
  388. ("audio_ins", c_uint),
  389. ("audio_outs", c_uint),
  390. ("cv_ins", c_uint),
  391. ("cv_outs", c_uint),
  392. ("midi_ins", POINTER(PedalboardHardwareMidiPort)),
  393. ("midi_outs", POINTER(PedalboardHardwareMidiPort)),
  394. ("serial_midi_in", c_bool),
  395. ("serial_midi_out", c_bool),
  396. ("midi_merger_out", c_bool),
  397. ("midi_broadcaster_in", c_bool),
  398. ]
  399. kPedalboardTimeAvailableBPB = 0x1
  400. kPedalboardTimeAvailableBPM = 0x2
  401. kPedalboardTimeAvailableRolling = 0x4
  402. class PedalboardTimeInfo(Structure):
  403. _fields_ = [
  404. ("available", c_uint),
  405. ("bpb", c_float),
  406. ("bpbCC", PedalboardMidiControl),
  407. ("bpm", c_float),
  408. ("bpmCC", PedalboardMidiControl),
  409. ("rolling", c_bool),
  410. ("rollingCC", PedalboardMidiControl),
  411. ]
  412. class PedalboardInfo(Structure):
  413. _fields_ = [
  414. ("title", c_char_p),
  415. ("width", c_int),
  416. ("height", c_int),
  417. ("factory", c_bool),
  418. ("midi_separated_mode", c_bool),
  419. ("midi_loopback", c_bool),
  420. ("plugins", POINTER(PedalboardPlugin)),
  421. ("connections", POINTER(PedalboardConnection)),
  422. ("hardware", PedalboardHardware),
  423. ("timeInfo", PedalboardTimeInfo),
  424. ("version", c_uint),
  425. ("formatVersion", c_uint),
  426. ]
  427. class PedalboardInfo_Mini(Structure):
  428. _fields_ = [
  429. ("broken", c_bool),
  430. ("factory", c_bool),
  431. ("hasTrialPlugins", c_bool),
  432. ("uri", c_char_p),
  433. ("bundle", c_char_p),
  434. ("title", c_char_p),
  435. ("version", c_uint),
  436. ]
  437. class StatePortValue(Structure):
  438. _fields_ = [
  439. ("valid", c_bool),
  440. ("symbol", c_char_p),
  441. ("value", c_float),
  442. ]
  443. class PedalboardPluginValues(Structure):
  444. _fields_ = [
  445. ("valid", c_bool),
  446. ("bypassed", c_bool),
  447. ("instance", c_char_p),
  448. ("preset", c_char_p),
  449. ("ports", POINTER(StatePortValue)),
  450. ]
  451. class JackData(Structure):
  452. _fields_ = [
  453. ("cpuLoad", c_float),
  454. ("xruns", c_uint),
  455. ("rolling", c_bool),
  456. ("bpb", c_double),
  457. ("bpm", c_double),
  458. ]
  459. JackBufSizeChanged = CFUNCTYPE(None, c_uint)
  460. JackPortAppeared = CFUNCTYPE(None, c_char_p, c_bool)
  461. JackPortDeleted = CFUNCTYPE(None, c_char_p)
  462. TrueBypassStateChanged = CFUNCTYPE(None, c_bool, c_bool)
  463. CvExpInputModeChanged = CFUNCTYPE(None, c_bool)
  464. c_struct_types = (PluginAuthor,
  465. PluginGUI,
  466. PluginGUI_Mini,
  467. PluginPortRanges,
  468. PluginPortUnits,
  469. PluginPortsI,
  470. PluginPorts,
  471. PluginLongParameterRanges,
  472. PedalboardMidiControl,
  473. PerformancePluginInfo,
  474. PedalboardHardware,
  475. PedalboardTimeInfo)
  476. c_structp_types = (POINTER(PluginGUIPort),
  477. POINTER(PluginPortScalePoint),
  478. POINTER(PluginPort),
  479. POINTER(PluginParameter),
  480. POINTER(PluginPortGroup),
  481. POINTER(PluginPreset),
  482. POINTER(PedalboardPlugin),
  483. POINTER(PedalboardConnection),
  484. POINTER(PedalboardPluginPort),
  485. POINTER(PedalboardHardwareMidiPort),
  486. POINTER(StatePortValue))
  487. c_structpp_types = (POINTER(POINTER(PluginInfo_Mini)),
  488. POINTER(POINTER(PedalboardInfo_Mini)))
  489. c_union_types = (PluginParameterRanges,)
  490. utils.init.argtypes = None
  491. utils.init.restype = None
  492. utils.cleanup.argtypes = None
  493. utils.cleanup.restype = None
  494. utils.is_bundle_loaded.argtypes = (c_char_p,)
  495. utils.is_bundle_loaded.restype = c_bool
  496. utils.add_bundle_to_lilv_world.argtypes = (c_char_p,)
  497. utils.add_bundle_to_lilv_world.restype = POINTER(c_char_p)
  498. utils.remove_bundle_from_lilv_world.argtypes = (c_char_p, c_char_p)
  499. utils.remove_bundle_from_lilv_world.restype = POINTER(c_char_p)
  500. utils.get_plugin_list.argtypes = None
  501. utils.get_plugin_list.restype = POINTER(c_char_p)
  502. utils.get_all_plugins.argtypes = None
  503. utils.get_all_plugins.restype = POINTER(POINTER(PluginInfo_Mini))
  504. utils.get_plugin_info.argtypes = (c_char_p,)
  505. utils.get_plugin_info.restype = POINTER(PluginInfo)
  506. utils.get_non_cached_plugin_info.argtypes = (c_char_p,)
  507. utils.get_non_cached_plugin_info.restype = POINTER(NonCachedPluginInfo)
  508. utils.get_plugin_gui.argtypes = (c_char_p,)
  509. utils.get_plugin_gui.restype = POINTER(PluginGUI)
  510. utils.get_plugin_gui_mini.argtypes = (c_char_p,)
  511. utils.get_plugin_gui_mini.restype = POINTER(PluginGUI_Mini)
  512. utils.get_plugin_control_inputs.argtypes = (c_char_p,)
  513. utils.get_plugin_control_inputs.restype = POINTER(PluginPort)
  514. utils.get_plugin_info_essentials.argtypes = (c_char_p,)
  515. utils.get_plugin_info_essentials.restype = POINTER(PluginInfo_Essentials)
  516. utils.is_plugin_preset_valid.argtypes = (c_char_p, c_char_p)
  517. utils.is_plugin_preset_valid.restype = c_bool
  518. utils.rescan_plugin_presets.argtypes = (c_char_p,)
  519. utils.rescan_plugin_presets.restype = None
  520. utils.get_all_pedalboards.argtypes = (c_int,)
  521. utils.get_all_pedalboards.restype = POINTER(POINTER(PedalboardInfo_Mini))
  522. utils.get_broken_pedalboards.argtypes = None
  523. utils.get_broken_pedalboards.restype = POINTER(c_char_p)
  524. utils.get_pedalboard_info.argtypes = (c_char_p,)
  525. utils.get_pedalboard_info.restype = POINTER(PedalboardInfo)
  526. utils.get_pedalboard_size.argtypes = (c_char_p,)
  527. utils.get_pedalboard_size.restype = POINTER(c_int)
  528. utils.get_pedalboard_plugin_values.argtypes = (c_char_p,)
  529. utils.get_pedalboard_plugin_values.restype = POINTER(PedalboardPluginValues)
  530. utils.reset_get_all_pedalboards_cache.argtypes = (c_int,)
  531. utils.reset_get_all_pedalboards_cache.restype = None
  532. utils.get_state_port_values.argtypes = (c_char_p,)
  533. utils.get_state_port_values.restype = POINTER(StatePortValue)
  534. utils.list_plugins_in_bundle.argtypes = (c_char_p,)
  535. utils.list_plugins_in_bundle.restype = POINTER(c_char_p)
  536. utils.file_uri_parse.argtypes = (c_char_p,)
  537. utils.file_uri_parse.restype = c_char_p
  538. utils.init_jack.argtypes = None
  539. utils.init_jack.restype = c_bool
  540. utils.close_jack.argtypes = None
  541. utils.close_jack.restype = None
  542. utils.get_jack_data.argtypes = (c_bool,)
  543. utils.get_jack_data.restype = POINTER(JackData)
  544. utils.get_jack_buffer_size.argtypes = None
  545. utils.get_jack_buffer_size.restype = c_uint
  546. utils.set_jack_buffer_size.argtypes = (c_uint,)
  547. utils.set_jack_buffer_size.restype = c_uint
  548. utils.get_jack_sample_rate.argtypes = None
  549. utils.get_jack_sample_rate.restype = c_float
  550. utils.get_jack_port_alias.argtypes = (c_char_p,)
  551. utils.get_jack_port_alias.restype = c_char_p
  552. utils.has_midi_beat_clock_sender_port.argtypes = None
  553. utils.has_midi_beat_clock_sender_port.restype = c_bool
  554. utils.has_serial_midi_input_port.argtypes = None
  555. utils.has_serial_midi_input_port.restype = c_bool
  556. utils.has_serial_midi_output_port.argtypes = None
  557. utils.has_serial_midi_output_port.restype = c_bool
  558. utils.has_midi_merger_output_port.argtypes = None
  559. utils.has_midi_merger_output_port.restype = c_bool
  560. utils.has_midi_broadcaster_input_port.argtypes = None
  561. utils.has_midi_broadcaster_input_port.restype = c_bool
  562. utils.has_duox_split_spdif.argtypes = None
  563. utils.has_duox_split_spdif.restype = c_bool
  564. utils.get_jack_hardware_ports.argtypes = (c_bool, c_bool)
  565. utils.get_jack_hardware_ports.restype = POINTER(c_char_p)
  566. utils.connect_jack_ports.argtypes = (c_char_p, c_char_p)
  567. utils.connect_jack_ports.restype = c_bool
  568. utils.connect_jack_midi_output_ports.argtypes = (c_char_p,)
  569. utils.connect_jack_midi_output_ports.restype = c_bool
  570. utils.disconnect_jack_ports.argtypes = (c_char_p, c_char_p)
  571. utils.disconnect_jack_ports.restype = c_bool
  572. utils.disconnect_all_jack_ports.argtypes = (c_char_p,)
  573. utils.disconnect_all_jack_ports.restype = c_bool
  574. utils.reset_xruns.argtypes = None
  575. utils.reset_xruns.restype = None
  576. utils.init_bypass.argtypes = None
  577. utils.init_bypass.restype = None
  578. utils.get_truebypass_value.argtypes = (c_bool,)
  579. utils.get_truebypass_value.restype = c_bool
  580. utils.set_truebypass_value.argtypes = (c_bool, c_bool)
  581. utils.set_truebypass_value.restype = c_bool
  582. utils.get_master_volume.argtypes = (c_bool,)
  583. utils.get_master_volume.restype = c_float
  584. utils.set_util_callbacks.argtypes = (JackBufSizeChanged, JackPortAppeared, JackPortDeleted, TrueBypassStateChanged)
  585. utils.set_util_callbacks.restype = None
  586. utils.set_extra_util_callbacks.argtypes = (CvExpInputModeChanged,)
  587. utils.set_extra_util_callbacks.restype = None
  588. # ------------------------------------------------------------------------------------------------------------
  589. # initialize
  590. def init():
  591. utils.init()
  592. # cleanup, cannot be used afterwards
  593. def cleanup():
  594. utils.cleanup()
  595. # ------------------------------------------------------------------------------------------------------------
  596. # check if a bundle is loaded in our lilv world
  597. def is_bundle_loaded(bundlepath):
  598. return bool(utils.is_bundle_loaded(bundlepath.encode("utf-8")))
  599. # add a bundle to our lilv world
  600. # returns uri list of added plugins
  601. def add_bundle_to_lilv_world(bundlepath):
  602. return charPtrPtrToStringList(utils.add_bundle_to_lilv_world(bundlepath.encode("utf-8")))
  603. # remove a bundle to our lilv world
  604. # returns uri list of removed plugins
  605. def remove_bundle_from_lilv_world(bundlepath, resource):
  606. return charPtrPtrToStringList(utils.remove_bundle_from_lilv_world(bundlepath.encode("utf-8"),
  607. resource.encode("utf-8") if resource else None))
  608. # ------------------------------------------------------------------------------------------------------------
  609. # get all available plugins
  610. # this triggers short scanning of all plugins
  611. def get_plugin_list():
  612. return charPtrPtrToStringList(utils.get_plugin_list())
  613. # get all available plugins
  614. # this triggers short scanning of all plugins
  615. def get_all_plugins():
  616. return structPtrPtrToList(utils.get_all_plugins())
  617. # Plugins implemented by mod-host itself that have no bypass: mod-host ignores "bypass" for them, so nothing
  618. # (dialog switch, addressing, MIDI learn) may offer one. mod-host detects them by the same URIs (src/effects.c).
  619. PLUGINS_WITHOUT_BYPASS = (
  620. "http://moddevices.com/plugins/mod-devel/Buffer",
  621. )
  622. def is_plugin_bypassable(uri):
  623. return uri not in PLUGINS_WITHOUT_BYPASS
  624. # get a specific plugin
  625. # NOTE: may throw
  626. def get_plugin_info(uri):
  627. info = utils.get_plugin_info(uri.encode("utf-8"))
  628. if not info:
  629. raise Exception
  630. info = structToDict(info.contents)
  631. info['bypassable'] = is_plugin_bypassable(uri)
  632. return info
  633. # get a specific plugin (non-cached specific info)
  634. # NOTE: may throw
  635. def get_non_cached_plugin_info(uri):
  636. info = utils.get_non_cached_plugin_info(uri.encode("utf-8"))
  637. if not info:
  638. raise Exception
  639. info = structToDict(info.contents)
  640. info['bypassable'] = is_plugin_bypassable(uri)
  641. return info
  642. # get a specific plugin's modgui
  643. # NOTE: may throw
  644. def get_plugin_gui(uri):
  645. info = utils.get_plugin_gui(uri.encode("utf-8"))
  646. if not info:
  647. raise Exception
  648. return structToDict(info.contents)
  649. # get a specific plugin's modgui (mini)
  650. # NOTE: may throw
  651. def get_plugin_gui_mini(uri):
  652. info = utils.get_plugin_gui_mini(uri.encode("utf-8"))
  653. if not info:
  654. raise Exception
  655. return structToDict(info.contents)
  656. def get_plugin_control_inputs(uri):
  657. return structPtrToList(utils.get_plugin_control_inputs(uri.encode("utf-8")))
  658. # get essential plugin info for host control (control inputs, monitored outputs, parameters and build environment)
  659. def get_plugin_info_essentials(uri):
  660. info = utils.get_plugin_info_essentials(uri.encode("utf-8"))
  661. if not info:
  662. return {
  663. 'error': True,
  664. 'controlInputs': [],
  665. 'monitoredOutputs': [],
  666. 'parameters': [],
  667. 'buildEnvironment': '',
  668. 'microVersion': 0,
  669. 'minorVersion': 0,
  670. 'release': 0,
  671. 'builder': 0,
  672. }
  673. return structToDict(info.contents)
  674. # check if a plugin preset uri is valid (must exist)
  675. def is_plugin_preset_valid(plugin, preset):
  676. return bool(utils.is_plugin_preset_valid(plugin.encode("utf-8"), preset.encode("utf-8")))
  677. # trigger a preset rescan for a plugin the next time it's loaded
  678. def rescan_plugin_presets(uri):
  679. utils.rescan_plugin_presets(uri.encode("utf-8"))
  680. # ------------------------------------------------------------------------------------------------------------
  681. _allpedalboards = None
  682. _alluserpedalboards = None
  683. _allfactorypedalboards = None
  684. def _get_all_pedalboards_user():
  685. global _alluserpedalboards
  686. if _alluserpedalboards is None:
  687. pbs = structPtrPtrToList(utils.get_all_pedalboards(kPedalboardInfoUserOnly))
  688. utitles = []
  689. for pb in pbs:
  690. ntitle = get_unique_name(pb['title'], utitles)
  691. if ntitle is not None:
  692. pb['title'] = ntitle
  693. utitles.append(pb['title'])
  694. _alluserpedalboards = pbs
  695. return _alluserpedalboards
  696. def _get_all_pedalboards_factory():
  697. global _allfactorypedalboards
  698. if _allfactorypedalboards is None:
  699. _allfactorypedalboards = structPtrPtrToList(utils.get_all_pedalboards(kPedalboardInfoFactoryOnly))
  700. return _allfactorypedalboards
  701. # get all available pedalboards (ie, plugins with pedalboard type)
  702. def get_all_pedalboards(ptype):
  703. if ptype == kPedalboardInfoUserOnly:
  704. return _get_all_pedalboards_user()
  705. if ptype == kPedalboardInfoFactoryOnly:
  706. return _get_all_pedalboards_factory()
  707. if ptype == kPedalboardInfoBoth:
  708. global _allpedalboards
  709. if _allpedalboards is None:
  710. _allpedalboards = _get_all_pedalboards_user() + _get_all_pedalboards_factory()
  711. return _allpedalboards
  712. return []
  713. def has_pedalboard_cache():
  714. global _allpedalboards
  715. return _allpedalboards is not None
  716. # handy function to reset our last call value
  717. def reset_get_all_pedalboards_cache(ptype):
  718. global _allpedalboards
  719. global _alluserpedalboards
  720. global _allfactorypedalboards
  721. if ptype in (kPedalboardInfoUserOnly, kPedalboardInfoBoth):
  722. _alluserpedalboards = None
  723. if ptype in (kPedalboardInfoFactoryOnly, kPedalboardInfoBoth):
  724. _allfactorypedalboards = None
  725. _allpedalboards = None
  726. utils.reset_get_all_pedalboards_cache(ptype)
  727. # handy function to update cached pedalboard version
  728. def update_cached_pedalboard_version(bundle):
  729. global _allpedalboards
  730. global _alluserpedalboards
  731. if _alluserpedalboards is None:
  732. return
  733. for pedalboard in _alluserpedalboards:
  734. if pedalboard['bundle'] == bundle:
  735. pedalboard['version'] += 1
  736. _allpedalboards = None
  737. return
  738. print("ERROR: update_cached_pedalboard_version() failed", bundle)
  739. # handy function to get only the names from all user pedalboards
  740. def get_all_user_pedalboard_names():
  741. return tuple(pb['title'] for pb in _get_all_pedalboards_user())
  742. # get all currently "broken" pedalboards (ie, pedalboards which contain unavailable plugins)
  743. def get_broken_pedalboards():
  744. return charPtrPtrToStringList(utils.get_broken_pedalboards())
  745. # Get a specific pedalboard
  746. # NOTE: may throw
  747. def get_pedalboard_info(bundle):
  748. info = utils.get_pedalboard_info(bundle.encode("utf-8"))
  749. if not info:
  750. raise Exception
  751. return structToDict(info.contents)
  752. # Get the size of a specific pedalboard
  753. # Returns a 2-size array with width and height
  754. # NOTE: may throw
  755. def get_pedalboard_size(bundle):
  756. size = utils.get_pedalboard_size(bundle.encode("utf-8"))
  757. if not size:
  758. raise Exception
  759. width = int(size[0])
  760. height = int(size[1])
  761. return (width, height)
  762. # Get plugin port values of a pedalboard
  763. def get_pedalboard_plugin_values(bundle):
  764. return structPtrToList(utils.get_pedalboard_plugin_values(bundle.encode("utf-8")))
  765. # Get port values from a plugin state
  766. def get_state_port_values(state):
  767. values = structPtrToList(utils.get_state_port_values(state.encode("utf-8")))
  768. return dict((v['symbol'], v['value']) for v in values)
  769. # list plugins present in a single bundle
  770. def list_plugins_in_bundle(bundle):
  771. return charPtrPtrToStringList(utils.list_plugins_in_bundle(bundle.encode("utf-8")))
  772. # ------------------------------------------------------------------------------------------------------------
  773. # Get the absolute directory of a file or bundle uri.
  774. def get_bundle_dirname(bundleuri):
  775. bundle = charPtrToString(utils.file_uri_parse(bundleuri))
  776. if not bundle:
  777. raise IOError(bundleuri)
  778. if not os.path.exists(bundle):
  779. raise IOError(bundleuri)
  780. if os.path.isfile(bundle):
  781. bundle = os.path.dirname(bundle)
  782. return bundle
  783. # ------------------------------------------------------------------------------------------------------------
  784. # jack stuff
  785. def init_jack():
  786. return bool(utils.init_jack())
  787. def close_jack():
  788. utils.close_jack()
  789. def get_jack_data(withTransport):
  790. data = utils.get_jack_data(withTransport)
  791. if not data:
  792. raise Exception
  793. return {
  794. 'cpuLoad': data.contents.cpuLoad,
  795. 'xruns' : data.contents.xruns,
  796. 'rolling': data.contents.rolling,
  797. 'bpb' : data.contents.bpb,
  798. 'bpm' : data.contents.bpm
  799. }
  800. def get_jack_buffer_size():
  801. return int(utils.get_jack_buffer_size())
  802. def set_jack_buffer_size(size):
  803. return int(utils.set_jack_buffer_size(size))
  804. def get_jack_sample_rate():
  805. return float(utils.get_jack_sample_rate())
  806. def get_jack_port_alias(portname):
  807. return charPtrToString(utils.get_jack_port_alias(portname.encode("utf-8")))
  808. def has_midi_beat_clock_sender_port():
  809. return bool(utils.has_midi_beat_clock_sender_port())
  810. def has_serial_midi_input_port():
  811. return bool(utils.has_serial_midi_input_port())
  812. def has_serial_midi_output_port():
  813. return bool(utils.has_serial_midi_output_port())
  814. def has_midi_merger_output_port():
  815. return bool(utils.has_midi_merger_output_port())
  816. def has_midi_broadcaster_input_port():
  817. return bool(utils.has_midi_broadcaster_input_port())
  818. def has_duox_split_spdif():
  819. return bool(utils.has_duox_split_spdif())
  820. def get_jack_hardware_ports(isAudio, isOutput):
  821. return charPtrPtrToStringList(utils.get_jack_hardware_ports(isAudio, isOutput))
  822. def connect_jack_ports(port1, port2):
  823. return bool(utils.connect_jack_ports(port1.encode("utf-8"), port2.encode("utf-8")))
  824. def connect_jack_midi_output_ports(port):
  825. return bool(utils.connect_jack_midi_output_ports(port.encode("utf-8")))
  826. def disconnect_jack_ports(port1, port2):
  827. return bool(utils.disconnect_jack_ports(port1.encode("utf-8"), port2.encode("utf-8")))
  828. def disconnect_all_jack_ports(port):
  829. return bool(utils.disconnect_all_jack_ports(port.encode("utf-8")))
  830. def reset_xruns():
  831. utils.reset_xruns()
  832. # ------------------------------------------------------------------------------------------------------------
  833. # alsa stuff
  834. def init_bypass():
  835. utils.init_bypass()
  836. def get_truebypass_value(right):
  837. return bool(utils.get_truebypass_value(right))
  838. def set_truebypass_value(right, bypassed):
  839. return bool(utils.set_truebypass_value(right, bypassed))
  840. def get_master_volume(right):
  841. return float(utils.get_master_volume(right))
  842. # ------------------------------------------------------------------------------------------------------------
  843. # callbacks
  844. global bufSizeChangedCb, portAppearedCb, portDeletedCb, trueBypassChangedCb
  845. bufSizeChangedCb = portAppearedCb = portDeletedCb = trueBypassChangedCb = None
  846. def set_util_callbacks(bufSizeChanged, portAppeared, portDeleted, trueBypassChanged):
  847. global bufSizeChangedCb, portAppearedCb, portDeletedCb, trueBypassChangedCb
  848. bufSizeChangedCb = JackBufSizeChanged(bufSizeChanged)
  849. portAppearedCb = JackPortAppeared(portAppeared)
  850. portDeletedCb = JackPortDeleted(portDeleted)
  851. trueBypassChangedCb = TrueBypassStateChanged(trueBypassChanged)
  852. utils.set_util_callbacks(bufSizeChangedCb, portAppearedCb, portDeletedCb, trueBypassChangedCb)
  853. # ------------------------------------------------------------------------------------------------------------
  854. # special case until HMI<->system comm is not in place yet
  855. global cvExpInputModeChangedCb
  856. cvExpInputModeChangedCb = None
  857. def set_extra_util_callbacks(cvExpInputModeChanged):
  858. global cvExpInputModeChangedCb
  859. cvExpInputModeChangedCb = CvExpInputModeChanged(cvExpInputModeChanged)
  860. utils.set_extra_util_callbacks(cvExpInputModeChangedCb)
  861. # ------------------------------------------------------------------------------------------------------------
  862. # set process name
  863. def set_process_name(newname):
  864. PR_SET_NAME = 15
  865. try:
  866. libc = cdll.LoadLibrary("libc.so.6")
  867. except:
  868. return
  869. libc.prctl.argtypes = [c_int, c_void_p, c_int, c_int, c_int]
  870. libc.prctl.restype = c_int
  871. libc.prctl(PR_SET_NAME, newname.encode("utf-8"), 0, 0, 0)
  872. # ------------------------------------------------------------------------------------------------------------