Skip to content

Commit

Permalink
Revert "Allow passing in 30000/1001 or 60000/1001 frame rates"
Browse files Browse the repository at this point in the history
  • Loading branch information
m1tk4 committed Jun 17, 2019
1 parent f5d284f commit eadbc9e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 38 deletions.
42 changes: 6 additions & 36 deletions smpte-timecode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// This should work both in node and in the browsers, so that's what this wrapper is about
;(function(root, undefined) {
// An arbitrary "close enough" number to check if the frame rate is 29.97 or
// 59.94
var DROP_FRAME_EPSILON = 0.0001;


/**
* Timecode object constructor
Expand All @@ -27,8 +25,7 @@

// If we are passed dropFrame, we need to use it
if (typeof dropFrame === 'boolean') this.dropFrame = dropFrame;
// Otherwise, default to DF for 29.97 and 59.94
else this.dropFrame = this._is30DF() || this._is60DF();
else this.dropFrame = (this.frameRate===29.97 || this.frameRate===59.94); // by default, assume DF for 29.97 and 59.94, NDF otherwise

// Now either get the frame count, string or datetime
if (typeof timeCode === 'number') {
Expand Down Expand Up @@ -83,13 +80,13 @@
Timecode.prototype._validate = function (timeCode) {

// Make sure dropFrame is only for 29.97 & 59.94
if (this.dropFrame && !this._is30DF() && !this._is60DF()) {
if (this.dropFrame && this.frameRate!==29.97 && this.frameRate!==59.94) {
throw new Error('Drop frame is only supported for 29.97 and 59.94 fps');
}

// make sure the numbers make sense
if (this.hours > 23 || this.minutes > 59 || this.seconds > 59 || this.frames >= this.frameRate ||
(this.dropFrame && this.seconds === 0 && this.minutes % 10 && this.frames < this._numOfFramesToDrop())) {
(this.dropFrame && this.seconds === 0 && this.minutes % 10 && this.frames < 2 * (this.frameRate / 29.97))) {
throw new Error("Invalid timecode" + JSON.stringify(timeCode));
}
};
Expand All @@ -102,7 +99,7 @@
var fc = this.frameCount;
// adjust for dropFrame
if (this.dropFrame) {
var df = this._numOfFramesToDrop();
var df = this.frameRate===29.97 ? 2 : 4; // 59.94 skips 4 frames
var d = Math.floor(this.frameCount / (17982*df/2));
var m = this.frameCount % (17982*df/2);
if (m<df) m=m+df;
Expand All @@ -124,38 +121,11 @@
// adjust for dropFrame
if (this.dropFrame) {
var totalMinutes = this.hours*60 + this.minutes;
var df = this._numOfFramesToDrop();
var df = this.frameRate === 29.97 ? 2 : 4;
this.frameCount -= df * (totalMinutes - Math.floor(totalMinutes/10));
}
};

/**
* Is the frame rate 29.97 (30000/1001) or close enough to it
* @private
*/
Timecode.prototype._is30DF = function () {
return Math.abs(30000/1001 - this.frameRate) < DROP_FRAME_EPSILON;
};

/**
* Is the frame rate 59.94 (60000/1001) or close enough to it
* @private
*/
Timecode.prototype._is60DF = function () {
return Math.abs(60000/1001 - this.frameRate) < DROP_FRAME_EPSILON;
};

/**
* Get the number of frames to drop — 29.97 skips 2 frames, 59.94 skips 4
* frames.
* @private
*/
Timecode.prototype._numOfFramesToDrop = function () {
if (this._is30DF()) return 2;
else if (this._is60DF()) return 4;
else return 0;
};

/**
* Convert Timecode to String
* @param {String} format output format
Expand Down
2 changes: 0 additions & 2 deletions test/smpte-timecode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ describe('Constructor tests', function(){
expect(Timecode(1).dropFrame).to.be(true);
expect(Timecode(1).frameRate).to.be(29.97);
expect(Timecode(1,29.97).dropFrame).to.be(true);
expect(Timecode(1,30000/1001).dropFrame).to.be(true);
expect(Timecode(1,59.94).dropFrame).to.be(true);
expect(Timecode(1,60000/1001).dropFrame).to.be(true);
expect(Timecode(1,25).dropFrame).to.be(false);
});

Expand Down

0 comments on commit eadbc9e

Please sign in to comment.