-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iss84 SHOW - Allow infinite looping of sequences
* Add checkbox to allow for infinite looping of shows * Add functionality to infinite loop sequences * Fix element length bug in case that sequence does not have audio file. * Change sendDMX.js functions into a module for more flexible use. * Rename sendDMX.js to ShowRunner.js to accurately describe functions.
- Loading branch information
Byron Ambright
committed
Apr 25, 2019
1 parent
0ccf913
commit b6b23ad
Showing
5 changed files
with
173 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
const fs = parent.require('fs'); | ||
const NanoTimer = parent.require('nanotimer'); | ||
const ShowElementConstructor = parent.require('./js/ShowElement.js'); | ||
|
||
function ShowRunner() { | ||
this.canPlay = false; // bool check for if should go to next sequence | ||
this.repeat = false; | ||
this.showElements = []; | ||
this.showLength = 0; | ||
} | ||
|
||
ShowRunner.prototype.setupShowRunner = async function (sequencePathArray) { | ||
// create show elements with sequence json path | ||
for (let i = 0; i < sequencePathArray.length; i += 1) { | ||
this.showElements.push(new ShowElementConstructor()); | ||
this.showElements[i].setSequenceJson(sequencePathArray[i]); | ||
// TODO make this a promise or something, so we can set up all asyncronously | ||
// eslint-disable-next-line no-await-in-loop | ||
const duration = await this.showElements[i].setUpSequence(); | ||
this.showElements[i].setElementLength(duration); | ||
} | ||
|
||
const elementsLength = this.showElements.map(x => x.elementLength); | ||
this.showLength = elementsLength.reduce((total, elementLength) => total + elementLength, 0); | ||
}; | ||
|
||
|
||
ShowRunner.prototype.setCanPlay = function (canPlay) { | ||
this.canPlay = canPlay; | ||
}; | ||
|
||
ShowRunner.prototype.setRepeat = function (repeat) { | ||
this.repeat = repeat; | ||
}; | ||
|
||
ShowRunner.prototype.stopPlaying = function (showElement) { | ||
// only stop audio if there is audio | ||
if (showElement.getAudio()) { | ||
showElement.getAudio().stop(); | ||
} | ||
showElement.getTimer().clearInterval(); | ||
}; | ||
|
||
ShowRunner.prototype.stopAllShowElements = function () { | ||
for (let j = 0; j < this.showElements.length; j += 1) { | ||
this.stopPlaying(this.showElements[j]); | ||
} | ||
}; | ||
|
||
ShowRunner.prototype.update = function (showElement) { | ||
const index = Math.ceil((new Date() - showElement.getStartTime()) / showElement.getInterval()); | ||
parent.parent.universe.update(showElement.getSequenceData()[index]); | ||
if (index > showElement.getSequenceData().length) { // check for end of song | ||
this.stopPlaying(showElement); | ||
} | ||
}; | ||
|
||
ShowRunner.prototype.playSequence = function (showElement) { | ||
if (showElement.audioPath) { | ||
showElement.getAudio().play(); | ||
} | ||
showElement.setTimer(new NanoTimer()); | ||
showElement.setStartTime(new Date()); | ||
showElement.getTimer().setInterval(this.update, [showElement], '20m'); | ||
}; | ||
|
||
ShowRunner.prototype.checkAudioFinish = function (showElement) { | ||
const index = Math.ceil((new Date() - showElement.getStartTime()) / 50); | ||
if (showElement.getElementLength()) { | ||
if (index > showElement.getElementLength()) { // check for end of song | ||
this.stopPlaying(showElement); | ||
} | ||
} | ||
}; | ||
|
||
ShowRunner.prototype.playAudio = function (showElement) { | ||
if (showElement.audioPath) { | ||
showElement.getAudio().play(); | ||
} | ||
parent.parent.universe.updateAll(0); | ||
showElement.setTimer(new NanoTimer()); | ||
showElement.setStartTime(new Date()); | ||
showElement.getTimer().setInterval(this.checkAudioFinish, [showElement], '20m'); | ||
}; | ||
|
||
ShowRunner.prototype.playElement = function (showElement) { | ||
const sequenceJSON = JSON.parse(fs.readFileSync(showElement.getSequenceJson())); | ||
if (sequenceJSON['Sequence Data Json'].length === 0) { | ||
this.playAudio(showElement); | ||
} else { | ||
this.playSequence(showElement); | ||
} | ||
}; | ||
|
||
ShowRunner.prototype.repeatShow = function (showElement) { | ||
|
||
}; | ||
|
||
ShowRunner.prototype.triggerShow = function () { | ||
const i = 1; | ||
this.playElement(this.showElements[0]); | ||
// recursively waits and plays elements of the show | ||
function playSequenceInShow(ind) { | ||
let k = ind; | ||
if (this.canPlay) { | ||
setTimeout(() => { | ||
if (this.canPlay) { | ||
if (k < elements.length) { | ||
this.playElement(this.showElements[k]); | ||
k += 1; | ||
playShowInSequence(k); | ||
} | ||
} | ||
}, this.showElements[k - 1].getElementLength()); | ||
} | ||
} | ||
playSequenceInShow(i); | ||
}; | ||
|
||
ShowRunner.prototype.playShow = async function () { | ||
if (this.repeat && this.canPlay) { | ||
setTimeout(() => { | ||
this.setCanPlay(false); | ||
this.stopAllShowElements(); | ||
this.setCanPlay(true); | ||
this.playShow(this.showElements); | ||
}, this.showLength); | ||
} | ||
|
||
this.triggerShow(); | ||
}; | ||
|
||
module.exports = ShowRunner; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.