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.

308 lines
9.4KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Common/Shared code
  4. # Copyright (C) 2010-2012 Filipe Coelho <falktx@gmail.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the COPYING file
  17. # Imports (Global)
  18. import os, sys
  19. from unicodedata import normalize
  20. from PyQt4.QtCore import qWarning, SIGNAL, SLOT
  21. from PyQt4.QtGui import QFileDialog, QIcon, QMessageBox
  22. # Set Platform
  23. if sys.platform == "darwin":
  24. from PyQt4.QtGui import qt_mac_set_menubar_icons
  25. qt_mac_set_menubar_icons(False)
  26. LINUX = False
  27. MACOS = True
  28. WINDOWS = False
  29. elif "linux" in sys.platform:
  30. LINUX = True
  31. MACOS = False
  32. WINDOWS = False
  33. elif sys.platform in ("win32", "win64"):
  34. LINUX = False
  35. MACOS = False
  36. WINDOWS = True
  37. else:
  38. LINUX = False
  39. MACOS = False
  40. WINDOWS = False
  41. try:
  42. from signal import signal, SIGINT, SIGTERM, SIGUSR1, SIGUSR2
  43. haveSignal = True
  44. except:
  45. haveSignal = False
  46. # Set Version
  47. VERSION = "0.5.0"
  48. # Set Debug mode
  49. DEBUG = True
  50. # Global variables
  51. global x_gui
  52. x_gui = None
  53. # Small integrity tests
  54. HOME = os.getenv("HOME")
  55. if HOME is None:
  56. if WINDOWS:
  57. HOME = os.getenv("TMP")
  58. else:
  59. qWarning("HOME variable not set")
  60. HOME = "/tmp"
  61. elif not os.path.exists(HOME):
  62. qWarning("HOME variable set but not valid")
  63. if WINDOWS:
  64. HOME = os.getenv("TMP")
  65. else:
  66. HOME = "/tmp"
  67. PATH_env = os.getenv("PATH")
  68. if PATH_env is None:
  69. qWarning("PATH variable not set")
  70. if LINUX:
  71. PATH = ("/usr/local/bin", "/usr/bin", "/bin", "/usr/games")
  72. elif MACOS:
  73. PATH = ("/opt/local/bin", "/usr/local/bin", "/usr/bin", "/bin")
  74. elif WINDOWS:
  75. WINDIR = os.getenv("WINDIR")
  76. PATH = (os.path.join(WINDIR, "system32"), WINDIR)
  77. del WINDIR
  78. else:
  79. PATH = ("/usr/local/bin", "/usr/bin", "/bin")
  80. else:
  81. PATH = PATH_env.split(os.pathsep)
  82. del PATH_env
  83. # ------------------------------------------------------------------------------------------------------------
  84. MIDI_CC_LIST = (
  85. #"0x00 Bank Select",
  86. "0x01 Modulation",
  87. "0x02 Breath",
  88. "0x03 (Undefined)",
  89. "0x04 Foot",
  90. "0x05 Portamento",
  91. #"0x06 (Data Entry MSB)",
  92. "0x07 Volume",
  93. "0x08 Balance",
  94. "0x09 (Undefined)",
  95. "0x0A Pan",
  96. "0x0B Expression",
  97. "0x0C FX Control 1",
  98. "0x0D FX Control 2",
  99. "0x0E (Undefined)",
  100. "0x0F (Undefined)",
  101. "0x10 General Purpose 1",
  102. "0x11 General Purpose 2",
  103. "0x12 General Purpose 3",
  104. "0x13 General Purpose 4",
  105. "0x14 (Undefined)",
  106. "0x15 (Undefined)",
  107. "0x16 (Undefined)",
  108. "0x17 (Undefined)",
  109. "0x18 (Undefined)",
  110. "0x19 (Undefined)",
  111. "0x1A (Undefined)",
  112. "0x1B (Undefined)",
  113. "0x1C (Undefined)",
  114. "0x1D (Undefined)",
  115. "0x1E (Undefined)",
  116. "0x1F (Undefined)",
  117. #"0x20 *Bank Select",
  118. #"0x21 *Modulation",
  119. #"0x22 *Breath",
  120. #"0x23 *(Undefined)",
  121. #"0x24 *Foot",
  122. #"0x25 *Portamento",
  123. #"0x26 *(Data Entry MSB)",
  124. #"0x27 *Volume",
  125. #"0x28 *Balance",
  126. #"0x29 *(Undefined)",
  127. #"0x2A *Pan",
  128. #"0x2B *Expression",
  129. #"0x2C *FX *Control 1",
  130. #"0x2D *FX *Control 2",
  131. #"0x2E *(Undefined)",
  132. #"0x2F *(Undefined)",
  133. #"0x30 *General Purpose 1",
  134. #"0x31 *General Purpose 2",
  135. #"0x32 *General Purpose 3",
  136. #"0x33 *General Purpose 4",
  137. #"0x34 *(Undefined)",
  138. #"0x35 *(Undefined)",
  139. #"0x36 *(Undefined)",
  140. #"0x37 *(Undefined)",
  141. #"0x38 *(Undefined)",
  142. #"0x39 *(Undefined)",
  143. #"0x3A *(Undefined)",
  144. #"0x3B *(Undefined)",
  145. #"0x3C *(Undefined)",
  146. #"0x3D *(Undefined)",
  147. #"0x3E *(Undefined)",
  148. #"0x3F *(Undefined)",
  149. #"0x40 Damper On/Off", # <63 off, >64 on
  150. #"0x41 Portamento On/Off", # <63 off, >64 on
  151. #"0x42 Sostenuto On/Off", # <63 off, >64 on
  152. #"0x43 Soft Pedal On/Off", # <63 off, >64 on
  153. #"0x44 Legato Footswitch", # <63 Normal, >64 Legato
  154. #"0x45 Hold 2", # <63 off, >64 on
  155. "0x46 Control 1 [Variation]",
  156. "0x47 Control 2 [Timbre]",
  157. "0x48 Control 3 [Release]",
  158. "0x49 Control 4 [Attack]",
  159. "0x4A Control 5 [Brightness]",
  160. "0x4B Control 6 [Decay]",
  161. "0x4C Control 7 [Vib Rate]",
  162. "0x4D Control 8 [Vib Depth]",
  163. "0x4E Control 9 [Vib Delay]",
  164. "0x4F Control 10 [Undefined]",
  165. "0x50 General Purpose 5",
  166. "0x51 General Purpose 6",
  167. "0x52 General Purpose 8",
  168. "0x53 General Purpose 9",
  169. "0x54 Portamento Control",
  170. "0x5B FX 1 Depth [Reverb]",
  171. "0x5C FX 2 Depth [Tremolo]",
  172. "0x5D FX 3 Depth [Chorus]",
  173. "0x5E FX 4 Depth [Detune]",
  174. "0x5F FX 5 Depth [Phaser]"
  175. )
  176. # ------------------------------------------------------------------------------------------------------------
  177. # Remove/convert non-ascii chars from a string
  178. def unicode2ascii(string):
  179. return normalize('NFKD', string).encode("ascii", "ignore").decode("utf-8")
  180. # Convert a ctypes c_char_p to a python string
  181. def cString(value):
  182. if value:
  183. return value.decode("utf-8", errors="ignore")
  184. else:
  185. return ""
  186. # Check if a value is a number (float support)
  187. def isNumber(value):
  188. try:
  189. float(value)
  190. return True
  191. except:
  192. return False
  193. # Convert a value to a list
  194. def toList(value):
  195. if value is None:
  196. return []
  197. elif not isinstance(value, list):
  198. return [value]
  199. else:
  200. return value
  201. # QLineEdit and QPushButton combo
  202. def getAndSetPath(self_, currentPath, lineEdit):
  203. newPath = QFileDialog.getExistingDirectory(self_, self_.tr("Set Path"), currentPath, QFileDialog.ShowDirsOnly)
  204. if newPath:
  205. lineEdit.setText(newPath)
  206. return newPath
  207. # Get Icon from user theme, using our own as backup (Oxygen)
  208. def getIcon(icon, size=16):
  209. return QIcon.fromTheme(icon, QIcon(":/%ix%i/%s.png" % (size, size, icon)))
  210. # Custom MessageBox
  211. def CustomMessageBox(self_, icon, title, text, extraText="", buttons=QMessageBox.Yes|QMessageBox.No, defButton=QMessageBox.No):
  212. msgBox = QMessageBox(self_)
  213. msgBox.setIcon(icon)
  214. msgBox.setWindowTitle(title)
  215. msgBox.setText(text)
  216. msgBox.setInformativeText(extraText)
  217. msgBox.setStandardButtons(buttons)
  218. msgBox.setDefaultButton(defButton)
  219. return msgBox.exec_()
  220. # ------------------------------------------------------------------------------------------------------------
  221. # signal handler for unix systems
  222. def set_up_signals(self_):
  223. if haveSignal:
  224. global x_gui
  225. x_gui = self_
  226. signal(SIGINT, signal_handler)
  227. signal(SIGTERM, signal_handler)
  228. signal(SIGUSR1, signal_handler)
  229. signal(SIGUSR2, signal_handler)
  230. x_gui.connect(x_gui, SIGNAL("SIGUSR2()"), lambda gui=x_gui: showWindow(gui))
  231. x_gui.connect(x_gui, SIGNAL("SIGTERM()"), SLOT("close()"))
  232. def signal_handler(sig, frame):
  233. global x_gui
  234. if sig in (SIGINT, SIGTERM):
  235. x_gui.emit(SIGNAL("SIGTERM()"))
  236. elif sig == SIGUSR1:
  237. x_gui.emit(SIGNAL("SIGUSR1()"))
  238. elif sig == SIGUSR2:
  239. x_gui.emit(SIGNAL("SIGUSR2()"))
  240. def showWindow(self_):
  241. if self_.isMaximized():
  242. self_.showMaximized()
  243. else:
  244. self_.showNormal()
  245. # ------------------------------------------------------------------------------------------------------------
  246. # Shared Icons
  247. def setIcons(self_, modes):
  248. if "canvas" in modes:
  249. self_.act_canvas_arrange.setIcon(getIcon("view-sort-ascending"))
  250. self_.act_canvas_refresh.setIcon(getIcon("view-refresh"))
  251. self_.act_canvas_zoom_fit.setIcon(getIcon("zoom-fit-best"))
  252. self_.act_canvas_zoom_in.setIcon(getIcon("zoom-in"))
  253. self_.act_canvas_zoom_out.setIcon(getIcon("zoom-out"))
  254. self_.act_canvas_zoom_100.setIcon(getIcon("zoom-original"))
  255. self_.act_canvas_print.setIcon(getIcon("document-print"))
  256. self_.b_canvas_zoom_fit.setIcon(getIcon("zoom-fit-best"))
  257. self_.b_canvas_zoom_in.setIcon(getIcon("zoom-in"))
  258. self_.b_canvas_zoom_out.setIcon(getIcon("zoom-out"))
  259. self_.b_canvas_zoom_100.setIcon(getIcon("zoom-original"))
  260. if "jack" in modes:
  261. self_.act_jack_clear_xruns.setIcon(getIcon("edit-clear"))
  262. self_.act_jack_configure.setIcon(getIcon("configure"))
  263. self_.act_jack_render.setIcon(getIcon("media-record"))
  264. self_.b_jack_clear_xruns.setIcon(getIcon("edit-clear"))
  265. self_.b_jack_configure.setIcon(getIcon("configure"))
  266. self_.b_jack_render.setIcon(getIcon("media-record"))
  267. if "transport" in modes:
  268. self_.act_transport_play.setIcon(getIcon("media-playback-start"))
  269. self_.act_transport_stop.setIcon(getIcon("media-playback-stop"))
  270. self_.act_transport_backwards.setIcon(getIcon("media-seek-backward"))
  271. self_.act_transport_forwards.setIcon(getIcon("media-seek-forward"))
  272. self_.b_transport_play.setIcon(getIcon("media-playback-start"))
  273. self_.b_transport_stop.setIcon(getIcon("media-playback-stop"))
  274. self_.b_transport_backwards.setIcon(getIcon("media-seek-backward"))
  275. self_.b_transport_forwards.setIcon(getIcon("media-seek-forward"))
  276. if "misc" in modes:
  277. self_.act_quit.setIcon(getIcon("application-exit"))
  278. self_.act_configure.setIcon(getIcon("configure"))