Comments (5)
But for the meantime, short answer is keep using this:
let numeric = incoming.realTimeValue.seconds - start.realTimeValue.seconds
Log.debug(numeric) // -3.199999999999818 -- this is right
The real time domain will achieve what you want to do here. (At least until a TimecodeDelta
object or some other solution can be introduced).
It's also possible to test if the float is a negative and then wrap it in fabs()
to invert it to a positive float, then feed it back into a Timecode constructor to get a "negative duration" as expressed by positive timecode.
from timecodekit.
Basically everything in your code snippet is behavior by design (or at least, not incorrect).
Timecode by nature is linear, and is never expressed as a negative (think of consecutive repeating 24 hour clocks). The library tries to adhere to that principle.
When determining the behaviors when using math operations around boundaries (underflowing 00:00:00:00, or overflowing 23:59:59:FF where FF is max frames) I picked sensible defaults, but they will not always make sense in every scenario.
The -
operator currently uses lhs.subtracting(clamping: rhs)
under the hood which clamps to lower and upper timecode bounds. Which explains why this results in 00:00:00:00
:
let subtracting = incoming - start
Log.debug(subtracting) // == 00:00:00:00
The alternatives are:
-
subtracting(_ exactly: ) -> Timecode?
- This would produce
nil
since a negative timecode is not possible (and should not be possible)
- This would produce
-
subtracting(wrapping: ) -> Timecode
- This would wrap around the 24 hour timecode upper bound
let subtracting = incoming.subtracting(wrapping: start.components) Log.debug(subtracting) // == 23:59:56:20
- This would wrap around the 24 hour timecode upper bound
I could have originally made subtracting(wrapping: )
the default behavior for the -
operator but that won't directly resolve your use case.
All of that said, this opens a discussion regarding delta values when it comes to timecode differentials (ie: distance between two timecodes, not producing an absolute timecode). Because of how the Timecode
object is designed to be representative of real timecode, it may make sense to introduce another struct called TimecodeDelta
that can be returned as the result of math operations that the consumer can then manipulate in the abstract before converting back to Timecode or real time values.
from timecodekit.
Introduced Timecode.Delta
in release 1.1.0: https://github.com/orchetect/TimecodeKit/releases/tag/1.1.0
from timecodekit.
New usage would be as follows:
func testNegativeTimecode() {
guard let incoming = Timecode("00:59:56:20", at: ._25),
let start = Timecode("01:00:00:00", at: ._25) else {
XCTFail()
return
}
let delta = start.delta(to: incoming)
Log.debug(delta.timecode) // 23:59:56:20
Log.debug(delta.delta) // 00:00:03:05
Log.debug(delta.isNegative) // true
let numeric = delta.realTimeValue
Log.debug(numeric) // -3.20000001
}
from timecodekit.
Bumped to 1.1.1 with minor fix: https://github.com/orchetect/TimecodeKit/releases/tag/1.1.1
from timecodekit.
Related Issues (20)
- Formatter coloration issues when focused or not (macOS) HOT 2
- Replace TimeValue with TimeInterval? HOT 1
- TCC from fractional seconds HOT 2
- Idea behind Timecode.frameRate ? HOT 8
- Add `Timecode(_ exactly: FrameCount)` init and `setTimecode(_ exactly: FrameCount)` methods HOT 1
- Update Documentation
- API change: `.setTimecode(...)` methods `throws` instead of returning `Bool` HOT 3
- Make all stored properties in `Timecode` struct mutable HOT 1
- Add methods to use a `Timecode` struct as a template HOT 2
- Documentation update
- Add `clamping`, `wrapping` overloads for to ancillary value type init/methods HOT 3
- Integer init and setter for Samples HOT 1
- Linux support HOT 1
- `Timecode.FrameRate`: Add failable init from rational frame rate (fraction) HOT 3
- Frame Rate & Timecode Conversion to/from Rational Numbers (fractions) HOT 3
- `Timecode`: Add `CMTime` init, `setTimecode()` methods and `cmTime` property HOT 1
- Negative rational values HOT 2
- Support for 96, 96/1.001 HOT 2
- AVFoundation Extensions Improvements
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from timecodekit.