hardware.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Special URI for non-addressed controls
  4. var kNullAddressURI = "null"
  5. // Special URIs for midi-learn
  6. var kMidiLearnURI = "/midi-learn"
  7. var kMidiUnlearnURI = "/midi-unlearn"
  8. var kMidiCustomPrefixURI = "/midi-custom_" // to show current one, ignored on save
  9. // URI for BPM sync (for non-addressed control ports)
  10. var kBpmURI ="/bpm"
  11. // Grouped options
  12. var deviceOption = "/hmi"
  13. var ccOption = "/cc"
  14. var cvOption = "/cv"
  15. // Port types supported by cv addressing
  16. var cvModes = ":float:integer:bypass:toggled:"
  17. // use pitchbend as midi cc, with an invalid MIDI controller number
  18. var MIDI_PITCHBEND_AS_CC = 131
  19. function create_midi_cc_uri (channel, controller) {
  20. if (controller == MIDI_PITCHBEND_AS_CC) {
  21. return sprintf("%sCh.%d_Pbend", kMidiCustomPrefixURI, channel+1)
  22. }
  23. return sprintf("%sCh.%d_CC#%d", kMidiCustomPrefixURI, channel+1, controller)
  24. }
  25. function startsWith (value, pattern) {
  26. return value != null && value.indexOf(pattern) === 0;
  27. };
  28. function is_control_chain_uri (uri) {
  29. if (startsWith(uri, deviceOption)) {
  30. return false;
  31. }
  32. if (uri == kMidiLearnURI || startsWith(uri, kMidiCustomPrefixURI)) {
  33. return false;
  34. }
  35. if (isCvUri(uri)) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. function isCvUri (uri) {
  41. if (startsWith(uri, cvOption)) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. function isHwCvUri (uri) {
  47. if (startsWith(uri, cvOption + '/graph/cv_')) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. // Units supported for tap tempo (lowercase)
  53. var kTapTempoUnits = ['bpm']
  54. function HardwareManager(options) {
  55. var self = this
  56. options = $.extend({
  57. // This is the function that will actually make the addressing
  58. address: function (instanceAndSymbol, addressing, callback) { callback(true) },
  59. // Callback to enable or disable a control in GUI
  60. setEnabled: function (instance, portSymbol, enabled, feedback, momentaryMode) {},
  61. // Renders the address html template
  62. renderForm: function (instance, port) {},
  63. // Running as mod-app
  64. isApp: function () { return false },
  65. }, options)
  66. this.beatsPerMinutePort = {
  67. ranges: { // XXX would be good to have a centralized place for this data, currently it's also in transport.js and others
  68. minimum: 20.0,
  69. maximum: 280.0
  70. },
  71. value: null
  72. }
  73. this.cvOutputPorts = []
  74. this.setBeatsPerMinuteValue = function (bpm) {
  75. if (self.beatsPerMinutePort.value === bpm) {
  76. return
  77. }
  78. self.beatsPerMinutePort.value = bpm
  79. }
  80. this.reset = function () {
  81. var addressingsByActuator = $.extend({}, self.addressingsByActuator)
  82. var cvOutputPorts = self.cvOutputPorts.slice()
  83. /* All adressings indexed by actuator
  84. key : "/actuator-uri"
  85. value: list("/instance/symbol")
  86. */
  87. self.addressingsByActuator = {}
  88. self.cvOutputPorts = []
  89. if (cvOutputPorts) {
  90. for (var i = 0; i < cvOutputPorts.length; i++) {
  91. // if hw cv port, keep it
  92. if (isHwCvUri(cvOutputPorts[i].uri)) {
  93. self.cvOutputPorts.push(cvOutputPorts[i])
  94. }
  95. }
  96. }
  97. if (addressingsByActuator) {
  98. for (var act in addressingsByActuator) {
  99. if (isCvUri(act) && cvOutputPorts.find(function (port) { return port.uri === act })) {
  100. self.addressingsByActuator[act] = []
  101. }
  102. }
  103. }
  104. /* All addressings indexed by instance + port symbol
  105. key : "/instance/symbol"
  106. value: "/actuator-uri"
  107. */
  108. self.addressingsByPortSymbol = {}
  109. /* Saved addressing data
  110. key : "/instance/symbol"
  111. value: dict(AddressData)
  112. */
  113. self.addressingsData = {}
  114. // Initializes actuators
  115. if (HARDWARE_PROFILE) {
  116. var uri
  117. for (var i in HARDWARE_PROFILE) {
  118. uri = HARDWARE_PROFILE[i].uri
  119. self.addressingsByActuator[uri] = []
  120. }
  121. }
  122. self.addressingsByActuator[kMidiLearnURI] = []
  123. self.addressingsByActuator[kBpmURI] = []
  124. }
  125. this.reset()
  126. // Get all addressing types that can be used for a port
  127. // Most of these are 1:1 match to LV2 hints, but we have extra details.
  128. this.availableAddressingTypes = function (port, tempo) {
  129. if (tempo) {
  130. return ["enumeration"]
  131. }
  132. var properties = port.properties
  133. var available = []
  134. // prevent some properties from going together
  135. if (properties.indexOf("trigger") >= 0) {
  136. available.push("trigger")
  137. } else if (properties.indexOf("enumeration") >= 0) {
  138. available.push("enumeration")
  139. } else if (properties.indexOf("toggled") >= 0) {
  140. available.push("toggled")
  141. } else if (properties.indexOf("integer") >= 0) {
  142. available.push("integer")
  143. } else {
  144. available.push("float")
  145. }
  146. if (properties.indexOf("logarithmic") >= 0)
  147. available.push("logarithmic")
  148. if (port.symbol === ":bpm" && properties.indexOf("tapTempo") >= 0 && kTapTempoUnits.indexOf(port.units.symbol.toLowerCase()) >= 0)
  149. available.push("taptempo")
  150. if (port.scalePoints.length >= 2)
  151. available.push("scalepoints")
  152. if (port.symbol == ":bypass")
  153. available.push("bypass")
  154. return available
  155. }
  156. this.availableActuatorsWithModes = function (list, types) {
  157. var available = {}
  158. if (list) {
  159. for (var i in list) {
  160. actuator = list[i]
  161. modes = actuator.modes
  162. // usedAddressings = self.addressingsByActuator[actuator.uri]
  163. // if (ADDRESSING_PAGES == 0 && usedAddressings.length >= actuator.max_assigns && usedAddressings.indexOf(key) < 0) {
  164. // continue
  165. // }
  166. if (
  167. (types.indexOf("integer" ) >= 0 && modes.search(":integer:" ) >= 0) ||
  168. (types.indexOf("float" ) >= 0 && modes.search(":float:" ) >= 0) ||
  169. (types.indexOf("enumeration") >= 0 && modes.search(":enumeration:") >= 0) ||
  170. (types.indexOf("logarithmic") >= 0 && modes.search(":logarithmic:") >= 0) ||
  171. (types.indexOf("toggled" ) >= 0 && modes.search(":toggled:" ) >= 0) ||
  172. (types.indexOf("trigger" ) >= 0 && modes.search(":trigger:" ) >= 0) ||
  173. (types.indexOf("taptempo" ) >= 0 && modes.search(":taptempo:" ) >= 0) ||
  174. (types.indexOf("scalepoints") >= 0 && modes.search(":scalepoints:") >= 0) ||
  175. (types.indexOf("bypass" ) >= 0 && modes.search(":bypass:" ) >= 0)
  176. )
  177. {
  178. available[actuator.uri] = actuator
  179. }
  180. }
  181. }
  182. return available
  183. }
  184. this.isCvAvailable = function (port) {
  185. var defaultTypes = self.availableAddressingTypes(port, false)
  186. var available = self.availableActuatorsWithModes([{ uri: cvOption, modes: cvModes }], defaultTypes)
  187. return available.hasOwnProperty(cvOption)
  188. }
  189. // Gets a list of available actuators for a port
  190. this.availableActuators = function (instance, port, tempo) {
  191. var key = instance+"/"+port.symbol
  192. var defaultTypes = self.availableAddressingTypes(port, false)
  193. var types = tempo ? self.availableAddressingTypes(port, tempo) : defaultTypes
  194. var available = self.availableActuatorsWithModes(HARDWARE_PROFILE, types)
  195. // midi-learn is always available, except for enumeration
  196. if (defaultTypes.indexOf("enumeration") < 0 || port.scalePoints.length == 2)
  197. {
  198. available[kMidiLearnURI] = {
  199. uri : kMidiLearnURI,
  200. name : "MIDI Learn...",
  201. modes: ":float:trigger:bypass:integer:toggled:",
  202. steps: [],
  203. max_assigns: 99
  204. }
  205. }
  206. available = $.extend(self.availableActuatorsWithModes(self.cvOutputPorts, defaultTypes), available)
  207. return available
  208. }
  209. this.buildDividerOptions = function (select, port, curDividers) {
  210. select.children().remove()
  211. var filteredDividers = getDividerOptions(port, self.beatsPerMinutePort.ranges.minimum, self.beatsPerMinutePort.ranges.maximum)
  212. // And build html select options
  213. for (i = 0; i < filteredDividers.length; i++) {
  214. $('<option>').attr('value', filteredDividers[i].value).html(filteredDividers[i].label).appendTo(select)
  215. }
  216. // Select previously saved divider or set first divider as default
  217. if (filteredDividers.length > 0) {
  218. var def = (curDividers !== null && curDividers !== undefined) ? curDividers : filteredDividers[0].value
  219. select.val(def)
  220. }
  221. return filteredDividers
  222. }
  223. this.buildSensitivityOptions = function (select, port, actuatorSteps, curStep) {
  224. select.children().remove()
  225. if (port.properties.indexOf("enumeration") >= 0 ||
  226. port.properties.indexOf("integer") >= 0 ||
  227. port.properties.indexOf("toggled") >= 0 ||
  228. port.properties.indexOf("trigger") >= 0)
  229. {
  230. var value
  231. if (port.properties.indexOf("enumeration") >= 0) {
  232. value = port.scalePoints.length - 1
  233. } else if (port.properties.indexOf("integer") >= 0) {
  234. value = port.ranges.maximum - port.ranges.minimum
  235. } else {
  236. value = 1
  237. }
  238. $('<option value='+value+'>').appendTo(select)
  239. select.val(value)
  240. select.hide()
  241. if (port.symbol != ":bypass" && port.symbol != ":presets") {
  242. select.parent().parent().hide()
  243. }
  244. return
  245. }
  246. var def, soptions = {}
  247. switch ((actuatorSteps ? actuatorSteps.length : null))
  248. {
  249. case 1:
  250. def = actuatorSteps[0]
  251. soptions[def] = 'Default'
  252. break
  253. case 2:
  254. def = actuatorSteps[0]
  255. soptions[actuatorSteps[0]] = 'Medium'
  256. soptions[actuatorSteps[1]] = 'High'
  257. break
  258. case 3:
  259. def = actuatorSteps[1]
  260. soptions[actuatorSteps[0]] = 'Low'
  261. soptions[actuatorSteps[1]] = 'Medium'
  262. soptions[actuatorSteps[2]] = 'High'
  263. break
  264. default:
  265. def = 33
  266. soptions = {
  267. 17: 'Low',
  268. 33: 'Medium',
  269. 65: 'High',
  270. }
  271. break
  272. }
  273. if (port.rangeSteps) {
  274. def = port.rangeSteps
  275. soptions[def] = 'Default'
  276. }
  277. var steps, label, keys = Object.keys(soptions).sort()
  278. for (var i in keys) {
  279. steps = keys[i]
  280. label = soptions[steps]
  281. label += ' (' + steps + ' steps)'
  282. $('<option>').attr('value', steps).html(label).appendTo(select)
  283. }
  284. select.val(curStep != null ? curStep : def)
  285. if (keys.length === 1) {
  286. select.parent().parent().hide()
  287. }
  288. }
  289. this.disableMinMaxSteps = function (form, disabled) {
  290. form.find('select[name=steps]').prop('disabled', disabled)
  291. form.find('input[name=min]').prop('disabled', disabled)
  292. form.find('input[name=max]').prop('disabled', disabled)
  293. }
  294. this.portSupportsSensitivity = function(port) {
  295. if (port.properties.indexOf("integer") >= 0)
  296. return false;
  297. if (port.properties.indexOf("toggled") >= 0)
  298. return false;
  299. if (port.properties.indexOf("trigger") >= 0)
  300. return false;
  301. if (port.symbol == ":bypass")
  302. return false;
  303. if (port.symbol == ":presets")
  304. return false;
  305. return true;
  306. }
  307. this.toggleAdvancedItemsVisibility = function (port,
  308. sensitivity, ledColourMode, momentarySwMode,
  309. currentActuator, curStep) {
  310. if (currentActuator && currentActuator.steps.length !== 0 && this.portSupportsSensitivity(port)) {
  311. sensitivity.removeClass('disabled').parent().parent().show()
  312. } else {
  313. sensitivity.addClass('disabled').parent().parent().hide()
  314. }
  315. if (currentActuator && currentActuator.modes.indexOf(":colouredlist:") >= 0 &&
  316. port.properties.indexOf("enumeration") >= 0)
  317. {
  318. ledColourMode.removeClass('disabled').parent().parent().show()
  319. }
  320. else
  321. {
  322. ledColourMode.addClass('disabled').parent().parent().hide()
  323. }
  324. if (currentActuator && currentActuator.modes.indexOf(":momentarytoggle:") >= 0 &&
  325. port.properties.indexOf("enumeration") < 0 &&
  326. port.properties.indexOf("tapTempo") < 0 &&
  327. port.properties.indexOf("trigger") < 0)
  328. {
  329. momentarySwMode.removeClass('disabled').parent().parent().show()
  330. }
  331. else
  332. {
  333. momentarySwMode.addClass('disabled').parent().parent().hide()
  334. }
  335. self.buildSensitivityOptions(sensitivity,
  336. port,
  337. currentActuator ? currentActuator.steps : null,
  338. curStep)
  339. }
  340. // Show dynamic field content based on selected type of addressing
  341. this.showDynamicField = function (form, typeInputVal, currentAddressing, port, cvUri, firstOpen) {
  342. // Hide all then show the relevant content
  343. form.find('.dynamic-field').hide()
  344. // Hide led-color and momentary modes, only usable for a few selections
  345. // These are enabled by various event triggers below as needed
  346. form.find('select[name=led-color-mode]').addClass('disabled').parent().parent().hide()
  347. form.find('select[name=momentary-sw-mode]').addClass('disabled').parent().parent().hide()
  348. if (typeInputVal === kMidiLearnURI)
  349. {
  350. form.find('.midi-learn-hint').show()
  351. if (currentAddressing && currentAddressing.uri && currentAddressing.uri.lastIndexOf(kMidiCustomPrefixURI, 0) === 0) {
  352. form.find('.midi-learn-hint').hide()
  353. var midiCustomLabel = "MIDI " + currentAddressing.uri.replace(kMidiCustomPrefixURI,"").replace(/_/g," ")
  354. form.find('.midi-custom-uri').text(midiCustomLabel)
  355. form.find('.midi-learn-custom').show()
  356. }
  357. }
  358. else if (typeInputVal === deviceOption)
  359. {
  360. form.find('.device-table').find('.selected').click()
  361. form.find('.device-table').show()
  362. }
  363. else if (typeInputVal === ccOption)
  364. {
  365. var ccActuatorSelect = form.find('select[name=cc-actuator]')
  366. if (ccActuatorSelect.children('option').length) {
  367. ccActuatorSelect.change()
  368. form.find('.cc-select').show()
  369. } else if (self.hasControlChainDevice()) {
  370. form.find('.cc-in-use').show()
  371. } else {
  372. form.find('.no-cc').show()
  373. }
  374. }
  375. else if (typeInputVal === cvOption)
  376. {
  377. if (self.cvOutputPorts.length) {
  378. form.find('.cv-select').show()
  379. } else {
  380. form.find('.no-cv').show()
  381. }
  382. }
  383. // Disabled/Enable save button
  384. if (currentAddressing && currentAddressing.uri) {
  385. if (typeInputVal === ccOption && !self.hasControlChainDevice() ||
  386. (typeInputVal === cvOption && !self.cvOutputPorts.length)) {
  387. form.find('.js-save').addClass('disabled')
  388. } else {
  389. form.find('.js-save').removeClass('disabled')
  390. }
  391. } else {
  392. if ((!form.find('input[name=tempo]').prop("checked") && typeInputVal === kNullAddressURI) ||
  393. (typeInputVal === ccOption && !self.hasControlChainDevice()) ||
  394. (typeInputVal === cvOption && !self.cvOutputPorts.length)) {
  395. form.find('.js-save').addClass('disabled')
  396. } else {
  397. form.find('.js-save').removeClass('disabled')
  398. }
  399. }
  400. // Hide/show extended specific content
  401. if (typeInputVal === kNullAddressURI ||
  402. typeInputVal === kMidiLearnURI || typeInputVal.lastIndexOf(kMidiCustomPrefixURI, 0) === 0 ||
  403. (typeInputVal === ccOption && !self.hasControlChainDevice()) ||
  404. typeInputVal === cvOption ||
  405. ! this.portSupportsSensitivity(port))
  406. {
  407. form.find('.sensitivity').css({ display: "none" })
  408. self.disableMinMaxSteps(form, false)
  409. }
  410. else
  411. {
  412. form.find('.sensitivity').css({ display: "block" })
  413. }
  414. if (typeInputVal === kMidiLearnURI || typeInputVal.lastIndexOf(kMidiCustomPrefixURI, 0) === 0 || typeInputVal === ccOption || typeInputVal === cvOption)
  415. {
  416. form.find('.tempo').css({ display: "none" })
  417. }
  418. else if (hasTempoRelatedDynamicScalePoints(port))
  419. {
  420. form.find('.tempo').css({ display: "block" })
  421. if (form.find('input[name=tempo]').prop("checked")) {
  422. self.disableMinMaxSteps(form, true)
  423. }
  424. }
  425. // Hide/show cv operational mode for everything except CV plugin ports
  426. if (typeInputVal !== cvOption || isHwCvUri(cvUri)) {
  427. form.find('.cv-op-mode').css({ display: "none" })
  428. } else {
  429. form.find('.cv-op-mode').css({ display: "block" })
  430. }
  431. // Set unipolar mode based on default cv port ranges or current addressing
  432. if (typeInputVal === cvOption) {
  433. var cvPort = self.cvOutputPorts.find(function (port) { return port.uri === cvUri })
  434. if (cvPort) {
  435. var operationalMode = cvPort.defaultOperationalMode
  436. if (firstOpen && currentAddressing && currentAddressing.uri &&
  437. isCvUri(currentAddressing.uri) && currentAddressing.operationalMode)
  438. {
  439. operationalMode = currentAddressing.operationalMode
  440. }
  441. form.find('select[name=cv-op-mode]').val(operationalMode)
  442. }
  443. }
  444. }
  445. this.buildDeviceTable = function (deviceTable, currentAddressing, actuators,
  446. hmiPageInput, hmiSubPageInput, hmiUriInput,
  447. sensitivity, ledColourMode, momentarySwMode, port) {
  448. var table = $('<table/>').addClass('hmi-table')
  449. var row, cell, ctable, uri, uriAddressings, usedAddressings, addressing
  450. var actuator, actuatorName, actuatorSubPages, groupActuator, groupAddressings, lastGroupName, subpageTables = {}
  451. if (ADDRESSING_PAGES > 0)
  452. {
  453. // build header row
  454. var headerRow = $('<tr/>')
  455. for (var i = 1; i <= ADDRESSING_PAGES; i++) {
  456. headerRow.append($('<th>Page '+i+'</th>'))
  457. }
  458. table.append(headerRow)
  459. for (var actuatorUri in actuators) {
  460. if (!startsWith(actuatorUri, deviceOption)) {
  461. continue
  462. }
  463. actuator = actuators[actuatorUri]
  464. actuatorSubPages = actuator.subpages || [null]
  465. usedAddressings = self.addressingsByActuator[actuatorUri]
  466. // pre-create groups for subpages
  467. if (actuator.subpages) {
  468. for (var i in actuator.subpages) {
  469. lastGroupName = actuator.subpages[i]
  470. if (!subpageTables[lastGroupName]) {
  471. deviceTable.append(table)
  472. deviceTable.append($('<div class="group-strike">'+ lastGroupName +'</div>'))
  473. table = subpageTables[lastGroupName] = $('<table/>').addClass('hmi-table')
  474. } else {
  475. table = subpageTables[lastGroupName]
  476. }
  477. }
  478. ctable = null
  479. lastGroupName = null
  480. // actuator belongs to a new group (compared to last one)
  481. } else if (actuator.group && actuator.group != lastGroupName) {
  482. deviceTable.append(table)
  483. deviceTable.append($('<div class="group-strike">'+ actuator.group +'</div>'))
  484. ctable = table = $('<table/>').addClass('hmi-table')
  485. lastGroupName = actuator.group
  486. // there was a group before, but not anymore, so create a "no-group" group
  487. } else if (lastGroupName && !actuator.group) {
  488. deviceTable.append(table)
  489. deviceTable.append($('<div class="group-strike">No Group</div>'))
  490. ctable = table = $('<table/>').addClass('hmi-table')
  491. lastGroupName = null
  492. // no groups ever in use, just act normal
  493. } else {
  494. ctable = table
  495. }
  496. for (var actSubPage = 0; actSubPage < actuatorSubPages.length; actSubPage++) {
  497. row = $('<tr/>')
  498. if (actuator.subpages) {
  499. ctable = subpageTables[actuatorSubPages[actSubPage]]
  500. }
  501. for (var addrPage = 0; addrPage < ADDRESSING_PAGES; addrPage++) {
  502. actuatorName = lastGroupName ? (actuator.gname || actuator.name) : actuator.name
  503. cell = $('<td data-page="'+ addrPage +'" data-subpage="'+ actSubPage +'" data-uri="'+ actuatorUri +'">'+ actuatorName +'</td>')
  504. if (currentAddressing &&
  505. currentAddressing.uri == actuatorUri &&
  506. currentAddressing.page == addrPage &&
  507. (currentAddressing.subpage == null || currentAddressing.subpage == actSubPage)) {
  508. hmiPageInput.val(currentAddressing.page)
  509. hmiSubPageInput.val(currentAddressing.subpage)
  510. hmiUriInput.val(currentAddressing.uri)
  511. cell.addClass('selected')
  512. } else {
  513. // Only allow actuator groups to be used when all their "child" actuators are not in use on current page
  514. if (actuator.actuator_group) {
  515. for (var i = 0; i < actuator.actuator_group.length; i++) {
  516. uri = actuator.actuator_group[i]
  517. uriAddressings = self.addressingsByActuator[uri]
  518. for (var j in uriAddressings) {
  519. instance = uriAddressings[j]
  520. addressing = self.addressingsData[instance]
  521. if (addressing.page == addrPage) {
  522. cell.addClass('disabled')
  523. }
  524. }
  525. }
  526. }
  527. // Check if page+uri already assigned, then disable cell
  528. for (var i in usedAddressings) {
  529. instance = usedAddressings[i]
  530. addressing = self.addressingsData[instance]
  531. if (addressing.page == addrPage &&
  532. (addressing.subpage == null || addressing.subpage == actSubPage)) {
  533. cell.addClass('disabled')
  534. }
  535. }
  536. }
  537. row.append(cell)
  538. }
  539. ctable.append(row)
  540. }
  541. }
  542. }
  543. else
  544. {
  545. for (var actuatorUri in actuators) {
  546. if (!startsWith(actuatorUri, deviceOption)) {
  547. continue
  548. }
  549. actuator = actuators[actuatorUri]
  550. usedAddressings = self.addressingsByActuator[actuatorUri]
  551. if (actuator.actuator_group && actuator.group && actuator.group != lastGroupName) {
  552. deviceTable.append(table)
  553. deviceTable.append($('<div class="group-strike">'+ actuator.group +'</div>'))
  554. table = $('<table/>').addClass('hmi-table')
  555. lastGroupName = actuator.group
  556. }
  557. row = $('<tr/>')
  558. cell = $('<td data-uri="'+ actuatorUri +'">'+ actuator.name+'</td>')
  559. if (currentAddressing && currentAddressing.uri == actuatorUri) {
  560. hmiUriInput.val(currentAddressing.uri)
  561. cell.addClass('selected')
  562. } else {
  563. // Only allow actuator groups to be used when all their "child" actuators are not in use
  564. if (actuator.actuator_group) {
  565. for (i = 0; i < actuator.actuator_group.length; i++) {
  566. uri = actuator.actuator_group[i]
  567. uriAddressings = self.addressingsByActuator[uri]
  568. if (uriAddressings.length) {
  569. cell.addClass('disabled')
  570. }
  571. }
  572. }
  573. if (usedAddressings.length >= actuator.max_assigns) {
  574. cell.addClass('disabled')
  575. }
  576. }
  577. row.append(cell)
  578. table.append(row)
  579. }
  580. }
  581. deviceTable.append(table)
  582. // when addressing an actuator group, all "child" actuators or intersecting actuator groups are no longer
  583. // available to be addressed to anything else except on different pages
  584. if (ADDRESSING_PAGES > 0)
  585. {
  586. for (var i in HARDWARE_PROFILE) {
  587. if (HARDWARE_PROFILE[i].actuator_group) {
  588. groupActuator = HARDWARE_PROFILE[i]
  589. for (var j in self.addressingsByActuator[groupActuator.uri]) {
  590. instance = self.addressingsByActuator[groupActuator.uri][j]
  591. groupAddressings = self.addressingsData[instance]
  592. for (var k in groupActuator.actuator_group) {
  593. deviceTable.find('[data-uri="' + groupActuator.actuator_group[k] + '"][data-page="' + groupAddressings.page + '"]').addClass('disabled')
  594. for (var l = 0 in actuators) {
  595. if (l !== groupActuator.uri && actuators[l].actuator_group && actuators[l].actuator_group.includes(groupActuator.actuator_group[k])) {
  596. deviceTable.find('[data-uri="' + l + '"][data-page="' + groupAddressings.page + '"]').addClass('disabled')
  597. }
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. else
  605. {
  606. for (var i in HARDWARE_PROFILE) {
  607. if (HARDWARE_PROFILE[i].actuator_group) {
  608. groupActuator = HARDWARE_PROFILE[i]
  609. if (self.addressingsByActuator[groupActuator.uri].length) {
  610. for (var j in groupActuator.actuator_group) {
  611. deviceTable.find('[data-uri="' + groupActuator.actuator_group[j] + '"]').addClass('disabled')
  612. }
  613. }
  614. }
  615. }
  616. }
  617. deviceTable.find('td').click(function () {
  618. if ($(this).hasClass('disabled')) {
  619. return
  620. }
  621. var actuatorUri = $(this).attr('data-uri')
  622. var page = $(this).attr('data-page')
  623. var subpage = $(this).attr('data-subpage')
  624. // Update hidden inputs value
  625. hmiPageInput.val(page)
  626. hmiSubPageInput.val(subpage)
  627. hmiUriInput.val(actuatorUri)
  628. // Remove 'selected' class to all cells then add it to the clicked one
  629. deviceTable.find('td').removeClass('selected')
  630. $(this).addClass('selected')
  631. self.toggleAdvancedItemsVisibility(port,
  632. sensitivity, ledColourMode, momentarySwMode,
  633. actuators[actuatorUri],
  634. currentAddressing.uri === actuatorUri ? currentAddressing.steps : null)
  635. })
  636. self.toggleAdvancedItemsVisibility(port,
  637. sensitivity, ledColourMode, momentarySwMode,
  638. actuators[currentAddressing.uri], currentAddressing.steps)
  639. }
  640. this.addOption = function (addressings, actuator, currentAddressing, select) {
  641. var addressedToMe = currentAddressing.uri && currentAddressing.uri === actuator.uri
  642. if ((addressings && addressings.length < actuator.max_assigns) || addressedToMe) {
  643. $('<option>').attr('value', actuator.uri).text(actuator.name).appendTo(select)
  644. if (addressedToMe) {
  645. select.val(currentAddressing.uri)
  646. }
  647. }
  648. }
  649. // Opens an addressing window to address this a port
  650. this.open = function (instance, port, pluginLabel) {
  651. var instanceAndSymbol = instance+"/"+port.symbol
  652. var currentAddressing = self.addressingsData[instanceAndSymbol] || {}
  653. // Renders the window
  654. var form = $(options.renderForm(instance, port))
  655. var typeSelect = form.find('select[name=type]')
  656. var typeInput = form.find('input[name=type]')
  657. var hmiPageInput = form.find('input[name=hmi-page]')
  658. var hmiSubPageInput = form.find('input[name=hmi-subpage]')
  659. var hmiUriInput = form.find('input[name=hmi-uri]')
  660. var deviceTable = form.find('.device-table')
  661. var sensitivity = form.find('select[name=steps]')
  662. var ledColourMode = form.find('select[name=led-color-mode]')
  663. var momentarySwMode = form.find('select[name=momentary-sw-mode]')
  664. var operationalMode = form.find('select[name=cv-op-mode]')
  665. // Create selectable buttons to choose addressings type and show relevant dynamic content
  666. var typeInputVal = kNullAddressURI
  667. if (currentAddressing && currentAddressing.uri)
  668. {
  669. if (currentAddressing.uri == kMidiLearnURI || currentAddressing.uri.lastIndexOf(kMidiCustomPrefixURI, 0) === 0) {
  670. typeInputVal = kMidiLearnURI
  671. } else if (startsWith(currentAddressing.uri, deviceOption)) {
  672. typeInputVal = deviceOption
  673. } else if (startsWith(currentAddressing.uri, cvOption)) {
  674. typeInputVal = cvOption
  675. } else if (currentAddressing.uri !== kBpmURI){
  676. typeInputVal = ccOption
  677. }
  678. // restore values
  679. ledColourMode.val(currentAddressing.coloured ? 1 : 0)
  680. momentarySwMode.val(currentAddressing.momentary || 0)
  681. }
  682. else
  683. {
  684. // If there is no addressing made yet, try to set some good defaults
  685. ledColourMode.val(port.properties.indexOf("preferColouredListByDefault") >= 0 ? 1 : 0)
  686. if (port.properties.indexOf("preferMomentaryOffByDefault") >= 0) {
  687. momentarySwMode.val(2)
  688. } else if (port.properties.indexOf("preferMomentaryOnByDefault") >= 0) {
  689. momentarySwMode.val(1)
  690. } else {
  691. momentarySwMode.val(0)
  692. }
  693. }
  694. typeInput.val(typeInputVal)
  695. var actuators = self.availableActuators(instance, port, currentAddressing.tempo)
  696. var typeOptions = [kNullAddressURI, deviceOption, kMidiLearnURI, ccOption, cvOption]
  697. var i = 0
  698. typeSelect.find('option').unwrap().each(function() {
  699. var btn = $('<div class="btn js-type" data-value="'+typeOptions[i]+'">'+$(this).text()+'</div>');
  700. var jbtn = $(btn);
  701. if(jbtn.attr('data-value') == typeInput.val()) {
  702. btn.addClass('selected')
  703. }
  704. // Device and Control Chain need MOD hardware. Under MOD Desktop the
  705. // tab stays, marked locked: selecting it shows the hardware card
  706. // instead of addressing options (see the click handler below).
  707. if (options.isApp() && (jbtn.attr('data-value') === deviceOption || jbtn.attr('data-value') === ccOption)) {
  708. jbtn.addClass('desktop-app-locked')
  709. }
  710. // Hide MIDI tab if not available
  711. else if (jbtn.attr('data-value') === kMidiLearnURI && !actuators[kMidiLearnURI]) {
  712. jbtn.hide()
  713. }
  714. // Hide CV tab if not available
  715. else if (jbtn.attr('data-value') === cvOption && !self.isCvAvailable(port)) {
  716. jbtn.hide()
  717. }
  718. $(this).replaceWith(btn)
  719. i++
  720. })
  721. // Add options to control chain and cv actuators select
  722. var actuator, addressings, ccUri, cvUri
  723. var ccActuatorSelect = form.find('select[name=cc-actuator]')
  724. var cvPortSelect = form.find('select[name=cv-port]')
  725. var cvActuators
  726. var ccActuators = []
  727. for (var uri in actuators) {
  728. ccUri = is_control_chain_uri(uri)
  729. cvUri = isCvUri(uri)
  730. if (!(cvUri || ccUri)) {
  731. continue
  732. }
  733. actuator = actuators[uri]
  734. addressings = self.addressingsByActuator[uri]
  735. if (ccUri) {
  736. ccActuators.push(actuator)
  737. self.addOption(addressings, actuator, currentAddressing, ccActuatorSelect)
  738. } else { // cvUri
  739. self.addOption(addressings, actuator, currentAddressing, cvPortSelect)
  740. }
  741. }
  742. if (ccActuators.length === 0) {
  743. ccActuatorSelect.hide()
  744. } else {
  745. ccActuatorSelect.change(function () {
  746. var actuatorUri = $(this).val()
  747. self.toggleAdvancedItemsVisibility(port,
  748. sensitivity, ledColourMode, momentarySwMode,
  749. actuators[actuatorUri],
  750. currentAddressing.uri === actuatorUri ? currentAddressing.steps : null)
  751. })
  752. }
  753. form.find('.js-type').click(function () {
  754. form.find('.js-type').removeClass('selected')
  755. $(this).addClass('selected')
  756. // MOD Desktop only (isApp): a locked tab shows the hardware card and
  757. // leaves typeInput alone, so nothing can be saved against it; any
  758. // other tab puts the options back. DesktopApp is only served there.
  759. if (options.isApp()) {
  760. if ($(this).hasClass('desktop-app-locked')) {
  761. DesktopApp.showAddressingUpsell(form)
  762. return
  763. }
  764. DesktopApp.hideAddressingUpsell(form)
  765. }
  766. typeInput.val($(this).attr('data-value'))
  767. self.showDynamicField(form, typeInput.val(), currentAddressing, port, cvPortSelect.val(), false)
  768. })
  769. cvPortSelect.change(function () {
  770. self.showDynamicField(form, typeInput.val(), currentAddressing, port, $(this).val(), false)
  771. })
  772. self.showDynamicField(form, typeInputVal, currentAddressing, port, cvPortSelect.val(), true)
  773. var pname = (port.symbol == ":bypass" || port.symbol == ":presets") ? pluginLabel : port.shortName
  774. var minv = currentAddressing.minimum != null ? currentAddressing.minimum : port.ranges.minimum
  775. var maxv = currentAddressing.maximum != null ? currentAddressing.maximum : port.ranges.maximum
  776. var min = form.find('input[name=min]').val(minv).attr("min", port.ranges.minimum).attr("max", port.ranges.maximum)
  777. var max = form.find('input[name=max]').val(maxv).attr("min", port.ranges.minimum).attr("max", port.ranges.maximum)
  778. var label = form.find('input[name=label]').val(currentAddressing.label || pname)
  779. var tempo = form.find('input[name=tempo]').prop("checked", currentAddressing.tempo || false)
  780. var divider = form.find('select[name=divider]')
  781. var dividerOptions = [];
  782. // Hide Tempo section if the ControlPort does not have the property mod:tempoRelatedDynamicScalePoints
  783. if (!hasTempoRelatedDynamicScalePoints(port)) {
  784. form.find('.tempo').css({display:"none"})
  785. // Else, build filtered list of divider values based on bpm and ControlPort min/max values
  786. } else {
  787. if (tempo.prop("checked")) {
  788. self.disableMinMaxSteps(form, true)
  789. }
  790. form.find('input[name=tempo]').bind('change', function() {
  791. self.disableMinMaxSteps(form, this.checked)
  792. if (currentAddressing.uri == null) {
  793. if (this.checked) {
  794. form.find('.js-save').removeClass('disabled')
  795. } else if (typeInput.val() === kNullAddressURI) {
  796. form.find('.js-save').addClass('disabled')
  797. }
  798. }
  799. actuators = self.availableActuators(instance, port, this.checked)
  800. deviceTable.empty()
  801. self.buildDeviceTable(deviceTable, currentAddressing, actuators,
  802. hmiPageInput, hmiSubPageInput, hmiUriInput,
  803. sensitivity, ledColourMode, momentarySwMode, port)
  804. })
  805. dividerOptions = self.buildDividerOptions(divider, port, currentAddressing.dividers)
  806. }
  807. if (port.properties.indexOf("toggled") >= 0 || port.properties.indexOf("trigger") >= 0) {
  808. // boolean, always min or max value
  809. var step = maxv-minv
  810. min.attr("step", step)
  811. max.attr("step", step)
  812. // hide ranges
  813. form.find('.range').hide()
  814. } else if (port.properties.indexOf("enumeration") >= 0) {
  815. // enumeration, step is list size - 1
  816. var step = port.scalePoints.length - 1
  817. min.attr("step", step)
  818. max.attr("step", step)
  819. // hide ranges
  820. form.find('.range').hide()
  821. } else if (port.properties.indexOf("integer") < 0) {
  822. // float, allow non-integer stepping
  823. var step = (maxv-minv)/100
  824. min.attr("step", step)
  825. max.attr("step", step)
  826. // Hide sensitivity and tempo options for MIDI
  827. // FIXME this whole section below can likely be removed without side effects
  828. var act = typeInput.val()
  829. if (act === kMidiLearnURI || act.lastIndexOf(kMidiCustomPrefixURI, 0) === 0 || act === cvOption) {
  830. form.find('.sensitivity').css({ display: "none" })
  831. form.find('.tempo').css({ display: "none" })
  832. }
  833. // Hide tempo option for CC or CV
  834. if (act === ccOption || act === cvOption) {
  835. form.find('.tempo').css({ display: "none" })
  836. }
  837. // Hide cv operational mode for everything except CV
  838. if (act !== cvOption) {
  839. form.find('.cv-op-mode').css({ display: "none" })
  840. }
  841. }
  842. self.buildDeviceTable(deviceTable, currentAddressing, actuators,
  843. hmiPageInput, hmiSubPageInput, hmiUriInput,
  844. sensitivity, ledColourMode, momentarySwMode, port)
  845. form.find('.js-save').click(function () {
  846. if ($(this).hasClass('disabled')) {
  847. return
  848. }
  849. self.saveAddressing(
  850. instance,
  851. port,
  852. actuators,
  853. typeInput,
  854. hmiPageInput,
  855. hmiSubPageInput,
  856. hmiUriInput,
  857. ccActuatorSelect,
  858. cvPortSelect,
  859. min,
  860. max,
  861. label,
  862. pname,
  863. sensitivity,
  864. ledColourMode,
  865. momentarySwMode,
  866. tempo,
  867. divider,
  868. dividerOptions,
  869. operationalMode,
  870. form
  871. );
  872. })
  873. form.find('.js-close').click(function () {
  874. form.remove()
  875. form = null
  876. })
  877. form.find('.advanced-toggle').click(function() {
  878. if (!form.find('.advanced-container').is(':visible')) {
  879. $('.mod-pedal-settings-address').find('.mod-box').animate({
  880. width: '916px'
  881. }, 100, function() {
  882. form.find('.advanced-container').toggle()
  883. });
  884. } else {
  885. form.find('.advanced-container').toggle(0, function() {
  886. $('.mod-pedal-settings-address').find('.mod-box').animate({
  887. width: '766px'
  888. }, 100)
  889. })
  890. }
  891. })
  892. $('body').keydown(function (e) {
  893. if (e.keyCode == 27 && form && form.is(':visible')) {
  894. form.remove()
  895. form = null
  896. return false
  897. }
  898. })
  899. form.keydown(function (e) {
  900. if (e.keyCode == 13) {
  901. self.saveAddressing(
  902. instance,
  903. port,
  904. actuators,
  905. typeInput,
  906. hmiPageInput,
  907. hmiSubPageInput,
  908. hmiUriInput,
  909. ccActuatorSelect,
  910. cvPortSelect,
  911. min,
  912. max,
  913. label,
  914. pname,
  915. sensitivity,
  916. ledColourMode,
  917. momentarySwMode,
  918. tempo,
  919. divider,
  920. dividerOptions,
  921. operationalMode,
  922. form
  923. );
  924. return false
  925. }
  926. })
  927. form.appendTo($('body'))
  928. form.focus()
  929. }
  930. this.addressNow = function (
  931. instance,
  932. port,
  933. actuator,
  934. minv,
  935. maxv,
  936. labelValue,
  937. sensitivityValue,
  938. tempoValue,
  939. dividerValue,
  940. dividerOptions,
  941. page,
  942. subpage,
  943. colouredValue,
  944. momentarySwValue,
  945. operationalModeValue,
  946. form
  947. ) {
  948. var instanceAndSymbol = instance+"/"+port.symbol;
  949. var currentAddressing = self.addressingsData[instanceAndSymbol] || {}
  950. var portValuesWithDividerLabels = []
  951. // Sync port value to bpm
  952. if (tempoValue && dividerValue && port.units && port.units.symbol) {
  953. if (port.units.symbol === 'BPM') {
  954. port.value = getPortValue(self.beatsPerMinutePort.value, dividerValue, port.units.symbol) // no need for conversion
  955. } else {
  956. port.value = convertSecondsToPortValueEquivalent(
  957. getPortValue(self.beatsPerMinutePort.value, dividerValue, port.units.symbol),
  958. port.units.symbol
  959. );
  960. }
  961. }
  962. var addressing = {
  963. uri : actuator.uri || kNullAddressURI,
  964. label : labelValue,
  965. minimum: minv,
  966. maximum: maxv,
  967. value : port.value,
  968. steps : sensitivityValue,
  969. tempo : tempoValue,
  970. dividers: dividerValue,
  971. feedback: actuator.feedback === false ? false : true, // backwards compatible, true by default
  972. page: page || null,
  973. subpage: subpage || null,
  974. coloured: colouredValue,
  975. momentary: momentarySwValue,
  976. operationalMode: operationalModeValue,
  977. }
  978. options.address(instanceAndSymbol, addressing, function (ok) {
  979. if (!ok) {
  980. console.log("Addressing failed for port " + port.symbol);
  981. return;
  982. }
  983. // remove old one first
  984. var unaddressing = false
  985. if (currentAddressing.uri && currentAddressing.uri != kNullAddressURI) {
  986. unaddressing = true
  987. if (startsWith(currentAddressing.uri, kMidiCustomPrefixURI)) {
  988. currentAddressing.uri = kMidiLearnURI
  989. }
  990. remove_from_array(self.addressingsByActuator[currentAddressing.uri], instanceAndSymbol)
  991. }
  992. // We're addressing
  993. if (actuator.uri && actuator.uri != kNullAddressURI)
  994. {
  995. var actuator_uri = actuator.uri
  996. if (startsWith(actuator_uri, kMidiCustomPrefixURI)) {
  997. actuator_uri = kMidiLearnURI
  998. }
  999. // add new one, print and error if already there
  1000. if (self.addressingsByActuator[actuator_uri].indexOf(instanceAndSymbol) < 0) {
  1001. self.addressingsByActuator[actuator_uri].push(instanceAndSymbol)
  1002. } else {
  1003. console.log("ERROR HERE, please fix!")
  1004. }
  1005. // remove data needed by the server, useless for us
  1006. delete addressing.value
  1007. // convert some values to proper type
  1008. addressing.coloured = !!addressing.coloured
  1009. addressing.momentary = parseInt(addressing.momentary)
  1010. // now save
  1011. self.addressingsByPortSymbol[instanceAndSymbol] = actuator.uri
  1012. self.addressingsData [instanceAndSymbol] = addressing
  1013. // disable this control
  1014. var feedback = actuator.feedback === false ? false : true // backwards compat, true by default
  1015. options.setEnabled(instance, port.symbol, false, feedback, true, addressing.momentary)
  1016. }
  1017. // We're unaddressing
  1018. else if (unaddressing)
  1019. {
  1020. delete self.addressingsByPortSymbol[instanceAndSymbol]
  1021. delete self.addressingsData [instanceAndSymbol]
  1022. // enable this control
  1023. options.setEnabled(instance, port.symbol, true)
  1024. }
  1025. if (form !== undefined) {
  1026. form.remove()
  1027. form = null
  1028. }
  1029. })
  1030. }
  1031. this.saveAddressing = function (
  1032. instance,
  1033. port,
  1034. actuators,
  1035. typeInput,
  1036. hmiPageInput,
  1037. hmiSubPageInput,
  1038. hmiUriInput,
  1039. ccActuatorSelect,
  1040. cvPortSelect,
  1041. min,
  1042. max,
  1043. label,
  1044. pname,
  1045. sensitivity,
  1046. ledColourMode,
  1047. momentarySwMode,
  1048. tempo,
  1049. divider,
  1050. dividerOptions,
  1051. operationalMode,
  1052. form
  1053. ) {
  1054. var instanceAndSymbol = instance+"/"+port.symbol
  1055. var currentAddressing = self.addressingsData[instanceAndSymbol] || {}
  1056. var page = hmiPageInput.val()
  1057. var subpage = hmiSubPageInput.val()
  1058. var typeInputVal = typeInput.val()
  1059. var uri = kNullAddressURI
  1060. if (typeInputVal === deviceOption && hmiUriInput.val()) {
  1061. uri = hmiUriInput.val()
  1062. } else if(typeInputVal === ccOption && ccActuatorSelect.val()) {
  1063. uri = ccActuatorSelect.val()
  1064. } else if(typeInputVal === cvOption && cvPortSelect.val()) {
  1065. uri = cvPortSelect.val()
  1066. } else if (typeInputVal === kMidiLearnURI) {
  1067. uri = kMidiLearnURI
  1068. }
  1069. var actuator = actuators[uri] || {}
  1070. var tempoValue = tempo.prop("checked")
  1071. // Sync port value to bpm with virtual bpm actuator
  1072. if (tempoValue && uri === kNullAddressURI) {
  1073. actuator = {
  1074. uri : kBpmURI,
  1075. modes: ":float:integer:",
  1076. steps: [],
  1077. max_assigns: 99
  1078. }
  1079. }
  1080. // no actuator selected or old one exists, do nothing
  1081. if (actuator.uri == null && currentAddressing.uri == null) {
  1082. console.log("Nothing to do")
  1083. if (form !== undefined) {
  1084. form.remove()
  1085. form = null
  1086. }
  1087. return
  1088. }
  1089. // Check values
  1090. var minv = min.val()
  1091. if (minv == undefined || minv == "")
  1092. minv = port.ranges.minimum
  1093. var maxv = max.val()
  1094. if (maxv == undefined || maxv == "")
  1095. maxv = port.ranges.maximum
  1096. if (parseFloat(minv) >= parseFloat(maxv)) {
  1097. alert("The minimum value is equal or higher than the maximum. We cannot address a control like this!")
  1098. return
  1099. }
  1100. var labelValue = label.val() || pname
  1101. var sensitivityValue = sensitivity.val()
  1102. var dividerValue = divider.val() ? parseFloat(divider.val()): divider.val()
  1103. var colouredValue = ledColourMode.hasClass('disabled') ? 0 : parseInt(ledColourMode.val())
  1104. var momentarySwValue = momentarySwMode.hasClass('disabled') ? 0 : parseInt(momentarySwMode.val())
  1105. var operationalModeValue = operationalMode.val()
  1106. // if changing from midi-learn, unlearn first
  1107. if (currentAddressing.uri == kMidiLearnURI) {
  1108. var addressing = {
  1109. uri : kMidiUnlearnURI,
  1110. label : labelValue,
  1111. minimum: minv,
  1112. maximum: maxv,
  1113. value : port.value,
  1114. steps : sensitivityValue,
  1115. }
  1116. options.address(instanceAndSymbol, addressing, function (ok) {
  1117. if (!ok) {
  1118. console.log("Failed to unmap for port " + port.symbol);
  1119. return;
  1120. }
  1121. // remove old one
  1122. remove_from_array(self.addressingsByActuator[kMidiLearnURI], instanceAndSymbol)
  1123. delete self.addressingsByPortSymbol[instanceAndSymbol]
  1124. delete self.addressingsData [instanceAndSymbol]
  1125. // enable this control
  1126. options.setEnabled(instance, port.symbol, true)
  1127. // now we can address if needed
  1128. if (actuator.uri) {
  1129. self.addressNow(
  1130. instance,
  1131. port,
  1132. actuator,
  1133. minv,
  1134. maxv,
  1135. labelValue,
  1136. sensitivityValue,
  1137. tempoValue,
  1138. dividerValue,
  1139. dividerOptions,
  1140. page,
  1141. subpage,
  1142. colouredValue,
  1143. momentarySwValue,
  1144. operationalModeValue,
  1145. form
  1146. );
  1147. // if not, just close the form
  1148. } else if (form !== undefined) {
  1149. form.remove()
  1150. form = null
  1151. }
  1152. })
  1153. }
  1154. // otherwise just address it now
  1155. else {
  1156. self.addressNow(
  1157. instance,
  1158. port,
  1159. actuator,
  1160. minv,
  1161. maxv,
  1162. labelValue,
  1163. sensitivityValue,
  1164. tempoValue,
  1165. dividerValue,
  1166. dividerOptions,
  1167. page,
  1168. subpage,
  1169. colouredValue,
  1170. momentarySwValue,
  1171. operationalModeValue,
  1172. form
  1173. );
  1174. }
  1175. }
  1176. this.addHardwareMapping = function (instance, portSymbol, actuator_uri,
  1177. label, minimum, maximum, steps,
  1178. tempo, dividers, page, subpage, group, feedback, coloured, momentary) {
  1179. var instanceAndSymbol = instance+"/"+portSymbol
  1180. self.addressingsByActuator [actuator_uri].push(instanceAndSymbol)
  1181. self.addressingsByPortSymbol[instanceAndSymbol] = actuator_uri
  1182. self.addressingsData [instanceAndSymbol] = {
  1183. uri : actuator_uri,
  1184. label : label,
  1185. minimum : minimum,
  1186. maximum : maximum,
  1187. steps : steps,
  1188. tempo : tempo,
  1189. dividers: dividers,
  1190. feedback: feedback,
  1191. page : page,
  1192. subpage : subpage,
  1193. group : group,
  1194. coloured: coloured,
  1195. momentary: momentary
  1196. }
  1197. // disable this control if needed
  1198. options.setEnabled(instance, portSymbol, false, feedback, true, momentary)
  1199. }
  1200. this.addCvMapping = function (instance, portSymbol, actuator_uri,
  1201. label, minimum, maximum, operationalMode, feedback) {
  1202. var instanceAndSymbol = instance+"/"+portSymbol
  1203. self.addressingsByActuator [actuator_uri].push(instanceAndSymbol)
  1204. self.addressingsByPortSymbol[instanceAndSymbol] = actuator_uri
  1205. self.addressingsData [instanceAndSymbol] = {
  1206. uri : actuator_uri,
  1207. label : label,
  1208. minimum : minimum,
  1209. maximum : maximum,
  1210. feedback: feedback,
  1211. operationalMode: operationalMode,
  1212. }
  1213. // disable this control
  1214. options.setEnabled(instance, portSymbol, false, feedback, true)
  1215. }
  1216. this.addMidiMapping = function (instance, portSymbol, channel, control, minimum, maximum) {
  1217. var instanceAndSymbol = instance+"/"+portSymbol
  1218. var actuator_uri = create_midi_cc_uri(channel, control)
  1219. if (self.addressingsByPortSymbol[instanceAndSymbol] == kMidiLearnURI) {
  1220. var controlstr = (control == MIDI_PITCHBEND_AS_CC) ? "Pitchbend" : ("Controller #" + control)
  1221. new Notification('info', "Parameter mapped to MIDI " + controlstr + ", Channel " + (channel+1), 8000)
  1222. }
  1223. self.addressingsByActuator [kMidiLearnURI].push(instanceAndSymbol)
  1224. self.addressingsByPortSymbol[instanceAndSymbol] = actuator_uri
  1225. self.addressingsData [instanceAndSymbol] = {
  1226. uri : actuator_uri,
  1227. label : null,
  1228. minimum : minimum,
  1229. maximum : maximum,
  1230. steps : null,
  1231. feedback: true,
  1232. }
  1233. // disable this control
  1234. options.setEnabled(instance, portSymbol, false, true, true)
  1235. }
  1236. this.addActuator = function (actuator) {
  1237. HARDWARE_PROFILE.push(actuator)
  1238. self.addressingsByActuator[actuator.uri] = []
  1239. }
  1240. this.hasControlChainDevice = function (actuator) {
  1241. for (var i in HARDWARE_PROFILE) {
  1242. if (is_control_chain_uri(HARDWARE_PROFILE[i].uri)) {
  1243. return true;
  1244. }
  1245. }
  1246. return false;
  1247. }
  1248. this.removeActuator = function (actuator_uri) {
  1249. var addressings = self.addressingsByActuator[actuator_uri]
  1250. for (var i in addressings) {
  1251. var instanceAndSymbol = addressings[i]
  1252. var instance = instanceAndSymbol.substring(0, instanceAndSymbol.lastIndexOf("/"))
  1253. var portsymbol = instanceAndSymbol.replace(instance+"/", "")
  1254. delete self.addressingsByPortSymbol[instanceAndSymbol]
  1255. delete self.addressingsData [instanceAndSymbol]
  1256. // enable this control
  1257. options.setEnabled(instance, portsymbol, true)
  1258. }
  1259. delete self.addressingsByActuator[actuator_uri]
  1260. for (var i in HARDWARE_PROFILE) {
  1261. var actuator = HARDWARE_PROFILE[i]
  1262. if (actuator.uri == actuator_uri) {
  1263. remove_from_array(HARDWARE_PROFILE, actuator)
  1264. break
  1265. }
  1266. }
  1267. }
  1268. // Removes an instance
  1269. this.removeInstance = function (instance) {
  1270. var i, j, index, actuator, instanceAndSymbol, instanceAndSymbols = []
  1271. var instanceSansGraph = instance.replace("/graph/","")
  1272. var keys = Object.keys(self.addressingsByPortSymbol)
  1273. for (i in keys) {
  1274. instanceAndSymbol = keys[i]
  1275. if (instanceAndSymbol.replace("/graph/","").split(/\//)[0] == instanceSansGraph) {
  1276. if (instanceAndSymbols.indexOf(instanceAndSymbol) < 0) {
  1277. instanceAndSymbols.push(instanceAndSymbol)
  1278. }
  1279. }
  1280. }
  1281. for (i in instanceAndSymbols) {
  1282. instanceAndSymbol = instanceAndSymbols[i]
  1283. delete self.addressingsByPortSymbol[instanceAndSymbol]
  1284. delete self.addressingsData [instanceAndSymbol]
  1285. for (j in HARDWARE_PROFILE) {
  1286. actuator = HARDWARE_PROFILE[j]
  1287. remove_from_array(self.addressingsByActuator[actuator.uri], instanceAndSymbol)
  1288. }
  1289. }
  1290. }
  1291. // used only for global pedalboard addressings
  1292. // don't use it for normal operations, as it skips setEnabled()
  1293. this.removeHardwareMappping = function (instanceAndSymbol) {
  1294. var actuator_uri = self.addressingsByPortSymbol[instanceAndSymbol]
  1295. delete self.addressingsByPortSymbol[instanceAndSymbol]
  1296. delete self.addressingsData [instanceAndSymbol]
  1297. if (actuator_uri && actuator_uri != kNullAddressURI) {
  1298. remove_from_array(self.addressingsByActuator[actuator_uri], instanceAndSymbol)
  1299. return true
  1300. }
  1301. return false
  1302. }
  1303. this.addCvOutputPort = function (uri, name, operationalMode) {
  1304. var existingPort = self.cvOutputPorts.find(function (port) {
  1305. return port.uri === uri;
  1306. })
  1307. if (existingPort) {
  1308. existingPort.name = name
  1309. } else {
  1310. self.cvOutputPorts.push({
  1311. uri: uri,
  1312. name: name,
  1313. modes: cvModes,
  1314. steps: [],
  1315. max_assigns: 99,
  1316. feedback: false,
  1317. defaultOperationalMode: operationalMode,
  1318. })
  1319. self.addressingsByActuator[uri] = []
  1320. }
  1321. }
  1322. this.removeCvOutputPort = function (uri) {
  1323. var isAddressable = false
  1324. self.cvOutputPorts = self.cvOutputPorts.filter(function (port) {
  1325. if (port.uri === uri) {
  1326. isAddressable = true
  1327. }
  1328. return port.uri !== uri
  1329. });
  1330. if (!isAddressable) {
  1331. return
  1332. }
  1333. for (var i in self.addressingsByActuator[uri]) {
  1334. instanceAndSymbol = self.addressingsByActuator[uri][i]
  1335. delete self.addressingsData[instanceAndSymbol]
  1336. delete self.addressingsByPortSymbol[instanceAndSymbol]
  1337. var separatedInstanceAndSymbol = getInstanceSymbol(instanceAndSymbol)
  1338. options.setEnabled(separatedInstanceAndSymbol[0], separatedInstanceAndSymbol[1], true)
  1339. }
  1340. delete self.addressingsByActuator[uri]
  1341. }
  1342. }