not really known
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.

6401 lines
263 KiB

  1. // Copyright (c) 2014-2017 Walter Bender
  2. // Copyright (c) 2015 Yash Khandelwal
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the The GNU Affero General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 3 of the License, or (at your option) any later version.
  8. //
  9. // You should have received a copy of the GNU Affero General Public
  10. // License along with this library; if not, write to the Free Software
  11. // Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
  12. const DEFAULTVOLUME = 50;
  13. const TONEBPM = 240; // Seems to be the default.
  14. const TARGETBPM = 90; // What we'd like to use for beats per minute
  15. const DEFAULTDELAY = 500; // milleseconds
  16. const TURTLESTEP = -1; // Run in step-by-step mode
  17. const NOTEDIV = 8; // Number of steps to divide turtle graphics
  18. const OSCVOLUMEADJUSTMENT = 1.5 // The oscillator runs hot. We need
  19. // to scale back its volume.
  20. const NOMICERRORMSG = 'The microphone is not available.';
  21. const NANERRORMSG = 'Not a number.';
  22. const NOSTRINGERRORMSG = 'Not a string.';
  23. const NOBOXERRORMSG = 'Cannot find box';
  24. const NOACTIONERRORMSG = 'Cannot find action.';
  25. const NOINPUTERRORMSG = 'Missing argument.';
  26. const NOSQRTERRORMSG = 'Cannot take square root of negative number.';
  27. const ZERODIVIDEERRORMSG = 'Cannot divide by zero.';
  28. const EMPTYHEAPERRORMSG = 'empty heap.';
  29. const INVALIDPITCH = 'Not a valid pitch name';
  30. const POSNUMBER = 'Argument must be a positive number';
  31. function Logo () {
  32. this.canvas = null;
  33. this.blocks = null;
  34. this.turtles = null;
  35. this.stage = null;
  36. this.refreshCanvas = null;
  37. this.textMsg = null;
  38. this.errorMsg = null;
  39. this.hideMsgs = null;
  40. this.onStopTurtle = null;
  41. this.onRunTurtle = null;
  42. this.getStageX = null;
  43. this.getStageY = null;
  44. this.getStageMouseDown = null;
  45. this.getCurrentKeyCode = null;
  46. this.clearCurrentKeyCode = null;
  47. this.meSpeak = null;
  48. this.saveLocally = null;
  49. this.pitchTimeMatrix = null;
  50. this.pitchDrumMatrix = null;
  51. this.rhythmRuler = null;
  52. this.pitchStaircase = null;
  53. this.tempo = null;
  54. this.pitchSlider = null;
  55. this.modeWidget = null;
  56. this.statusMatrix = null;
  57. this.evalFlowDict = {};
  58. this.evalArgDict = {};
  59. this.evalParameterDict = {};
  60. this.evalSetterDict = {};
  61. this.evalOnStartList = {};
  62. this.evalOnStopList = {};
  63. this.eventList = {};
  64. this.boxes = {};
  65. this.actions = {};
  66. this.returns = [];
  67. this.turtleHeaps = {};
  68. this.invertList = {};
  69. // When we leave a clamp block, we need to dispatch a signal.
  70. this.endOfClampSignals = {};
  71. this.time = 0;
  72. this.firstNoteTime = null;
  73. this.waitTimes = {};
  74. this.turtleDelay = 0;
  75. this.sounds = [];
  76. this.cameraID = null;
  77. this.stopTurtle = false;
  78. this.lastKeyCode = null;
  79. this.saveTimeout = 0;
  80. // Music-related attributes
  81. this.notesPlayed = {};
  82. // pitch-drum matrix
  83. this.showPitchDrumMatrix = false;
  84. this.inPitchDrumMatrix = false;
  85. //rhythm-ruler
  86. this.inRhythmRuler = false;
  87. this.rhythmRulerMeasure = null;
  88. this.inPitchStaircase = false;
  89. this.inTempo = false;
  90. this.inPitchSlider = false;
  91. this._currentDrumBlock = null;
  92. // pitch-rhythm matrix
  93. this.inMatrix = false;
  94. this.keySignature = {};
  95. this.tupletRhythms = [];
  96. this.addingNotesToTuplet = false;
  97. this.drumBlocks = [];
  98. this.pitchBlocks = [];
  99. this.inNoteBlock = [];
  100. this.whichNoteBlock = [];
  101. // parameters used by pitch
  102. this.transposition = {};
  103. // parameters used by notes
  104. this._masterBPM = TARGETBPM;
  105. this.defaultBPMFactor = TONEBPM / this._masterBPM;
  106. this.beatFactor = {};
  107. this.dotCount = {};
  108. this.noteBeat = {};
  109. this.oscList = {};
  110. this.noteDrums = {};
  111. this.notePitches = {};
  112. this.noteOctaves = {};
  113. this.noteCents = {};
  114. this.noteHertz = {};
  115. this.noteTranspositions = {};
  116. this.noteBeatValues = {};
  117. this.lastNotePlayed = {};
  118. this.noteStatus = {};
  119. this.pitchNumberOffset = 39; // C4
  120. // graphics listeners during note play
  121. this.forwardListener = {};
  122. this.rightListener = {};
  123. this.arcListener = {};
  124. // status of note being played
  125. // Deprecated (ref lastNotePlayed)
  126. this.currentNotes = {};
  127. this.currentOctaves = {};
  128. // parameters used by the note block
  129. this.bpm = {};
  130. this.turtleTime = [];
  131. this.noteDelay = 0;
  132. this.playedNote = {};
  133. this.playedNoteTimes = {};
  134. this.pushedNote = {};
  135. this.duplicateFactor = {};
  136. this.skipFactor = {};
  137. this.skipIndex = {};
  138. this.crescendoDelta = {};
  139. this.crescendoVolume = {};
  140. this.crescendoInitialVolume = {};
  141. this.intervals = {};
  142. this.perfect = {};
  143. this.diminished = {};
  144. this.augmented = {};
  145. this.major = {};
  146. this.minor = {};
  147. this.staccato = {};
  148. this.swing = {};
  149. this.swingTarget = {};
  150. this.swingCarryOver = {};
  151. this.tie = {};
  152. this.tieNote = {};
  153. this.tieCarryOver = {};
  154. this.polyVolume = {};
  155. this.validNote = true;
  156. this.drift = {};
  157. this.drumStyle = {};
  158. this.voices = {};
  159. this.backward = {};
  160. this.vibratoIntensity = {};
  161. this.vibratoRate = {};
  162. this.justCounting = {};
  163. // When counting notes or generating lilypond output...
  164. this.suppressOutput = {};
  165. // scale factor for turtle graphics embedded in notes
  166. this.dispatchFactor = {};
  167. // tuplet
  168. this.tuplet = false;
  169. this.tupletParams = [];
  170. // pitch to drum mapping
  171. this.pitchDrumTable = {};
  172. // parameters used by notations
  173. this.checkingLilypond = false;
  174. this.checkingLilypond = false;
  175. this.lilypondNotes = {};
  176. this.lilypondStaging = {};
  177. this.lilypondOutput = getLilypondHeader();
  178. this.runningLilypond = false;
  179. this.numerator = 3;
  180. this.denominator = 4;
  181. if (_THIS_IS_MUSIC_BLOCKS_) {
  182. // Load the default synthesizer
  183. this.synth = new Synth();
  184. this.synth.loadSynth('poly');
  185. } else {
  186. this.turtleOscs = {};
  187. }
  188. // Mode widget
  189. this._modeBlock = null;
  190. // Status matrix
  191. this.inStatusMatrix = false;
  192. this.updatingStatusMatrix = false;
  193. this.statusFields = [];
  194. // When running in step-by-step mode, the next command to run is
  195. // queued here.
  196. this.stepQueue = {};
  197. this.unhighlightStepQueue = {};
  198. // Control points for bezier curves
  199. this.cp1x = {};
  200. this.cp1y = {};
  201. this.cp2x = {};
  202. this.cp2y = {};
  203. this.svgOutput = '';
  204. this.svgBackground = true;
  205. if (_THIS_IS_MUSIC_BLOCKS_) {
  206. this.mic = new Tone.UserMedia();
  207. this.limit = 1024;
  208. this.analyser = new Tone.Analyser({
  209. "type" : "waveform",
  210. "size" : this.limit
  211. });
  212. this.mic.connect(this.analyser);
  213. } else {
  214. try {
  215. this.mic = new p5.AudioIn()
  216. } catch (e) {
  217. console.log(e);
  218. console.log(NOMICERRORMSG);
  219. this.mic = null;
  220. }
  221. }
  222. this.setCanvas = function (canvas) {
  223. this.canvas = canvas;
  224. return this;
  225. };
  226. this.setBlocks = function (blocks) {
  227. this.blocks = blocks;
  228. return this;
  229. };
  230. this.setTurtles = function (turtles) {
  231. this.turtles = turtles;
  232. return this;
  233. };
  234. this.setStage = function (stage) {
  235. this.stage = stage;
  236. return this;
  237. };
  238. this.setRefreshCanvas = function (refreshCanvas) {
  239. this.refreshCanvas = refreshCanvas;
  240. return this;
  241. };
  242. this.setTextMsg = function (textMsg) {
  243. this.textMsg = textMsg;
  244. return this;
  245. };
  246. this.setHideMsgs = function (hideMsgs) {
  247. this.hideMsgs = hideMsgs;
  248. return this;
  249. };
  250. this.setErrorMsg = function (errorMsg) {
  251. this.errorMsg = errorMsg;
  252. return this;
  253. };
  254. this.setOnStopTurtle = function (onStopTurtle) {
  255. this.onStopTurtle = onStopTurtle;
  256. return this;
  257. };
  258. this.setOnRunTurtle = function (onRunTurtle) {
  259. this.onRunTurtle = onRunTurtle;
  260. return this;
  261. };
  262. this.setGetStageX = function (getStageX) {
  263. this.getStageX = getStageX;
  264. return this;
  265. };
  266. this.setGetStageY = function (getStageY) {
  267. this.getStageY = getStageY;
  268. return this;
  269. };
  270. this.setGetStageMouseDown = function (getStageMouseDown) {
  271. this.getStageMouseDown = getStageMouseDown;
  272. return this;
  273. };
  274. this.setGetCurrentKeyCode = function (getCurrentKeyCode) {
  275. this.getCurrentKeyCode = getCurrentKeyCode;
  276. return this;
  277. };
  278. this.setClearCurrentKeyCode = function (clearCurrentKeyCode) {
  279. this.clearCurrentKeyCode = clearCurrentKeyCode;
  280. return this;
  281. };
  282. this.setMeSpeak = function (meSpeak) {
  283. this.meSpeak = meSpeak;
  284. return this;
  285. };
  286. this.setSaveLocally = function (saveLocally) {
  287. this.saveLocally = saveLocally;
  288. return this;
  289. };
  290. // Used to pause between each block as the program executes.
  291. this.setTurtleDelay = function (turtleDelay) {
  292. this.turtleDelay = turtleDelay;
  293. this.noteDelay = 0;
  294. };
  295. // Used to pause between each note as the program executes.
  296. this.setNoteDelay = function (noteDelay) {
  297. this.noteDelay = noteDelay;
  298. this.turtleDelay = 0;
  299. };
  300. this.step = function () {
  301. // Take one step for each turtle in excuting Logo commands.
  302. for (var turtle in this.stepQueue) {
  303. if (this.stepQueue[turtle].length > 0) {
  304. if (turtle in this.unhighlightStepQueue && this.unhighlightStepQueue[turtle] != null) {
  305. if (this.blocks.visible) {
  306. this.blocks.unhighlight(this.unhighlightStepQueue[turtle]);
  307. }
  308. this.unhighlightStepQueue[turtle] = null;
  309. }
  310. var blk = this.stepQueue[turtle].pop();
  311. if (blk != null) {
  312. this._runFromBlockNow(this, turtle, blk, 0, null);
  313. }
  314. }
  315. }
  316. };
  317. this.stepNote = function () {
  318. // Step through one note for each turtle in excuting Logo
  319. // commands, but run through other blocks at full speed.
  320. var tempStepQueue = {};
  321. var notesFinish = {};
  322. var thisNote = {};
  323. var that = this;
  324. __stepNote();
  325. function __stepNote() {
  326. for (var turtle in that.stepQueue) {
  327. // Have we already played a note for this turtle?
  328. if (turtle in that.playedNote && that.playedNote[turtle]) {
  329. continue;
  330. }
  331. if (that.stepQueue[turtle].length > 0) {
  332. if (turtle in that.unhighlightStepQueue && that.unhighlightStepQueue[turtle] != null) {
  333. if (that.blocks.visible) {
  334. that.blocks.unhighlight(that.unhighlightStepQueue[turtle]);
  335. }
  336. that.unhighlightStepQueue[turtle] = null;
  337. }
  338. var blk = that.stepQueue[turtle].pop();
  339. if (blk != null && blk !== notesFinish[turtle]) {
  340. var block = that.blocks.blockList[blk];
  341. if (block.name === 'newnote') {
  342. tempStepQueue[turtle] = blk;
  343. notesFinish[turtle] = last(block.connections);
  344. if (notesFinish[turtle] == null) { // end of flow
  345. notesFinish[turtle] = last(that.turtles.turtleList[turtle].queue) && last(that.turtles.turtleList[turtle].queue).blk;
  346. // catch case of null - end of project
  347. }
  348. // that.playedNote[turtle] = true;
  349. that.playedNoteTimes[turtle] = that.playedNoteTimes[turtle] || 0;
  350. thisNote[turtle] = Math.pow(that.parseArg(that, turtle, block.connections[1], blk, null), -1);
  351. that.playedNoteTimes[turtle] += thisNote[turtle];
  352. // Keep track of how long the note played for, so we can go back and play it again if needed
  353. }
  354. that._runFromBlockNow(that, turtle, blk, 0, null);
  355. } else {
  356. that.playedNote[turtle] = true;
  357. }
  358. }
  359. }
  360. // At this point, some turtles have played notes and others
  361. // have not. We need to keep stepping until they all have.
  362. var keepGoing = false;
  363. for (var turtle in that.stepQueue) {
  364. if (that.stepQueue[turtle].length > 0 && !that.playedNote[turtle]) {
  365. keepGoing = true;
  366. break;
  367. }
  368. }
  369. if (keepGoing) {
  370. __stepNote();
  371. // that.step();
  372. } else {
  373. var notesArray = [];
  374. for (var turtle in that.playedNote) {
  375. that.playedNote[turtle] = false;
  376. notesArray.push(that.playedNoteTimes[turtle]);
  377. }
  378. // If some notes are supposed to play for longer, add
  379. // them back to the queue
  380. var shortestNote = Math.min.apply(null, notesArray);
  381. var continueFrom;
  382. for (var turtle in that.playedNoteTimes) {
  383. if (that.playedNoteTimes[turtle] > shortestNote) {
  384. continueFrom = tempStepQueue[turtle];
  385. // Subtract the time, as if we haven't played it yet
  386. that.playedNoteTimes[turtle] -= thisNote[turtle];
  387. } else {
  388. continueFrom = notesFinish[turtle];
  389. }
  390. that._runFromBlock(that, turtle, continueFrom, 0, null);
  391. }
  392. if (shortestNote === Math.max.apply(null, notesArray)) {
  393. that.playedNoteTimes = {};
  394. }
  395. }
  396. };
  397. };
  398. this.doStopTurtle = function () {
  399. // The stop button was pressed. Stop the turtle and clean up a
  400. // few odds and ends.
  401. this.stopTurtle = true;
  402. this.turtles.markAsStopped();
  403. for (var sound in this.sounds) {
  404. this.sounds[sound].stop();
  405. }
  406. this.sounds = [];
  407. if (_THIS_IS_MUSIC_BLOCKS_) {
  408. this.synth.stopSound('default');
  409. this.synth.stop();
  410. }
  411. if (this.cameraID != null) {
  412. doStopVideoCam(this.cameraID, this.setCameraID);
  413. }
  414. this.onStopTurtle();
  415. this.blocks.bringToTop();
  416. this.stepQueue = {};
  417. this.unhighlightQueue = {};
  418. };
  419. this._clearParameterBlocks = function () {
  420. for (var blk in this.blocks.blockList) {
  421. if (this.blocks.blockList[blk].parameter) {
  422. this.blocks.blockList[blk].text.text = '';
  423. this.blocks.blockList[blk].container.updateCache();
  424. }
  425. }
  426. this.refreshCanvas();
  427. };
  428. this._updateParameterBlock = function (that, turtle, blk) {
  429. // Update the label on parameter blocks.
  430. if (this.blocks.blockList[blk].protoblock.parameter) {
  431. var name = this.blocks.blockList[blk].name;
  432. var value = 0;
  433. switch (name) {
  434. case 'and':
  435. case 'or':
  436. case 'not':
  437. case 'less':
  438. case 'greater':
  439. case 'equal':
  440. if (this.blocks.blockList[blk].value) {
  441. value = _('true');
  442. } else {
  443. value = _('false');
  444. }
  445. break;
  446. case 'random':
  447. case 'mod':
  448. case 'sqrt':
  449. case 'int':
  450. case 'plus':
  451. case 'minus':
  452. case 'multiply':
  453. case 'power':
  454. value = toFixed2(this.blocks.blockList[blk].value);
  455. break;
  456. case 'divide':
  457. value = this.blocks.blockList[blk].value;
  458. break;
  459. case 'namedbox':
  460. var name = this.blocks.blockList[blk].privateData;
  461. if (name in this.boxes) {
  462. value = this.boxes[name];
  463. } else {
  464. this.errorMsg(NOBOXERRORMSG, blk, name);
  465. }
  466. break;
  467. case 'box':
  468. var cblk = this.blocks.blockList[blk].connections[1];
  469. var boxname = this.parseArg(that, turtle, cblk, blk);
  470. if (boxname in this.boxes) {
  471. value = this.boxes[boxname];
  472. } else {
  473. this.errorMsg(NOBOXERRORMSG, blk, boxname);
  474. }
  475. break;
  476. case 'x':
  477. value = toFixed2(this.turtles.turtleList[turtle].x);
  478. break;
  479. case 'y':
  480. value = toFixed2(this.turtles.turtleList[turtle].y);
  481. break;
  482. case 'heading':
  483. value = toFixed2(this.turtles.turtleList[turtle].orientation);
  484. break;
  485. case 'color':
  486. case 'hue':
  487. value = toFixed2(this.turtles.turtleList[turtle].color);
  488. break;
  489. case 'shade':
  490. value = toFixed2(this.turtles.turtleList[turtle].value);
  491. break;
  492. case 'grey':
  493. value = toFixed2(this.turtles.turtleList[turtle].chroma);
  494. break;
  495. case 'pensize':
  496. value = toFixed2(this.turtles.turtleList[turtle].stroke);
  497. break;
  498. case 'time':
  499. var d = new Date();
  500. value = (d.getTime() - this.time) / 1000;
  501. break;
  502. case 'mousex':
  503. value = toFixed2(this.getStageX());
  504. break;
  505. case 'mousey':
  506. value = toFixed2(this.getStageY());
  507. break;
  508. case 'keyboard':
  509. value = this.lastKeyCode;
  510. break;
  511. case 'loudness':
  512. if (that.mic == null) {
  513. that.errorMsg(NOMICERRORMSG);
  514. value = 0;
  515. } else {
  516. if (_THIS_IS_TURTLE_BLOCKS) {
  517. value = Math.round(that.mic.getLevel() * 1000);
  518. } else {
  519. var values = that.analyser.analyse();
  520. var sum = 0;
  521. for (var k = 0; k < that.limit; k++){
  522. sum += (values[k] * values[k]);
  523. }
  524. value = Math.round(Math.sqrt(sum / that.limit));
  525. }
  526. }
  527. break;
  528. case 'consonantstepsizeup':
  529. if (this.lastNotePlayed[turtle] !== null) {
  530. var len = this.lastNotePlayed[turtle][0].length;
  531. value = getStepSizeUp(this.keySignature[turtle], this.lastNotePlayed[turtle][0].slice(0, len - 1));
  532. } else {
  533. value = getStepSizeUp(this.keySignature[turtle], 'A');
  534. }
  535. break;
  536. case 'consonantstepsizedown':
  537. if (this.lastNotePlayed[turtle] !== null) {
  538. var len = this.lastNotePlayed[turtle][0].length;
  539. value = getStepSizeDown(this.keySignature[turtle], this.lastNotePlayed[turtle][0].slice(0, len - 1));
  540. } else {
  541. value = getStepSizeDown(this.keySignature[turtle], 'A');
  542. }
  543. break;
  544. case 'transpositionfactor':
  545. value = this.transposition[turtle];
  546. break;
  547. case 'staccatofactor':
  548. value = last(this.staccato[turtle]);
  549. break;
  550. case 'slurfactor':
  551. value = -last(this.staccato[turtle]);
  552. break;
  553. case 'beatfactor':
  554. value = this.beatFactor[turtle];
  555. break;
  556. case 'elapsednotes':
  557. value = this.notesPlayed[turtle];
  558. break;
  559. case 'duplicatefactor':
  560. value = this.duplicateFactor[turtle];
  561. break;
  562. case 'skipfactor':
  563. value = this.skipFactor[turtle];
  564. break;
  565. case 'notevolumefactor':
  566. // FIX ME: bias and scaling
  567. value = last(this.polyVolume[turtle]);
  568. break;
  569. case 'turtlepitch':
  570. if (this.lastNotePlayed[turtle] !== null) {
  571. var len = this.lastNotePlayed[turtle][0].length;
  572. value = pitchToNumber(this.lastNotePlayed[turtle][0].slice(0, len - 1), parseInt(this.lastNotePlayed[turtle][0].slice(len - 1)), this.keySignature[turtle]) - that.pitchNumberOffset;
  573. } else {
  574. console.log('Could not find a note for turtle ' + turtle);
  575. value = pitchToNumber('A', 4, this.keySignature[turtle]) - that.pitchNumberOffset;
  576. }
  577. break;
  578. // Deprecated
  579. case 'currentnote':
  580. value = this.currentNotes[turtle];
  581. break;
  582. // Deprecated
  583. case 'currentoctave':
  584. value = this.currentoctave[turtle];
  585. break;
  586. case 'bpmfactor':
  587. if (this.bpm[turtle].length > 0) {
  588. value = last(this.bpm[turtle]);
  589. } else {
  590. value = this._masterBPM;
  591. }
  592. break;
  593. default:
  594. if (name in this.evalParameterDict) {
  595. eval(this.evalParameterDict[name]);
  596. } else {
  597. return;
  598. }
  599. break;
  600. }
  601. if (typeof(value) === 'string') {
  602. if (value.length > 6) {
  603. value = value.substr(0, 5) + '...';
  604. }
  605. this.blocks.blockList[blk].text.text = value;
  606. } else if (name === 'divide') {
  607. this.blocks.blockList[blk].text.text = mixedNumber(value);
  608. }
  609. this.blocks.blockList[blk].container.updateCache();
  610. this.refreshCanvas();
  611. }
  612. };
  613. this.runLogoCommands = function (startHere, env) {
  614. // Save the state before running.
  615. this.saveLocally();
  616. for (var arg in this.evalOnStartList) {
  617. eval(this.evalOnStartList[arg]);
  618. }
  619. this.stopTurtle = false;
  620. this.blocks.unhighlightAll();
  621. this.blocks.bringToTop(); // Draw under blocks.
  622. this.hideMsgs();
  623. // We run the Logo commands here.
  624. var d = new Date();
  625. this.time = d.getTime();
  626. this.firstNoteTime = null;
  627. // Ensure we have at least one turtle.
  628. if (this.turtles.turtleList.length === 0) {
  629. this.turtles.add(null);
  630. }
  631. for (var turtle in this.forwardListener) {
  632. for (var b in this.forwardListener[turtle]) {
  633. for (var i = 0; i < this.forwardListener[turtle][b].length; i++) {
  634. this.stage.removeEventListener('_forward_' + turtle, this.forwardListener[turtle][b][i], false);
  635. }
  636. }
  637. }
  638. for (var turtle in this.rightListener) {
  639. for (var b in this.rightListener[turtle]) {
  640. for (var i = 0; i < this.rightListener[turtle][b].length; i++) {
  641. this.stage.removeEventListener('_right_' + turtle, this.rightListener[turtle][b][i], false);
  642. }
  643. }
  644. }
  645. for (var turtle in this.arcListener) {
  646. for (var b in this.arcListener[turtle]) {
  647. for (var i = 0; i < this.arcListener[turtle][b].length; i++) {
  648. this.stage.removeEventListener('_arc_' + turtle, this.arcListener[turtle][b][i], false);
  649. }
  650. }
  651. }
  652. this._masterBPM = TARGETBPM;
  653. this.defaultBPMFactor = TONEBPM / this._masterBPM;
  654. // Each turtle needs to keep its own wait time and music
  655. // states.
  656. for (var turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
  657. this.turtleTime[turtle] = 0;
  658. this.waitTimes[turtle] = 0;
  659. this.endOfClampSignals[turtle] = {};
  660. this.cp1x[turtle] = 0;
  661. this.cp1y[turtle] = 100;
  662. this.cp2x[turtle] = 100;
  663. this.cp2y[turtle] = 100;
  664. this.inNoteBlock[turtle] = 0;
  665. this.transposition[turtle] = 0;
  666. this.noteBeat[turtle] = [];
  667. this.noteCents[turtle] = [];
  668. this.noteHertz[turtle] = [];
  669. this.lastNotePlayed[turtle] = null;
  670. this.noteStatus[turtle] = null;
  671. this.noteDrums[turtle] = [];
  672. this.notePitches[turtle] = [];
  673. this.noteOctaves[turtle] = [];
  674. this.currentNotes[turtle] = 'G';
  675. this.currentOctaves[turtle] = 4;
  676. this.noteTranspositions[turtle] = [];
  677. this.noteBeatValues[turtle] = [];
  678. this.forwardListener[turtle] = {};
  679. this.rightListener[turtle] = {};
  680. this.arcListener[turtle] = {};
  681. this.beatFactor[turtle] = 1;
  682. this.dotCount[turtle] = 0;
  683. this.invertList[turtle] = [];
  684. this.duplicateFactor[turtle] = 1;
  685. this.skipFactor[turtle] = 1;
  686. this.skipIndex[turtle] = 0;
  687. this.notesPlayed[turtle] = 0;
  688. this.keySignature[turtle] = 'C ' + _('major');
  689. this.pushedNote[turtle] = false;
  690. this.polyVolume[turtle] = [DEFAULTVOLUME];
  691. this.oscList[turtle] = [];
  692. this.bpm[turtle] = [];
  693. this.crescendoDelta[turtle] = [];
  694. this.crescendoInitialVolume[turtle] = [];
  695. this.crescendoVolume[turtle] = [];
  696. this.intervals[turtle] = [];
  697. this.perfect[turtle] = [];
  698. this.diminished[turtle] = [];
  699. this.augmented[turtle] = [];
  700. this.major[turtle] = [];
  701. this.minor[turtle] = [];
  702. this.staccato[turtle] = [];
  703. this.swing[turtle] = [];
  704. this.swingTarget[turtle] = [];
  705. this.swingCarryOver[turtle] = 0;
  706. this.tie[turtle] = false;
  707. this.tieNote[turtle] = [];
  708. this.tieCarryOver[turtle] = 0;
  709. this.drift[turtle] = 0;
  710. this.drumStyle[turtle] = [];
  711. this.voices[turtle] = [];
  712. this.pitchDrumTable[turtle] = {};
  713. this.backward[turtle] = [];
  714. this.vibratoIntensity[turtle] = [];
  715. this.vibratoRate[turtle] = [];
  716. this.dispatchFactor[turtle] = 1;
  717. this.justCounting[turtle] = false;
  718. this.suppressOutput[turtle] = this.runningLilypond;
  719. }
  720. this.pitchNumberOffset = 39; // C4
  721. if (!this.suppressOutput[turtle]) {
  722. this._setSynthVolume(DEFAULTVOLUME, Math.max(this.turtles.turtleList.length - 1), 0);
  723. }
  724. this.inPitchDrumMatrix = false;
  725. this.inMatrix = false;
  726. this.inRhythmRuler = false;
  727. this.rhythmRulerMeasure = null;
  728. this._currentDrumBlock = null;
  729. this.inStatusMatrix = false;
  730. this.pitchBlocks = [];
  731. this.drumBlocks = [];
  732. this.tuplet = false;
  733. this._modeBlock = null;
  734. // Remove any listeners that might be still active
  735. for (var turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
  736. for (var listener in this.turtles.turtleList[turtle].listeners) {
  737. this.stage.removeEventListener(listener, this.turtles.turtleList[turtle].listeners[listener], false);
  738. }
  739. this.turtles.turtleList[turtle].listeners = {};
  740. }
  741. // First we need to reconcile the values in all the value
  742. // blocks with their associated textareas.
  743. // FIXME: Do we still need this check???
  744. for (var blk = 0; blk < this.blocks.blockList.length; blk++) {
  745. if (this.blocks.blockList[blk].label != null) {
  746. if (this.blocks.blockList[blk].labelattr != null && this.blocks.blockList[blk].labelattr.value !== '♮') {
  747. this.blocks.blockList[blk].value = this.blocks.blockList[blk].label.value + this.blocks.blockList[blk].labelattr.value;
  748. } else {
  749. this.blocks.blockList[blk].value = this.blocks.blockList[blk].label.value;
  750. }
  751. }
  752. }
  753. // Init the graphic state.
  754. for (var turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
  755. this.turtles.turtleList[turtle].container.x = this.turtles.turtleX2screenX(this.turtles.turtleList[turtle].x);
  756. this.turtles.turtleList[turtle].container.y = this.turtles.turtleY2screenY(this.turtles.turtleList[turtle].y);
  757. }
  758. // Set up status block
  759. if (docById('statusDiv').style.visibility === 'visible') {
  760. this.statusMatrix.init(this);
  761. }
  762. // Execute turtle code here... Find the start block (or the
  763. // top of each stack) and build a list of all of the named
  764. // action stacks.
  765. var startBlocks = [];
  766. this.blocks.findStacks();
  767. this.actions = {};
  768. for (var blk = 0; blk < this.blocks.stackList.length; blk++) {
  769. if (this.blocks.blockList[this.blocks.stackList[blk]].name === 'start' || this.blocks.blockList[this.blocks.stackList[blk]].name === 'drum') {
  770. // Don't start on a start block in the trash.
  771. if (!this.blocks.blockList[this.blocks.stackList[blk]].trash) {
  772. // Don't start on a start block with no connections.
  773. if (this.blocks.blockList[this.blocks.stackList[blk]].connections[1] != null) {
  774. startBlocks.push(this.blocks.stackList[blk]);
  775. }
  776. }
  777. } else if (this.blocks.blockList[this.blocks.stackList[blk]].name === 'action') {
  778. // Does the action stack have a name?
  779. var c = this.blocks.blockList[this.blocks.stackList[blk]].connections[1];
  780. var b = this.blocks.blockList[this.blocks.stackList[blk]].connections[2];
  781. if (c != null && b != null) {
  782. // Don't use an action block in the trash.
  783. if (!this.blocks.blockList[this.blocks.stackList[blk]].trash) {
  784. this.actions[this.blocks.blockList[c].value] = b;
  785. }
  786. }
  787. }
  788. }
  789. this.svgOutput = '';
  790. this.svgBackground = true;
  791. this.parentFlowQueue = {};
  792. this.unhightlightQueue = {};
  793. this.parameterQueue = {};
  794. if (this.turtleDelay === 0) {
  795. // Don't update parameters when running full speed.
  796. this._clearParameterBlocks();
  797. }
  798. this.onRunTurtle();
  799. // And mark all turtles as not running.
  800. for (var turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
  801. this.turtles.turtleList[turtle].running = false;
  802. }
  803. // (2) Execute the stack.
  804. // A bit complicated because we have lots of corner cases:
  805. if (startHere != null) {
  806. // console.log('startHere is ' + this.blocks.blockList[startHere].name);
  807. // If a block to start from was passed, find its
  808. // associated turtle, i.e., which turtle should we use?
  809. var turtle = 0;
  810. while(this.blocks.turtles.turtleList[turtle].trash && turtle < this.turtles.turtleList.length) {
  811. turtle += 1;
  812. }
  813. if (this.blocks.blockList[startHere].name === 'start' || this.blocks.blockList[startHere].name === 'drum') {
  814. var turtle = this.blocks.blockList[startHere].value;
  815. // console.log('starting on start with turtle ' + turtle);
  816. // } else {
  817. // console.log('starting on ' + this.blocks.blockList[startHere].name + ' with turtle ' + turtle);
  818. }
  819. this.turtles.turtleList[turtle].queue = [];
  820. this.parentFlowQueue[turtle] = [];
  821. this.unhightlightQueue[turtle] = [];
  822. this.parameterQueue[turtle] = [];
  823. this.turtles.turtleList[turtle].running = true;
  824. this._runFromBlock(this, turtle, startHere, 0, env);
  825. } else if (startBlocks.length > 0) {
  826. // If there are start blocks, run them all.
  827. for (var b = 0; b < startBlocks.length; b++) {
  828. var turtle = this.blocks.blockList[startBlocks[b]].value;
  829. this.turtles.turtleList[turtle].queue = [];
  830. this.parentFlowQueue[turtle] = [];
  831. this.unhightlightQueue[turtle] = [];
  832. this.parameterQueue[turtle] = [];
  833. if (!this.turtles.turtleList[turtle].trash) {
  834. this.turtles.turtleList[turtle].running = true;
  835. this._runFromBlock(this, turtle, startBlocks[b], 0, env);
  836. }
  837. }
  838. } else {
  839. // console.log('nothing to run');
  840. if (this.suppressOutput[turtle]) {
  841. this.errorMsg(NOACTIONERRORMSG, null, _('start'));
  842. this.suppressOutput[turtle] = false;
  843. this.checkingLilypond = false;
  844. // Reset cursor.
  845. document.body.style.cursor = 'default';
  846. }
  847. }
  848. this.refreshCanvas();
  849. };
  850. this._runFromBlock = function (that, turtle, blk, isflow, receivedArg) {
  851. if (blk == null) {
  852. return;
  853. }
  854. var delay = that.turtleDelay + that.waitTimes[turtle];
  855. that.waitTimes[turtle] = 0;
  856. if (!that.stopTurtle) {
  857. if (that.turtleDelay === TURTLESTEP) {
  858. // Step mode
  859. if (!(turtle in that.stepQueue)) {
  860. that.stepQueue[turtle] = [];
  861. }
  862. that.stepQueue[turtle].push(blk);
  863. } else {
  864. setTimeout(function () {
  865. that._runFromBlockNow(that, turtle, blk, isflow, receivedArg);
  866. }, delay);
  867. }
  868. }
  869. };
  870. this._blockSetter = function (blk, value, turtle) {
  871. var turtleObj = this.turtles.turtleList[turtle];
  872. switch (this.blocks.blockList[blk].name) {
  873. case 'x':
  874. turtleObj.doSetXY(value, turtleObj.x);
  875. break;
  876. case 'y':
  877. turtleObj.doSetXY(turtleObj.y, value);
  878. break;
  879. case 'heading':
  880. turtleObj.doSetHeading(value);
  881. break;
  882. case 'color':
  883. turtleObj.doSetColor(value);
  884. break;
  885. case 'shade':
  886. turtleObj.doSetValue(value);
  887. break;
  888. case 'grey':
  889. turtleObj.doSetChroma(value);
  890. break;
  891. case 'pensize':
  892. turtleObj.doSetPensize(value);
  893. break;
  894. case 'namedbox':
  895. var name = this.blocks.blockList[blk].privateData;
  896. if (name in this.boxes) {
  897. this.boxes[name] = value;
  898. } else {
  899. this.errorMsg(NOBOXERRORMSG, blk, name);
  900. }
  901. break;
  902. case 'box':
  903. var cblk = this.blocks.blockList[blk].connections[1];
  904. var name = this.parseArg(this, turtle, cblk, blk);
  905. if (name in this.boxes) {
  906. this.boxes[name] = value;
  907. } else {
  908. this.errorMsg(NOBOXERRORMSG, blk, name);
  909. }
  910. break;
  911. case 'bpmfactor':
  912. var len = this.bpm[turtle].length;
  913. if (len > 0) {
  914. this.bpm[turtle][len - 1] = value;
  915. }
  916. break;
  917. case 'transpositionfactor':
  918. var len = this.transposition[turtle].length;
  919. if (len > 0) {
  920. this.transposition[turtle][len - 1] = value;
  921. }
  922. break;
  923. case 'staccatofactor':
  924. var len = this.staccato[turtle].length;
  925. if (len > 0) {
  926. this.staccato[turtle][len - 1] = value;
  927. }
  928. break;
  929. case 'slurfactor':
  930. // Slur is stored as a negative staccato.
  931. var len = this.staccato[turtle].length;
  932. if (len > 0) {
  933. this.staccato[turtle][len - 1] = -value;
  934. }
  935. break;
  936. case 'beatfactor':
  937. this.beatFactor[turtle] = value;
  938. break;
  939. case 'duplicatefactor':
  940. var len = this.duplicateFactor[turtle].length;
  941. if (len > 0) {
  942. this.duplicateFactor[turtle][len - 1] = value;
  943. }
  944. break;
  945. case 'skipfactor':
  946. var len = this.skipFactor[turtle].length;
  947. if (len > 0) {
  948. this.skipFactor[turtle][len - 1] = value;
  949. }
  950. break;
  951. case 'turtlepitch':
  952. var obj = numberToPitch(value + this.pitchNumberOffset);
  953. this.lastNotePlayed[turtle] = [obj[0]+obj[1], this.lastNotePlayed[turtle][1]];
  954. break;
  955. // Deprecated
  956. case 'currentnote':
  957. // A bit ugly because the setter call added the value
  958. // to the current note.
  959. var len = this.currentNotes[turtle].length;
  960. value = parseInt(value.slice(len));
  961. var newNoteObj = getNote(this.currentNotes[turtle], this.currentOctaves[turtle], value, this.keySignature[turtle]);
  962. this.currentNotes[turtle] = newNoteObj[0];
  963. this.currentOctaves[turtle] = newNoteObj[1];
  964. break;
  965. // Deprecated
  966. case 'currentoctave':
  967. this.currentOctaves[turtle] = Math.round(value);
  968. if (this.currentOctaves[turtle] < 1) {
  969. this.currentOctaves[turtle] = 1;
  970. }
  971. break;
  972. case 'notevolumefactor':
  973. var len = this.transposition[turtle].length;
  974. this.polyVolume[turtle][len - 1] = value;
  975. this._setSynthVolume(value, turtle);
  976. break;
  977. default:
  978. if (this.blocks.blockList[blk].name in this.evalSetterDict) {
  979. eval(this.evalSetterDict[this.blocks.blockList[blk].name]);
  980. break;
  981. }
  982. this.errorMsg(_('Block does not support incrementing.'), blk);
  983. }
  984. };
  985. this._runFromBlockNow = function (that, turtle, blk, isflow, receivedArg, queueStart) {
  986. // Run a stack of blocks, beginning with blk.
  987. // Sometimes we don't want to unwind the entire queue.
  988. if (queueStart === undefined) {
  989. queueStart = 0;
  990. }
  991. // (1) Evaluate any arguments (beginning with connection[1]);
  992. var args = [];
  993. if (that.blocks.blockList[blk].protoblock.args > 0) {
  994. for (var i = 1; i < that.blocks.blockList[blk].protoblock.args + 1; i++) {
  995. if (that.blocks.blockList[blk].protoblock.dockTypes[i] === 'in' && that.blocks.blockList[blk].connections[i] == null){
  996. console.log('skipping null inflow args');
  997. } else {
  998. args.push(that.parseArg(that, turtle, that.blocks.blockList[blk].connections[i], blk, receivedArg));
  999. }
  1000. }
  1001. }
  1002. // (2) Run function associated with the block;
  1003. if (that.blocks.blockList[blk].isValueBlock()) {
  1004. var nextFlow = null;
  1005. } else {
  1006. // All flow blocks have a nextFlow, but it can be null
  1007. // (i.e., end of a flow).
  1008. if (that.backward[turtle].length > 0) {
  1009. // We only run backwards in the "first generation" children.
  1010. if (that.blocks.blockList[last(that.backward[turtle])].name === 'backward') {
  1011. var c = 1;
  1012. } else {
  1013. var c = 2;
  1014. }
  1015. if (!that.blocks.sameGeneration(that.blocks.blockList[last(that.backward[turtle])].connections[c], blk)) {
  1016. var nextFlow = last(that.blocks.blockList[blk].connections);
  1017. } else {
  1018. var nextFlow = that.blocks.blockList[blk].connections[0];
  1019. if (that.blocks.blockList[nextFlow].name === 'action' || that.blocks.blockList[nextFlow].name === 'backward') {
  1020. nextFlow = null;
  1021. } else {
  1022. if (!that.blocks.sameGeneration(that.blocks.blockList[last(that.backward[turtle])].connections[c], nextFlow)) {
  1023. var nextFlow = last(that.blocks.blockList[blk].connections);
  1024. } else {
  1025. var nextFlow = that.blocks.blockList[blk].connections[0];
  1026. }
  1027. }
  1028. }
  1029. } else {
  1030. var nextFlow = last(that.blocks.blockList[blk].connections);
  1031. }
  1032. if (nextFlow === -1) {
  1033. nextFlow = null;
  1034. }
  1035. var queueBlock = new Queue(nextFlow, 1, blk, receivedArg);
  1036. if (nextFlow != null) { // This could be the last block
  1037. that.turtles.turtleList[turtle].queue.push(queueBlock);
  1038. }
  1039. }
  1040. // Some flow blocks have childflows, e.g., repeat.
  1041. var childFlow = null;
  1042. var childFlowCount = 0;
  1043. var actionArgs = [];
  1044. if (that.blocks.visible) {
  1045. that.blocks.highlight(blk, false);
  1046. }
  1047. switch (that.blocks.blockList[blk].name) {
  1048. case 'dispatch':
  1049. // Dispatch an event.
  1050. if (args.length === 1) {
  1051. // If the event is not in the event list, add it.
  1052. if (!(args[0] in that.eventList)) {
  1053. var event = new Event(args[0]);
  1054. that.eventList[args[0]] = event;
  1055. }
  1056. that.stage.dispatchEvent(args[0]);
  1057. }
  1058. break;
  1059. case 'listen':
  1060. if (args.length === 2) {
  1061. if (!(args[1] in that.actions)) {
  1062. that.errorMsg(NOACTIONERRORMSG, blk, args[1]);
  1063. that.stopTurtle = true;
  1064. } else {
  1065. var __listener = function (event) {
  1066. if (that.turtles.turtleList[turtle].running) {
  1067. var queueBlock = new Queue(that.actions[args[1]], 1, blk);
  1068. that.parentFlowQueue[turtle].push(blk);
  1069. that.turtles.turtleList[turtle].queue.push(queueBlock);
  1070. } else {
  1071. // Since the turtle has stopped
  1072. // running, we need to run the stack
  1073. // from here.
  1074. if (isflow) {
  1075. that._runFromBlockNow(that, turtle, that.actions[args[1]], isflow, receivedArg);
  1076. } else {
  1077. that._runFromBlock(that, turtle, that.actions[args[1]], isflow, receivedArg);
  1078. }
  1079. }
  1080. };
  1081. // If there is already a listener, remove it
  1082. // before adding the new one.
  1083. that._setListener(turtle, args[0], __listener);
  1084. }
  1085. }
  1086. break;
  1087. case 'start':
  1088. case 'drum':
  1089. if (args.length === 1) {
  1090. childFlow = args[0];
  1091. childFlowCount = 1;
  1092. }
  1093. break;
  1094. case 'nameddo':
  1095. var name = that.blocks.blockList[blk].privateData;
  1096. if (name in that.actions) {
  1097. if (!that.justCounting[turtle]) {
  1098. lilypondLineBreak(that, turtle);
  1099. }
  1100. if (that.backward[turtle].length > 0) {
  1101. childFlow = that.blocks.findBottomBlock(that.actions[name]);
  1102. var actionBlk = that.blocks.findTopBlock(that.actions[name]);
  1103. that.backward[turtle].push(actionBlk);
  1104. var listenerName = '_backward_action_' + turtle + '_' + blk;
  1105. var nextBlock = this.blocks.blockList[actionBlk].connections[2];
  1106. if (nextBlock == null) {
  1107. that.backward[turtle].pop();
  1108. } else {
  1109. that.endOfClampSignals[turtle][nextBlock] = [listenerName];
  1110. }
  1111. var __listener = function (event) {
  1112. that.backward[turtle].pop();
  1113. };
  1114. that._setListener(turtle, listenerName, __listener);
  1115. } else {
  1116. childFlow = that.actions[name];
  1117. }
  1118. childFlowCount = 1;
  1119. } else {
  1120. that.errorMsg(NOACTIONERRORMSG, blk, name);
  1121. that.stopTurtle = true;
  1122. }
  1123. break;
  1124. // If we clicked on an action block, treat it like a do
  1125. // block.
  1126. case 'action':
  1127. case 'do':
  1128. if (args.length > 0) {
  1129. if (args[0] in that.actions) {
  1130. if (!that.justCounting[turtle]) {
  1131. lilypondLineBreak(that, turtle);
  1132. }
  1133. childFlow = that.actions[args[0]];
  1134. childFlowCount = 1;
  1135. } else {
  1136. console.log('action ' + args[0] + ' not found');
  1137. that.errorMsg(NOACTIONERRORMSG, blk, args[0]);
  1138. that.stopTurtle = true;
  1139. }
  1140. }
  1141. break;
  1142. case 'nameddoArg':
  1143. var name = that.blocks.blockList[blk].privateData;
  1144. while(actionArgs.length > 0) {
  1145. actionArgs.pop();
  1146. }
  1147. if (that.blocks.blockList[blk].argClampSlots.length > 0) {
  1148. for (var i = 0; i < that.blocks.blockList[blk].argClampSlots.length; i++){
  1149. var t = (that.parseArg(that, turtle, that.blocks.blockList[blk].connections[i + 1], blk, receivedArg));
  1150. actionArgs.push(t);
  1151. }
  1152. }
  1153. if (name in that.actions) {
  1154. if (!that.justCounting[turtle]) {
  1155. lilypondLineBreak(that, turtle);
  1156. }
  1157. if (that.backward[turtle].length > 0) {
  1158. childFlow = that.blocks.findBottomBlock(that.actions[name]);
  1159. var actionBlk = that.blocks.findTopBlock(that.actions[name]);
  1160. that.backward[turtle].push(actionBlk);
  1161. var listenerName = '_backward_action_' + turtle + '_' + blk;
  1162. var nextBlock = this.blocks.blockList[actionBlk].connections[2];
  1163. if (nextBlock == null) {
  1164. that.backward[turtle].pop();
  1165. } else {
  1166. that.endOfClampSignals[turtle][nextBlock] = [listenerName];
  1167. }
  1168. var __listener = function (event) {
  1169. that.backward[turtle].pop();
  1170. };
  1171. that._setListener(turtle, listenerName, __listener);
  1172. } else {
  1173. childFlow = that.actions[name]
  1174. }
  1175. childFlowCount = 1;
  1176. } else{
  1177. that.errorMsg(NOACTIONERRORMSG, blk, name);
  1178. that.stopTurtle = true;
  1179. }
  1180. break;
  1181. case 'doArg':
  1182. while(actionArgs.length > 0) {
  1183. actionArgs.pop();
  1184. }
  1185. if (that.blocks.blockList[blk].argClampSlots.length > 0) {
  1186. for (var i = 0; i < that.blocks.blockList[blk].argClampSlots.length; i++){
  1187. var t = (that.parseArg(that, turtle, that.blocks.blockList[blk].connections[i + 2], blk, receivedArg));
  1188. actionArgs.push(t);
  1189. }
  1190. }
  1191. if (args.length >= 1) {
  1192. if (args[0] in that.actions) {
  1193. if (!that.justCounting[turtle]) {
  1194. lilypondLineBreak(that, turtle);
  1195. }
  1196. actionName = args[0];
  1197. childFlow = that.actions[args[0]];
  1198. childFlowCount = 1;
  1199. } else {
  1200. that.errorMsg(NOACTIONERRORMSG, blk, args[0]);
  1201. that.stopTurtle = true;
  1202. }
  1203. }
  1204. break;
  1205. case 'forever':
  1206. if (args.length === 1) {
  1207. childFlow = args[0];
  1208. // If we are running in non-interactive mode, we
  1209. // need to put a bounds on "forever".
  1210. if (that.suppressOutput[turtle]) {
  1211. childFlowCount = 10;
  1212. } else {
  1213. childFlowCount = -1;
  1214. }
  1215. }
  1216. break;
  1217. case 'hidden':
  1218. case 'hiddennoflow':
  1219. // Hidden block is used at end of clamps and actions to
  1220. // trigger listeners.
  1221. break;
  1222. case 'break':
  1223. that._doBreak(turtle);
  1224. // Since we pop the queue, we need to unhighlight our
  1225. // parent.
  1226. var parentBlk = that.blocks.blockList[blk].connections[0];
  1227. if (parentBlk != null) {
  1228. that.unhightlightQueue[turtle].push(parentBlk);
  1229. }
  1230. break;
  1231. case 'wait':
  1232. if (args.length === 1) {
  1233. that._doWait(turtle, args[0]);
  1234. }
  1235. break;
  1236. case 'print':
  1237. if (!that.inStatusMatrix) {
  1238. if (args.length === 1) {
  1239. if (args[0] !== null) {
  1240. that.textMsg(args[0].toString());
  1241. }
  1242. }
  1243. }
  1244. break;
  1245. case 'speak':
  1246. if (args.length === 1) {
  1247. if (that.meSpeak) {
  1248. var text = args[0];
  1249. var new_text = "";
  1250. for (var i = 0; i < text.length; i++){
  1251. if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') || text[i] === ',' || text[i] === '.' || text[i] === ' ')
  1252. new_text += text[i];
  1253. }
  1254. that.meSpeak.speak(new_text);
  1255. }
  1256. }
  1257. break;
  1258. case 'repeat':
  1259. if (args.length === 2) {
  1260. if (typeof(args[0]) === 'string') {
  1261. that.errorMsg(NANERRORMSG, blk);
  1262. that.stopTurtle = true;
  1263. } else if (args[0] <= 0) {
  1264. that.errorMsg(POSNUMBER,blk);
  1265. } else {
  1266. childFlow = args[1];
  1267. childFlowCount = Math.floor(args[0]);
  1268. }
  1269. }
  1270. break;
  1271. case 'clamp':
  1272. if (args.length === 1) {
  1273. childFlow = args[0];
  1274. childFlowCount = 1;
  1275. }
  1276. break;
  1277. case 'until':
  1278. // Similar to 'while'
  1279. if (args.length === 2) {
  1280. // Queue the child flow.
  1281. childFlow = args[1];
  1282. childFlowCount = 1;
  1283. if (!args[0]) {
  1284. // We will add the outflow of the until block
  1285. // each time through, so we pop it off so as
  1286. // to not accumulate multiple copies.
  1287. var queueLength = that.turtles.turtleList[turtle].queue.length;
  1288. if (queueLength > 0) {
  1289. if (that.turtles.turtleList[turtle].queue[queueLength - 1].parentBlk === blk) {
  1290. that.turtles.turtleList[turtle].queue.pop();
  1291. }
  1292. }
  1293. // Requeue.
  1294. var parentBlk = that.blocks.blockList[blk].connections[0];
  1295. var queueBlock = new Queue(blk, 1, parentBlk);
  1296. that.parentFlowQueue[turtle].push(parentBlk);
  1297. that.turtles.turtleList[turtle].queue.push(queueBlock);
  1298. } else {
  1299. // Since an until block was requeued each
  1300. // time, we need to flush the queue of all but
  1301. // the last one, otherwise the child of the
  1302. // until block is executed multiple times.
  1303. var queueLength = that.turtles.turtleList[turtle].queue.length;
  1304. for (var i = queueLength - 1; i > 0; i--) {
  1305. if (that.turtles.turtleList[turtle].queue[i].parentBlk === blk) {
  1306. that.turtles.turtleList[turtle].queue.pop();
  1307. }
  1308. }
  1309. }
  1310. }
  1311. break;
  1312. case 'waitFor':
  1313. if (args.length === 1) {
  1314. if (!args[0]) {
  1315. // Requeue.
  1316. var parentBlk = that.blocks.blockList[blk].connections[0];
  1317. var queueBlock = new Queue(blk, 1, parentBlk);
  1318. that.parentFlowQueue[turtle].push(parentBlk);
  1319. that.turtles.turtleList[turtle].queue.push(queueBlock);
  1320. that._doWait(0.05);
  1321. } else {
  1322. // Since a wait for block was requeued each
  1323. // time, we need to flush the queue of all but
  1324. // the last one, otherwise the child of the
  1325. // while block is executed multiple times.
  1326. var queueLength = that.turtles.turtleList[turtle].queue.length;
  1327. for (var i = queueLength - 1; i > 0; i--) {
  1328. if (that.turtles.turtleList[turtle].queue[i].parentBlk === blk) {
  1329. that.turtles.turtleList[turtle].queue.pop();
  1330. }
  1331. }
  1332. }
  1333. }
  1334. break;
  1335. case 'if':
  1336. if (args.length === 2) {
  1337. if (args[0]) {
  1338. childFlow = args[1];
  1339. childFlowCount = 1;
  1340. }
  1341. }
  1342. break;
  1343. case 'ifthenelse':
  1344. if (args.length === 3) {
  1345. if (args[0]) {
  1346. childFlow = args[1];
  1347. childFlowCount = 1;
  1348. } else {
  1349. childFlow = args[2];
  1350. childFlowCount = 1;
  1351. }
  1352. }
  1353. break;
  1354. case 'while':
  1355. // While is tricky because we need to recalculate
  1356. // args[0] each time, so we requeue the While block
  1357. // itself.
  1358. if (args.length === 2) {
  1359. if (args[0]) {
  1360. // We will add the outflow of the while block
  1361. // each time through, so we pop it off so as
  1362. // to not accumulate multiple copies.
  1363. var queueLength = that.turtles.turtleList[turtle].queue.length;
  1364. if (queueLength > 0) {
  1365. if (that.turtles.turtleList[turtle].queue[queueLength - 1].parentBlk === blk) {
  1366. that.turtles.turtleList[turtle].queue.pop();
  1367. }
  1368. }
  1369. var parentBlk = that.blocks.blockList[blk].connections[0];
  1370. var queueBlock = new Queue(blk, 1, parentBlk);
  1371. that.parentFlowQueue[turtle].push(parentBlk);
  1372. that.turtles.turtleList[turtle].queue.push(queueBlock);
  1373. // and queue the interior child flow.
  1374. childFlow = args[1];
  1375. childFlowCount = 1;
  1376. } else {
  1377. // Since a while block was requeued each time,
  1378. // we need to flush the queue of all but the
  1379. // last one, otherwise the child of the while
  1380. // block is executed multiple times.
  1381. var queueLength = that.turtles.turtleList[turtle].queue.length;
  1382. for (var i = queueLength - 1; i > 0; i--) {
  1383. if (that.turtles.turtleList[turtle].queue[i].parentBlk === blk) {
  1384. // if (that.turtles.turtleList[turtle].queue[i].blk === blk) {
  1385. that.turtles.turtleList[turtle].queue.pop();
  1386. }
  1387. }
  1388. }
  1389. }
  1390. break;
  1391. case 'storein':
  1392. if (args.length === 2) {
  1393. that.boxes[args[0]] = args[1];
  1394. }
  1395. break;
  1396. case 'incrementOne':
  1397. var i = 1;
  1398. case 'increment':
  1399. // If the 2nd arg is not set, default to 1.
  1400. if (args.length === 2) {
  1401. var i = args[1];
  1402. }
  1403. if (args.length >= 1) {
  1404. var settingBlk = that.blocks.blockList[blk].connections[1];
  1405. that._blockSetter(settingBlk, args[0] + i, turtle);
  1406. }
  1407. break;
  1408. case 'clear':
  1409. that.svgBackground = true;
  1410. that.turtles.turtleList[turtle].doClear(true, true);
  1411. break;
  1412. case 'setxy':
  1413. if (args.length === 2) {
  1414. if (typeof(args[0]) === 'string' || typeof(args[1]) === 'string') {
  1415. that.errorMsg(NANERRORMSG, blk);
  1416. that.stopTurtle = true;
  1417. } else if (that.inMatrix) {
  1418. that.pitchTimeMatrix.addRowBlock(blk);
  1419. if (that.pitchBlocks.indexOf(blk) === -1) {
  1420. that.pitchBlocks.push(blk);
  1421. }
  1422. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1423. that.pitchTimeMatrix.rowArgs.push([args[0], args[1]]);
  1424. } else {
  1425. that.turtles.turtleList[turtle].doSetXY(args[0], args[1]);
  1426. }
  1427. }
  1428. break;
  1429. case 'arc':
  1430. if (args.length === 2) {
  1431. if (typeof(args[0]) === 'string' || typeof(args[1]) === 'string') {
  1432. that.errorMsg(NANERRORMSG, blk);
  1433. that.stopTurtle = true;
  1434. } else if (that.inMatrix) {
  1435. that.pitchTimeMatrix.addRowBlock(blk);
  1436. if (that.pitchBlocks.indexOf(blk) === -1) {
  1437. that.pitchBlocks.push(blk);
  1438. }
  1439. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1440. that.pitchTimeMatrix.rowArgs.push([args[0], args[1]]);
  1441. } else if (that.inNoteBlock[turtle] > 0) {
  1442. if (!that.suppressOutput[turtle]) {
  1443. var delta = args[0] / NOTEDIV;
  1444. var listenerName = '_arc_' + turtle + '_' + that.whichNoteBlock[turtle];
  1445. var __listener = function (event) {
  1446. that.turtles.turtleList[turtle].doArc(delta * that.dispatchFactor[turtle], args[1]);
  1447. };
  1448. if (that.whichNoteBlock[turtle] in that.arcListener[turtle]) {
  1449. that.arcListener[turtle][that.whichNoteBlock[turtle]].push(__listener);
  1450. } else {
  1451. that.arcListener[turtle][that.whichNoteBlock[turtle]] = [__listener];
  1452. }
  1453. that.stage.addEventListener(listenerName, __listener, false);
  1454. }
  1455. } else {
  1456. that.turtles.turtleList[turtle].doArc(args[0], args[1]);
  1457. }
  1458. }
  1459. break;
  1460. case 'bezier':
  1461. if (args.length === 2) {
  1462. if (typeof(args[0]) === 'string' || typeof(args[1]) === 'string') {
  1463. that.errorMsg(NANERRORMSG, blk);
  1464. that.stopTurtle = true;
  1465. } else {
  1466. that.turtles.turtleList[turtle].doBezier(that.cp1x[turtle], that.cp1y[turtle], that.cp2x[turtle], that.cp2y[turtle], args[0], args[1]);
  1467. }
  1468. }
  1469. break;
  1470. case 'controlpoint1':
  1471. if (args.length === 2) {
  1472. if (typeof(args[0]) === 'string' || typeof(args[1]) === 'string') {
  1473. that.errorMsg(NANERRORMSG, blk);
  1474. that.stopTurtle = true;
  1475. } else {
  1476. that.cp1x[turtle] = args[0];
  1477. that.cp1y[turtle] = args[1];
  1478. }
  1479. }
  1480. break;
  1481. case 'controlpoint2':
  1482. if (args.length === 2) {
  1483. if (typeof(args[0]) === 'string' || typeof(args[1]) === 'string') {
  1484. that.errorMsg(NANERRORMSG, blk);
  1485. that.stopTurtle = true;
  1486. } else {
  1487. that.cp2x[turtle] = args[0];
  1488. that.cp2y[turtle] = args[1];
  1489. }
  1490. }
  1491. break;
  1492. case 'return':
  1493. if (args.length === 1) {
  1494. that.returns.push(args[0]);
  1495. }
  1496. break;
  1497. case 'returnToUrl':
  1498. var URL = window.location.href;
  1499. var urlParts;
  1500. var outurl;
  1501. if (URL.indexOf('?') > 0) {
  1502. var urlParts = URL.split('?');
  1503. if (urlParts[1].indexOf('&') >0) {
  1504. var newUrlParts = urlParts[1].split('&');
  1505. for (var i = 0; i < newUrlParts.length; i++) {
  1506. if (newUrlParts[i].indexOf('=') > 0) {
  1507. var tempargs = newUrlParts[i].split('=');
  1508. switch (tempargs[0].toLowerCase()) {
  1509. case 'outurl':
  1510. outurl = tempargs[1];
  1511. break;
  1512. }
  1513. }
  1514. }
  1515. }
  1516. }
  1517. if (args.length === 1) {
  1518. var jsonRet = {};
  1519. jsonRet['result'] = args[0];
  1520. var json= JSON.stringify(jsonRet);
  1521. var xmlHttp = new XMLHttpRequest();
  1522. xmlHttp.open('POST',outurl, true);
  1523. // Call a function when the state changes.
  1524. xmlHttp.onreadystatechange = function () {
  1525. if(xmlHttp.readyState === 4 && xmlHttp.status === 200) {
  1526. alert(xmlHttp.responseText);
  1527. }
  1528. };
  1529. xmlHttp.send(json);
  1530. }
  1531. break;
  1532. case 'forward':
  1533. if (args.length === 1) {
  1534. if (typeof(args[0]) === 'string') {
  1535. that.errorMsg(NANERRORMSG, blk);
  1536. that.stopTurtle = true;
  1537. } else if (that.inMatrix) {
  1538. that.pitchTimeMatrix.addRowBlock(blk);
  1539. if (that.pitchBlocks.indexOf(blk) === -1) {
  1540. that.pitchBlocks.push(blk);
  1541. }
  1542. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1543. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1544. } else if (that.inNoteBlock[turtle] > 0) {
  1545. if (!that.suppressOutput[turtle]) {
  1546. var dist = args[0] / NOTEDIV;
  1547. var listenerName = '_forward_' + turtle + '_' + that.whichNoteBlock[turtle];
  1548. var __listener = function (event) {
  1549. that.turtles.turtleList[turtle].doForward(dist * that.dispatchFactor[turtle]);
  1550. };
  1551. if (that.whichNoteBlock[turtle] in that.forwardListener[turtle]) {
  1552. that.forwardListener[turtle][that.whichNoteBlock[turtle]].push(__listener);
  1553. } else {
  1554. that.forwardListener[turtle][that.whichNoteBlock[turtle]] = [__listener];
  1555. }
  1556. that.stage.addEventListener(listenerName, __listener, false);
  1557. }
  1558. } else {
  1559. that.turtles.turtleList[turtle].doForward(args[0]);
  1560. }
  1561. }
  1562. break;
  1563. case 'back':
  1564. if (args.length === 1) {
  1565. if (typeof(args[0]) === 'string') {
  1566. that.errorMsg(NANERRORMSG, blk);
  1567. that.stopTurtle = true;
  1568. } else if (that.inMatrix) {
  1569. that.pitchTimeMatrix.addRowBlock(blk);
  1570. if (that.pitchBlocks.indexOf(blk) === -1) {
  1571. that.pitchBlocks.push(blk);
  1572. }
  1573. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1574. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1575. } else if (that.inNoteBlock[turtle] > 0) {
  1576. if (!that.suppressOutput[turtle]) {
  1577. var dist = -args[0] / NOTEDIV;
  1578. var listenerName = '_forward_' + turtle + '_' + that.whichNoteBlock[turtle];
  1579. var __listener = function (event) {
  1580. that.turtles.turtleList[turtle].doForward(dist * that.dispatchFactor[turtle]);
  1581. };
  1582. if (that.whichNoteBlock[turtle] in that.forwardListener[turtle]) {
  1583. that.forwardListener[turtle][that.whichNoteBlock[turtle]].push(__listener);
  1584. } else {
  1585. that.forwardListener[turtle][that.whichNoteBlock[turtle]] = [__listener];
  1586. }
  1587. that.stage.addEventListener(listenerName, __listener, false);
  1588. }
  1589. } else {
  1590. that.turtles.turtleList[turtle].doForward(-args[0]);
  1591. }
  1592. }
  1593. break;
  1594. case 'right':
  1595. if (args.length === 1) {
  1596. if (typeof(args[0]) === 'string') {
  1597. that.errorMsg(NANERRORMSG, blk);
  1598. that.stopTurtle = true;
  1599. } else if (that.inMatrix) {
  1600. that.pitchTimeMatrix.addRowBlock(blk);
  1601. if (that.pitchBlocks.indexOf(blk) === -1) {
  1602. that.pitchBlocks.push(blk);
  1603. }
  1604. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1605. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1606. } else if (that.inNoteBlock[turtle] > 0) {
  1607. if (!that.suppressOutput[turtle]) {
  1608. var delta = args[0] / NOTEDIV;
  1609. var listenerName = '_right_' + turtle + '_' + that.whichNoteBlock[turtle];
  1610. var __listener = function (event) {
  1611. that.turtles.turtleList[turtle].doRight(delta * that.dispatchFactor[turtle]);
  1612. };
  1613. if (that.whichNoteBlock[turtle] in that.rightListener[turtle]) {
  1614. that.rightListener[turtle][that.whichNoteBlock[turtle]].push(__listener);
  1615. } else {
  1616. that.rightListener[turtle][that.whichNoteBlock[turtle]] = [__listener];
  1617. }
  1618. that.stage.addEventListener(listenerName, __listener, false);
  1619. }
  1620. } else {
  1621. that.turtles.turtleList[turtle].doRight(args[0]);
  1622. }
  1623. }
  1624. break;
  1625. case 'left':
  1626. if (args.length === 1) {
  1627. if (typeof(args[0]) === 'string') {
  1628. that.errorMsg(NANERRORMSG, blk);
  1629. that.stopTurtle = true;
  1630. } else if (that.inMatrix) {
  1631. that.pitchTimeMatrix.addRowBlock(blk);
  1632. if (that.pitchBlocks.indexOf(blk) === -1) {
  1633. that.pitchBlocks.push(blk);
  1634. }
  1635. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1636. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1637. } else if (that.inNoteBlock[turtle] > 0) {
  1638. if (!that.suppressOutput[turtle]) {
  1639. var delta = -args[0] / NOTEDIV;
  1640. var listenerName = '_right_' + turtle + '_' + that.whichNoteBlock[turtle];
  1641. var __listener = function (event) {
  1642. that.turtles.turtleList[turtle].doRight(delta * that.dispatchFactor[turtle]);
  1643. };
  1644. if (that.whichNoteBlock[turtle] in that.rightListener[turtle]) {
  1645. that.rightListener[turtle][that.whichNoteBlock[turtle]].push(__listener);
  1646. } else {
  1647. that.rightListener[turtle][that.whichNoteBlock[turtle]] = [__listener];
  1648. }
  1649. that.stage.addEventListener(listenerName, __listener, false);
  1650. }
  1651. } else {
  1652. that.turtles.turtleList[turtle].doRight(-args[0]);
  1653. }
  1654. }
  1655. break;
  1656. case 'setheading':
  1657. if (args.length === 1) {
  1658. if (typeof(args[0]) === 'string') {
  1659. that.errorMsg(NANERRORMSG, blk);
  1660. that.stopTurtle = true;
  1661. } else if (that.inMatrix) {
  1662. that.pitchTimeMatrix.addRowBlock(blk);
  1663. if (that.pitchBlocks.indexOf(blk) === -1) {
  1664. that.pitchBlocks.push(blk);
  1665. }
  1666. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1667. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1668. } else {
  1669. that.turtles.turtleList[turtle].doSetHeading(args[0]);
  1670. }
  1671. }
  1672. break;
  1673. case 'show':
  1674. if (args.length === 2) {
  1675. if (typeof(args[1]) === 'string') {
  1676. var len = args[1].length;
  1677. if (len === 14 && args[1].substr(0, 14) === CAMERAVALUE) {
  1678. doUseCamera(args, that.turtles, turtle, false, that.cameraID, that.setCameraID, that.errorMsg);
  1679. } else if (len === 13 && args[1].substr(0, 13) === VIDEOVALUE) {
  1680. doUseCamera(args, that.turtles, turtle, true, that.cameraID, that.setCameraID, that.errorMsg);
  1681. } else if (len > 10 && args[1].substr(0, 10) === 'data:image') {
  1682. that.turtles.turtleList[turtle].doShowImage(args[0], args[1]);
  1683. } else if (len > 8 && args[1].substr(0, 8) === 'https://') {
  1684. that.turtles.turtleList[turtle].doShowURL(args[0], args[1]);
  1685. } else if (len > 7 && args[1].substr(0, 7) === 'http://') {
  1686. that.turtles.turtleList[turtle].doShowURL(args[0], args[1]);
  1687. } else if (len > 7 && args[1].substr(0, 7) === 'file://') {
  1688. that.turtles.turtleList[turtle].doShowURL(args[0], args[1]);
  1689. } else {
  1690. that.turtles.turtleList[turtle].doShowText(args[0], args[1]);
  1691. }
  1692. } else if (typeof(args[1]) === 'object' && that.blocks.blockList[that.blocks.blockList[blk].connections[2]].name === 'loadFile') {
  1693. if (args[1]) {
  1694. that.turtles.turtleList[turtle].doShowText(args[0], args[1][1]);
  1695. } else {
  1696. that.errorMsg(_('You must select a file.'));
  1697. }
  1698. } else {
  1699. that.turtles.turtleList[turtle].doShowText(args[0], args[1]);
  1700. }
  1701. }
  1702. break;
  1703. case 'turtleshell':
  1704. if (args.length === 2) {
  1705. if (typeof(args[0]) === 'string') {
  1706. that.errorMsg(NANERRORMSG, blk);
  1707. that.stopTurtle = true;
  1708. } else {
  1709. that.turtles.turtleList[turtle].doTurtleShell(args[0], args[1]);
  1710. }
  1711. }
  1712. break;
  1713. case 'setturtlename':
  1714. var foundTargetTurtle = false;
  1715. if (args.length === 2) {
  1716. for (var i = 0; i < that.turtles.turtleList.length; i++) {
  1717. if (that.turtles.turtleList[i].name === args[0]) {
  1718. that.turtles.turtleList[i].rename(args[1]);
  1719. foundTargetTurtle = true;
  1720. break;
  1721. }
  1722. }
  1723. }
  1724. if (!foundTargetTurtle) {
  1725. that.errorMsg('Could not find turtle ' + args[0], blk);
  1726. }
  1727. break;
  1728. case 'startTurtle':
  1729. var targetTurtle = that._getTargetTurtle(args);
  1730. if (targetTurtle == null) {
  1731. that.errorMsg('Cannot find turtle: ' + args[0], blk)
  1732. } else {
  1733. if (that.turtles.turtleList[targetTurtle].running) {
  1734. that.errorMsg('Turtle is already running.', blk);
  1735. break;
  1736. }
  1737. that.turtles.turtleList[targetTurtle].queue = [];
  1738. that.turtles.turtleList[targetTurtle].running = true;
  1739. that.parentFlowQueue[targetTurtle] = [];
  1740. that.unhightlightQueue[targetTurtle] = [];
  1741. that.parameterQueue[targetTurtle] = [];
  1742. // Find the start block associated with this turtle.
  1743. var foundStartBlock = false;
  1744. for (var i = 0; i < that.blocks.blockList.length; i++) {
  1745. if (that.blocks.blockList[i] === that.turtles.turtleList[targetTurtle].startBlock) {
  1746. foundStartBlock = true;
  1747. break;
  1748. }
  1749. }
  1750. if (foundStartBlock) {
  1751. that._runFromBlock(that, targetTurtle, i, isflow, receivedArg);
  1752. } else {
  1753. that.errorMsg('Cannot find start block for turtle: ' + args[0], blk)
  1754. }
  1755. }
  1756. break;
  1757. case 'stopTurtle':
  1758. var targetTurtle = that._getTargetTurtle(args);
  1759. if (targetTurtle == null) {
  1760. that.errorMsg('Cannot find turtle: ' + args[0], blk)
  1761. } else {
  1762. that.turtles.turtleList[targetTurtle].queue = [];
  1763. that.parentFlowQueue[targetTurtle] = [];
  1764. that.unhightlightQueue[targetTurtle] = [];
  1765. that.parameterQueue[targetTurtle] = [];
  1766. that._doBreak(targetTurtle);
  1767. }
  1768. break;
  1769. case 'setcolor':
  1770. if (args.length === 1) {
  1771. if (typeof(args[0]) === 'string') {
  1772. that.errorMsg(NANERRORMSG, blk);
  1773. that.stopTurtle = true;
  1774. } else if (that.inMatrix) {
  1775. that.pitchTimeMatrix.addRowBlock(blk);
  1776. if (that.pitchBlocks.indexOf(blk) === -1) {
  1777. that.pitchBlocks.push(blk);
  1778. }
  1779. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1780. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1781. } else {
  1782. that.turtles.turtleList[turtle].doSetColor(args[0]);
  1783. }
  1784. }
  1785. break;
  1786. case 'setfont':
  1787. if (args.length === 1) {
  1788. if (typeof(args[0]) === 'string') {
  1789. that.turtles.turtleList[turtle].doSetFont(args[0]);
  1790. } else {
  1791. that.errorMsg(NOSTRINGERRORMSG, blk);
  1792. that.stopTurtle = true;
  1793. }
  1794. }
  1795. break;
  1796. case 'sethue':
  1797. if (args.length === 1) {
  1798. if (typeof(args[0]) === 'string') {
  1799. that.errorMsg(NANERRORMSG, blk);
  1800. that.stopTurtle = true;
  1801. } else if (that.inMatrix) {
  1802. that.pitchTimeMatrix.addRowBlock(blk);
  1803. if (that.pitchBlocks.indexOf(blk) === -1) {
  1804. that.pitchBlocks.push(blk);
  1805. }
  1806. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1807. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1808. } else {
  1809. that.turtles.turtleList[turtle].doSetHue(args[0]);
  1810. }
  1811. }
  1812. break;
  1813. case 'setshade':
  1814. if (args.length === 1) {
  1815. if (typeof(args[0]) === 'string') {
  1816. that.errorMsg(NANERRORMSG, blk);
  1817. that.stopTurtle = true;
  1818. } else if (that.inMatrix) {
  1819. that.pitchTimeMatrix.addRowBlock(blk);
  1820. if (that.pitchBlocks.indexOf(blk) === -1) {
  1821. that.pitchBlocks.push(blk);
  1822. }
  1823. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1824. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1825. } else {
  1826. that.turtles.turtleList[turtle].doSetValue(args[0]);
  1827. }
  1828. }
  1829. break;
  1830. case 'settranslucency':
  1831. if (args.length === 1) {
  1832. if (typeof(args[0]) === 'string') {
  1833. that.errorMsg(NANERRORMSG, blk);
  1834. that.stopTurtle = true;
  1835. } else if (that.inMatrix) {
  1836. that.pitchTimeMatrix.addRowBlock(blk);
  1837. if (that.pitchBlocks.indexOf(blk) === -1) {
  1838. that.pitchBlocks.push(blk);
  1839. }
  1840. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1841. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1842. } else {
  1843. args[0] %= 101;
  1844. var alpha = 1.0 - (args[0] / 100);
  1845. that.turtles.turtleList[turtle].doSetPenAlpha(alpha);
  1846. }
  1847. }
  1848. break;
  1849. case 'setgrey':
  1850. if (args.length === 1) {
  1851. if (typeof(args[0]) === 'string') {
  1852. that.errorMsg(NANERRORMSG, blk);
  1853. that.stopTurtle = true;
  1854. } else if (that.inMatrix) {
  1855. that.pitchTimeMatrix.addRowBlock(blk);
  1856. if (that.pitchBlocks.indexOf(blk) === -1) {
  1857. that.pitchBlocks.push(blk);
  1858. }
  1859. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1860. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1861. } else {
  1862. that.turtles.turtleList[turtle].doSetChroma(args[0]);
  1863. }
  1864. }
  1865. break;
  1866. case 'setpensize':
  1867. if (args.length === 1) {
  1868. if (typeof(args[0]) === 'string') {
  1869. that.errorMsg(NANERRORMSG, blk);
  1870. that.stopTurtle = true;
  1871. } else if (that.inMatrix) {
  1872. that.pitchTimeMatrix.addRowBlock(blk);
  1873. if (that.pitchBlocks.indexOf(blk) === -1) {
  1874. that.pitchBlocks.push(blk);
  1875. }
  1876. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  1877. that.pitchTimeMatrix.rowArgs.push(args[0]);
  1878. } else {
  1879. that.turtles.turtleList[turtle].doSetPensize(args[0]);
  1880. }
  1881. }
  1882. break;
  1883. case 'fill':
  1884. that.turtles.turtleList[turtle].doStartFill();
  1885. childFlow = args[0];
  1886. childFlowCount = 1;
  1887. var listenerName = '_fill_' + turtle;
  1888. that._setDispatchBlock(blk, turtle, listenerName);
  1889. var __listener = function (event) {
  1890. that.turtles.turtleList[turtle].doEndFill();
  1891. };
  1892. that._setListener(turtle, listenerName, __listener);
  1893. break;
  1894. // Deprecated
  1895. case 'beginfill':
  1896. that.turtles.turtleList[turtle].doStartFill();
  1897. break;
  1898. // Deprecated
  1899. case 'endfill':
  1900. that.turtles.turtleList[turtle].doEndFill();
  1901. break;
  1902. case 'hollowline':
  1903. that.turtles.turtleList[turtle].doStartHollowLine();
  1904. childFlow = args[0];
  1905. childFlowCount = 1;
  1906. var listenerName = '_hollowline_' + turtle;
  1907. that._setDispatchBlock(blk, turtle, listenerName);
  1908. var __listener = function (event) {
  1909. that.turtles.turtleList[turtle].doEndHollowLine();
  1910. };
  1911. that._setListener(turtle, listenerName, __listener);
  1912. break;
  1913. // Deprecated
  1914. case 'beginhollowline':
  1915. that.turtles.turtleList[turtle].doStartHollowLine();
  1916. break;
  1917. // Deprecated
  1918. case 'endhollowline':
  1919. that.turtles.turtleList[turtle].doEndHollowLine();
  1920. break;
  1921. case 'fillscreen':
  1922. if (args.length === 3) {
  1923. var hue = that.turtles.turtleList[turtle].color;
  1924. var value = that.turtles.turtleList[turtle].value;
  1925. var chroma = that.turtles.turtleList[turtle].chroma;
  1926. that.turtles.turtleList[turtle].doSetHue(args[0]);
  1927. that.turtles.turtleList[turtle].doSetValue(args[1]);
  1928. that.turtles.turtleList[turtle].doSetChroma(args[2]);
  1929. that.setBackgroundColor(turtle);
  1930. that.turtles.turtleList[turtle].doSetHue(hue);
  1931. that.turtles.turtleList[turtle].doSetValue(value);
  1932. that.turtles.turtleList[turtle].doSetChroma(chroma);
  1933. }
  1934. break;
  1935. case 'nobackground':
  1936. that.svgBackground = false;
  1937. break;
  1938. case 'background':
  1939. that.setBackgroundColor(turtle);
  1940. break;
  1941. case 'penup':
  1942. that.turtles.turtleList[turtle].doPenUp();
  1943. break;
  1944. case 'pendown':
  1945. that.turtles.turtleList[turtle].doPenDown();
  1946. break;
  1947. case 'openProject':
  1948. url = args[0];
  1949. function ValidURL(str) {
  1950. var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
  1951. '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
  1952. '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
  1953. '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
  1954. '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
  1955. '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  1956. if(!pattern.test(str)) {
  1957. that.errorMsg('Please enter a valid URL.');
  1958. return false;
  1959. } else {
  1960. return true;
  1961. }
  1962. };
  1963. if (ValidURL(url)) {
  1964. var win = window.open(url, '_blank')
  1965. if (win) {
  1966. // Browser has allowed it to be opened.
  1967. win.focus();
  1968. } else {
  1969. // Broswer has blocked it.
  1970. alert('Please allow popups for this site');
  1971. }
  1972. }
  1973. break;
  1974. case 'vspace':
  1975. break;
  1976. case 'playback':
  1977. var sound = new Howl({
  1978. urls: [args[0]]
  1979. });
  1980. that.sounds.push(sound);
  1981. sound.play();
  1982. break;
  1983. case 'stopplayback':
  1984. for (var sound in that.sounds) {
  1985. that.sounds[sound].stop();
  1986. }
  1987. that.sounds = [];
  1988. break;
  1989. case 'stopvideocam':
  1990. if (cameraID != null) {
  1991. doStopVideoCam(that.cameraID, that.setCameraID);
  1992. }
  1993. break;
  1994. case 'showblocks':
  1995. that.showBlocks();
  1996. that.setTurtleDelay(DEFAULTDELAY);
  1997. break;
  1998. case 'hideblocks':
  1999. that.hideBlocks();
  2000. that.setTurtleDelay(0);
  2001. break;
  2002. case 'savesvg':
  2003. if (args.length === 1) {
  2004. if (that.svgBackground) {
  2005. that.svgOutput = '<rect x="0" y="0" height="' + this.canvas.height + '" width="' + this.canvas.width + '" fill="' + body.style.background + '"/> ' + that.svgOutput;
  2006. }
  2007. doSaveSVG(that, args[0]);
  2008. }
  2009. break;
  2010. case 'showHeap':
  2011. if (!(turtle in that.turtleHeaps)) {
  2012. that.turtleHeaps[turtle] = [];
  2013. }
  2014. that.textMsg(JSON.stringify(that.turtleHeaps[turtle]));
  2015. break;
  2016. case 'emptyHeap':
  2017. that.turtleHeaps[turtle] = [];
  2018. break;
  2019. case 'push':
  2020. if (args.length === 1) {
  2021. if (!(turtle in that.turtleHeaps)) {
  2022. that.turtleHeaps[turtle] = [];
  2023. }
  2024. that.turtleHeaps[turtle].push(args[0]);
  2025. }
  2026. break;
  2027. case 'saveHeap':
  2028. function downloadFile(filename, mimetype, content) {
  2029. var download = document.createElement('a');
  2030. download.setAttribute('href', 'data:' + mimetype + ';charset-utf-8,' + content);
  2031. download.setAttribute('download', filename);
  2032. document.body.appendChild(download);
  2033. download.click();
  2034. document.body.removeChild(download);
  2035. };
  2036. if (args[0] && turtle in that.turtleHeaps) {
  2037. downloadFile(args[0], 'text/json', JSON.stringify(that.turtleHeaps[turtle]));
  2038. }
  2039. break;
  2040. case 'loadHeap':
  2041. var block = that.blocks.blockList[blk];
  2042. if (turtle in that.turtleHeaps) {
  2043. var oldHeap = that.turtleHeaps[turtle];
  2044. } else {
  2045. var oldHeap = [];
  2046. }
  2047. var c = block.connections[1];
  2048. if (c != null && that.blocks.blockList[c].name === 'loadFile') {
  2049. if (args.length !== 1) {
  2050. that.errorMsg(_('You must select a file.'));
  2051. } else {
  2052. try {
  2053. that.turtleHeaps[turtle] = JSON.parse(that.blocks.blockList[c].value[1]);
  2054. if (!Array.isArray(that.turtleHeaps[turtle])) {
  2055. throw 'is not array';
  2056. }
  2057. } catch (e) {
  2058. that.turtleHeaps[turtle] = oldHeap;
  2059. that.errorMsg(_('The file you selected does not contain a valid heap.'));
  2060. }
  2061. }
  2062. } else {
  2063. that.errorMsg(_('The loadHeap block needs a loadFile block.'))
  2064. }
  2065. break;
  2066. case 'loadHeapFromApp':
  2067. var data = [];
  2068. var url = args[1];
  2069. var name = args [0]
  2070. var xmlHttp = new XMLHttpRequest();
  2071. xmlHttp.open('GET', url, false );
  2072. xmlHttp.send();
  2073. if (xmlHttp.readyState === 4 && xmlHttp.status === 200){
  2074. console.log(xmlHttp.responseText);
  2075. try {
  2076. var data = JSON.parse(xmlHttp.responseText);
  2077. } catch (e) {
  2078. console.log(e);
  2079. that.errorMsg(_('Error parsing JSON data:') + e);
  2080. }
  2081. }
  2082. else if (xmlHttp.readyState === 4 && xmlHttp.status !== 200) {
  2083. console.log('fetched the wrong page or network error...');
  2084. that.errorMsg(_('404: Page not found'));
  2085. break;
  2086. }
  2087. else {
  2088. that.errorMsg('xmlHttp.readyState: ' + xmlHttp.readyState);
  2089. break;
  2090. }
  2091. if (name in that.turtleHeaps){
  2092. var oldHeap = turtleHeaps[turtle];
  2093. } else {
  2094. var oldHeap = [];
  2095. }
  2096. that.turtleHeaps[name] = data;
  2097. break;
  2098. case 'saveHeapToApp':
  2099. var name = args[0];
  2100. var url = args[1];
  2101. if (name in that.turtleHeaps) {
  2102. var data = JSON.stringify(that.turtleHeaps[name]);
  2103. var xmlHttp = new XMLHttpRequest();
  2104. xmlHttp.open('POST', url, true);
  2105. xmlHttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  2106. xmlHttp.send(data);
  2107. } else {
  2108. that.errorMsg(_('turtleHeaps does not contain a valid heap for') + ' ' + name);
  2109. }
  2110. break;
  2111. case 'setHeapEntry':
  2112. if (args.length === 2) {
  2113. if (!(turtle in that.turtleHeaps)) {
  2114. that.turtleHeaps[turtle] = [];
  2115. }
  2116. var idx = Math.floor(args[0]);
  2117. if (idx < 1) {
  2118. that.errorMsg(_('Index must be > 0.'))
  2119. }
  2120. // If index > heap length, grow the heap.
  2121. while (that.turtleHeaps[turtle].length < idx) {
  2122. that.turtleHeaps[turtle].push(null);
  2123. }
  2124. that.turtleHeaps[turtle][idx - 1] = args[1];
  2125. }
  2126. break;
  2127. // Actions for music-related blocks
  2128. case 'savelilypond':
  2129. if (args.length === 1) {
  2130. saveLilypondOutput(that, args[0]);
  2131. }
  2132. break;
  2133. case 'setmasterbpm':
  2134. if (args.length === 1 && typeof(args[0] === 'number')) {
  2135. if (args[0] < 30) {
  2136. that.errorMsg(_('Beats per minute must be > 30.'))
  2137. that._masterBPM = 30;
  2138. } else if (args[0] > 1000) {
  2139. that.errorMsg(_('Maximum beats per minute is 1000.'))
  2140. that._masterBPM = 1000;
  2141. } else {
  2142. that._masterBPM = args[0];
  2143. }
  2144. that.defaultBPMFactor = TONEBPM / this._masterBPM;
  2145. }
  2146. if (this.inTempo) {
  2147. that.tempo.BPMBlocks.push(blk);
  2148. var bpmnumberblock = that.blocks.blockList[blk].connections[1]
  2149. that.tempo.BPMs.push(that.blocks.blockList[bpmnumberblock].text.text);
  2150. }
  2151. break;
  2152. case 'setbpm':
  2153. if (args.length === 2 && typeof(args[0] === 'number')) {
  2154. if (args[0] < 30) {
  2155. that.errorMsg(_('Beats per minute must be > 30.'))
  2156. var bpm = 30;
  2157. } else if (args[0] > 1000) {
  2158. that.errorMsg(_('Maximum beats per minute is 1000.'))
  2159. var bpm = 1000;
  2160. } else {
  2161. var bpm = args[0];
  2162. }
  2163. that.bpm[turtle].push(bpm);
  2164. childFlow = args[1];
  2165. childFlowCount = 1;
  2166. var listenerName = '_bpm_' + turtle;
  2167. that._setDispatchBlock(blk, turtle, listenerName);
  2168. var __listener = function (event) {
  2169. that.bpm[turtle].pop();
  2170. };
  2171. that._setListener(turtle, listenerName, __listener);
  2172. }
  2173. break;
  2174. // Deprecated
  2175. case 'setkey':
  2176. if (args.length === 1) {
  2177. that.keySignature[turtle] = args[0];
  2178. }
  2179. break;
  2180. case 'setkey2':
  2181. if (args.length === 2) {
  2182. var modename = 'major';
  2183. for (var i = 0; i < MODENAMES.length; i++) {
  2184. if (MODENAMES[i][0] === args[1]) {
  2185. modename = MODENAMES[i][1];
  2186. that._modeBlock = that.blocks.blockList[blk].connections[2];
  2187. console.log(modename);
  2188. break;
  2189. }
  2190. }
  2191. that.keySignature[turtle] = args[0] + ' ' + modename;
  2192. }
  2193. break;
  2194. case 'rhythmruler':
  2195. console.log("running rhythmruler");
  2196. childFlow = args[1];
  2197. childFlowCount = 1;
  2198. if (that.rhythmRuler == null) {
  2199. that.rhythmRuler = new RhythmRuler();
  2200. }
  2201. // To do: restore previous state
  2202. that.rhythmRuler.Rulers = [];
  2203. that.rhythmRuler.Drums = [];
  2204. that.inRhythmRuler = true;
  2205. var listenerName = '_rhythmruler_' + turtle;
  2206. that._setDispatchBlock(blk, turtle, listenerName);
  2207. var __listener = function (event) {
  2208. that.rhythmRuler.init(that);
  2209. };
  2210. that._setListener(turtle, listenerName, __listener);
  2211. break;
  2212. case 'pitchstaircase':
  2213. if (that.pitchStaircase == null) {
  2214. that.pitchStaircase = new PitchStaircase();
  2215. }
  2216. that.pitchStaircase.Stairs = [];
  2217. childFlow = args[0];
  2218. childFlowCount = 1;
  2219. that.inPitchStaircase = true;
  2220. var listenerName = '_pitchstaircase_' + turtle;
  2221. that._setDispatchBlock(blk, turtle, listenerName);
  2222. var __listener = function (event) {
  2223. that.pitchStaircase.init(that);
  2224. that.inPitchStaircase = false;
  2225. };
  2226. that._setListener(turtle, listenerName, __listener);
  2227. break;
  2228. case 'tempo':
  2229. console.log("running Tempo");
  2230. childFlow = args[0];
  2231. childFlowCount = 1;
  2232. if (that.tempo == null) {
  2233. that.tempo = new Tempo();
  2234. }
  2235. that.inTempo = true;
  2236. that.tempo.BPMblocks = [];
  2237. that.tempo.BPMs = [];
  2238. var listenerName = '_tempo_' + turtle;
  2239. that._setDispatchBlock(blk, turtle, listenerName);
  2240. var __listener = function (event) {
  2241. that.tempo.init(that);
  2242. };
  2243. that._setListener(turtle, listenerName, __listener);
  2244. break;
  2245. case 'pitchslider':
  2246. if (that.pitchSlider == null) {
  2247. that.pitchSlider = new PitchSlider();
  2248. }
  2249. that.pitchSlider.Sliders = [];
  2250. childFlow = args[0];
  2251. childFlowCount = 1;
  2252. that.inPitchSlider = true;
  2253. var listenerName = '_pitchslider_' + turtle;
  2254. that._setDispatchBlock(blk, turtle, listenerName);
  2255. var __listener = function (event) {
  2256. that.pitchSlider.init(that);
  2257. that.inPitchSlider = false;
  2258. };
  2259. that._setListener(turtle, listenerName, __listener);
  2260. break;
  2261. case 'pitchdrummatrix':
  2262. if (args.length === 1) {
  2263. childFlow = args[0];
  2264. childFlowCount = 1;
  2265. }
  2266. if (that.pitchDrumMatrix == null) {
  2267. that.pitchDrumMatrix = new PitchDrumMatrix();
  2268. }
  2269. that.inPitchDrumMatrix = true;
  2270. that.pitchDrumMatrix.rowLabels = [];
  2271. that.pitchDrumMatrix.rowArgs = [];
  2272. that.pitchDrumMatrix.drums = [];
  2273. that.pitchDrumMatrix.clearBlocks();
  2274. var listenerName = '_pitchdrummatrix_' + turtle;
  2275. that._setDispatchBlock(blk, turtle, listenerName);
  2276. var __listener = function (event) {
  2277. if (that.pitchDrumMatrix.drums.length === 0 || that.pitchDrumMatrix.rowLabels.length === 0) {
  2278. that.errorMsg(_('You must have at least one pitch block and one drum block in the matrix.'), blk);
  2279. } else {
  2280. // Process queued up rhythms.
  2281. that.pitchDrumMatrix.init(that);
  2282. that.pitchDrumMatrix.makeClickable();
  2283. }
  2284. };
  2285. that._setListener(turtle, listenerName, __listener);
  2286. break;
  2287. case 'modewidget':
  2288. if (args.length === 1) {
  2289. childFlow = args[0];
  2290. childFlowCount = 1;
  2291. }
  2292. if (that.modeWidget == null) {
  2293. that.modeWidget = new ModeWidget();
  2294. }
  2295. var listenerName = '_modewidget_' + turtle;
  2296. that._setDispatchBlock(blk, turtle, listenerName);
  2297. var __listener = function (event) {
  2298. console.log(that.keySignature[turtle]);
  2299. that.modeWidget.init(that, that._modeBlock);
  2300. }
  2301. that._setListener(turtle, listenerName, __listener);
  2302. break;
  2303. case 'status':
  2304. if (that.statusMatrix == null) {
  2305. that.statusMatrix = new StatusMatrix();
  2306. }
  2307. that.statusMatrix.init(that);
  2308. that.statusFields = [];
  2309. if (args.length === 1) {
  2310. childFlow = args[0];
  2311. childFlowCount = 1;
  2312. }
  2313. that.inStatusMatrix = true;
  2314. var listenerName = '_status_' + turtle;
  2315. that._setDispatchBlock(blk, turtle, listenerName);
  2316. var __listener = function (event) {
  2317. that.statusMatrix.init(that);
  2318. that.inStatusMatrix = false;
  2319. }
  2320. that._setListener(turtle, listenerName, __listener);
  2321. break;
  2322. case 'matrix':
  2323. if (args.length === 1) {
  2324. childFlow = args[0];
  2325. childFlowCount = 1;
  2326. }
  2327. that.inMatrix = true;
  2328. if (that.pitchTimeMatrix == null) {
  2329. that.pitchTimeMatrix = new PitchTimeMatrix();
  2330. }
  2331. that.pitchTimeMatrix.rowLabels = [];
  2332. that.pitchTimeMatrix.rowArgs = [];
  2333. that.pitchTimeMatrix.graphicsBlocks = [];
  2334. that.pitchTimeMatrix.clearBlocks();
  2335. that.tupletRhythms = [];
  2336. that.tupletParams = [];
  2337. that.addingNotesToTuplet = false;
  2338. var listenerName = '_matrix_' + turtle;
  2339. that._setDispatchBlock(blk, turtle, listenerName);
  2340. var __listener = function (event) {
  2341. if (that.tupletRhythms.length === 0 || that.pitchTimeMatrix.rowLabels.length === 0) {
  2342. that.errorMsg(_('You must have at least one pitch block and one rhythm block in the matrix.'), blk);
  2343. } else {
  2344. // Process queued up rhythms.
  2345. that.pitchTimeMatrix.init(that);
  2346. for (var i = 0; i < that.tupletRhythms.length; i++) {
  2347. // We have two cases: (1) notes in a tuplet;
  2348. // and (2) rhythm block outside of a
  2349. // tuplet. Rhythm blocks in a tuplet are
  2350. // converted to notes.
  2351. switch (that.tupletRhythms[i][0]) {
  2352. case 'notes':
  2353. case 'simple':
  2354. var tupletParam = [that.tupletParams[that.tupletRhythms[i][1]]];
  2355. tupletParam.push([]);
  2356. for (var j = 2; j < that.tupletRhythms[i].length; j++) {
  2357. tupletParam[1].push(that.tupletRhythms[i][j]);
  2358. }
  2359. that.pitchTimeMatrix.addTuplet(tupletParam);
  2360. break;
  2361. default:
  2362. that.pitchTimeMatrix.addNotes(that.tupletRhythms[i][1], that.tupletRhythms[i][2]);
  2363. break;
  2364. }
  2365. }
  2366. that.pitchTimeMatrix.makeClickable();
  2367. }
  2368. };
  2369. that._setListener(turtle, listenerName, __listener);
  2370. break;
  2371. case 'invert1':
  2372. if (typeof(args[2]) === 'number') {
  2373. if (args[2] % 2 === 0){
  2374. args[2] = 'even';
  2375. } else {
  2376. args[2] = 'odd';
  2377. }
  2378. }
  2379. if (args[2] === _('even')) {
  2380. args2 = 'even';
  2381. }
  2382. if (args[2] === _('odd')) {
  2383. args2 = 'odd';
  2384. }
  2385. if (args[2] === 'even' || args[2] === 'odd'){
  2386. var octave = calcOctave(that.currentOctaves[turtle], args[1]);
  2387. that.invertList[turtle].push([args[0], octave, args[2]]);
  2388. } else {
  2389. that.errorMsg(NOINPUTERRORMSG, blk);
  2390. that.stopTurtle = true;
  2391. break;
  2392. }
  2393. childFlow = args[3];
  2394. childFlowCount = 1;
  2395. var listenerName = '_invert_' + turtle;
  2396. that._setDispatchBlock(blk, turtle, listenerName);
  2397. var __listener = function (event) {
  2398. that.invertList[turtle].pop();
  2399. };
  2400. that._setListener(turtle, listenerName, __listener);
  2401. break;
  2402. case 'invert2':
  2403. case 'invert':
  2404. // Deprecated
  2405. if (that.blocks.blockList[blk].name === 'invert') {
  2406. that.invertList[turtle].push([args[0], args[1], 'even']);
  2407. } else {
  2408. that.invertList[turtle].push([args[0], args[1], 'odd']);
  2409. }
  2410. childFlow = args[2];
  2411. childFlowCount = 1;
  2412. var listenerName = '_invert_' + turtle;
  2413. that._setDispatchBlock(blk, turtle, listenerName);
  2414. var __listener = function (event) {
  2415. that.invertList[turtle].pop();
  2416. };
  2417. that._setListener(turtle, listenerName, __listener);
  2418. break;
  2419. case 'backward':
  2420. that.backward[turtle].push(blk);
  2421. // Set child to bottom block inside clamp
  2422. childFlow = that.blocks.findBottomBlock(args[0]);
  2423. childFlowCount = 1;
  2424. var listenerName = '_backward_' + turtle + '_' + blk;
  2425. var nextBlock = this.blocks.blockList[blk].connections[1];
  2426. if (nextBlock == null) {
  2427. that.backward[turtle].pop();
  2428. } else {
  2429. that.endOfClampSignals[turtle][nextBlock] = [listenerName];
  2430. }
  2431. var __listener = function (event) {
  2432. that.backward[turtle].pop();
  2433. // Since a backward block was requeued each
  2434. // time, we need to flush it from the queue.
  2435. // that.turtles.turtleList[turtle].queue.pop();
  2436. };
  2437. that._setListener(turtle, listenerName, __listener);
  2438. break;
  2439. case 'rest2':
  2440. that.notePitches[turtle].push('rest');
  2441. that.noteOctaves[turtle].push(4);
  2442. that.noteCents[turtle].push(0);
  2443. that.noteHertz[turtle].push(0);
  2444. if (turtle in that.beatFactor) {
  2445. that.noteBeatValues[turtle].push(that.beatFactor[turtle]);
  2446. } else {
  2447. that.noteBeatValues[turtle].push(1);
  2448. }
  2449. that.pushedNote[turtle] = true;
  2450. break;
  2451. case 'steppitch':
  2452. // Similar to pitch but calculated from previous note played.
  2453. if (that.inNoteBlock[turtle] === 0) {
  2454. that.errorMsg(_('The Step Pitch Block must be used inside of a Note Block.'), blk);
  2455. that.stopTurtle = true;
  2456. break;
  2457. }
  2458. if (typeof(args[0]) !== 'number') {
  2459. that.errorMsg(NANERRORMSG, blk);
  2460. that.stopTurtle = true;
  2461. break;
  2462. }
  2463. if (that.lastNotePlayed[turtle] == null) {
  2464. that.errorMsg('The Step Pitch Block must be preceded by a Pitch Block.', blk);
  2465. that.stopTurtle = true;
  2466. break;
  2467. }
  2468. function addPitch(note, octave, cents) {
  2469. if (that.drumStyle[turtle].length > 0) {
  2470. var drumname = last(that.drumStyle[turtle]);
  2471. var note2 = that.getNote(note, octave, transposition, that.keySignature[turtle]);
  2472. that.pitchDrumTable[turtle][note2[0] + note2[1]] = drumname;
  2473. }
  2474. that.notePitches[turtle].push(note);
  2475. that.noteOctaves[turtle].push(octave);
  2476. that.noteCents[turtle].push(cents);
  2477. if (cents !== 0) {
  2478. that.noteHertz[turtle].push(pitchToFrequency(note, octave, cents, that.keySignature[turtle]));
  2479. } else {
  2480. that.noteHertz[turtle].push(0);
  2481. }
  2482. }
  2483. var len = that.lastNotePlayed[turtle][0].length;
  2484. if (args[0] >= 1) {
  2485. var n = Math.floor(args[0]);
  2486. var value = getStepSizeUp(that.keySignature[turtle], that.lastNotePlayed[turtle][0].slice(0, len - 1));
  2487. var noteObj = that.getNote(that.lastNotePlayed[turtle][0].slice(0, len - 1), parseInt(that.lastNotePlayed[turtle][0].slice(len - 1)), value, that.keySignature[turtle]);
  2488. for (var i = 1; i < n; i++) {
  2489. var value = getStepSizeUp(that.keySignature[turtle], noteObj[0]);
  2490. noteObj = that.getNote(noteObj[0], noteObj[1], value, that.keySignature[turtle]);
  2491. }
  2492. } else if (args[0] <= -1) {
  2493. var n = -Math.ceil(args[0]);
  2494. value = getStepSizeDown(that.keySignature[turtle], that.lastNotePlayed[turtle][0].slice(0, len - 1));
  2495. var noteObj = that.getNote(that.lastNotePlayed[turtle][0].slice(0, len - 1), parseInt(that.lastNotePlayed[turtle][0].slice(len - 1)), value, that.keySignature[turtle]);
  2496. for (var i = 1; i < n; i++) {
  2497. var value = getStepSizeDown(that.keySignature[turtle], noteObj[0]);
  2498. noteObj = that.getNote(noteObj[0], noteObj[1], value, that.keySignature[turtle]);
  2499. }
  2500. } else { // Repeat last pitch played.
  2501. var noteObj = that.getNote(that.lastNotePlayed[turtle][0].slice(0, len - 1), parseInt(that.lastNotePlayed[turtle][0].slice(len - 1)), 0, that.keySignature[turtle]);
  2502. }
  2503. var delta = 0;
  2504. if (!(that.invertList[turtle].length === 0)) {
  2505. var len = that.invertList[turtle].length;
  2506. var note1 = that.getNote(noteObj[0], noteObj[1], 0, that.keySignature[turtle]);
  2507. var num1 = getNumber(note1[0], note1[1]);
  2508. for (var i = len - 1; i > -1; i--) {
  2509. var note2 = that.getNote(that.invertList[turtle][i][0], that.invertList[turtle][i][1], 0, that.keySignature[turtle]);
  2510. var num2 = getNumber(note2[0], note2[1]);
  2511. // var a = getNumNote(num1, 0);
  2512. if (that.invertList[turtle][i][2] === 'even') {
  2513. delta += num2 - num1;
  2514. } else { // odd
  2515. delta += num2 - num1 + 0.5;
  2516. }
  2517. num1 += 2 * delta;
  2518. }
  2519. }
  2520. addPitch(noteObj[0], noteObj[1], 0);
  2521. if (turtle in that.intervals && that.intervals[turtle].length > 0) {
  2522. for (var i = 0; i < that.intervals[turtle].length; i++) {
  2523. var ii = getInterval(that.intervals[turtle][i], that.keySignature[turtle], noteObj[0]);
  2524. var noteObj2 = that.getNote(noteObj[0], noteObj[1], ii, that.keySignature[turtle]);
  2525. addPitch(noteObj2[0], noteObj2[1], 0);
  2526. }
  2527. }
  2528. if (turtle in that.perfect && that.perfect[turtle].length > 0) {
  2529. var noteObj2 = that.getNote(noteObj[0], noteObj[1], calcPerfect(last(that.perfect[turtle])), that.keySignature[turtle]);
  2530. addPitch(noteObj2[0], noteObj2[1], 0);
  2531. }
  2532. if (turtle in that.diminished && that.diminished[turtle].length > 0) {
  2533. var noteObj2 = that.getNote(noteObj[0], noteObj[1], calcDiminished(last(that.diminished[turtle])), that.keySignature[turtle]);
  2534. addPitch(noteObj2[0], noteObj2[1], 0);
  2535. }
  2536. if (turtle in that.augmented && that.augmented[turtle].length > 0) {
  2537. var noteObj2 = that.getNote(noteObj[0], noteObj[1], calcAugmented(last(that.augmented[turtle])), that.keySignature[turtle]);
  2538. addPitch(noteObj2[0], noteObj2[1], 0);
  2539. }
  2540. if (turtle in that.major && that.major[turtle].length > 0) {
  2541. var noteObj2 = that.getNote(noteObj[0], noteObj[1], calcMajor(last(that.major[turtle])), that.keySignature[turtle]);
  2542. addPitch(noteObj2[0], noteObj2[1], 0);
  2543. }
  2544. if (turtle in that.minor && that.minor[turtle].length > 0) {
  2545. var noteObj2 = that.getNote(noteObj[0], noteObj[1], calcMinor(last(that.minor[turtle])), that.keySignature[turtle]);
  2546. addPitch(noteObj2[0], noteObj2[1], 0);
  2547. }
  2548. if (turtle in that.transposition) {
  2549. that.noteTranspositions[turtle].push(that.transposition[turtle] + 2 * delta);
  2550. } else {
  2551. that.noteTranspositions[turtle].push(2 * delta);
  2552. }
  2553. if (turtle in that.beatFactor) {
  2554. that.noteBeatValues[turtle].push(that.beatFactor[turtle]);
  2555. } else {
  2556. that.noteBeatValues[turtle].push(1);
  2557. }
  2558. that.pushedNote[turtle] = true;
  2559. break;
  2560. case 'playdrum':
  2561. if (args.length !== 1 || args[0] == null) {
  2562. that.errorMsg(NOINPUTERRORMSG, blk);
  2563. that.stopTurtle = true;
  2564. break;
  2565. }
  2566. if (typeof(args[0]) !== 'string') {
  2567. that.errorMsg(NANERRORMSG, blk);
  2568. that.stopTurtle = true;
  2569. break;
  2570. }
  2571. var drumname = 'kick';
  2572. if (args[0].slice(0, 4) === 'http') {
  2573. drumname = args[0];
  2574. } else {
  2575. for (var drum in DRUMNAMES) {
  2576. if (DRUMNAMES[drum][0] === args[0]) {
  2577. drumname = DRUMNAMES[drum][1];
  2578. break;
  2579. } else if (DRUMNAMES[drum][1] === args[0]) {
  2580. drumname = args[0];
  2581. break;
  2582. }
  2583. }
  2584. }
  2585. // If we are in a setdrum clamp, override the drum name.
  2586. if (that.drumStyle[turtle].length > 0) {
  2587. drumname = last(that.drumStyle[turtle]);
  2588. }
  2589. if (that.inPitchDrumMatrix) {
  2590. that.pitchDrumMatrix.drums.push(drumname);
  2591. that.pitchDrumMatrix.addColBlock(blk);
  2592. if (that.drumBlocks.indexOf(blk) === -1) {
  2593. that.drumBlocks.push(blk);
  2594. }
  2595. } else if (that.inMatrix) {
  2596. that.pitchTimeMatrix.rowLabels.push(drumname);
  2597. that.pitchTimeMatrix.rowArgs.push(-1);
  2598. that.pitchTimeMatrix.addRowBlock(blk);
  2599. if (that.drumBlocks.indexOf(blk) === -1) {
  2600. that.drumBlocks.push(blk);
  2601. }
  2602. } else if (that.inNoteBlock[turtle] > 0) {
  2603. that.noteDrums[turtle].push(drumname);
  2604. } else {
  2605. that.errorMsg(_('Drum Block: Did you mean to use a Note block?'), blk);
  2606. break;
  2607. }
  2608. if (turtle in that.beatFactor) {
  2609. that.noteBeatValues[turtle].push(that.beatFactor[turtle]);
  2610. } else {
  2611. that.noteBeatValues[turtle].push(1);
  2612. }
  2613. that.pushedNote[turtle] = true;
  2614. break;
  2615. case 'setpitchnumberoffset':
  2616. if (args.length !== 2 || args[0] == null || args[1] == null) {
  2617. that.errorMsg(NOINPUTERRORMSG, blk);
  2618. that.stopTurtle = true;
  2619. break;
  2620. }
  2621. var octave = Math.floor(calcOctave(that.currentOctaves[turtle], args[1]));
  2622. that.pitchNumberOffset = pitchToNumber(args[0], octave, that.keySignature[turtle]);
  2623. break;
  2624. case 'pitchnumber':
  2625. case 'scaledegree':
  2626. case 'pitch':
  2627. if (that.blocks.blockList[blk].name == 'pitchnumber') {
  2628. if (args.length !== 1 || args[0] == null) {
  2629. that.errorMsg(NOINPUTERRORMSG, blk);
  2630. that.stopTurtle = true;
  2631. break;
  2632. }
  2633. if (typeof(args[0]) !== 'number') {
  2634. that.errorMsg(NANERRORMSG, blk);
  2635. that.stopTurtle = true;
  2636. break;
  2637. }
  2638. // In number to pitch we assume A0 == 0. Here we
  2639. // assume that C4 == 0, so we need an offset of 39.
  2640. var obj = numberToPitch(Math.floor(args[0] + that.pitchNumberOffset));
  2641. note = obj[0];
  2642. octave = obj[1];
  2643. cents = 0;
  2644. } else {
  2645. if (args.length !== 2 || args[0] == null || args[1] == null) {
  2646. that.errorMsg(NOINPUTERRORMSG, blk);
  2647. that.stopTurtle = true;
  2648. break;
  2649. }
  2650. /*
  2651. if (typeof(args[1]) !== 'number') {
  2652. that.errorMsg(NANERRORMSG, blk);
  2653. that.stopTurtle = true;
  2654. break;
  2655. }
  2656. */
  2657. if (typeof(args[0]) === 'number' && that.blocks.blockList[blk].name == 'pitch') {
  2658. // We interpret numbers two different ways:
  2659. // (1) a positive integer between 1 and 12 is taken to be
  2660. // a moveable solfege, e.g., 1 == do; 2 == re...
  2661. // (2) if frequency is input, ignore octave (args[1]).
  2662. // Negative numbers will throw an error.
  2663. if (args[0] < 1) {
  2664. note = '?'; // throws an error
  2665. } else if (args[0] < 13) { // moveable solfege
  2666. note = scaleDegreeToPitch(that.keySignature[turtle], Math.floor(args[0]));
  2667. var octave = Math.floor(calcOctave(that.currentOctaves[turtle], args[1]));
  2668. var cents = 0;
  2669. } else if (args[0] < A0 || args[0] > C8) {
  2670. note = '?'; // throws an error
  2671. } else {
  2672. var obj = frequencyToPitch(args[0]);
  2673. var note = obj[0];
  2674. var octave = obj[1];
  2675. var cents = obj[2];
  2676. }
  2677. if (note === '?') {
  2678. that.errorMsg(INVALIDPITCH, blk);
  2679. that.stopTurtle = true;
  2680. break;
  2681. }
  2682. } else if (typeof(args[0]) === 'number' && that.blocks.blockList[blk].name == 'scaledegree') {
  2683. if (args[0] < 0) {
  2684. var neg = true;
  2685. args[0] = -args[0];
  2686. } else {
  2687. var neg = false;
  2688. }
  2689. if (args[0] == 0) {
  2690. note = '?'; // throws an error
  2691. } else {
  2692. var obj = keySignatureToMode(that.keySignature[turtle]);
  2693. var modeLength = MUSICALMODES[obj[1]].length;
  2694. var scaleDegree = Math.floor(args[0] - 1) % modeLength;
  2695. scaleDegree += 1;
  2696. if (neg) {
  2697. // -1, 4 --> do 4; -2, 4 --> ti 3; -8, 4 --> do 3
  2698. if (scaleDegree > 1) {
  2699. scaleDegree = modeLength - scaleDegree + 2;
  2700. }
  2701. note = scaleDegreeToPitch(that.keySignature[turtle], scaleDegree);
  2702. var deltaOctave = Math.floor((args[0] + modeLength - 2) / modeLength);
  2703. var octave = Math.floor(calcOctave(that.currentOctaves[turtle], args[1])) - deltaOctave;
  2704. } else {
  2705. // 1, 4 --> do 4; 2, 4 --> re 4; 8, 4 --> do 5
  2706. note = scaleDegreeToPitch(that.keySignature[turtle], scaleDegree);
  2707. var deltaOctave = Math.floor((args[0] - 1) / modeLength);
  2708. var octave = Math.floor(calcOctave(that.currentOctaves[turtle], args[1])) + deltaOctave;
  2709. }
  2710. var cents = 0;
  2711. }
  2712. if (note === '?') {
  2713. that.errorMsg(INVALIDPITCH, blk);
  2714. that.stopTurtle = true;
  2715. break;
  2716. }
  2717. } else {
  2718. var cents = 0;
  2719. var note = args[0];
  2720. if (calcOctave(that.currentOctaves[turtle], args[1]) < 0) {
  2721. console.log('minimum allowable octave is 0');
  2722. var octave = 0;
  2723. } else if (calcOctave(that.currentOctaves[turtle], args[1]) > 10) {
  2724. // Humans can only hear 10 octaves.
  2725. console.log('clipping octave at 10');
  2726. var octave = 10;
  2727. } else {
  2728. // Octave must be a whole number.
  2729. var octave = Math.floor(calcOctave(that.currentOctaves[turtle], args[1]));
  2730. }
  2731. that.getNote(note, octave, 0, that.keySignature[turtle]);
  2732. if (!that.validNote) {
  2733. that.errorMsg(INVALIDPITCH, blk);
  2734. that.stopTurtle = true;
  2735. break;
  2736. }
  2737. }
  2738. }
  2739. var delta = 0;
  2740. if (that.inPitchDrumMatrix) {
  2741. if (note.toLowerCase() !== 'rest') {
  2742. that.pitchDrumMatrix.addRowBlock(blk);
  2743. if (that.pitchBlocks.indexOf(blk) === -1) {
  2744. that.pitchBlocks.push(blk);
  2745. }
  2746. }
  2747. if (!(that.invertList[turtle].length === 0)) {
  2748. var delta = 0;
  2749. var len = that.invertList[turtle].length;
  2750. // Get an anchor note and its corresponding
  2751. // number, which is used to calculate delta.
  2752. var note1 = that.getNote(note, octave, 0, that.keySignature[turtle]);
  2753. var num1 = that.getNumber(note1[0], note1[1]);
  2754. for (var i = len - 1; i > -1; i--) {
  2755. // Note from which delta is calculated.
  2756. var note2 = that.getNote(that.invertList[turtle][i][0], that.invertList[turtle][i][1], 0, that.keySignature[turtle]);
  2757. var num2 = getNumber(note2[0], note2[1]);
  2758. // var a = getNumNote(num1, 0);
  2759. if (that.invertList[turtle][i][2] === 'even') {
  2760. delta += num2 - num1;
  2761. } else { // odd
  2762. delta += num2 - num1 + 0.5;
  2763. }
  2764. num1 += 2 * delta;
  2765. }
  2766. }
  2767. if (that.duplicateFactor[turtle].length > 0) {
  2768. var duplicateFactor = that.duplicateFactor[turtle];
  2769. } else {
  2770. var duplicateFactor = 1;
  2771. }
  2772. for (var i = 0; i < duplicateFactor; i++) {
  2773. // Apply transpositions
  2774. var transposition = 2 * delta;
  2775. if (turtle in that.transposition) {
  2776. transposition += that.transposition[turtle];
  2777. }
  2778. var nnote = that.getNote(note, octave, transposition, that.keySignature[turtle]);
  2779. if (noteIsSolfege(note)) {
  2780. nnote[0] = getSolfege(nnote[0]);
  2781. }
  2782. if (that.drumStyle[turtle].length > 0) {
  2783. that.pitchDrumMatrix.drums.push(last(that.drumStyle[turtle]));
  2784. } else {
  2785. that.pitchDrumMatrix.rowLabels.push(nnote[0]);
  2786. that.pitchDrumMatrix.rowArgs.push(nnote[1]);
  2787. }
  2788. }
  2789. } else if (that.inMatrix) {
  2790. if (note.toLowerCase() !== 'rest') {
  2791. that.pitchTimeMatrix.addRowBlock(blk);
  2792. if (that.pitchBlocks.indexOf(blk) === -1) {
  2793. that.pitchBlocks.push(blk);
  2794. }
  2795. }
  2796. if (!(that.invertList[turtle].length === 0)) {
  2797. var delta = 0;
  2798. var len = that.invertList[turtle].length;
  2799. // Get an anchor note and its corresponding
  2800. // number, which is used to calculate delta.
  2801. var note1 = that.getNote(note, octave, 0, that.keySignature[turtle]);
  2802. var num1 = that.getNumber(note1[0], note1[1]);
  2803. for (var i = len - 1; i > -1; i--) {
  2804. // Note from which delta is calculated.
  2805. var note2 = that.getNote(that.invertList[turtle][i][0], that.invertList[turtle][i][1], 0, that.keySignature[turtle]);
  2806. var num2 = getNumber(note2[0], note2[1]);
  2807. // var a = getNumNote(num1, 0);
  2808. if (that.invertList[turtle][i][2] === 'even') {
  2809. delta += num2 - num1;
  2810. } else { // odd
  2811. delta += num2 - num1 + 0.5;
  2812. }
  2813. num1 += 2 * delta;
  2814. }
  2815. }
  2816. if (that.duplicateFactor[turtle].length > 0) {
  2817. var duplicateFactor = that.duplicateFactor[turtle];
  2818. } else {
  2819. var duplicateFactor = 1;
  2820. }
  2821. for (var i = 0; i < duplicateFactor; i++) {
  2822. // Apply transpositions
  2823. var transposition = 2 * delta;
  2824. if (turtle in that.transposition) {
  2825. transposition += that.transposition[turtle];
  2826. }
  2827. var nnote = that.getNote(note, octave, transposition, that.keySignature[turtle]);
  2828. if (noteIsSolfege(note)) {
  2829. nnote[0] = getSolfege(nnote[0]);
  2830. }
  2831. // If we are in a setdrum clamp, override the pitch.
  2832. if (that.drumStyle[turtle].length > 0) {
  2833. that.pitchTimeMatrix.rowLabels.push(last(that.drumStyle[turtle]));
  2834. that.pitchTimeMatrix.rowArgs.push(-1);
  2835. } else {
  2836. that.pitchTimeMatrix.rowLabels.push(nnote[0]);
  2837. that.pitchTimeMatrix.rowArgs.push(nnote[1]);
  2838. }
  2839. }
  2840. } else if (that.inNoteBlock[turtle] > 0) {
  2841. function addPitch(note, octave, cents) {
  2842. if (that.drumStyle[turtle].length > 0) {
  2843. var drumname = last(that.drumStyle[turtle]);
  2844. var note2 = that.getNote(note, octave, transposition, that.keySignature[turtle]);
  2845. that.pitchDrumTable[turtle][note2[0]+note2[1]] = drumname;
  2846. }
  2847. that.notePitches[turtle].push(note);
  2848. that.noteOctaves[turtle].push(octave);
  2849. that.noteCents[turtle].push(cents);
  2850. if (cents !== 0) {
  2851. that.noteHertz[turtle].push(pitchToFrequency(note, octave, cents, that.keySignature[turtle]));
  2852. } else {
  2853. that.noteHertz[turtle].push(0);
  2854. }
  2855. }
  2856. if (!(that.invertList[turtle].length === 0)) {
  2857. var len = that.invertList[turtle].length;
  2858. var note1 = that.getNote(note, octave, 0, that.keySignature[turtle]);
  2859. var num1 = getNumber(note1[0], note1[1]);
  2860. for (var i = len - 1; i > -1; i--) {
  2861. var note2 = that.getNote(that.invertList[turtle][i][0], that.invertList[turtle][i][1], 0, that.keySignature[turtle]);
  2862. var num2 = getNumber(note2[0], note2[1]);
  2863. // var a = getNumNote(num1, 0);
  2864. if (that.invertList[turtle][i][2] === 'even') {
  2865. delta += num2 - num1;
  2866. } else { // odd
  2867. delta += num2 - num1 + 0.5;
  2868. }
  2869. num1 += 2 * delta;
  2870. }
  2871. }
  2872. addPitch(note, octave, cents);
  2873. if (turtle in that.intervals && that.intervals[turtle].length > 0) {
  2874. for (var i = 0; i < that.intervals[turtle].length; i++) {
  2875. var ii = getInterval(that.intervals[turtle][i], that.keySignature[turtle], note);
  2876. var noteObj = that.getNote(note, octave, ii, that.keySignature[turtle]);
  2877. addPitch(noteObj[0], noteObj[1], cents);
  2878. }
  2879. }
  2880. if (turtle in that.perfect && that.perfect[turtle].length > 0) {
  2881. var noteObj = that.getNote(note, octave, calcPerfect(last(that.perfect[turtle])), that.keySignature[turtle]);
  2882. addPitch(noteObj[0], noteObj[1], cents);
  2883. }
  2884. if (turtle in that.diminished && that.diminished[turtle].length > 0) {
  2885. var noteObj = that.getNote(note, octave, calcDiminished(last(that.diminished[turtle])), that.keySignature[turtle]);
  2886. addPitch(noteObj[0], noteObj[1], cents);
  2887. }
  2888. if (turtle in that.augmented && that.augmented[turtle].length > 0) {
  2889. var noteObj = that.getNote(note, octave, calcAugmented(last(that.augmented[turtle])), that.keySignature[turtle]);
  2890. addPitch(noteObj[0], noteObj[1], cents);
  2891. }
  2892. if (turtle in that.major && that.major[turtle].length > 0) {
  2893. var noteObj = that.getNote(note, octave, calcMajor(last(that.major[turtle])), that.keySignature[turtle]);
  2894. addPitch(noteObj[0], noteObj[1], cents);
  2895. }
  2896. if (turtle in that.minor && that.minor[turtle].length > 0) {
  2897. var noteObj = that.getNote(note, octave, calcMinor(last(that.minor[turtle])), that.keySignature[turtle]);
  2898. addPitch(noteObj[0], noteObj[1], cents);
  2899. }
  2900. if (turtle in that.transposition) {
  2901. that.noteTranspositions[turtle].push(that.transposition[turtle] + 2 * delta);
  2902. } else {
  2903. that.noteTranspositions[turtle].push(2 * delta);
  2904. }
  2905. if (turtle in that.beatFactor) {
  2906. that.noteBeatValues[turtle].push(that.beatFactor[turtle]);
  2907. } else {
  2908. that.noteBeatValues[turtle].push(1);
  2909. }
  2910. that.pushedNote[turtle] = true;
  2911. } else if (that.drumStyle[turtle].length > 0) {
  2912. var drumname = last(that.drumStyle[turtle]);
  2913. var note2 = that.getNote(note, octave, transposition, that.keySignature[turtle]);
  2914. that.pitchDrumTable[turtle][note2[0]+note2[1]] = drumname;
  2915. } else if (that.inPitchStaircase) {
  2916. var frequency = pitchToFrequency(args[0], calcOctave(that.currentOctaves[turtle], args[1]), 0, that.keySignature[turtle]);
  2917. var note = that.getNote(args[0], calcOctave(that.currentOctaves[turtle], args[1]), 0, that.keySignature[turtle]);
  2918. var flag = 0;
  2919. for (var i = 0 ; i < that.pitchStaircase.Stairs.length; i++) {
  2920. if (that.pitchStaircase.Stairs[i][2] < parseFloat(frequency)) {
  2921. that.pitchStaircase.Stairs.splice(i, 0, [note[0], note[1], parseFloat(frequency), 1, 1]);
  2922. flag = 1;
  2923. break;
  2924. }
  2925. if (that.pitchStaircase.Stairs[i][2] === parseFloat(frequency)) {
  2926. that.pitchStaircase.Stairs.splice(i, 1, [note[0], note[1], parseFloat(frequency), 1, 1]);
  2927. flag = 1;
  2928. break;
  2929. }
  2930. }
  2931. if (flag === 0) {
  2932. that.pitchStaircase.Stairs.push([note[0], note[1], parseFloat(frequency), 1, 1]);
  2933. }
  2934. }
  2935. else {
  2936. that.errorMsg(_('Pitch Block: Did you mean to use a Note block?'), blk);
  2937. }
  2938. break;
  2939. case 'rhythm2':
  2940. case 'rhythm':
  2941. if (args.length < 2 || typeof(args[0]) !== 'number' || typeof(args[1]) !== 'number' || args[0] < 1 || args[1] <= 0) {
  2942. that.errorMsg(NOINPUTERRORMSG, blk);
  2943. that.stopTurtle = true;
  2944. break;
  2945. }
  2946. if (that.blocks.blockList[blk].name === 'rhythm2') {
  2947. var noteBeatValue = 1 / args[1];
  2948. } else {
  2949. var noteBeatValue = args[1];
  2950. }
  2951. if (that.inMatrix) {
  2952. that.pitchTimeMatrix.addColBlock(blk, args[0]);
  2953. for (var i = 0; i < args[0]; i++) {
  2954. that._processNote(noteBeatValue, blk, turtle);
  2955. }
  2956. } else if (that.inRhythmRuler) {
  2957. // Temporary work-around to #272.
  2958. if (that.rhythmRulerMeasure === null) {
  2959. that.rhythmRulerMeasure = args[0] * args[1];
  2960. } else if (that.rhythmRulerMeasure != (args[0] * args[1])) {
  2961. that.errorMsg('Rhythm Ruler imbalance.', blk);
  2962. that.stopTurtle = true;
  2963. }
  2964. // Since there maybe more than one instance of the
  2965. // same drum, e.g., if a repeat is used, we look from
  2966. // end of the list instead of the beginning of the
  2967. // list.
  2968. var drumIndex = -1;
  2969. for (var i = 0; i < that.rhythmRuler.Drums.length; i++) {
  2970. var j = that.rhythmRuler.Drums.length - i - 1;
  2971. if (that.rhythmRuler.Drums[j] === that._currentDrumBlock) {
  2972. drumIndex = j;
  2973. break;
  2974. }
  2975. }
  2976. if (drumIndex !== -1) {
  2977. for (var i = 0; i < args[0]; i++) {
  2978. that.rhythmRuler.Rulers[drumIndex][0].push(noteBeatValue);
  2979. }
  2980. }
  2981. } else {
  2982. that.errorMsg(_('Rhythm Block: Did you mean to use a Matrix block?'), blk);
  2983. }
  2984. break;
  2985. // &#x1D15D; &#x1D15E; &#x1D15F; &#x1D160; &#x1D161; &#x1D162; &#x1D163; &#x1D164;
  2986. case 'wholeNote':
  2987. that._processNote(1, blk, turtle);
  2988. break;
  2989. case 'halfNote':
  2990. that._processNote(2, blk, turtle);
  2991. break;
  2992. case 'quarterNote':
  2993. that._processNote(4, blk, turtle);
  2994. break;
  2995. case 'eighthNote':
  2996. that._processNote(8, blk, turtle);
  2997. break;
  2998. case 'sixteenthNote':
  2999. that._processNote(16, blk, turtle);
  3000. break;
  3001. case 'thirtysecondNote':
  3002. that._processNote(32, blk, turtle);
  3003. break;
  3004. case 'sixtyfourthNote':
  3005. that._processNote(64, blk, turtle);
  3006. break;
  3007. case 'meter':
  3008. // See Issue #337
  3009. break;
  3010. case 'osctime':
  3011. case 'newnote':
  3012. case 'note':
  3013. // We queue up the child flow of the note clamp and
  3014. // once all of the children are run, we trigger a
  3015. // _playnote_ event, then wait for the note to play.
  3016. // The note can be specified by pitch or synth blocks.
  3017. // The osctime block specifies the duration in
  3018. // milleseconds while the note block specifies
  3019. // duration as a beat value. Note: we should consider
  3020. // the use of the global timer in Tone.js for more
  3021. // accuracy.
  3022. // A note can contain multiple pitch blocks to create
  3023. // a chord. The chord is accumuated in these arrays,
  3024. // which are used when we play the note.
  3025. that.oscList[turtle] = [];
  3026. that.noteBeat[turtle] = [];
  3027. that.notePitches[turtle] = [];
  3028. that.noteOctaves[turtle] = [];
  3029. that.noteCents[turtle] = [];
  3030. that.noteHertz[turtle] = [];
  3031. that.noteTranspositions[turtle] = [];
  3032. that.noteBeatValues[turtle] = [];
  3033. that.noteDrums[turtle] = [];
  3034. // Ensure that note duration is positive.
  3035. if (args[0] < 0) {
  3036. that.errorMsg(_('Note value must be greater than 0'), blk);
  3037. var noteBeatValue = 0;
  3038. } else {
  3039. if (that.blocks.blockList[blk].name === 'newnote') {
  3040. var noteBeatValue = 1 / args[0];
  3041. } else {
  3042. var noteBeatValue = args[0];
  3043. }
  3044. }
  3045. that.inNoteBlock[turtle] += 1;
  3046. that.whichNoteBlock[turtle] = blk;
  3047. childFlow = args[1];
  3048. childFlowCount = 1;
  3049. var listenerName = '_playnote_' + turtle;
  3050. that._setDispatchBlock(blk, turtle, listenerName);
  3051. var __listener = function (event) {
  3052. that._processNote(noteBeatValue, blk, turtle);
  3053. that.inNoteBlock[turtle] -= 1;
  3054. that.pitchBlocks = [];
  3055. that.drumBlocks = [];
  3056. };
  3057. that._setListener(turtle, listenerName, __listener);
  3058. break;
  3059. case 'rhythmicdot':
  3060. // Dotting a note will increase its play time by
  3061. // a(2 - 1/2^n)
  3062. var currentDotFactor = 2 - (1 / Math.pow(2, that.dotCount[turtle]));
  3063. that.beatFactor[turtle] *= currentDotFactor;
  3064. that.dotCount[turtle] += 1;
  3065. var newDotFactor = 2 - (1 / Math.pow(2, that.dotCount[turtle]));
  3066. that.beatFactor[turtle] /= newDotFactor;
  3067. childFlow = args[0];
  3068. childFlowCount = 1;
  3069. var listenerName = '_dot_' + turtle;
  3070. that._setDispatchBlock(blk, turtle, listenerName);
  3071. var __listener = function (event) {
  3072. var currentDotFactor = 2 - (1 / Math.pow(2, that.dotCount[turtle]));
  3073. that.beatFactor[turtle] *= currentDotFactor;
  3074. that.dotCount[turtle] -= 1;
  3075. var newDotFactor = 2 - (1 / Math.pow(2, that.dotCount[turtle]));
  3076. that.beatFactor[turtle] /= newDotFactor;
  3077. };
  3078. that._setListener(turtle, listenerName, __listener);
  3079. break;
  3080. case 'crescendo':
  3081. if (args.length > 1 && args[0] !== 0) {
  3082. that.crescendoDelta[turtle].push(args[0]);
  3083. that.crescendoVolume[turtle].push(last(that.polyVolume[turtle]));
  3084. that.crescendoInitialVolume[turtle].push(last(that.polyVolume[turtle]));
  3085. that.polyVolume[turtle].push(last(that.crescendoVolume[turtle]));
  3086. childFlow = args[1];
  3087. childFlowCount = 1;
  3088. var listenerName = '_crescendo_' + turtle;
  3089. that._setDispatchBlock(blk, turtle, listenerName);
  3090. var __listener = function (event) {
  3091. that.crescendoDelta[turtle].pop();
  3092. that.crescendoVolume[turtle].pop();
  3093. that.polyVolume[turtle].pop();
  3094. that.crescendoInitialVolume[turtle].pop();
  3095. if (!that.justCounting[turtle]) {
  3096. lilypondEndCrescendo(that, turtle);
  3097. }
  3098. };
  3099. that._setListener(turtle, listenerName, __listener);
  3100. }
  3101. break;
  3102. case 'darbuka':
  3103. case 'clang':
  3104. case 'bottle':
  3105. case 'duck':
  3106. case 'snare':
  3107. case 'hihat':
  3108. case 'tom':
  3109. case 'kick':
  3110. case 'pluck':
  3111. case 'triangle1':
  3112. case 'slap':
  3113. case 'frogs':
  3114. case 'fingercymbals':
  3115. case 'cup':
  3116. case 'cowbell':
  3117. case 'splash':
  3118. case 'ridebell':
  3119. case 'floortom':
  3120. case 'crash':
  3121. case 'chine':
  3122. case 'dog':
  3123. case 'cat':
  3124. case 'clap':
  3125. case 'bubbles':
  3126. case 'cricket':
  3127. that.drumStyle[turtle].push(that.blocks.blockList[blk].name);
  3128. childFlow = args[0];
  3129. childFlowCount = 1;
  3130. var listenerName = '_drum_' + turtle;
  3131. that._setDispatchBlock(blk, turtle, listenerName);
  3132. var __listener = function (event) {
  3133. that.drumStyle[turtle].pop();
  3134. };
  3135. that._setListener(turtle, listenerName, __listener);
  3136. break;
  3137. case 'setdrum':
  3138. var drumname = 'kick';
  3139. for (var drum in DRUMNAMES) {
  3140. if (DRUMNAMES[drum][0] === args[0]) {
  3141. drumname = DRUMNAMES[drum][1];
  3142. } else if (DRUMNAMES[drum][1] === args[0]) {
  3143. drumname = args[0];
  3144. }
  3145. }
  3146. that.drumStyle[turtle].push(drumname);
  3147. childFlow = args[1];
  3148. childFlowCount = 1;
  3149. var listenerName = '_setdrum_' + turtle;
  3150. that._setDispatchBlock(blk, turtle, listenerName);
  3151. var __listener = function (event) {
  3152. that.drumStyle[turtle].pop();
  3153. };
  3154. that._setListener(turtle, listenerName, __listener);
  3155. if (that.inRhythmRuler) {
  3156. that._currentDrumBlock = blk;
  3157. that.rhythmRuler.Drums.push(blk);
  3158. that.rhythmRuler.Rulers.push([[],[]]);
  3159. }
  3160. break;
  3161. case 'setvoice':
  3162. var voicename = null;
  3163. for (var voice in VOICENAMES) {
  3164. if (VOICENAMES[voice][0] === args[0]) {
  3165. voicename = VOICENAMES[voice][1];
  3166. } else if (VOICENAMES[voice][1] === args[0]) {
  3167. voicename = args[0];
  3168. }
  3169. }
  3170. // Maybe it is a drum?
  3171. if (voicename == null) {
  3172. for (var drum in DRUMNAMES) {
  3173. if (DRUMNAMES[drum][0] === args[0]) {
  3174. voicename = DRUMNAMES[drum][1];
  3175. } else if (DRUMNAMES[drum][1] === args[0]) {
  3176. voicename = args[0];
  3177. }
  3178. }
  3179. }
  3180. if (voicename == null) {
  3181. that.errorMsg(NOINPUTERRORMSG, blk);
  3182. childFlow = args[1];
  3183. childFlowCount = 1;
  3184. } else {
  3185. that.voices[turtle].push(voicename);
  3186. childFlow = args[1];
  3187. childFlowCount = 1;
  3188. var listenerName = '_setvoice_' + turtle;
  3189. that._setDispatchBlock(blk, turtle, listenerName);
  3190. var __listener = function (event) {
  3191. that.voices[turtle].pop();
  3192. };
  3193. that._setListener(turtle, listenerName, __listener);
  3194. }
  3195. break;
  3196. case 'vibrato':
  3197. var intensity = args[0];
  3198. var rate = args[1];
  3199. if (intensity < 1 || intensity > 100) {
  3200. that.errorMsg(_('Vibrato intensity must be between 1 and 100.'), blk);
  3201. that.stopTurtle = true;
  3202. }
  3203. if (rate <= 0) {
  3204. that.errorMsg(_('Vibrato rate must be greater than 0.'), blk);
  3205. that.stopTurtle = true;
  3206. }
  3207. childFlow = args[2];
  3208. childFlowCount = 1;
  3209. that.vibratoIntensity[turtle].push(intensity / 100);
  3210. that.vibratoRate[turtle].push(Math.floor(Math.pow(rate, -1)));
  3211. var listenerName = '_vibrato_' + turtle;
  3212. that._setDispatchBlock(blk, turtle, listenerName);
  3213. var __listener = function (event) {
  3214. that.vibratoIntensity[turtle].pop();
  3215. that.vibratoRate[turtle].pop();
  3216. };
  3217. that._setListener(turtle, listenerName, __listener);
  3218. break;
  3219. case 'interval':
  3220. if (typeof(args[0]) !== 'number') {
  3221. that.errorMsg(NOINPUTERRORMSG, blk);
  3222. that.stopTurtle = true;
  3223. break;
  3224. }
  3225. if (args[0] > 0) {
  3226. var i = Math.floor(args[0]);
  3227. } else {
  3228. var i = Math.ceil(args[0]);
  3229. }
  3230. if (i !== 0) {
  3231. that.intervals[turtle].push(i);
  3232. var listenerName = '_interval_' + turtle;
  3233. that._setDispatchBlock(blk, turtle, listenerName);
  3234. var __listener = function (event) {
  3235. that.intervals[turtle].pop();
  3236. };
  3237. that._setListener(turtle, listenerName, __listener);
  3238. }
  3239. childFlow = args[1];
  3240. childFlowCount = 1;
  3241. break;
  3242. case 'perfectx':
  3243. if (args[0] < 0) {
  3244. var interval = mod12(-args[0]);
  3245. } else {
  3246. var interval = mod12(args[0]);
  3247. }
  3248. var deltaOctave = calcOctaveInterval(args[1]);
  3249. if ([1, 4, 5, 8].indexOf(interval) !== -1) {
  3250. that.perfect[turtle].push([args[0], deltaOctave]);
  3251. var listenerName = '_perfect_' + turtle;
  3252. that._setDispatchBlock(blk, turtle, listenerName);
  3253. var __listener = function (event) {
  3254. that.perfect[turtle].pop();
  3255. };
  3256. that._setListener(turtle, listenerName, __listener);
  3257. } else {
  3258. that.errorMsg(_('Input to Perfect Block must be 1, 4, 5, or 8'), blk);
  3259. }
  3260. childFlow = args[2];
  3261. childFlowCount = 1;
  3262. break;
  3263. case 'perfect':
  3264. // Deprecated
  3265. var mod12Arg = mod12(args[0]);
  3266. if ([1, 4, 5, 8].indexOf(mod12Arg) !== -1) {
  3267. that.perfect[turtle].push([args[0], 0]);
  3268. var listenerName = '_perfect_' + turtle;
  3269. that._setDispatchBlock(blk, turtle, listenerName);
  3270. var __listener = function (event) {
  3271. that.perfect[turtle].pop();
  3272. };
  3273. that._setListener(turtle, listenerName, __listener);
  3274. } else {
  3275. that.errorMsg(_('Input to Perfect Block must be 1, 4, 5, or 8'), blk);
  3276. }
  3277. childFlow = args[1];
  3278. childFlowCount = 1;
  3279. break;
  3280. case 'diminishedx':
  3281. if (args[0] < 0) {
  3282. var interval = mod12(-args[0]);
  3283. } else {
  3284. var interval = mod12(args[0]);
  3285. }
  3286. var deltaOctave = calcOctaveInterval(args[1]);
  3287. if ([1, 2, 3, 4, 5, 6, 7, 8].indexOf(interval) !== -1) {
  3288. that.diminished[turtle].push([args[0], deltaOctave]);
  3289. var listenerName = '_diminished_' + turtle;
  3290. that._setDispatchBlock(blk, turtle, listenerName);
  3291. var __listener = function (event) {
  3292. that.diminished[turtle].pop();
  3293. };
  3294. that._setListener(turtle, listenerName, __listener);
  3295. } else {
  3296. that.errorMsg(_('Input to Diminished Block must be 1, 2, 3, 4, 5, 6, 7, or 8'), blk);
  3297. }
  3298. childFlow = args[2];
  3299. childFlowCount = 1;
  3300. break;
  3301. case 'diminished':
  3302. // Deprecated
  3303. var mod12Arg = mod12(args[0]);
  3304. if ([1, 2, 3, 4, 5, 6, 7, 8].indexOf(mod12Arg) !== -1) {
  3305. that.diminished[turtle].push([args[0], 0]);
  3306. var listenerName = '_diminished_' + turtle;
  3307. that._setDispatchBlock(blk, turtle, listenerName);
  3308. var __listener = function (event) {
  3309. that.diminished[turtle].pop();
  3310. };
  3311. that._setListener(turtle, listenerName, __listener);
  3312. } else {
  3313. that.errorMsg(_('Input to Diminished Block must be 1, 2, 3, 4, 5, 6, 7, or 8'), blk);
  3314. }
  3315. childFlow = args[1];
  3316. childFlowCount = 1;
  3317. break;
  3318. case 'augmentedx':
  3319. if (args[0] < 0) {
  3320. var interval = mod12(-args[0]);
  3321. } else {
  3322. var interval = mod12(args[0]);
  3323. }
  3324. var deltaOctave = calcOctaveInterval(args[1]);
  3325. if ([1, 2, 3, 4, 5, 6, 7, 8].indexOf(interval) !== -1) {
  3326. that.augmented[turtle].push([args[0], deltaOctave]);
  3327. var listenerName = '_augmented_' + turtle;
  3328. that._setDispatchBlock(blk, turtle, listenerName);
  3329. var __listener = function (event) {
  3330. that.augmented[turtle].pop();
  3331. };
  3332. that._setListener(turtle, listenerName, __listener);
  3333. } else {
  3334. that.errorMsg(_('Input to Augmented Block must be 1, 2, 3, 4, 5, 6, 7, or 8'), blk);
  3335. }
  3336. childFlow = args[2];
  3337. childFlowCount = 1;
  3338. break;
  3339. case 'augmented':
  3340. // Deprecated
  3341. var mod12Arg = mod12(args[0]);
  3342. if ([1, 2, 3, 4, 5, 6, 7, 8].indexOf(mod12Arg) !== -1) {
  3343. that.augmented[turtle].push([args[0], 0]);
  3344. var listenerName = '_tritone_' + turtle;
  3345. that._setDispatchBlock(blk, turtle, listenerName);
  3346. var __listener = function (event) {
  3347. that.augmented[turtle].pop();
  3348. };
  3349. that._setListener(turtle, listenerName, __listener);
  3350. } else {
  3351. that.errorMsg(_('Input to Augmented Block must be 1, 2, 3, 4, 5, 6, 7, or 8'), blk);
  3352. }
  3353. childFlow = args[1];
  3354. childFlowCount = 1;
  3355. break;
  3356. case 'majorx':
  3357. if (args[0] < 0) {
  3358. var interval = mod12(-args[0]);
  3359. } else {
  3360. var interval = mod12(args[0]);
  3361. }
  3362. var deltaOctave = calcOctaveInterval(args[1]);
  3363. if ([2, 3, 6, 7].indexOf(interval) !== -1) {
  3364. that.major[turtle].push([args[0], deltaOctave]);
  3365. var listenerName = '_major_' + turtle;
  3366. that._setDispatchBlock(blk, turtle, listenerName);
  3367. var __listener = function (event) {
  3368. that.major[turtle].pop();
  3369. };
  3370. that._setListener(turtle, listenerName, __listener);
  3371. } else {
  3372. that.errorMsg(_('Input to Major Block must be 2, 3, 6, or 7'), blk);
  3373. }
  3374. childFlow = args[2];
  3375. childFlowCount = 1;
  3376. break;
  3377. case 'major':
  3378. // Deprecated
  3379. var mod12Arg = mod12(args[0]);
  3380. var octaveArg = args[1];
  3381. if ([2, 3, 6, 7].indexOf(mod12Arg) !== -1) {
  3382. that.major[turtle].push([args[0], 0]);
  3383. var listenerName = '_major_' + turtle;
  3384. that._setDispatchBlock(blk, turtle, listenerName);
  3385. var __listener = function (event) {
  3386. that.major[turtle].pop();
  3387. };
  3388. that._setListener(turtle, listenerName, __listener);
  3389. } else {
  3390. that.errorMsg(_('Input to Major Block must be 2, 3, 6, or 7'), blk);
  3391. }
  3392. childFlow = args[1];
  3393. childFlowCount = 1;
  3394. break;
  3395. case 'minorx':
  3396. if (args[0] < 0) {
  3397. var interval = mod12(-args[0]);
  3398. } else {
  3399. var interval = mod12(args[0]);
  3400. }
  3401. var deltaOctave = calcOctaveInterval(args[1]);
  3402. if ([2, 3, 6, 7].indexOf(interval) !== -1) {
  3403. that.minor[turtle].push([args[0], deltaOctave]);
  3404. var listenerName = '_minor_' + turtle;
  3405. that._setDispatchBlock(blk, turtle, listenerName);
  3406. var __listener = function (event) {
  3407. that.minor[turtle].pop();
  3408. };
  3409. that._setListener(turtle, listenerName, __listener);
  3410. } else {
  3411. that.errorMsg(_('Input to Minor Block must be 2, 3, 6, or 7'), blk);
  3412. }
  3413. childFlow = args[2];
  3414. childFlowCount = 1;
  3415. break;
  3416. case 'minor':
  3417. // Deprecated
  3418. var mod12Arg = mod12(args[0]);
  3419. if ([2, 3, 6, 7].indexOf(mod12Arg) !== -1) {
  3420. that.minor[turtle].push([args[0], 0]);
  3421. var listenerName = '_minor_' + turtle;
  3422. that._setDispatchBlock(blk, turtle, listenerName);
  3423. var __listener = function (event) {
  3424. that.minor[turtle].pop();
  3425. };
  3426. that._setListener(turtle, listenerName, __listener);
  3427. } else {
  3428. that.errorMsg(_('Input to Minor Block must be 2, 3, 6, or 7'), blk);
  3429. }
  3430. childFlow = args[1];
  3431. childFlowCount = 1;
  3432. break;
  3433. case 'newstaccato':
  3434. case 'staccato':
  3435. if (args.length > 1) {
  3436. if (that.blocks.blockList[blk].name === 'newstaccato') {
  3437. that.staccato[turtle].push(1 / args[0]);
  3438. } else {
  3439. that.staccato[turtle].push(args[0]);
  3440. }
  3441. childFlow = args[1];
  3442. childFlowCount = 1;
  3443. var listenerName = '_staccato_' + turtle;
  3444. that._setDispatchBlock(blk, turtle, listenerName);
  3445. var __listener = function (event) {
  3446. that.staccato[turtle].pop();
  3447. };
  3448. that._setListener(turtle, listenerName, __listener);
  3449. }
  3450. break;
  3451. case 'newslur':
  3452. case 'slur':
  3453. if (args.length > 1) {
  3454. if (that.blocks.blockList[blk].name === 'newslur') {
  3455. that.staccato[turtle].push(-1 / args[0]);
  3456. } else {
  3457. that.staccato[turtle].push(-args[0]);
  3458. }
  3459. if (!that.justCounting[turtle]) {
  3460. lilypondBeginSlur(that, turtle);
  3461. }
  3462. childFlow = args[1];
  3463. childFlowCount = 1;
  3464. var listenerName = '_staccato_' + turtle;
  3465. that._setDispatchBlock(blk, turtle, listenerName);
  3466. var __listener = function (event) {
  3467. that.staccato[turtle].pop();
  3468. if (!that.justCounting[turtle]) {
  3469. lilypondEndSlur(that, turtle);
  3470. }
  3471. };
  3472. that._setListener(turtle, listenerName, __listener);
  3473. }
  3474. break;
  3475. case 'drift':
  3476. that.drift[turtle] += 1;
  3477. childFlow = args[0];
  3478. childFlowCount = 1;
  3479. var listenerName = '_drift_' + turtle;
  3480. that._setDispatchBlock(blk, turtle, listenerName);
  3481. var __listener = function (event) {
  3482. that.drift[turtle] -= 1;
  3483. };
  3484. that._setListener(turtle, listenerName, __listener);
  3485. break;
  3486. case 'tie':
  3487. // Tie notes together in pairs.
  3488. that.tie[turtle] = true;
  3489. that.tieNote[turtle] = [];
  3490. that.tieCarryOver[turtle] = 0;
  3491. childFlow = args[0];
  3492. childFlowCount = 1;
  3493. var listenerName = '_tie_' + turtle;
  3494. that._setDispatchBlock(blk, turtle, listenerName);
  3495. var __listener = function (event) {
  3496. that.tie[turtle] = false;
  3497. // If tieCarryOver > 0, we have one more note to
  3498. // play.
  3499. if (that.tieCarryOver[turtle] > 0) {
  3500. if (!that.justCounting[turtle]) {
  3501. // Remove the note from the Lilypond list.
  3502. for (var i = 0; i < that.notePitches[turtle].length; i++) {
  3503. lilypondRemoveTie(that, turtle);
  3504. }
  3505. }
  3506. var noteValue = that.tieCarryOver[turtle];
  3507. that.tieCarryOver[turtle] = 0;
  3508. that._processNote(noteValue, blk, turtle);
  3509. }
  3510. that.tieNote[turtle] = [];
  3511. };
  3512. that._setListener(turtle, listenerName, __listener);
  3513. break;
  3514. case 'newswing':
  3515. case 'newswing2':
  3516. case 'swing':
  3517. // Grab a bit from the next note to give to the current note.
  3518. if (that.blocks.blockList[blk].name === 'newswing2') {
  3519. that.swing[turtle].push(1 / args[0]);
  3520. that.swingTarget[turtle].push(1 / args[1]);
  3521. childFlow = args[2];
  3522. } else if (that.blocks.blockList[blk].name === 'newswing') {
  3523. that.swing[turtle].push(1 / args[0]);
  3524. that.swingTarget[turtle].push(null);
  3525. childFlow = args[1];
  3526. } else {
  3527. that.swing[turtle].push(args[0]);
  3528. that.swingTarget[turtle].push(null);
  3529. childFlow = args[1];
  3530. }
  3531. that.swingCarryOver[turtle] = 0;
  3532. childFlowCount = 1;
  3533. var listenerName = '_swing_' + turtle;
  3534. that._setDispatchBlock(blk, turtle, listenerName);
  3535. var __listener = function (event) {
  3536. that.swingTarget[turtle].pop();
  3537. that.swing[turtle].pop();
  3538. that.swingCarryOver[turtle] = 0;
  3539. };
  3540. that._setListener(turtle, listenerName, __listener);
  3541. break;
  3542. case 'duplicatenotes':
  3543. var factor = args[0];
  3544. if (factor === 0) {
  3545. that.errorMsg(ZERODIVIDEERRORMSG, blk);
  3546. that.stopTurtle = true;
  3547. } else {
  3548. that.duplicateFactor[turtle] *= factor;
  3549. childFlow = args[1];
  3550. childFlowCount = 1;
  3551. var listenerName = '_duplicate_' + turtle;
  3552. that._setDispatchBlock(blk, turtle, listenerName);
  3553. var __listener = function (event) {
  3554. that.duplicateFactor[turtle] /= factor;
  3555. };
  3556. that._setListener(turtle, listenerName, __listener);
  3557. }
  3558. break;
  3559. case 'skipnotes':
  3560. var factor = args[0];
  3561. if (factor === 0) {
  3562. that.errorMsg(ZERODIVIDEERRORMSG, blk);
  3563. that.stopTurtle = true;
  3564. } else {
  3565. that.skipFactor[turtle] *= factor;
  3566. childFlow = args[1];
  3567. childFlowCount = 1;
  3568. var listenerName = '_skip_' + turtle;
  3569. that._setDispatchBlock(blk, turtle, listenerName);
  3570. var __listener = function (event) {
  3571. that.skipFactor[turtle] /= factor;
  3572. if (that.skipFactor[turtle] === 1) {
  3573. that.skipIndex[turtle] = 0;
  3574. }
  3575. };
  3576. that._setListener(turtle, listenerName, __listener);
  3577. }
  3578. break;
  3579. case 'multiplybeatfactor':
  3580. var factor = args[0];
  3581. if (factor === 0) {
  3582. that.errorMsg(ZERODIVIDEERRORMSG, blk);
  3583. that.stopTurtle = true;
  3584. } else {
  3585. that.beatFactor[turtle] *= factor;
  3586. childFlow = args[1];
  3587. childFlowCount = 1;
  3588. var listenerName = '_multiplybeat_' + turtle;
  3589. that._setDispatchBlock(blk, turtle, listenerName);
  3590. var __listener = function (event) {
  3591. that.beatFactor[turtle] /= factor;
  3592. };
  3593. that._setListener(turtle, listenerName, __listener);
  3594. }
  3595. break;
  3596. case 'dividebeatfactor':
  3597. var factor = args[0];
  3598. if (factor === 0) {
  3599. that.errorMsg(ZERODIVIDEERRORMSG, blk);
  3600. that.stopTurtle = true;
  3601. } else {
  3602. that.beatFactor[turtle] /= factor;
  3603. childFlow = args[1];
  3604. childFlowCount = 1;
  3605. var listenerName = '_dividebeat_' + turtle;
  3606. that._setDispatchBlock(blk, turtle, listenerName);
  3607. var __listener = function (event) {
  3608. that.beatFactor[turtle] *= factor;
  3609. };
  3610. that._setListener(turtle, listenerName, __listener);
  3611. }
  3612. break;
  3613. case 'settransposition':
  3614. var transValue = args[0];
  3615. if (!(that.invertList[turtle].length === 0)) {
  3616. that.transposition[turtle] -= transValue;
  3617. } else {
  3618. that.transposition[turtle] += transValue;
  3619. }
  3620. childFlow = args[1];
  3621. childFlowCount = 1;
  3622. var listenerName = '_transposition_' + turtle;
  3623. that._setDispatchBlock(blk, turtle, listenerName);
  3624. var __listener = function (event) {
  3625. if (!(that.invertList[turtle].length === 0)) {
  3626. that.transposition[turtle] += transValue;
  3627. } else {
  3628. that.transposition[turtle] -= transValue;
  3629. }
  3630. };
  3631. that._setListener(turtle, listenerName, __listener);
  3632. break;
  3633. case 'sharp':
  3634. if (!(that.invertList[turtle].length === 0)) {
  3635. that.transposition[turtle] -= 1;
  3636. } else {
  3637. that.transposition[turtle] += 1;
  3638. }
  3639. childFlow = args[0];
  3640. childFlowCount = 1;
  3641. var listenerName = '_sharp_' + turtle;
  3642. that._setDispatchBlock(blk, turtle, listenerName);
  3643. var __listener = function (event) {
  3644. if (!(that.invertList[turtle].length === 0)) {
  3645. that.transposition[turtle] += 1;
  3646. } else {
  3647. that.transposition[turtle] -= 1;
  3648. }
  3649. };
  3650. that._setListener(turtle, listenerName, __listener);
  3651. break;
  3652. case 'flat':
  3653. if (!(that.invertList[turtle].length === 0)) {
  3654. that.transposition[turtle] += 1;
  3655. } else {
  3656. that.transposition[turtle] -= 1;
  3657. }
  3658. childFlow = args[0];
  3659. childFlowCount = 1;
  3660. var listenerName = '_flat_' + turtle;
  3661. that._setDispatchBlock(blk, turtle, listenerName);
  3662. var __listener = function (event) {
  3663. if (!(that.invertList[turtle].length === 0)) {
  3664. that.transposition[turtle] -= 1;
  3665. } else {
  3666. that.transposition[turtle] += 1;
  3667. }
  3668. };
  3669. that._setListener(turtle, listenerName, __listener);
  3670. break;
  3671. case 'articulation':
  3672. if (args.length === 2 && typeof(args[0]) === 'number' && args[0] > 0) {
  3673. var newVolume = last(that.polyVolume[turtle]) * (100 + args[0]) / 100;
  3674. if (newVolume > 100) {
  3675. console.log('articulated volume exceeds 100%. clipping');
  3676. newVolume = 100;
  3677. }
  3678. that.polyVolume[turtle].push(newVolume);
  3679. that._setSynthVolume(newVolume, turtle);
  3680. if (!that.justCounting[turtle]) {
  3681. lilypondBeginArticulation(that, turtle);
  3682. }
  3683. childFlow = args[1];
  3684. childFlowCount = 1;
  3685. var listenerName = '_articulation_' + turtle;
  3686. that._setDispatchBlock(blk, turtle, listenerName);
  3687. var __listener = function (event) {
  3688. that.polyVolume[turtle].pop();
  3689. if (!that.justCounting[turtle]) {
  3690. lilypondEndArticulation(that, turtle);
  3691. }
  3692. };
  3693. that._setListener(turtle, listenerName, __listener);
  3694. }
  3695. break;
  3696. case 'setnotevolume2':
  3697. if (args.length === 2 && typeof(args[0]) === 'number') {
  3698. that.polyVolume[turtle].push(args[0]);
  3699. that._setSynthVolume(args[0], turtle);
  3700. childFlow = args[1];
  3701. childFlowCount = 1;
  3702. var listenerName = '_volume_' + turtle;
  3703. that._setDispatchBlock(blk, turtle, listenerName);
  3704. var __listener = function (event) {
  3705. that.polyVolume[turtle].pop();
  3706. };
  3707. that._setListener(turtle, listenerName, __listener);
  3708. }
  3709. break;
  3710. // Deprecated
  3711. case 'setnotevolume':
  3712. if (args.length === 1) {
  3713. if (typeof(args[0]) === 'string') {
  3714. that.errorMsg(NANERRORMSG, blk);
  3715. that.stopTurtle = true;
  3716. } else {
  3717. that._setSynthVolume(args[0], turtle);
  3718. }
  3719. }
  3720. break;
  3721. // Deprecated P5 tone generator replaced by macro.
  3722. case 'tone':
  3723. break;
  3724. case 'tone2':
  3725. if (_THIS_IS_TURTLE_BLOCKS_) {
  3726. if (typeof(that.turtleOscs[turtle]) === 'undefined') {
  3727. that.turtleOscs[turtle] = new p5.TriOsc();
  3728. }
  3729. osc = that.turtleOscs[turtle];
  3730. osc.stop();
  3731. osc.start();
  3732. osc.amp(0);
  3733. osc.freq(args[0]);
  3734. osc.fade(0.5, 0.2);
  3735. setTimeout(function(osc) {
  3736. osc.fade(0, 0.2);
  3737. }, args[1], osc);
  3738. }
  3739. break;
  3740. case 'hertz':
  3741. if (args.length !== 1 || args[0] == null) {
  3742. that.errorMsg(NOINPUTERRORMSG, blk);
  3743. that.stopTurtle = true;
  3744. break;
  3745. }
  3746. if (typeof(args[0]) !== 'number') {
  3747. that.errorMsg(NANERRORMSG, blk);
  3748. that.stopTurtle = true;
  3749. break;
  3750. }
  3751. var obj = frequencyToPitch(args[0]);
  3752. var note = obj[0];
  3753. var octave = obj[1];
  3754. var cents = obj[2];
  3755. var delta = 0;
  3756. if (note === '?') {
  3757. that.errorMsg(INVALIDPITCH, blk);
  3758. that.stopTurtle = true;
  3759. } else if (that.inMatrix) {
  3760. that.pitchTimeMatrix.addRowBlock(blk);
  3761. if (that.pitchBlocks.indexOf(blk) === -1) {
  3762. that.pitchBlocks.push(blk);
  3763. }
  3764. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  3765. that.pitchTimeMatrix.rowArgs.push(args[0]);
  3766. } else if (that.inNoteBlock[turtle] > 0) {
  3767. function addPitch(note, octave, cents, frequency) {
  3768. if (that.drumStyle[turtle].length > 0) {
  3769. var drumname = last(that.drumStyle[turtle]);
  3770. var note2 = that.getNote(note, octave, transposition, that.keySignature[turtle]);
  3771. that.pitchDrumTable[turtle][note2[0] + note2[1]] = drumname;
  3772. }
  3773. that.notePitches[turtle].push(note);
  3774. that.noteOctaves[turtle].push(octave);
  3775. that.noteCents[turtle].push(cents);
  3776. that.noteHertz[turtle].push(frequency);
  3777. }
  3778. if (!(that.invertList[turtle].length === 0)) {
  3779. var len = that.invertList[turtle].length;
  3780. var note1 = that.getNote(note, octave, 0, that.keySignature[turtle]);
  3781. var num1 = getNumber(note1[0], note1[1]);
  3782. for (var i = len - 1; i > -1; i--) {
  3783. var note2 = that.getNote(that.invertList[turtle][i][0], that.invertList[turtle][i][1], 0, that.keySignature[turtle]);
  3784. var num2 = getNumber(note2[0], note2[1]);
  3785. // var a = getNumNote(num1, 0);
  3786. if (that.invertList[turtle][i][2] === 'even') {
  3787. delta += num2 - num1;
  3788. } else { // odd
  3789. delta += num2 - num1 + 0.5;
  3790. }
  3791. num1 += 2 * delta;
  3792. }
  3793. }
  3794. addPitch(note, octave, cents, args[0]);
  3795. if (turtle in that.intervals && that.intervals[turtle].length > 0) {
  3796. for (var i = 0; i < that.intervals[turtle].length; i++) {
  3797. var ii = getInterval(that.intervals[turtle][i], that.keySignature[turtle], note);
  3798. var noteObj = that.getNote(note, octave, ii, that.keySignature[turtle]);
  3799. addPitch(noteObj[0], noteObj[1], cents, 0);
  3800. }
  3801. }
  3802. if (turtle in that.perfect && that.perfect[turtle].length > 0) {
  3803. var noteObj = that.getNote(note, octave, calcPerfect(last(that.perfect[turtle])), that.keySignature[turtle]);
  3804. addPitch(noteObj[0], noteObj[1], cents, 0);
  3805. }
  3806. if (turtle in that.diminished && that.diminished[turtle].length > 0) {
  3807. var noteObj = that.getNote(note, octave, calcDiminished(last(that.diminished[turtle])), that.keySignature[turtle]);
  3808. addPitch(noteObj[0], noteObj[1], cents, 0);
  3809. }
  3810. if (turtle in that.augmented && that.augmented[turtle].length > 0) {
  3811. var noteObj = that.getNote(note, octave, calcAugmented(last(that.augmented[turtle])), that.keySignature[turtle]);
  3812. addPitch(noteObj[0], noteObj[1], cents, 0);
  3813. }
  3814. if (turtle in that.major && that.major[turtle].length > 0) {
  3815. var noteObj = that.getNote(note, octave, calcMajor(last(that.major[turtle])), that.keySignature[turtle]);
  3816. addPitch(noteObj[0], noteObj[1], cents, 0);
  3817. }
  3818. if (turtle in that.minor && that.minor[turtle].length > 0) {
  3819. var noteObj = that.getNote(note, octave, calcMinor(last(that.minor[turtle])), that.keySignature[turtle]);
  3820. addPitch(noteObj[0], noteObj[1], cents, 0);
  3821. }
  3822. if (turtle in that.transposition) {
  3823. that.noteTranspositions[turtle].push(that.transposition[turtle] + 2 * delta);
  3824. } else {
  3825. that.noteTranspositions[turtle].push(2 * delta);
  3826. }
  3827. if (turtle in that.beatFactor) {
  3828. that.noteBeatValues[turtle].push(that.beatFactor[turtle]);
  3829. } else {
  3830. that.noteBeatValues[turtle].push(1);
  3831. }
  3832. that.pushedNote[turtle] = true;
  3833. } else if (that.inPitchStaircase) {
  3834. var frequency = args[0];
  3835. var note = frequencyToPitch(args[0]);
  3836. var flag = 0;
  3837. for (var i = 0 ; i < that.pitchStaircase.Stairs.length; i++) {
  3838. if (that.pitchStaircase.Stairs[i][2] < parseFloat(frequency)) {
  3839. that.pitchStaircase.Stairs.splice(i, 0, [note[0], note[1], parseFloat(frequency)]);
  3840. flag = 1;
  3841. break;
  3842. }
  3843. if (that.pitchStaircase.Stairs[i][2] === parseFloat(frequency)) {
  3844. that.pitchStaircase.Stairs.splice(i, 1, [note[0], note[1], parseFloat(frequency)]);
  3845. flag = 1;
  3846. break;
  3847. }
  3848. }
  3849. if (flag === 0) {
  3850. that.pitchStaircase.Stairs.push([note[0], note[1], parseFloat(frequency)]);
  3851. }
  3852. } else if (that.inPitchSlider) {
  3853. that.pitchSlider.Sliders.push([args[0], 0, 0]);
  3854. } else {
  3855. that.errorMsg(_('Hertz Block: Did you mean to use a Note block?'), blk);
  3856. }
  3857. break;
  3858. // deprecated
  3859. case 'triangle':
  3860. case 'sine':
  3861. case 'square':
  3862. case 'sawtooth':
  3863. if (args.length === 1) {
  3864. var obj = frequencyToPitch(args[0]);
  3865. // obj[2] is cents
  3866. if (that.inMatrix) {
  3867. that.pitchTimeMatrix.addRowBlock(blk);
  3868. if (that.pitchBlocks.indexOf(blk) === -1) {
  3869. that.pitchBlocks.push(blk);
  3870. }
  3871. that.pitchTimeMatrix.rowLabels.push(that.blocks.blockList[blk].name);
  3872. that.pitchTimeMatrix.rowArgs.push(args[0]);
  3873. } else if (that.inPitchSlider) {
  3874. that.pitchSlider.Sliders.push([args[0], 0, 0]);
  3875. } else {
  3876. that.oscList[turtle].push(that.blocks.blockList[blk].name);
  3877. // We keep track of pitch and octave for notation purposes.
  3878. that.notePitches[turtle].push(obj[0]);
  3879. that.noteOctaves[turtle].push(obj[1]);
  3880. that.noteCents[turtle].push(obj[2]);
  3881. if (obj[2] !== 0) {
  3882. that.noteHertz[turtle].push(pitchToFrequency(obj[0], obj[1], obj[2], that.keySignature[turtle]));
  3883. } else {
  3884. that.noteHertz[turtle].push(0);
  3885. }
  3886. if (turtle in that.transposition) {
  3887. that.noteTranspositions[turtle].push(that.transposition[turtle]);
  3888. } else {
  3889. that.noteTranspositions[turtle].push(0);
  3890. }
  3891. if (turtle in that.beatFactor) {
  3892. that.noteBeatValues[turtle].push(that.beatFactor[turtle]);
  3893. } else {
  3894. that.noteBeatValues[turtle].push(1);
  3895. }
  3896. that.pushedNote[turtle] = true;
  3897. }
  3898. }
  3899. break;
  3900. // Deprecated
  3901. case 'playfwd':
  3902. that.pitchTimeMatrix.playDirection = 1;
  3903. that._runFromBlock(that, turtle, args[0]);
  3904. break;
  3905. // Deprecated
  3906. case 'playbwd':
  3907. that.pitchTimeMatrix.playDirection = -1;
  3908. that._runFromBlock(that, turtle, args[0]);
  3909. break;
  3910. case 'stuplet':
  3911. if (that.inMatrix) {
  3912. that.pitchTimeMatrix.addColBlock(blk, args[0]);
  3913. var noteBeatValue = (1 / args[1]) * that.beatFactor[turtle];
  3914. if (that.tuplet === true) {
  3915. // The simple-tuplet block is inside.
  3916. for (var i = 0; i < args[0]; i++) {
  3917. if (!that.addingNotesToTuplet) {
  3918. that.tupletRhythms.push(['notes', 0]);
  3919. that.addingNotesToTuplet = true;
  3920. }
  3921. that._processNote(noteBeatValue, blk, turtle);
  3922. }
  3923. } else {
  3924. that.tupletParams.push([1, noteBeatValue]);
  3925. var obj = ['simple', 0];
  3926. for (var i = 0; i < args[0]; i++) {
  3927. obj.push((1 / args[1]) * that.beatFactor[turtle]);
  3928. }
  3929. that.tupletRhythms.push(obj);
  3930. }
  3931. }
  3932. break;
  3933. case 'tuplet2':
  3934. case 'tuplet3':
  3935. if (that.inMatrix) {
  3936. if (that.blocks.blockList[blk].name === 'tuplet3') {
  3937. that.tupletParams.push([args[0], (1 / args[1]) * that.beatFactor[turtle]]);
  3938. } else {
  3939. that.tupletParams.push([args[0], args[1] * that.beatFactor[turtle]]);
  3940. }
  3941. that.tuplet = true;
  3942. that.addingNotesToTuplet = false;
  3943. } else {
  3944. that.errorMsg(_('Tuplet Block: Did you mean to use a Matrix block?'), blk);
  3945. }
  3946. childFlow = args[2];
  3947. childFlowCount = 1;
  3948. var listenerName = '_tuplet_' + turtle;
  3949. that._setDispatchBlock(blk, turtle, listenerName);
  3950. var __listener = function (event) {
  3951. if (that.inMatrix) {
  3952. that.tuplet = false;
  3953. that.addingNotesToTuplet = false;
  3954. } else {
  3955. }
  3956. };
  3957. that._setListener(turtle, listenerName, __listener);
  3958. break;
  3959. case 'tuplet4':
  3960. if (that.inMatrix) {
  3961. that.tupletParams.push([1, (1 / args[0]) * that.beatFactor[turtle]]);
  3962. that.tuplet = true;
  3963. that.addingNotesToTuplet = false;
  3964. } else {
  3965. that.errorMsg(_('Tuplet Block: Did you mean to use a Matrix block?'), blk);
  3966. }
  3967. childFlow = args[1];
  3968. childFlowCount = 1;
  3969. var listenerName = '_tuplet_' + turtle;
  3970. that._setDispatchBlock(blk, turtle, listenerName);
  3971. var __listener = function (event) {
  3972. if (that.inMatrix) {
  3973. that.tuplet = false;
  3974. that.addingNotesToTuplet = false;
  3975. } else {
  3976. }
  3977. };
  3978. that._setListener(turtle, listenerName, __listener);
  3979. break;
  3980. default:
  3981. if (that.blocks.blockList[blk].name in that.evalFlowDict) {
  3982. eval(that.evalFlowDict[that.blocks.blockList[blk].name]);
  3983. } else {
  3984. // Could be an arg block, so we need to print its value.
  3985. if (that.blocks.blockList[blk].isArgBlock()) {
  3986. args.push(that.parseArg(that, turtle, blk));
  3987. console.log('block: ' + blk + ' turtle: ' + turtle);
  3988. console.log('block name: ' + that.blocks.blockList[blk].name);
  3989. console.log('block value: ' + that.blocks.blockList[blk].value);
  3990. if (that.blocks.blockList[blk].value == null) {
  3991. that.textMsg('null block value');
  3992. } else {
  3993. that.textMsg(that.blocks.blockList[blk].value.toString());
  3994. }
  3995. } else {
  3996. that.errorMsg('I do not know how to ' + that.blocks.blockList[blk].name + '.', blk);
  3997. }
  3998. that.stopTurtle = true;
  3999. }
  4000. break;
  4001. }
  4002. // (3) Queue block below the current block.
  4003. // Is the block in a queued clamp?
  4004. if (blk in that.endOfClampSignals[turtle]) {
  4005. for (var i = that.endOfClampSignals[turtle][blk].length - 1; i >= 0; i--) {
  4006. var signal = that.endOfClampSignals[turtle][blk][i];
  4007. if (signal != null) {
  4008. that.stage.dispatchEvent(that.endOfClampSignals[turtle][blk][i]);
  4009. // Mark issued signals as null
  4010. that.endOfClampSignals[turtle][blk][i] = null;
  4011. }
  4012. }
  4013. var cleanSignals = [];
  4014. for (var i = 0; i < that.endOfClampSignals[turtle][blk].length; i++) {
  4015. if (that.endOfClampSignals[turtle][blk][i] != null) {
  4016. cleanSignals.push(that.endOfClampSignals[turtle][blk][i]);
  4017. }
  4018. }
  4019. that.endOfClampSignals[turtle][blk] = cleanSignals;
  4020. }
  4021. if (docById('statusDiv').style.visibility === 'visible') {
  4022. that.statusMatrix.updateAll();
  4023. }
  4024. // If there is a child flow, queue it.
  4025. if (childFlow != null) {
  4026. if (that.blocks.blockList[blk].name==='doArg' || that.blocks.blockList[blk].name==='nameddoArg') {
  4027. var queueBlock = new Queue(childFlow, childFlowCount, blk, actionArgs);
  4028. } else {
  4029. var queueBlock = new Queue(childFlow, childFlowCount, blk, receivedArg);
  4030. }
  4031. // We need to keep track of the parent block to the child
  4032. // flow so we can unlightlight the parent block after the
  4033. // child flow completes.
  4034. that.parentFlowQueue[turtle].push(blk);
  4035. that.turtles.turtleList[turtle].queue.push(queueBlock);
  4036. }
  4037. var nextBlock = null;
  4038. var parentBlk = null;
  4039. // Run the last flow in the queue.
  4040. if (that.turtles.turtleList[turtle].queue.length > queueStart) {
  4041. nextBlock = last(that.turtles.turtleList[turtle].queue).blk;
  4042. parentBlk = last(that.turtles.turtleList[turtle].queue).parentBlk;
  4043. passArg = last(that.turtles.turtleList[turtle].queue).args;
  4044. // Since the forever block starts at -1, it will never === 1.
  4045. if (last(that.turtles.turtleList[turtle].queue).count === 1) {
  4046. // Finished child so pop it off the queue.
  4047. that.turtles.turtleList[turtle].queue.pop();
  4048. } else {
  4049. // Decrement the counter for repeating that flow.
  4050. last(that.turtles.turtleList[turtle].queue).count -= 1;
  4051. }
  4052. }
  4053. if (nextBlock != null) {
  4054. if (parentBlk !== blk) {
  4055. // The wait block waits waitTimes longer than other
  4056. // blocks before it is unhighlighted.
  4057. if (that.turtleDelay === TURTLESTEP) {
  4058. that.unhighlightStepQueue[turtle] = blk;
  4059. } else {
  4060. setTimeout(function () {
  4061. if (that.blocks.visible) {
  4062. that.blocks.unhighlight(blk);
  4063. }
  4064. }, that.turtleDelay + that.waitTimes[turtle]);
  4065. }
  4066. }
  4067. if ((that.backward[turtle].length > 0 && that.blocks.blockList[blk].connections[0] == null) || (that.backward[turtle].length === 0 && last(that.blocks.blockList[blk].connections) == null)) {
  4068. // If we are at the end of the child flow, queue the
  4069. // unhighlighting of the parent block to the flow.
  4070. if (that.parentFlowQueue[turtle].length > 0 && that.turtles.turtleList[turtle].queue.length > 0 && last(that.turtles.turtleList[turtle].queue).parentBlk !== last(that.parentFlowQueue[turtle])) {
  4071. that.unhightlightQueue[turtle].push(last(that.parentFlowQueue[turtle]));
  4072. // that.unhightlightQueue[turtle].push(that.parentFlowQueue[turtle].pop());
  4073. } else if (that.unhightlightQueue[turtle].length > 0) {
  4074. // The child flow is finally complete, so unhighlight.
  4075. setTimeout(function () {
  4076. if (that.blocks.visible) {
  4077. that.blocks.unhighlight(that.unhightlightQueue[turtle].pop());
  4078. } else {
  4079. that.unhightlightQueue[turtle].pop();
  4080. }
  4081. }, that.turtleDelay);
  4082. }
  4083. }
  4084. // We don't update parameter blocks when running full speed.
  4085. if (that.turtleDelay !== 0) {
  4086. for (var pblk in that.parameterQueue[turtle]) {
  4087. that._updateParameterBlock(that, turtle, that.parameterQueue[turtle][pblk]);
  4088. }
  4089. }
  4090. if (isflow){
  4091. that._runFromBlockNow(that, turtle, nextBlock, isflow, passArg, queueStart);
  4092. }
  4093. else{
  4094. that._runFromBlock(that, turtle, nextBlock, isflow, passArg);
  4095. }
  4096. } else {
  4097. // Make sure any unissued signals are dispatched.
  4098. for (var b in that.endOfClampSignals[turtle]) {
  4099. for (var i = 0; i < that.endOfClampSignals[turtle][b].length; i++) {
  4100. if (that.endOfClampSignals[turtle][b][i] != null) {
  4101. that.stage.dispatchEvent(that.endOfClampSignals[turtle][b][i]);
  4102. }
  4103. }
  4104. }
  4105. if (turtle in that.forwardListener) {
  4106. for (var b in that.forwardListener[turtle]) {
  4107. for (var i = 0; i < this.forwardListener[turtle][b].length; i++) {
  4108. that.stage.removeEventListener('_forward_' + turtle, that.forwardListener[turtle][b][i], false);
  4109. }
  4110. }
  4111. }
  4112. if (turtle in that.rightListener) {
  4113. for (var b in that.rightListener[turtle]) {
  4114. for (var i = 0; i < this.rightListener[turtle][b].length; i++) {
  4115. that.stage.removeEventListener('_right_' + turtle, that.rightListener[turtle][b][i], false);
  4116. }
  4117. }
  4118. }
  4119. if (turtle in that.arcListener) {
  4120. for (var b in that.arcListener[turtle]) {
  4121. for (var i = 0; i < this.arcListener[turtle][b].length; i++) {
  4122. that.stage.removeEventListener('_arc_' + turtle, that.arcListener[turtle][b][i], false);
  4123. }
  4124. }
  4125. }
  4126. // Make sure SVG path is closed.
  4127. that.turtles.turtleList[turtle].closeSVG();
  4128. // Mark the turtle as not running.
  4129. that.turtles.turtleList[turtle].running = false;
  4130. if (!that.turtles.running() && queueStart === 0) {
  4131. that.onStopTurtle();
  4132. }
  4133. // Because flow can come from calc blocks, we are not
  4134. // ensured that the turtle is really finished running
  4135. // yet. Hence the timeout.
  4136. __checkLilypond = function () {
  4137. if (!that.turtles.running() && queueStart === 0 && that.suppressOutput[turtle] && !that.justCounting[turtle]) {
  4138. console.log('saving lilypond output: ' + that.lilypondStaging);
  4139. saveLilypondOutput(that, _('My Project') + '.ly');
  4140. that.suppressOutput[turtle] = false;
  4141. that.checkingLilypond = false;
  4142. that.runningLilypond = false;
  4143. // Reset cursor.
  4144. document.body.style.cursor = 'default';
  4145. } else if (that.suppressOutput[turtle]) {
  4146. setTimeout(function () {
  4147. __checkLilypond();
  4148. }, 250);
  4149. }
  4150. };
  4151. if (!that.turtles.running() && queueStart === 0 && that.suppressOutput[turtle] && !that.justCounting[turtle]) {
  4152. if (!that.checkingLilypond) {
  4153. that.checkingLilypond = true;
  4154. setTimeout(function () {
  4155. __checkLilypond();
  4156. }, 250);
  4157. }
  4158. }
  4159. // Nothing else to do... so cleaning up.
  4160. if (that.turtles.turtleList[turtle].queue.length === 0 || blk !== last(that.turtles.turtleList[turtle].queue).parentBlk) {
  4161. setTimeout(function () {
  4162. if (that.blocks.visible) {
  4163. that.blocks.unhighlight(blk);
  4164. }
  4165. }, that.turtleDelay);
  4166. }
  4167. // Unhighlight any parent blocks still highlighted.
  4168. for (var b in that.parentFlowQueue[turtle]) {
  4169. if (that.blocks.visible) {
  4170. that.blocks.unhighlight(that.parentFlowQueue[turtle][b]);
  4171. }
  4172. }
  4173. // Make sure the turtles are on top.
  4174. var i = that.stage.getNumChildren() - 1;
  4175. that.stage.setChildIndex(that.turtles.turtleList[turtle].container, i);
  4176. that.refreshCanvas();
  4177. for (var arg in that.evalOnStopList) {
  4178. eval(that.evalOnStopList[arg]);
  4179. }
  4180. }
  4181. clearTimeout(this.saveTimeout);
  4182. var me = this;
  4183. this.saveTimeout = setTimeout(function () {
  4184. // Save at the end to save an image
  4185. // console.log('in saveTimeout');
  4186. me.saveLocally();
  4187. }, DEFAULTDELAY * 1.5);
  4188. };
  4189. this._setSynthVolume = function (vol, turtle) {
  4190. if (vol > 100) {
  4191. vol = 100;
  4192. } else if (vol < 0) {
  4193. vol = 0;
  4194. }
  4195. if (_THIS_IS_MUSIC_BLOCKS_) {
  4196. this.synth.setVolume(vol);
  4197. }
  4198. };
  4199. this._processNote = function (noteValue, blk, turtle) {
  4200. if (this.bpm[turtle].length > 0) {
  4201. var bpmFactor = TONEBPM / last(this.bpm[turtle]);
  4202. } else {
  4203. var bpmFactor = TONEBPM / this._masterBPM;
  4204. }
  4205. if (this.blocks.blockList[blk].name === 'osctime') {
  4206. // Convert msecs to note value.
  4207. if (noteValue == 0) {
  4208. var noteBeatValue = 0;
  4209. } else {
  4210. var noteBeatValue = (bpmFactor * 1000) / noteValue;
  4211. }
  4212. } else {
  4213. var noteBeatValue = noteValue;
  4214. }
  4215. // How best to expose this in the UI? What units?
  4216. this.notesPlayed[turtle] += (1 / (noteValue * this.beatFactor[turtle]));
  4217. var vibratoRate = 0;
  4218. var vibratoValue = 0;
  4219. var vibratoIntensity = 0;
  4220. var doVibrato = false;
  4221. if (this.vibratoRate[turtle].length > 0) {
  4222. vibratoRate = last(this.vibratoRate[turtle]);
  4223. vibratoIntensity = last(this.vibratoIntensity[turtle]);
  4224. doVibrato = true;
  4225. }
  4226. var carry = 0;
  4227. if (this.crescendoDelta[turtle].length === 0) {
  4228. this._setSynthVolume(last(this.polyVolume[turtle]), turtle);
  4229. }
  4230. if (this.inMatrix) {
  4231. if (this.inNoteBlock[turtle] > 0) {
  4232. this.pitchTimeMatrix.addColBlock(blk, 1);
  4233. for (var i = 0; i < this.pitchBlocks.length; i++) {
  4234. this.pitchTimeMatrix.addNode(this.pitchBlocks[i], blk, 0);
  4235. }
  4236. for (var i = 0; i < this.drumBlocks.length; i++) {
  4237. this.pitchTimeMatrix.addNode(this.drumBlocks[i], blk, 0);
  4238. }
  4239. }
  4240. noteBeatValue *= this.beatFactor[turtle];
  4241. if (this.tuplet === true) {
  4242. if (this.addingNotesToTuplet) {
  4243. var i = this.tupletRhythms.length - 1;
  4244. this.tupletRhythms[i].push(noteBeatValue);
  4245. } else {
  4246. this.tupletRhythms.push(['notes', this.tupletParams.length - 1, noteBeatValue]);
  4247. this.addingNotesToTuplet = true;
  4248. }
  4249. } else {
  4250. this.tupletRhythms.push(['', 1, noteBeatValue]);
  4251. }
  4252. } else {
  4253. // We start the music clock as the first note is being
  4254. // played.
  4255. if (this.firstNoteTime == null && !this.suppressOutput[turtle]) {
  4256. var d = new Date();
  4257. this.firstNoteTime = d.getTime();
  4258. }
  4259. // Calculate a lag: In case this turtle has fallen behind,
  4260. // we need to catch up.
  4261. var d = new Date();
  4262. var elapsedTime = (d.getTime() - this.firstNoteTime) / 1000;
  4263. if (this.drift[turtle] === 0) {
  4264. // How far behind is this turtle lagging?
  4265. var turtleLag = elapsedTime - this.turtleTime[turtle];
  4266. } else {
  4267. // When we are "drifting", we don't bother with lag.
  4268. var turtleLag = 0;
  4269. }
  4270. // If we are in a tie, depending upon parity, we either
  4271. // add the duration from the previous note to the current
  4272. // note, or we cache the duration and set the wait to
  4273. // zero. FIXME: Will not work when using dup and skip.
  4274. if (this.tie[turtle]) {
  4275. // We need to check to see if we are tying together
  4276. // similar notes.
  4277. if (this.tieCarryOver[turtle] > 0) {
  4278. var match = true;
  4279. if (this.tieNote[turtle].length !== this.notePitches[turtle].length) {
  4280. match = false;
  4281. } else {
  4282. for (var i = 0; i < this.tieNote[turtle].length; i++) {
  4283. if (this.tieNote[turtle][i][0] != this.notePitches[turtle][i]) {
  4284. match = false;
  4285. break;
  4286. }
  4287. if (this.tieNote[turtle][i][1] != this.noteOctaves[turtle][i]) {
  4288. match = false;
  4289. break;
  4290. }
  4291. }
  4292. }
  4293. if (!match) {
  4294. var tmpBeatValue = this.tieCarryOver[turtle];
  4295. this.tieCarryOver[turtle] = 0;
  4296. this.tie[turtle] = false;
  4297. // Save the current note.
  4298. var saveNote = [];
  4299. for (var i = 0; i < this.notePitches[turtle].length; i++) {
  4300. saveNote.push([this.notePitches[turtle][i], this.noteOctaves[turtle][i], this.noteCents[turtle][i], this.noteHertz[turtle][i]]);
  4301. }
  4302. // Swap in the previous note.
  4303. this.notePitches[turtle] = [];
  4304. this.noteOctaves[turtle] = [];
  4305. this.noteCents[turtle] = [];
  4306. this.noteHertz[turtle] = [];
  4307. for (var i = 0; i < this.tieNote[turtle].length; i++) {
  4308. this.notePitches[turtle].push(this.tieNote[turtle][i][0]);
  4309. this.noteOctaves[turtle].push(this.tieNote[turtle][i][1]);
  4310. this.noteCents[turtle].push(this.tieNote[turtle][i][2]);
  4311. this.noteHertz[turtle].push(this.tieNote[turtle][i][3]);
  4312. }
  4313. this.tieNote[turtle] = [];
  4314. if (!this.justCounting[turtle]) {
  4315. // Remove the note from the Lilypond list.
  4316. for (var i = 0; i < this.notePitches[turtle].length; i++) {
  4317. lilypondRemoveTie(this, turtle);
  4318. }
  4319. }
  4320. this._processNote(tmpBeatValue, blk, turtle);
  4321. // Restore the current note.
  4322. this.tie[turtle] = true;
  4323. this.notePitches[turtle] = [];
  4324. this.noteOctaves[turtle] = [];
  4325. this.noteCents[turtle] = [];
  4326. this.noteHertz[turtle] = [];
  4327. for (var i = 0; i < saveNote.length; i++) {
  4328. this.notePitches[turtle].push(saveNote[i][0]);
  4329. this.noteOctaves[turtle].push(saveNote[i][1]);
  4330. this.noteCents[turtle].push(saveNote[i][2]);
  4331. this.noteHertz[turtle].push(saveNote[i][3]);
  4332. }
  4333. }
  4334. }
  4335. if (this.tieCarryOver[turtle] === 0) {
  4336. this.tieNote[turtle] = [];
  4337. this.tieCarryOver[turtle] = noteBeatValue;
  4338. for (var i = 0; i < this.notePitches[turtle].length; i++) {
  4339. this.tieNote[turtle].push([this.notePitches[turtle][i], this.noteOctaves[turtle][i], this.noteCents[turtle][i], this.noteHertz[turtle][i]]);
  4340. }
  4341. noteBeatValue = 0;
  4342. } else {
  4343. carry = this.tieCarryOver[turtle];
  4344. noteBeatValue = 1 / ((1 / noteBeatValue) + (1 / this.tieCarryOver[turtle]));
  4345. this.tieCarryOver[turtle] = 0;
  4346. }
  4347. }
  4348. // If we are in a swing, depending upon parity, we either
  4349. // add the duration from the current note or we substract
  4350. // duration from the next note. Swing is triggered by an
  4351. // initial notevalue. When that notevalue is encountered
  4352. // again, the swing terminates, e.g., 8->4->4->4->8
  4353. // 8->4->4->4->8
  4354. // FIXME: Will not work when using dup and skip.
  4355. // FIXME: Could behave weirdly with tie.
  4356. if (this.swing[turtle].length > 0) {
  4357. // Deprecated
  4358. // newswing2 takes the target as an argument
  4359. if (last(this.swingTarget[turtle]) == null) {
  4360. // When we start a swing we need to keep track of
  4361. // the initial beat value.
  4362. this.swingTarget[turtle][this.swingTarget[turtle].length - 1] = noteBeatValue;
  4363. }
  4364. var swingValue = last(this.swing[turtle]);
  4365. // If this notevalue matches the target, either we are
  4366. // starting a swing or ending a swing.
  4367. if (noteBeatValue === last(this.swingTarget[turtle])) {
  4368. if (this.swingCarryOver[turtle] === 0) {
  4369. noteBeatValue = 1 / ((1 / noteBeatValue) + (1 / swingValue));
  4370. this.swingCarryOver[turtle] = swingValue;
  4371. } else {
  4372. if (noteBeatValue === swingValue) {
  4373. noteBeatValue = 0;
  4374. } else {
  4375. noteBeatValue = 1 / ((1 / noteBeatValue) - (1 / swingValue));
  4376. }
  4377. this.swingCarryOver[turtle] = 0;
  4378. }
  4379. if (noteBeatValue < 0) {
  4380. noteBeatValue = 0;
  4381. }
  4382. }
  4383. }
  4384. // Duration is the duration of the note to be
  4385. // played. doWait sets the wait time for the turtle before
  4386. // the next block is executed.
  4387. var duration = noteBeatValue * this.beatFactor[turtle]; // beat value
  4388. if (duration > 0 && !this.suppressOutput[turtle]) {
  4389. this.turtleTime[turtle] += ((bpmFactor / duration) + (this.noteDelay / 1000)) * this.duplicateFactor[turtle];
  4390. }
  4391. if (!this.suppressOutput[turtle]) {
  4392. if (duration > 0) {
  4393. this._doWait(turtle, Math.max(((bpmFactor / duration) + (this.noteDelay / 1000)) * this.duplicateFactor[turtle] - turtleLag, 0));
  4394. }
  4395. }
  4396. var waitTime = 0;
  4397. for (var j = 0; j < this.duplicateFactor[turtle]; j++) {
  4398. if (this.skipFactor[turtle] > 1 && this.skipIndex[turtle] % this.skipFactor[turtle] > 0) {
  4399. this.skipIndex[turtle] += 1;
  4400. // Lessen delay time by one note since we are
  4401. // skipping a note.
  4402. if (duration > 0) {
  4403. this.waitTimes[turtle] -= ((bpmFactor / duration) + (this.noteDelay / 1000)) * 1000;
  4404. if (this.waitTimes[turtle] < 0) {
  4405. this.waitTimes[turtle] = 0;
  4406. }
  4407. }
  4408. continue;
  4409. }
  4410. if (this.skipFactor[turtle] > 1) {
  4411. this.skipIndex[turtle] += 1;
  4412. }
  4413. if (!this.suppressOutput[turtle]) {
  4414. if (j > 0) {
  4415. if (duration > 0) {
  4416. waitTime += (bpmFactor * 1000 / duration);
  4417. }
  4418. }
  4419. } else {
  4420. waitTime = 0;
  4421. }
  4422. __playnote = function (that) {
  4423. var notes = [];
  4424. var drums = [];
  4425. var insideChord = -1;
  4426. if ((that.notePitches[turtle].length + that.oscList[turtle].length) > 1) {
  4427. if (turtle in that.lilypondStaging && !that.justCounting[turtle]) {
  4428. var insideChord = that.lilypondStaging[turtle].length + 1;
  4429. } else {
  4430. var insideChord = 1;
  4431. }
  4432. }
  4433. that.noteBeat[turtle] = noteBeatValue;
  4434. // Do not process a note if its duration is equal
  4435. // to infinity or NaN.
  4436. if (!isFinite(duration)) {
  4437. return;
  4438. }
  4439. // Use the beatValue of the first note in
  4440. // the group since there can only be one.
  4441. if (that.staccato[turtle].length > 0) {
  4442. var staccatoBeatValue = last(that.staccato[turtle]);
  4443. if (staccatoBeatValue < 0) {
  4444. // slur
  4445. var beatValue = bpmFactor / ((noteBeatValue) * that.noteBeatValues[turtle][0]) + bpmFactor / (-staccatoBeatValue * that.noteBeatValues[turtle][0]);
  4446. } else if (staccatoBeatValue > noteBeatValue) {
  4447. // staccato
  4448. var beatValue = bpmFactor / (staccatoBeatValue * that.noteBeatValues[turtle][0]);
  4449. } else {
  4450. var beatValue = bpmFactor / (noteBeatValue * that.noteBeatValues[turtle][0]);
  4451. }
  4452. } else {
  4453. var beatValue = bpmFactor / (noteBeatValue * that.noteBeatValues[turtle][0]);
  4454. }
  4455. if (doVibrato) {
  4456. vibratoValue = beatValue * (duration / vibratoRate);
  4457. }
  4458. that._dispatchTurtleSignals(turtle, beatValue, blk, noteBeatValue);
  4459. // Process pitches
  4460. if (that.notePitches[turtle].length > 0) {
  4461. for (var i = 0; i < that.notePitches[turtle].length; i++) {
  4462. if (that.notePitches[turtle][i] === 'rest') {
  4463. note = 'R';
  4464. } else {
  4465. var noteObj = that.getNote(that.notePitches[turtle][i], that.noteOctaves[turtle][i], that.noteTranspositions[turtle][i], that.keySignature[turtle]);
  4466. // If the cents for this note != 0, then
  4467. // we need to convert to frequency and add
  4468. // in the cents.
  4469. if (that.noteCents[turtle][i] !== 0) {
  4470. if (that.noteHertz[turtle][i] !== 0 && that.noteTranspositions[turtle][i] === 0) {
  4471. var note = that.noteHertz[turtle][i];
  4472. } else {
  4473. var note = Math.floor(pitchToFrequency(noteObj[0], noteObj[1], that.noteCents[turtle][i], that.keySignature[turtle]));
  4474. }
  4475. } else {
  4476. var note = noteObj[0] + noteObj[1];
  4477. }
  4478. }
  4479. if (note !== 'R') {
  4480. notes.push(note);
  4481. }
  4482. if (duration > 0) {
  4483. if (carry > 0) {
  4484. if (i === 0 && !that.justCounting[turtle]) {
  4485. lilypondInsertTie(that, turtle);
  4486. }
  4487. originalDuration = 1 / ((1 / duration) - (1 / carry));
  4488. } else {
  4489. originalDuration = duration;
  4490. }
  4491. if (!that.justCounting[turtle]) {
  4492. updateLilypondNotation(that, note, originalDuration, turtle, insideChord);
  4493. }
  4494. } else if (that.tieCarryOver[turtle] > 0) {
  4495. if (!that.justCounting[turtle]) {
  4496. updateLilypondNotation(that, note, that.tieCarryOver[turtle], turtle, insideChord);
  4497. }
  4498. } else {
  4499. // console.log('duration == ' + duration + ' and tieCarryOver === 0 and drift is ' + drift);
  4500. }
  4501. }
  4502. if (!that.justCounting[turtle]) {
  4503. console.log("notes to play " + notes + ' ' + noteBeatValue);
  4504. } else {
  4505. console.log("notes to count " + notes + ' ' + noteBeatValue);
  4506. }
  4507. if (!that.suppressOutput[turtle]) {
  4508. that.turtles.turtleList[turtle].blink(duration,last(that.polyVolume[turtle]));
  4509. }
  4510. if (notes.length > 0) {
  4511. var len = notes[0].length;
  4512. // Deprecated
  4513. if (typeof(notes[i]) === 'string') {
  4514. that.currentNotes[turtle] = notes[0].slice(0, len - 1);
  4515. that.currentOctaves[turtle] = parseInt(notes[0].slice(len - 1));
  4516. }
  4517. if (that.turtles.turtleList[turtle].drum) {
  4518. for (var i = 0; i < notes.length; i++) {
  4519. notes[i] = notes[i].replace(/♭/g, 'b').replace(/♯/g, '#'); // 'C2'; // Remove pitch
  4520. }
  4521. } else {
  4522. for (var i = 0; i < notes.length; i++) {
  4523. if (typeof(notes[i]) === 'string') {
  4524. notes[i] = notes[i].replace(/♭/g, 'b').replace(/♯/g, '#');
  4525. }
  4526. }
  4527. }
  4528. if (!that.suppressOutput[turtle] && duration > 0) {
  4529. if (_THIS_IS_MUSIC_BLOCKS_) {
  4530. if (that.oscList[turtle].length > 0) {
  4531. if (notes.length > 1) {
  4532. that.errorMsg(last(that.oscList[turtle]) + ': ' + _('synth cannot play chords.'), blk);
  4533. }
  4534. that.synth.trigger(notes, beatValue, last(that.oscList[turtle]), [vibratoIntensity, vibratoValue]);
  4535. } else if (that.drumStyle[turtle].length > 0) {
  4536. that.synth.trigger(notes, beatValue, last(that.drumStyle[turtle]), []);
  4537. } else if (that.turtles.turtleList[turtle].drum) {
  4538. that.synth.trigger(notes, beatValue, 'drum', []);
  4539. } else {
  4540. // Look for any notes in the chord that might be in the pitchDrumTable.
  4541. for (var d = 0; d < notes.length; d++) {
  4542. if (notes[d] in that.pitchDrumTable[turtle]) {
  4543. that.synth.trigger(notes[d], beatValue, that.pitchDrumTable[turtle][notes[d]], []);
  4544. } else if (turtle in that.voices && last(that.voices[turtle])) {
  4545. that.synth.trigger(notes[d], beatValue, last(that.voices[turtle]), [vibratoIntensity, vibratoValue]);
  4546. } else {
  4547. that.synth.trigger(notes[d], beatValue, 'default', [vibratoIntensity, vibratoValue]);
  4548. }
  4549. }
  4550. }
  4551. that.synth.start();
  4552. }
  4553. }
  4554. that.lastNotePlayed[turtle] = [notes[0], noteBeatValue];
  4555. that.noteStatus[turtle] = [notes, noteBeatValue];
  4556. }
  4557. }
  4558. // Process drums
  4559. if (that.noteDrums[turtle].length > 0) {
  4560. for (var i = 0; i < that.noteDrums[turtle].length; i++) {
  4561. drums.push(that.noteDrums[turtle][i]);
  4562. }
  4563. // console.log("drums to play " + drums + ' ' + noteBeatValue);
  4564. if (!that.suppressOutput[turtle] && duration > 0) {
  4565. if (_THIS_IS_MUSIC_BLOCKS_) {
  4566. for (var i = 0; i < drums.length; i++) {
  4567. if (that.drumStyle[turtle].length > 0) {
  4568. that.synth.trigger(['C2'], beatValue, last(that.drumStyle[turtle]), []);
  4569. } else {
  4570. that.synth.trigger(['C2'], beatValue, drums[i], []);
  4571. }
  4572. }
  4573. }
  4574. }
  4575. }
  4576. // Ensure note value block unhighlights after note plays.
  4577. setTimeout(function () {
  4578. if (that.blocks.visible) {
  4579. that.blocks.unhighlight(blk);
  4580. }
  4581. }, beatValue * 1000);
  4582. };
  4583. if (waitTime === 0 || this.suppressOutput[turtle]) {
  4584. __playnote(this);
  4585. } else {
  4586. var that = this;
  4587. setTimeout(function () {
  4588. __playnote(that);
  4589. }, waitTime + this.noteDelay);
  4590. }
  4591. if (this.crescendoDelta[turtle].length > 0) {
  4592. if (last(this.crescendoVolume[turtle]) === last(this.crescendoInitialVolume[turtle]) && !this.justCounting[turtle]) {
  4593. lilypondBeginCrescendo(this, turtle, last(this.crescendoDelta[turtle]));
  4594. }
  4595. var len = this.crescendoVolume[turtle].length
  4596. this.crescendoVolume[turtle][len - 1] += this.crescendoDelta[turtle][len - 1];
  4597. this._setSynthVolume(this.crescendoVolume[turtle][len - 1], turtle);
  4598. var len2 = this.polyVolume[turtle].length;
  4599. this.polyVolume[turtle][len2 - 1] = this.crescendoVolume[turtle][len - 1];
  4600. }
  4601. }
  4602. this.pushedNote[turtle] = false;
  4603. }
  4604. };
  4605. this._dispatchTurtleSignals = function (turtle, beatValue, blk, noteBeatValue) {
  4606. // When turtle commands (forward, right, arc) are inside of Notes,
  4607. // they are progressive.
  4608. var that = this;
  4609. var stepTime = beatValue * 1000 / (NOTEDIV + 4);
  4610. // We want to update the turtle graphics every 50ms with a note.
  4611. // FIXME: Do this more efficiently
  4612. if (stepTime > 200) {
  4613. that.dispatchFactor[turtle] = 0.25;
  4614. } else if (stepTime > 100) {
  4615. that.dispatchFactor[turtle] = 0.5;
  4616. } else if (stepTime > 50) {
  4617. that.dispatchFactor[turtle] = 1;
  4618. } else if (stepTime > 25) {
  4619. that.dispatchFactor[turtle] = 2;
  4620. } else if (stepTime > 12.5) {
  4621. that.dispatchFactor[turtle] = 4;
  4622. } else {
  4623. that.dispatchFactor[turtle] = 8;
  4624. }
  4625. for (var t = 0; t < (NOTEDIV / that.dispatchFactor[turtle]); t++) {
  4626. setTimeout(function () {
  4627. if (turtle in that.forwardListener && blk in that.forwardListener[turtle]) {
  4628. that.stage.dispatchEvent('_forward_' + turtle + '_' + blk);
  4629. }
  4630. if (turtle in that.rightListener && blk in that.rightListener[turtle]) {
  4631. that.stage.dispatchEvent('_right_' + turtle + '_' + blk);
  4632. }
  4633. if (turtle in that.arcListener && blk in that.arcListener[turtle]) {
  4634. that.stage.dispatchEvent('_arc_' + turtle + '_' + blk);
  4635. }
  4636. // (NOTEDIV + 4) is a workaround so that the graphics
  4637. // finish a bit ahead of the note in order t minimize the
  4638. // risk that there is overlap with the next note scheduled
  4639. // to trigger.
  4640. }, t * stepTime * that.dispatchFactor[turtle]);
  4641. }
  4642. setTimeout(function () {
  4643. if (turtle in that.forwardListener && blk in that.forwardListener[turtle]) {
  4644. for (var i = 0; i < that.forwardListener[turtle][blk].length; i++) {
  4645. that.stage.removeEventListener('_forward_' + turtle + '_' + blk, that.forwardListener[turtle][blk][i], false);
  4646. }
  4647. delete that.forwardListener[turtle][blk];
  4648. }
  4649. if (turtle in that.rightListener && blk in that.rightListener[turtle]) {
  4650. for (var i = 0; i < that.rightListener[turtle][blk].length; i++) {
  4651. that.stage.removeEventListener('_right_' + turtle + '_' + blk, that.rightListener[turtle][blk][i], false);
  4652. }
  4653. delete that.rightListener[turtle][blk];
  4654. }
  4655. if (turtle in that.arcListener && blk in that.arcListener[turtle]) {
  4656. for (var i = 0; i < that.arcListener[turtle][blk].length; i++) {
  4657. that.stage.removeEventListener('_arc_' + turtle + '_' + blk, that.arcListener[turtle][blk][i], false);
  4658. }
  4659. delete that.arcListener[turtle][blk];
  4660. }
  4661. }, beatValue * 1000);
  4662. };
  4663. this._setListener = function (turtle, listenerName, listener) {
  4664. if (listenerName in this.turtles.turtleList[turtle].listeners) {
  4665. this.stage.removeEventListener(listenerName, this.turtles.turtleList[turtle].listeners[listenerName], false);
  4666. }
  4667. this.turtles.turtleList[turtle].listeners[listenerName] = listener;
  4668. this.stage.addEventListener(listenerName, listener, false);
  4669. };
  4670. this._setDispatchBlock = function (blk, turtle, listenerName) {
  4671. if (this.backward[turtle].length > 0) {
  4672. if (this.blocks.blockList[last(this.backward[turtle])].name === 'backward') {
  4673. var c = 1;
  4674. } else {
  4675. var c = 2;
  4676. }
  4677. if (this.blocks.sameGeneration(this.blocks.blockList[last(this.backward[turtle])].connections[c], blk)) {
  4678. var nextBlock = this.blocks.blockList[blk].connections[0];
  4679. this.endOfClampSignals[turtle][nextBlock] = [listenerName];
  4680. } else {
  4681. var nextBlock = null;
  4682. }
  4683. } else {
  4684. var nextBlock = last(this.blocks.blockList[blk].connections);
  4685. }
  4686. if (nextBlock == null) {
  4687. return;
  4688. }
  4689. this.endOfClampSignals[turtle][nextBlock] = [listenerName];
  4690. };
  4691. this._getTargetTurtle = function (args) {
  4692. // The target turtle name can be a string or an int. Make
  4693. // sure there is a turtle by this name and then find the
  4694. // associated start block.
  4695. var targetTurtle = args[0];
  4696. // We'll compare the names as strings.
  4697. if (typeof(targetTurtle) === 'number') {
  4698. targetTurtle = targetTurtle.toString();
  4699. }
  4700. for (var i = 0; i < this.turtles.turtleList.length; i++) {
  4701. var turtleName = this.turtles.turtleList[i].name;
  4702. if (typeof(turtleName) === 'number') {
  4703. turtleName = turtleName.toString();
  4704. }
  4705. if (turtleName === targetTurtle) {
  4706. return i;
  4707. }
  4708. }
  4709. return null;
  4710. };
  4711. this._loopBlock = function (name) {
  4712. return ['forever', 'repeat', 'while', 'until'].indexOf(name) !== -1;
  4713. };
  4714. this._doBreak = function (turtle) {
  4715. // Look for a parent loopBlock in queue and set its count to 1.
  4716. var parentLoopBlock = null;
  4717. var loopBlkIdx = -1;
  4718. var queueLength = this.turtles.turtleList[turtle].queue.length;
  4719. for (var i = queueLength - 1; i > -1; i--) {
  4720. if (this._loopBlock(this.blocks.blockList[this.turtles.turtleList[turtle].queue[i].blk].name)) {
  4721. // while or until
  4722. loopBlkIdx = this.turtles.turtleList[turtle].queue[i].blk;
  4723. parentLoopBlock = this.blocks.blockList[loopBlkIdx];
  4724. // Flush the parent from the queue.
  4725. this.turtles.turtleList[turtle].queue.pop();
  4726. break;
  4727. } else if (this._loopBlock(this.blocks.blockList[this.turtles.turtleList[turtle].queue[i].parentBlk].name)) {
  4728. // repeat or forever
  4729. loopBlkIdx = this.turtles.turtleList[turtle].queue[i].parentBlk;
  4730. parentLoopBlock = this.blocks.blockList[loopBlkIdx];
  4731. // Flush the parent from the queue.
  4732. this.turtles.turtleList[turtle].queue.pop();
  4733. break;
  4734. }
  4735. }
  4736. if (parentLoopBlock == null) {
  4737. // In this case, we flush the child flow.
  4738. this.turtles.turtleList[turtle].queue.pop();
  4739. return;
  4740. }
  4741. // For while and until, we need to add any childflow from the
  4742. // parent to the queue.
  4743. if (parentLoopBlock.name === 'while' || parentLoopBlock.name === 'until') {
  4744. var childFlow = last(parentLoopBlock.connections);
  4745. if (childFlow != null) {
  4746. var queueBlock = new Queue(childFlow, 1, loopBlkIdx);
  4747. // We need to keep track of the parent block to the
  4748. // child flow so we can unlightlight the parent block
  4749. // after the child flow completes.
  4750. this.parentFlowQueue[turtle].push(loopBlkIdx);
  4751. this.turtles.turtleList[turtle].queue.push(queueBlock);
  4752. }
  4753. }
  4754. };
  4755. this.parseArg = function (that, turtle, blk, parentBlk, receivedArg) {
  4756. // Retrieve the value of a block.
  4757. if (blk == null) {
  4758. that.errorMsg('Missing argument.', parentBlk);
  4759. that.stopTurtle = true;
  4760. return null
  4761. }
  4762. if (that.blocks.blockList[blk].protoblock.parameter) {
  4763. if (turtle in that.parameterQueue) {
  4764. if (that.parameterQueue[turtle].indexOf(blk) === -1) {
  4765. that.parameterQueue[turtle].push(blk);
  4766. }
  4767. } else {
  4768. // console.log('turtle ' + turtle + ' has no parameterQueue');
  4769. }
  4770. }
  4771. if (that.blocks.blockList[blk].isValueBlock()) {
  4772. if (that.blocks.blockList[blk].name === 'number' && typeof(that.blocks.blockList[blk].value) === 'string') {
  4773. try {
  4774. that.blocks.blockList[blk].value = Number(that.blocks.blockList[blk].value);
  4775. } catch (e) {
  4776. console.log(e);
  4777. }
  4778. }
  4779. return that.blocks.blockList[blk].value;
  4780. } else if (that.blocks.blockList[blk].isArgBlock() || that.blocks.blockList[blk].isArgClamp() || that.blocks.blockList[blk].isArgFlowClampBlock()) {
  4781. switch (that.blocks.blockList[blk].name) {
  4782. case 'loudness':
  4783. if (_THIS_IS_TURTLE_BLOCKS_) {
  4784. try { // DEBUGGING P5 MIC
  4785. if (!that.mic.enabled) {
  4786. that.mic.start();
  4787. that.blocks.blockList[blk].value = 0;
  4788. } else {
  4789. that.blocks.blockList[blk].value = Math.round(that.mic.getLevel() * 1000);
  4790. }
  4791. } catch (e) { // MORE DEBUGGING
  4792. console.log(e);
  4793. that.mic.start();
  4794. that.blocks.blockList[blk].value = Math.round(that.mic.getLevel() * 1000);
  4795. }
  4796. } else {
  4797. var values = that.analyser.analyse();
  4798. var sum = 0;
  4799. for(var k=0; k<that.limit; k++){
  4800. sum += (values[k] * values[k]);
  4801. }
  4802. var rms = Math.sqrt(sum/that.limit);
  4803. try {
  4804. if (!that.mic.open()) {
  4805. that.mic.open();
  4806. that.blocks.blockList[blk].value = 0;
  4807. } else {
  4808. that.blocks.blockList[blk].value = Math.round(rms);
  4809. }
  4810. } catch (e) { // MORE DEBUGGING
  4811. console.log(e);
  4812. that.mic.open();
  4813. that.blocks.blockList[blk].value = Math.round(rms);
  4814. }
  4815. }
  4816. break;
  4817. case 'eval':
  4818. var cblk1 = that.blocks.blockList[blk].connections[1];
  4819. var cblk2 = that.blocks.blockList[blk].connections[2];
  4820. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4821. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4822. // Restricted to math methods
  4823. that.blocks.blockList[blk].value = Number(eval('Math.' + a.replace(/x/g, b.toString())));
  4824. break;
  4825. case 'arg':
  4826. var cblk = that.blocks.blockList[blk].connections[1];
  4827. var name = that.parseArg(that, turtle, cblk, blk, receivedArg);
  4828. var action_args=receivedArg
  4829. if(action_args.length >= Number(name)){
  4830. var value = action_args[Number(name)-1];
  4831. that.blocks.blockList[blk].value = value;
  4832. }else {
  4833. that.errorMsg('Invalid argument',blk);
  4834. that.stopTurtle = true;
  4835. }
  4836. return that.blocks.blockList[blk].value;
  4837. break;
  4838. case 'box':
  4839. var cblk = that.blocks.blockList[blk].connections[1];
  4840. var name = that.parseArg(that, turtle, cblk, blk, receivedArg);
  4841. if (name in that.boxes) {
  4842. that.blocks.blockList[blk].value = that.boxes[name];
  4843. } else {
  4844. that.errorMsg(NOBOXERRORMSG, blk, name);
  4845. that.stopTurtle = true;
  4846. that.blocks.blockList[blk].value = null;
  4847. }
  4848. break;
  4849. case 'turtlename':
  4850. that.blocks.blockList[blk].value = that.turtles.turtleList[turtle].name;
  4851. break;
  4852. case 'namedbox':
  4853. var name = that.blocks.blockList[blk].privateData;
  4854. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4855. that.statusFields.push([blk, that.blocks.blockList[blk].name]);
  4856. } else if (!that.updatingStatusMatrix) {
  4857. if (name in that.boxes) {
  4858. that.blocks.blockList[blk].value = that.boxes[name];
  4859. } else {
  4860. that.errorMsg(NOBOXERRORMSG, blk, name);
  4861. that.stopTurtle = true;
  4862. that.blocks.blockList[blk].value = null;
  4863. }
  4864. }
  4865. break;
  4866. case 'namedarg' :
  4867. var name = that.blocks.blockList[blk].privateData;
  4868. var action_args = receivedArg;
  4869. // If an action block with an arg is clicked,
  4870. // the arg will have no value.
  4871. if (action_args == null) {
  4872. that.errorMsg('Invalid argument', blk);
  4873. that.stopTurtle = true;
  4874. that.blocks.blockList[blk].value = null;
  4875. return;
  4876. }
  4877. if (action_args.length >= Number(name)) {
  4878. var value = action_args[Number(name)-1];
  4879. that.blocks.blockList[blk].value = value;
  4880. } else {
  4881. that.errorMsg('Invalid argument', blk);
  4882. }
  4883. return that.blocks.blockList[blk].value;
  4884. break;
  4885. case 'sqrt':
  4886. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4887. that.statusFields.push([blk, that.blocks.blockList[blk].name]);
  4888. } else {
  4889. var cblk = that.blocks.blockList[blk].connections[1];
  4890. var a = that.parseArg(that, turtle, cblk, blk, receivedArg);
  4891. if (a < 0) {
  4892. that.errorMsg(NOSQRTERRORMSG, blk);
  4893. that.stopTurtle = true;
  4894. a = -a;
  4895. }
  4896. that.blocks.blockList[blk].value = that._doSqrt(a);
  4897. }
  4898. break;
  4899. case 'int':
  4900. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4901. that.statusFields.push([blk, 'int']);
  4902. } else {
  4903. var cblk = that.blocks.blockList[blk].connections[1];
  4904. var a = that.parseArg(that, turtle, cblk, blk, receivedArg);
  4905. that.blocks.blockList[blk].value = Math.floor(a);
  4906. }
  4907. break;
  4908. case 'mod':
  4909. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4910. that.statusFields.push([blk, 'mod']);
  4911. } else {
  4912. var cblk1 = that.blocks.blockList[blk].connections[1];
  4913. var cblk2 = that.blocks.blockList[blk].connections[2];
  4914. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4915. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4916. that.blocks.blockList[blk].value = that._doMod(a, b);
  4917. }
  4918. break;
  4919. case 'not':
  4920. var cblk = that.blocks.blockList[blk].connections[1];
  4921. var a = that.parseArg(that, turtle, cblk, blk, receivedArg);
  4922. that.blocks.blockList[blk].value = !a;
  4923. break;
  4924. case 'greater':
  4925. var cblk1 = that.blocks.blockList[blk].connections[1];
  4926. var cblk2 = that.blocks.blockList[blk].connections[2];
  4927. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4928. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4929. that.blocks.blockList[blk].value = (Number(a) > Number(b));
  4930. break;
  4931. case 'equal':
  4932. var cblk1 = that.blocks.blockList[blk].connections[1];
  4933. var cblk2 = that.blocks.blockList[blk].connections[2];
  4934. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4935. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4936. that.blocks.blockList[blk].value = (a === b);
  4937. break;
  4938. case 'less':
  4939. var cblk1 = that.blocks.blockList[blk].connections[1];
  4940. var cblk2 = that.blocks.blockList[blk].connections[2];
  4941. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4942. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4943. var result = (Number(a) < Number(b));
  4944. that.blocks.blockList[blk].value = result;
  4945. break;
  4946. case 'random':
  4947. var cblk1 = that.blocks.blockList[blk].connections[1];
  4948. var cblk2 = that.blocks.blockList[blk].connections[2];
  4949. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4950. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4951. that.blocks.blockList[blk].value = that._doRandom(a, b);
  4952. break;
  4953. case 'oneOf':
  4954. var cblk1 = that.blocks.blockList[blk].connections[1];
  4955. var cblk2 = that.blocks.blockList[blk].connections[2];
  4956. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4957. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4958. that.blocks.blockList[blk].value = that._doOneOf(a, b);
  4959. break;
  4960. case 'plus':
  4961. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4962. that.statusFields.push([blk, 'plus']);
  4963. } else {
  4964. var cblk1 = that.blocks.blockList[blk].connections[1];
  4965. var cblk2 = that.blocks.blockList[blk].connections[2];
  4966. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4967. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4968. that.blocks.blockList[blk].value = that._doPlus(a, b);
  4969. }
  4970. break;
  4971. case 'multiply':
  4972. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4973. that.statusFields.push([blk, 'multiply']);
  4974. } else {
  4975. var cblk1 = that.blocks.blockList[blk].connections[1];
  4976. var cblk2 = that.blocks.blockList[blk].connections[2];
  4977. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4978. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4979. that.blocks.blockList[blk].value = that._doMultiply(a, b);
  4980. }
  4981. break;
  4982. case 'power':
  4983. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4984. that.statusFields.push([blk, 'power']);
  4985. } else {
  4986. var cblk1 = that.blocks.blockList[blk].connections[1];
  4987. var cblk2 = that.blocks.blockList[blk].connections[2];
  4988. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  4989. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  4990. that.blocks.blockList[blk].value = that._doPower(a, b);
  4991. }
  4992. break;
  4993. case 'divide':
  4994. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  4995. that.statusFields.push([blk, 'divide']);
  4996. } else {
  4997. var cblk1 = that.blocks.blockList[blk].connections[1];
  4998. var cblk2 = that.blocks.blockList[blk].connections[2];
  4999. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5000. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  5001. that.blocks.blockList[blk].value = that._doDivide(a, b);
  5002. }
  5003. break;
  5004. case 'minus':
  5005. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5006. that.statusFields.push([blk, 'minus']);
  5007. } else {
  5008. var cblk1 = that.blocks.blockList[blk].connections[1];
  5009. var cblk2 = that.blocks.blockList[blk].connections[2];
  5010. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5011. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  5012. that.blocks.blockList[blk].value = that._doMinus(a, b);
  5013. }
  5014. break;
  5015. case 'neg':
  5016. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5017. that.statusFields.push([blk, 'neg']);
  5018. } else {
  5019. var cblk1 = that.blocks.blockList[blk].connections[1];
  5020. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5021. that.blocks.blockList[blk].value = that._doMinus(0, a);
  5022. }
  5023. break;
  5024. case 'toascii':
  5025. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5026. that.statusFields.push([blk, 'toascii']);
  5027. } else {
  5028. var cblk1 = that.blocks.blockList[blk].connections[1];
  5029. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5030. that.blocks.blockList[blk].value = String.fromCharCode(a);
  5031. }
  5032. break;
  5033. case 'myclick':
  5034. console.log('[click' + that.turtles.turtleList[turtle].name + ']');
  5035. that.blocks.blockList[blk].value = 'click' + that.turtles.turtleList[turtle].name;
  5036. break;
  5037. case 'heading':
  5038. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5039. that.statusFields.push([blk, 'heading']);
  5040. } else {
  5041. that.blocks.blockList[blk].value = that.turtles.turtleList[turtle].orientation;
  5042. }
  5043. break;
  5044. case 'x':
  5045. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5046. that.statusFields.push([blk, 'x']);
  5047. } else {
  5048. that.blocks.blockList[blk].value = that.turtles.screenX2turtleX(that.turtles.turtleList[turtle].container.x);
  5049. }
  5050. break;
  5051. case 'y':
  5052. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5053. that.statusFields.push([blk, 'y']);
  5054. } else {
  5055. that.blocks.blockList[blk].value = that.turtles.screenY2turtleY(that.turtles.turtleList[turtle].container.y);
  5056. }
  5057. break;
  5058. case 'xturtle':
  5059. case 'yturtle':
  5060. var cblk = that.blocks.blockList[blk].connections[1];
  5061. var targetTurtle = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5062. for (var i = 0; i < that.turtles.turtleList.length; i++) {
  5063. var thisTurtle = that.turtles.turtleList[i];
  5064. if (targetTurtle === thisTurtle.name) {
  5065. if (that.blocks.blockList[blk].name === 'yturtle') {
  5066. that.blocks.blockList[blk].value = that.turtles.screenY2turtleY(thisTurtle.container.y);
  5067. } else {
  5068. that.blocks.blockList[blk].value = that.turtles.screenX2turtleX(thisTurtle.container.x);
  5069. }
  5070. break;
  5071. }
  5072. }
  5073. if (i === that.turtles.turtleList.length) {
  5074. that.errorMsg('Could not find turtle ' + targetTurtle, blk);
  5075. that.blocks.blockList[blk].value = 0;
  5076. }
  5077. break;
  5078. case 'bpmfactor':
  5079. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5080. that.statusFields.push([blk, 'bpm']);
  5081. } else if (that.bpm[turtle].length > 0) {
  5082. that.blocks.blockList[blk].value = last(that.bpm[turtle]);
  5083. } else {
  5084. that.blocks.blockList[blk].value = that._masterBPM;
  5085. }
  5086. break;
  5087. case 'staccatofactor':
  5088. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5089. that.statusFields.push([blk, 'staccato']);
  5090. } else if (that.staccato[turtle].length > 0) {
  5091. that.blocks.blockList[blk].value = last(that.staccato[turtle]);
  5092. } else {
  5093. that.blocks.blockList[blk].value = 0;
  5094. }
  5095. break;
  5096. case 'slurfactor':
  5097. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5098. that.statusFields.push([blk, 'slur']);
  5099. } else if (that.staccato[turtle].length > 0) {
  5100. that.blocks.blockList[blk].value = -last(that.staccato[turtle]);
  5101. } else {
  5102. that.blocks.blockList[blk].value = 0;
  5103. }
  5104. break;
  5105. case 'key':
  5106. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5107. that.statusFields.push([blk, 'key']);
  5108. } else {
  5109. that.blocks.blockList[blk].value = that.keySignature[turtle];
  5110. }
  5111. break;
  5112. case 'consonantstepsizeup':
  5113. if (that.lastNotePlayed[turtle] !== null) {
  5114. var len = that.lastNotePlayed[turtle][0].length;
  5115. that.blocks.blockList[blk].value = getStepSizeUp(that.keySignature[turtle], that.lastNotePlayed[turtle][0].slice(0, len - 1));
  5116. } else {
  5117. that.blocks.blockList[blk].value = getStepSizeUp(that.keySignature[turtle], 'A');
  5118. }
  5119. break;
  5120. case 'consonantstepsizedown':
  5121. if (that.lastNotePlayed[turtle] !== null) {
  5122. var len = that.lastNotePlayed[turtle][0].length;
  5123. that.blocks.blockList[blk].value = getStepSizeDown(that.keySignature[turtle], that.lastNotePlayed[turtle][0].slice(0, len - 1));
  5124. } else {
  5125. that.blocks.blockList[blk].value = getStepSizeDown(that.keySignature[turtle], 'A');
  5126. }
  5127. break;
  5128. case 'transpositionfactor':
  5129. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5130. that.statusFields.push([blk, 'transposition']);
  5131. } else {
  5132. that.blocks.blockList[blk].value = that.transposition[turtle];
  5133. }
  5134. break;
  5135. case 'duplicatefactor':
  5136. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5137. that.statusFields.push([blk, 'duplicate']);
  5138. } else {
  5139. that.blocks.blockList[blk].value = that.duplicateFactor[turtle];
  5140. }
  5141. break;
  5142. case 'skipfactor':
  5143. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5144. that.statusFields.push([blk, 'skip']);
  5145. } else {
  5146. that.blocks.blockList[blk].value = that.skipFactor[turtle];
  5147. }
  5148. break;
  5149. case 'notevolumefactor':
  5150. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5151. that.statusFields.push([blk, 'volume']);
  5152. } else {
  5153. that.blocks.blockList[blk].value = last(that.polyVolume[turtle]);
  5154. }
  5155. break;
  5156. case 'elapsednotes':
  5157. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5158. that.statusFields.push([blk, 'elapsednotes']);
  5159. } else {
  5160. that.blocks.blockList[blk].value = that.notesPlayed[turtle];
  5161. }
  5162. break;
  5163. case 'beatfactor':
  5164. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5165. that.statusFields.push([blk, 'beatfactor']);
  5166. } else {
  5167. that.blocks.blockList[blk].value = that.beatFactor[turtle];
  5168. }
  5169. break;
  5170. case 'number2pitch':
  5171. case 'number2octave':
  5172. var cblk = that.blocks.blockList[blk].connections[1];
  5173. var num = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5174. if (num != null && typeof(num) === 'number') {
  5175. var obj = numberToPitch(num + that.pitchNumberOffset);
  5176. if (that.blocks.blockList[blk].name === 'number2pitch') {
  5177. that.blocks.blockList[blk].value = obj[0];
  5178. } else {
  5179. that.blocks.blockList[blk].value = obj[1];
  5180. }
  5181. } else {
  5182. that.errorMsg('Invalid argument', blk);
  5183. that.stopTurtle = true;
  5184. }
  5185. break;
  5186. case 'turtlepitch':
  5187. var value = null;
  5188. var cblk = that.blocks.blockList[blk].connections[1];
  5189. var targetTurtle = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5190. for (var i = 0; i < that.turtles.turtleList.length; i++) {
  5191. var thisTurtle = that.turtles.turtleList[i];
  5192. if (targetTurtle === thisTurtle.name) {
  5193. if (that.lastNotePlayed[turtle] !== null) {
  5194. var len = that.lastNotePlayed[turtle][0].length;
  5195. var pitch = that.lastNotePlayed[turtle][0].slice(0, len - 1);
  5196. var octave = parseInt(that.lastNotePlayed[turtle][0].slice(len - 1));
  5197. var obj = [pitch, octave];
  5198. } else if(that.notePitches[i].length > 0) {
  5199. var obj = that.getNote(that.notePitches[i][0], that.noteOctaves[i][0], that.noteTranspositions[i][0], that.keySignature[i]);
  5200. } else {
  5201. console.log('Could not find a note for turtle ' + turtle);
  5202. var obj = ['C', 0];
  5203. }
  5204. value = pitchToNumber(obj[0], obj[1], that.keySignature[turtle]) - that.pitchNumberOffset;
  5205. that.blocks.blockList[blk].value = value;
  5206. }
  5207. }
  5208. if (value == null) {
  5209. that.errorMsg('Could not find turtle ' + targetTurtle, blk);
  5210. that.blocks.blockList[blk].value = 0;
  5211. }
  5212. break;
  5213. case 'turtlenote':
  5214. case 'turtlenote2':
  5215. var value = null;
  5216. var cblk = that.blocks.blockList[blk].connections[1];
  5217. var targetTurtle = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5218. for (var i = 0; i < that.turtles.turtleList.length; i++) {
  5219. var thisTurtle = that.turtles.turtleList[i];
  5220. if (targetTurtle === thisTurtle.name) {
  5221. if (that.lastNotePlayed[turtle] !== null) {
  5222. value = that.lastNotePlayed[turtle][1];
  5223. } else if(that.notePitches[i].length > 0) {
  5224. value = that.noteBeat[i];
  5225. } else {
  5226. console.log('Could not find a note for turtle ' + turtle);
  5227. value = -1;
  5228. }
  5229. if (that.blocks.blockList[blk].name === 'turtlenote') {
  5230. that.blocks.blockList[blk].value = value;
  5231. } else if (value !== 0) {
  5232. that.blocks.blockList[blk].value = 1 / value;
  5233. } else {
  5234. that.blocks.blockList[blk].value = 0;
  5235. }
  5236. }
  5237. }
  5238. if (value == null) {
  5239. that.errorMsg('Could not find turtle ' + targetTurtle, blk);
  5240. that.blocks.blockList[blk].value = -1;
  5241. }
  5242. break;
  5243. // Deprecated
  5244. case 'currentnote':
  5245. that.blocks.blockList[blk].value = that.currentNotes[turtle];
  5246. break;
  5247. // Deprecated
  5248. case 'currentoctave':
  5249. that.blocks.blockList[blk].value = that.currentOctaves[turtle];
  5250. break;
  5251. case 'color':
  5252. case 'hue':
  5253. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5254. that.statusFields.push([blk, 'color']);
  5255. } else {
  5256. that.blocks.blockList[blk].value = that.turtles.turtleList[turtle].color;
  5257. }
  5258. break;
  5259. case 'shade':
  5260. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5261. that.statusFields.push([blk, 'shade']);
  5262. } else {
  5263. that.blocks.blockList[blk].value = that.turtles.turtleList[turtle].value;
  5264. }
  5265. break;
  5266. case 'grey':
  5267. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5268. that.statusFields.push([blk, 'grey']);
  5269. } else {
  5270. that.blocks.blockList[blk].value = that.turtles.turtleList[turtle].chroma;
  5271. }
  5272. break;
  5273. case 'pensize':
  5274. if (that.inStatusMatrix && that.blocks.blockList[that.blocks.blockList[blk].connections[0]].name === 'print') {
  5275. that.statusFields.push([blk, 'pensize']);
  5276. } else {
  5277. that.blocks.blockList[blk].value = that.turtles.turtleList[turtle].stroke;
  5278. }
  5279. break;
  5280. case 'and':
  5281. var cblk1 = that.blocks.blockList[blk].connections[1];
  5282. var cblk2 = that.blocks.blockList[blk].connections[2];
  5283. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5284. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  5285. that.blocks.blockList[blk].value = a && b;
  5286. break;
  5287. case 'or':
  5288. var cblk1 = that.blocks.blockList[blk].connections[1];
  5289. var cblk2 = that.blocks.blockList[blk].connections[2];
  5290. var a = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5291. var b = that.parseArg(that, turtle, cblk2, blk, receivedArg);
  5292. that.blocks.blockList[blk].value = a || b;
  5293. break;
  5294. case 'time':
  5295. var d = new Date();
  5296. that.blocks.blockList[blk].value = (d.getTime() - that.time) / 1000;
  5297. break;
  5298. case 'hspace':
  5299. var cblk = that.blocks.blockList[blk].connections[1];
  5300. var v = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5301. that.blocks.blockList[blk].value = v;
  5302. break;
  5303. case 'mousex':
  5304. that.blocks.blockList[blk].value = that.getStageX();
  5305. break;
  5306. case 'mousey':
  5307. that.blocks.blockList[blk].value = that.getStageY();
  5308. break;
  5309. case 'mousebutton':
  5310. that.blocks.blockList[blk].value = that.getStageMouseDown();
  5311. break;
  5312. case 'keyboard':
  5313. that.lastKeyCode = that.getCurrentKeyCode();
  5314. that.blocks.blockList[blk].value = that.lastKeyCode;
  5315. that.clearCurrentKeyCode();
  5316. break;
  5317. case 'getred':
  5318. case 'getgreen':
  5319. case 'getblue':
  5320. var colorString = that.turtles.turtleList[turtle].canvasColor;
  5321. // 'rgba(255,0,49,1)' or '#ff0031'
  5322. if (colorString[0] === "#") {
  5323. colorString = hex2rgb(colorString.split("#")[1]);
  5324. }
  5325. var obj = colorString.split('(');
  5326. var obj = obj[1].split(',');
  5327. switch (that.blocks.blockList[blk].name) {
  5328. case 'getred':
  5329. that.blocks.blockList[blk].value = parseInt(Number(obj[0]) / 2.55);
  5330. break;
  5331. case 'getgreen':
  5332. that.blocks.blockList[blk].value = parseInt(Number(obj[1]) / 2.55);
  5333. break;
  5334. case 'getblue':
  5335. that.blocks.blockList[blk].value = parseInt(Number(obj[2]) / 2.55);
  5336. break;
  5337. }
  5338. break;
  5339. case 'getcolorpixel':
  5340. var wasVisible = that.turtles.turtleList[turtle].container.visible;
  5341. that.turtles.turtleList[turtle].container.visible = false;
  5342. var x = that.turtles.turtleList[turtle].container.x;
  5343. var y = that.turtles.turtleList[turtle].container.y;
  5344. that.refreshCanvas();
  5345. var ctx = that.canvas.getContext('2d');
  5346. var imgData = ctx.getImageData(x, y, 1, 1).data;
  5347. var color = searchColors(imgData[0], imgData[1], imgData[2]);
  5348. if (imgData[3] === 0) {
  5349. color = body.style.background.substring(body.style.background.indexOf('(') + 1, body.style.background.lastIndexOf(')')).split(/,\s*/),
  5350. color = searchColors(color[0], color[1], color[2]);
  5351. }
  5352. that.blocks.blockList[blk].value = color;
  5353. if (wasVisible) {
  5354. that.turtles.turtleList[turtle].container.visible = true;
  5355. }
  5356. break;
  5357. case 'loadFile':
  5358. // No need to do anything here.
  5359. break;
  5360. case 'tofrequency':
  5361. if (_THIS_IS_MUSIC_BLOCKS_) {
  5362. var block = that.blocks.blockList[blk];
  5363. var cblk1 = that.blocks.blockList[blk].connections[1];
  5364. var cblk2 = that.blocks.blockList[blk].connections[2];
  5365. var note = that.parseArg(that, turtle, cblk1, blk, receivedArg);
  5366. var octave = Math.floor(calcOctave(that.currentOctaves[turtle], that.parseArg(that, turtle, cblk2, blk, receivedArg)));
  5367. block.value = Math.round(pitchToFrequency(note, octave, 0, that.keySignature[turtle]));
  5368. } else {
  5369. const NOTENAMES = ['A', 'B♭', 'B', 'C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭'];
  5370. const NOTECONVERSION = {'A♯': 'B♭', 'C♯': 'D♭', 'D♯': 'E♭', 'F♯': 'G♭', 'G♯': 'A♭'};
  5371. var block = that.blocks.blockList[blk];
  5372. var cblk = block.connections[1];
  5373. var noteName = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5374. if (typeof(noteName) === 'string') {
  5375. noteName = noteName.replace('b', '♭');
  5376. noteName = noteName.replace('#', '♯');
  5377. if (noteName in NOTECONVERSION) {
  5378. noteName = NOTECONVERSION[noteName];
  5379. }
  5380. var idx = NOTENAMES.indexOf(noteName);
  5381. if (idx === -1) {
  5382. this.errorMsg(_('Note name must be one of A, A♯, B♭, B, C, C♯, D♭, D, D♯, E♭, E, F, F♯, G♭, G, G♯ or A♭.'));
  5383. block.value = 440;
  5384. } else {
  5385. var cblk = block.connections[2];
  5386. var octave = Math.floor(that.parseArg(that, turtle, cblk, blk, receivedArg));
  5387. if (octave < 1) {
  5388. octave = 1;
  5389. }
  5390. if (idx > 2) {
  5391. octave -= 1; // New octave starts on C
  5392. }
  5393. var i = octave * 12 + idx;
  5394. block.value = 27.5 * Math.pow(1.05946309435929, i);
  5395. }
  5396. } else {
  5397. block.value = 440 * Math.pow(2, (noteName - 69) / 12);
  5398. }
  5399. }
  5400. break;
  5401. case 'pop':
  5402. var block = that.blocks.blockList[blk];
  5403. if (turtle in that.turtleHeaps && that.turtleHeaps[turtle].length > 0) {
  5404. block.value = that.turtleHeaps[turtle].pop();
  5405. } else {
  5406. that.errorMsg(_('empty heap'));
  5407. block.value = null;
  5408. }
  5409. break;
  5410. case 'indexHeap':
  5411. var block = that.blocks.blockList[blk];
  5412. var cblk = that.blocks.blockList[blk].connections[1];
  5413. var a = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5414. if (!(turtle in that.turtleHeaps)) {
  5415. that.turtleHeaps[turtle] = [];
  5416. }
  5417. // If index > heap length, grow the heap.
  5418. while (that.turtleHeaps[turtle].length < a) {
  5419. that.turtleHeaps[turtle].push(null);
  5420. }
  5421. block.value = that.turtleHeaps[turtle][a - 1];
  5422. break;
  5423. case 'heapLength':
  5424. var block = that.blocks.blockList[blk];
  5425. if (!(turtle in that.turtleHeaps)) {
  5426. that.turtleHeaps[turtle] = [];
  5427. }
  5428. // console.log(that.turtleHeaps[turtle].length);
  5429. block.value = that.turtleHeaps[turtle].length;
  5430. break;
  5431. case 'heapEmpty':
  5432. var block = that.blocks.blockList[blk];
  5433. if (turtle in that.turtleHeaps) {
  5434. block.value = (that.turtleHeaps[turtle].length === 0);
  5435. } else {
  5436. block.value = true;
  5437. }
  5438. break;
  5439. case 'notecounter':
  5440. var saveCountingStatus = that.justCounting[turtle];
  5441. var saveSuppressStatus = that.suppressOutput[turtle];
  5442. that.suppressOutput[turtle] = true;
  5443. that.justCounting[turtle] = true;
  5444. var actionArgs = [];
  5445. var saveNoteCount = that.notesPlayed[turtle];
  5446. var cblk = that.blocks.blockList[blk].connections[1];
  5447. if (cblk == null) {
  5448. that.blocks.blockList[blk].value = 0;
  5449. } else {
  5450. that.turtles.turtleList[turtle].running = true;
  5451. that._runFromBlockNow(that, turtle, cblk, true, actionArgs, that.turtles.turtleList[turtle].queue.length);
  5452. that.blocks.blockList[blk].value = that.notesPlayed[turtle] - saveNoteCount;
  5453. that.notesPlayed[turtle] = saveNoteCount;
  5454. }
  5455. that.justCounting[turtle] = saveCountingStatus;
  5456. that.suppressOutput[turtle] = saveSuppressStatus;
  5457. break;
  5458. case 'calc':
  5459. var actionArgs = [];
  5460. var cblk = that.blocks.blockList[blk].connections[1];
  5461. var name = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5462. actionArgs = receivedArg;
  5463. // that.getBlockAtStartOfArg(blk);
  5464. if (name in that.actions) {
  5465. that.turtles.turtleList[turtle].running = true;
  5466. that._runFromBlockNow(that, turtle, that.actions[name], true, actionArgs, that.turtles.turtleList[turtle].queue.length);
  5467. that.blocks.blockList[blk].value = that.returns.shift();
  5468. } else {
  5469. that.errorMsg(NOACTIONERRORMSG, blk, name);
  5470. that.stopTurtle = true;
  5471. }
  5472. break;
  5473. case 'namedcalc':
  5474. var name = that.blocks.blockList[blk].privateData;
  5475. var actionArgs = [];
  5476. actionArgs = receivedArg;
  5477. // that.getBlockAtStartOfArg(blk);
  5478. if (name in that.actions) {
  5479. that.turtles.turtleList[turtle].running = true;
  5480. that._runFromBlockNow(that, turtle, that.actions[name], true, actionArgs, that.turtles.turtleList[turtle].queue.length);
  5481. that.blocks.blockList[blk].value = that.returns.shift();
  5482. } else {
  5483. that.errorMsg(NOACTIONERRORMSG, blk, name);
  5484. that.stopTurtle = true;
  5485. }
  5486. break;
  5487. case 'calcArg':
  5488. var actionArgs = [];
  5489. // that.getBlockAtStartOfArg(blk);
  5490. if (that.blocks.blockList[blk].argClampSlots.length > 0) {
  5491. for (var i = 0; i < that.blocks.blockList[blk].argClampSlots.length; i++){
  5492. var t = (that.parseArg(that, turtle, that.blocks.blockList[blk].connections[i + 2], blk, receivedArg));
  5493. actionArgs.push(t);
  5494. }
  5495. }
  5496. var cblk = that.blocks.blockList[blk].connections[1];
  5497. var name = that.parseArg(that, turtle, cblk, blk, receivedArg);
  5498. if (name in that.actions) {
  5499. that.turtles.turtleList[turtle].running = true;
  5500. that._runFromBlockNow(that, turtle, that.actions[name], true, actionArgs, that.turtles.turtleList[turtle].queue.length);
  5501. that.blocks.blockList[blk].value = that.returns.pop();
  5502. } else {
  5503. that.errorMsg(NOACTIONERRORMSG, blk, name);
  5504. that.stopTurtle = true;
  5505. }
  5506. break;
  5507. case 'namedcalcArg':
  5508. var name = that.blocks.blockList[blk].privateData;
  5509. var actionArgs = [];
  5510. // that.getBlockAtStartOfArg(blk);
  5511. if (that.blocks.blockList[blk].argClampSlots.length > 0) {
  5512. for (var i = 0; i < that.blocks.blockList[blk].argClampSlots.length; i++){
  5513. var t = (that.parseArg(that, turtle, that.blocks.blockList[blk].connections[i + 1], blk, receivedArg));
  5514. actionArgs.push(t);
  5515. }
  5516. }
  5517. if (name in that.actions) {
  5518. // Just run the stack.
  5519. that.turtles.turtleList[turtle].running = true;
  5520. that._runFromBlockNow(that, turtle, that.actions[name], true, actionArgs, that.turtles.turtleList[turtle].queue.length);
  5521. that.blocks.blockList[blk].value = that.returns.pop();
  5522. } else {
  5523. that.errorMsg(NOACTIONERRORMSG, blk, name);
  5524. that.stopTurtle = true;
  5525. }
  5526. break;
  5527. case 'doArg':
  5528. return blk;
  5529. break;
  5530. case 'nameddoArg':
  5531. return blk;
  5532. break;
  5533. case 'returnValue':
  5534. if (that.returns.length > 0) {
  5535. that.blocks.blockList[blk].value = that.returns.pop();
  5536. } else {
  5537. console.log('WARNING: No return value.');
  5538. that.blocks.blockList[blk].value = 0;
  5539. }
  5540. break;
  5541. default:
  5542. if (that.blocks.blockList[blk].name in that.evalArgDict) {
  5543. eval(that.evalArgDict[that.blocks.blockList[blk].name]);
  5544. } else {
  5545. console.log('ERROR: I do not know how to ' + that.blocks.blockList[blk].name);
  5546. }
  5547. break;
  5548. }
  5549. return that.blocks.blockList[blk].value;
  5550. } else {
  5551. return blk;
  5552. }
  5553. };
  5554. this._doWait = function (turtle, secs) {
  5555. this.waitTimes[turtle] = Number(secs) * 1000;
  5556. };
  5557. // Math functions
  5558. this._doRandom = function (a, b) {
  5559. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5560. this.errorMsg(NANERRORMSG);
  5561. this.stopTurtle = true;
  5562. return 0;
  5563. }
  5564. return Math.floor(Math.random() * (Number(b) - Number(a) + 1) + Number(a));
  5565. };
  5566. this._doOneOf = function (a, b) {
  5567. if (Math.random() < 0.5) {
  5568. return a;
  5569. } else {
  5570. return b;
  5571. }
  5572. };
  5573. this._doMod = function (a, b) {
  5574. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5575. this.errorMsg(NANERRORMSG);
  5576. this.stopTurtle = true;
  5577. return 0;
  5578. }
  5579. return Number(a) % Number(b);
  5580. };
  5581. this._doSqrt = function (a) {
  5582. if (typeof(a) === 'string') {
  5583. this.errorMsg(NANERRORMSG);
  5584. this.stopTurtle = true;
  5585. return 0;
  5586. }
  5587. return Math.sqrt(Number(a));
  5588. };
  5589. this._doPlus = function (a, b) {
  5590. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5591. if (typeof(a) === 'string') {
  5592. var aString = a;
  5593. } else {
  5594. var aString = a.toString();
  5595. }
  5596. if (typeof(b) === 'string') {
  5597. var bString = b;
  5598. } else {
  5599. var bString = b.toString();
  5600. }
  5601. return aString + bString;
  5602. } else {
  5603. return Number(a) + Number(b);
  5604. }
  5605. };
  5606. this._doMinus = function (a, b) {
  5607. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5608. this.errorMsg(NANERRORMSG);
  5609. this.stopTurtle = true;
  5610. return 0;
  5611. }
  5612. return Number(a) - Number(b);
  5613. };
  5614. this._doMultiply = function (a, b) {
  5615. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5616. this.errorMsg(NANERRORMSG);
  5617. this.stopTurtle = true;
  5618. return 0;
  5619. }
  5620. return Number(a) * Number(b);
  5621. };
  5622. this._doPower = function (a, b) {
  5623. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5624. this.errorMsg(NANERRORMSG);
  5625. this.stopTurtle = true;
  5626. return 0;
  5627. }
  5628. return Math.pow(a, b);
  5629. };
  5630. this._doDivide = function (a, b) {
  5631. if (typeof(a) === 'string' || typeof(b) === 'string') {
  5632. this.errorMsg(NANERRORMSG);
  5633. this.stopTurtle = true;
  5634. return 0;
  5635. }
  5636. if (Number(b) === 0) {
  5637. this.errorMsg(ZERODIVIDEERRORMSG);
  5638. this.stopTurtle = true;
  5639. return 0;
  5640. } else {
  5641. return Number(a) / Number(b);
  5642. }
  5643. };
  5644. this.setBackgroundColor = function (turtle) {
  5645. /// Change body background in DOM to current color.
  5646. if (turtle === -1) {
  5647. var c = platformColor.background;
  5648. } else {
  5649. var c = this.turtles.turtleList[turtle].canvasColor;
  5650. }
  5651. docById('myCanvas').style.background = c;
  5652. this.svgOutput = '';
  5653. };
  5654. this.setCameraID = function (id) {
  5655. this.cameraID = id;
  5656. };
  5657. this.hideBlocks = function () {
  5658. // Hide all the blocks.
  5659. this.blocks.hide();
  5660. this.refreshCanvas();
  5661. };
  5662. this.showBlocks = function () {
  5663. // Show all the blocks.
  5664. this.blocks.show();
  5665. this.blocks.bringToTop();
  5666. this.refreshCanvas();
  5667. };
  5668. this.getNote = function (solfege, octave, transposition, keySignature) {
  5669. this.validNote = true;
  5670. var sharpFlat = false;
  5671. octave = Math.round(octave);
  5672. transposition = Math.round(transposition);
  5673. if (typeof(solfege) === 'number') {
  5674. solfege = solfege.toString();
  5675. }
  5676. // Check for double flat or double sharp.
  5677. var len = solfege.length;
  5678. if (len > 2) {
  5679. var lastTwo = solfege.slice(len - 2);
  5680. if (lastTwo === 'bb' || lastTwo === '♭♭') {
  5681. solfege = solfege.slice(0, len - 1);
  5682. transposition -= 1;
  5683. } else if (lastTwo === '##' || lastTwo === '♯♯') {
  5684. solfege = solfege.slice(0, len - 1);
  5685. transposition += 1;
  5686. } else if (lastTwo === '#b' || lastTwo === '♯♭' || lastTwo === 'b#' || lastTwo === '♭♯') {
  5687. // Not sure this could occur... but just in case.
  5688. solfege = solfege.slice(0, len - 2);
  5689. }
  5690. }
  5691. // Already a note? No need to convert from solfege.
  5692. if (solfege in BTOFLAT) {
  5693. solfege = BTOFLAT[solfege];
  5694. } else if (solfege in STOSHARP) {
  5695. solfege = STOSHARP[solfege];
  5696. }
  5697. if (solfege in EXTRATRANSPOSITIONS) {
  5698. octave += EXTRATRANSPOSITIONS[solfege][1];
  5699. note = EXTRATRANSPOSITIONS[solfege][0];
  5700. } else if (NOTESSHARP.indexOf(solfege.toUpperCase()) !== -1) {
  5701. note = solfege.toUpperCase();
  5702. } else if (NOTESFLAT.indexOf(solfege) !== -1) {
  5703. note = solfege;
  5704. } else if (NOTESFLAT2.indexOf(solfege) !== -1) {
  5705. // Convert to uppercase, e.g., d♭ -> D♭.
  5706. note = NOTESFLAT[notesFlat2.indexOf(solfege)];
  5707. } else {
  5708. // Not a note, so convert from Solfege.
  5709. // Could be mi#<sub>4</sub> (from matrix) or mi# (from note).
  5710. if (solfege.substr(-1) === '>') {
  5711. // Read octave and solfege from HTML
  5712. octave = parseInt(solfege.slice(solfege.indexOf('>') + 1, solfege.indexOf('/') - 1));
  5713. solfege = solfege.substr(0, solfege.indexOf('<'));
  5714. }
  5715. if(['#', '♯', '♭', 'b'].indexOf(solfege.substr(-1)) !== -1) {
  5716. sharpFlat = true;
  5717. }
  5718. if (!keySignature) {
  5719. keySignature = 'C';
  5720. }
  5721. var obj = getScaleAndHalfSteps(keySignature);
  5722. var thisScale = obj[0];
  5723. var halfSteps = obj[1];
  5724. var myKeySignature = obj[2];
  5725. var mode = obj[3];
  5726. // Ensure it is a valid key signature.
  5727. offset = thisScale.indexOf(myKeySignature);
  5728. if (offset === -1) {
  5729. console.log('WARNING: Key ' + myKeySignature + ' not found in ' + thisScale + '. Using default of C');
  5730. var offset = 0;
  5731. var thisScale = NOTESSHARP;
  5732. }
  5733. if (sharpFlat) {
  5734. if (solfege.substr(-1) === '#') {
  5735. offset += 1;
  5736. } else if (solfege.substr(-1) === '♯') {
  5737. offset += 1;
  5738. } else if (solfege.substr(-1) === '♭') {
  5739. offset -= 1;
  5740. } else if(solfege.substr(-1) === 'b') {
  5741. offset -= 1;
  5742. }
  5743. }
  5744. // Reverse any i18n
  5745. // solfnotes_ is used in the interface for i18n
  5746. //.TRANS: the note names must be separated by single spaces
  5747. var solfnotes_ = _('ti la sol fa mi re do').split(' ');
  5748. if (solfnotes_.indexOf(solfege.substr(0, 2).toLowerCase()) !== -1) {
  5749. var solfegePart = SOLFNOTES[solfnotes_.indexOf(solfege.substr(0, 2).toLowerCase())];
  5750. } else if (solfnotes_.indexOf(solfege.substr(0, 3).toLowerCase()) !== -1) {
  5751. var solfegePart = SOLFNOTES[solfnotes_.indexOf(solfege.substr(0, 3).toLowerCase())];
  5752. } else {
  5753. var solfegePart = solfege.substr(0, 2).toLowerCase();
  5754. }
  5755. if (solfege.toLowerCase().substr(0, 4) === 'rest') {
  5756. return ['R', ''];
  5757. } else if (halfSteps.indexOf(solfegePart) !== -1) {
  5758. var index = halfSteps.indexOf(solfegePart) + offset;
  5759. if (index > 11) {
  5760. index -= 12;
  5761. octave += 1;
  5762. }
  5763. note = thisScale[index];
  5764. } else {
  5765. console.log('WARNING: Note ' + solfege + ' not found in ' + halfSteps + '. Returning REST');
  5766. // this.validNote = false;
  5767. this.errorMsg(INVALIDPITCH, null);
  5768. return ['R', ''];
  5769. }
  5770. if (note in EXTRATRANSPOSITIONS) {
  5771. octave += EXTRATRANSPOSITIONS[note][1];
  5772. note = EXTRATRANSPOSITIONS[note][0];
  5773. }
  5774. }
  5775. if (transposition && transposition !== 0) {
  5776. if (transposition < 0) {
  5777. deltaOctave = -Math.floor(-transposition / 12);
  5778. deltaNote = -(-transposition % 12);
  5779. } else {
  5780. deltaOctave = Math.floor(transposition / 12);
  5781. deltaNote = transposition % 12;
  5782. }
  5783. octave += deltaOctave;
  5784. if (NOTESSHARP.indexOf(note) !== -1) {
  5785. i = NOTESSHARP.indexOf(note);
  5786. i += deltaNote;
  5787. if (i < 0) {
  5788. i += 12;
  5789. octave -= 1;
  5790. } else if (i > 11) {
  5791. i -= 12;
  5792. octave += 1;
  5793. }
  5794. note = NOTESSHARP[i];
  5795. } else if (NOTESFLAT.indexOf(note) !== -1) {
  5796. i = NOTESFLAT.indexOf(note);
  5797. i += deltaNote;
  5798. if (i < 0) {
  5799. i += 12;
  5800. octave -= 1;
  5801. } else if (i > 11) {
  5802. i -= 12;
  5803. octave += 1;
  5804. }
  5805. note = NOTESFLAT[i];
  5806. } else {
  5807. console.log('note not found? ' + note);
  5808. }
  5809. }
  5810. if (octave < 0) {
  5811. return [note, 0];
  5812. } else if (octave > 10) {
  5813. return [note, 10];
  5814. } else {
  5815. return [note, octave];
  5816. }
  5817. };
  5818. };