Collection of tools useful for audio production
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.

248 lines
7.5KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Imports (Global)
  4. import dbus, sys
  5. from PyQt4.QtCore import QCoreApplication
  6. # Imports (Custom Stuff)
  7. from shared_cadence import *
  8. # Cadence Global Settings
  9. GlobalSettings = QSettings("Cadence", "GlobalSettings")
  10. # DBus
  11. class DBus(object):
  12. __slots__ = [
  13. 'bus',
  14. 'a2j',
  15. 'jack'
  16. ]
  17. DBus = DBus()
  18. def forceReset():
  19. # Kill all audio processes
  20. stopAllAudioProcesses()
  21. # Remove configs
  22. configFiles = (
  23. # Cadence GlobalSettings
  24. os.path.join(HOME, ".asoundrc"),
  25. # ALSA settings
  26. os.path.join(HOME, ".config", "Cadence", "GlobalSettings.conf"),
  27. # JACK2 settings
  28. os.path.join(HOME, ".config", "jack", "conf.xml"),
  29. # JACK1 settings
  30. os.path.join(HOME, ".config", "jack", "conf-jack1.xml")
  31. )
  32. for config in configFiles:
  33. if os.path.exists(config):
  34. os.remove(config)
  35. # Start JACK, A2J and Pulse, according to user settings
  36. def startSession(systemStarted, secondSystemStartAttempt):
  37. # Check if JACK is set to auto-start
  38. if systemStarted and not GlobalSettings.value("JACK/AutoStart", False, type=bool):
  39. print("JACK is set to NOT auto-start on login")
  40. return True
  41. # Called via autostart desktop file
  42. if systemStarted and secondSystemStartAttempt:
  43. tmp_bus = dbus.SessionBus()
  44. tmp_jack = tmp_bus.get_object("org.jackaudio.service", "/org/jackaudio/Controller")
  45. started = bool(tmp_jack.IsStarted())
  46. # Cleanup
  47. del tmp_bus, tmp_jack
  48. # If already started, do nothing
  49. if started:
  50. return True
  51. # Kill all audio processes first
  52. stopAllAudioProcesses()
  53. # Connect to DBus
  54. DBus.bus = dbus.SessionBus()
  55. DBus.jack = DBus.bus.get_object("org.jackaudio.service", "/org/jackaudio/Controller")
  56. try:
  57. DBus.a2j = dbus.Interface(DBus.bus.get_object("org.gna.home.a2jmidid", "/"), "org.gna.home.a2jmidid.control")
  58. except:
  59. DBus.a2j = None
  60. if GlobalSettings.value("JACK/AutoLoadLadishStudio", False, type=bool):
  61. try:
  62. ladish_control = DBus.bus.get_object("org.ladish", "/org/ladish/Control")
  63. except:
  64. startJack()
  65. return False
  66. try:
  67. ladish_conf = DBus.bus.get_object("org.ladish.conf", "/org/ladish/conf")
  68. except:
  69. ladish_conf = None
  70. ladishStudioName = dbus.String(GlobalSettings.value("JACK/LadishStudioName", "", type=str))
  71. ladishStudioListDump = ladish_control.GetStudioList()
  72. for thisStudioName, thisStudioDict in ladishStudioListDump:
  73. if ladishStudioName == thisStudioName:
  74. try:
  75. if ladish_conf and ladish_conf.get('/org/ladish/daemon/notify')[0] == "true":
  76. ladish_conf.set('/org/ladish/daemon/notify', "false")
  77. ladishNotifyHack = True
  78. else:
  79. ladishNotifyHack = False
  80. except:
  81. ladishNotifyHack = False
  82. ladish_control.LoadStudio(thisStudioName)
  83. ladish_studio = DBus.bus.get_object("org.ladish", "/org/ladish/Studio")
  84. if not bool(ladish_studio.IsStarted()):
  85. ladish_studio.Start()
  86. if ladishNotifyHack:
  87. ladish_conf.set('/org/ladish/daemon/notify', "true")
  88. break
  89. else:
  90. startJack()
  91. else:
  92. startJack()
  93. if not bool(DBus.jack.IsStarted()):
  94. print("JACK Failed to Start")
  95. return False
  96. # Start bridges according to user settings
  97. # ALSA-Audio
  98. if GlobalSettings.value("ALSA-Audio/BridgeIndexType", iAlsaFileNone, type=int) == iAlsaFileLoop:
  99. startAlsaAudioLoopBridge()
  100. sleep(0.5)
  101. # ALSA-MIDI
  102. if GlobalSettings.value("A2J/AutoStart", True, type=bool) and DBus.a2j and not bool(DBus.a2j.is_started()):
  103. a2jExportHW = GlobalSettings.value("A2J/ExportHW", True, type=bool)
  104. DBus.a2j.set_hw_export(a2jExportHW)
  105. DBus.a2j.start()
  106. # PulseAudio
  107. if GlobalSettings.value("Pulse2JACK/AutoStart", True, type=bool):
  108. if GlobalSettings.value("Pulse2JACK/PlaybackModeOnly", False, type=bool):
  109. os.system("cadence-pulse2jack -p")
  110. else:
  111. os.system("cadence-pulse2jack")
  112. print("JACK Started Successfully")
  113. return True
  114. def startJack():
  115. if not bool(DBus.jack.IsStarted()):
  116. DBus.jack.StartServer()
  117. def printLADSPA_PATH():
  118. EXTRA_LADSPA_DIRS = GlobalSettings.value("AudioPlugins/EXTRA_LADSPA_PATH", "", type=str).split(":")
  119. LADSPA_PATH_str = ":".join(DEFAULT_LADSPA_PATH)
  120. for i in range(len(EXTRA_LADSPA_DIRS)):
  121. if EXTRA_LADSPA_DIRS[i]:
  122. LADSPA_PATH_str += ":"+EXTRA_LADSPA_DIRS[i]
  123. print(LADSPA_PATH_str)
  124. def printDSSI_PATH():
  125. EXTRA_DSSI_DIRS = GlobalSettings.value("AudioPlugins/EXTRA_DSSI_PATH", "", type=str).split(":")
  126. DSSI_PATH_str = ":".join(DEFAULT_DSSI_PATH)
  127. for i in range(len(EXTRA_DSSI_DIRS)):
  128. if EXTRA_DSSI_DIRS[i]:
  129. DSSI_PATH_str += ":"+EXTRA_DSSI_DIRS[i]
  130. print(DSSI_PATH_str)
  131. def printLV2_PATH():
  132. EXTRA_LV2_DIRS = GlobalSettings.value("AudioPlugins/EXTRA_LV2_PATH", "", type=str).split(":")
  133. LV2_PATH_str = ":".join(DEFAULT_LV2_PATH)
  134. for i in range(len(EXTRA_LV2_DIRS)):
  135. if EXTRA_LV2_DIRS[i]:
  136. LV2_PATH_str += ":"+EXTRA_LV2_DIRS[i]
  137. print(LV2_PATH_str)
  138. def printVST_PATH():
  139. EXTRA_VST_DIRS = GlobalSettings.value("AudioPlugins/EXTRA_VST_PATH", "", type=str).split(":")
  140. VST_PATH_str = ":".join(DEFAULT_VST_PATH)
  141. for i in range(len(EXTRA_VST_DIRS)):
  142. if EXTRA_VST_DIRS[i]:
  143. VST_PATH_str += ":"+EXTRA_VST_DIRS[i]
  144. print(VST_PATH_str)
  145. def printArguments():
  146. print("\t-s|--start \tStart session")
  147. print("\t --reset \tForce-reset all JACK daemons and settings (disables auto-start at login)")
  148. print("")
  149. print("\t-h|--help \tShow this help message")
  150. print("\t-v|--version\tShow version")
  151. def printError(cmd):
  152. print("Invalid arguments")
  153. print("Run '%s -h' for help" % (cmd))
  154. def printHelp(cmd):
  155. print("Usage: %s [cmd]" % (cmd))
  156. printArguments()
  157. def printVersion():
  158. print("Cadence version %s" % (VERSION))
  159. print("Developed by falkTX and the rest of the KXStudio Team")
  160. #--------------- main ------------------
  161. if __name__ == '__main__':
  162. # App initialization
  163. app = QCoreApplication(sys.argv)
  164. app.setApplicationName("Cadence")
  165. app.setApplicationVersion(VERSION)
  166. app.setOrganizationName("Cadence")
  167. # Check arguments
  168. cmd = sys.argv[0]
  169. if len(app.arguments()) == 1:
  170. printHelp(cmd)
  171. elif len(app.arguments()) == 2:
  172. arg = app.arguments()[1]
  173. if arg == "--printLADSPA_PATH":
  174. printLADSPA_PATH()
  175. elif arg == "--printDSSI_PATH":
  176. printDSSI_PATH()
  177. elif arg == "--printLV2_PATH":
  178. printLV2_PATH()
  179. elif arg == "--printVST_PATH":
  180. printVST_PATH()
  181. elif arg == "--reset":
  182. forceReset()
  183. elif arg in ("--system-start", "--system-start-desktop"):
  184. sys.exit(startSession(True, arg == "--system-start-desktop"))
  185. elif arg in ("-s", "--s", "-start", "--start"):
  186. sys.exit(startSession(False, False))
  187. elif arg in ("-h", "--h", "-help", "--help"):
  188. printHelp(cmd)
  189. elif arg in ("-v", "--v", "-version", "--version"):
  190. printVersion()
  191. else:
  192. printError(cmd)
  193. else:
  194. printError(cmd)
  195. # Exit
  196. sys.exit(0)