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.

280 lines
8.2KB

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