Class: HeadMusic::Time::ClockPosition
- Inherits:
-
Object
- Object
- HeadMusic::Time::ClockPosition
- Includes:
- Comparable
- Defined in:
- lib/head_music/time/clock_position.rb
Overview
A value object representing elapsed nanoseconds of clock time
ClockPosition provides a high-precision representation of time elapsed from a reference point, stored as nanoseconds. This allows for precise temporal calculations in musical contexts where millisecond-level accuracy is required for MIDI timing, audio synchronization, and SMPTE timecode.
Instance Attribute Summary collapse
-
#nanoseconds ⇒ Integer
readonly
The number of nanoseconds since the reference point.
Instance Method Summary collapse
-
#+(other) ⇒ ClockPosition
Add another clock position to this one.
-
#<=>(other) ⇒ Integer
Compare this position to another.
-
#initialize(nanoseconds) ⇒ ClockPosition
constructor
Create a new clock position.
-
#to_i ⇒ Integer
Convert to integer representation (nanoseconds).
-
#to_microseconds ⇒ Float
Convert nanoseconds to microseconds.
-
#to_milliseconds ⇒ Float
Convert nanoseconds to milliseconds.
-
#to_seconds ⇒ Float
Convert nanoseconds to seconds.
Constructor Details
#initialize(nanoseconds) ⇒ ClockPosition
Create a new clock position
35 36 37 |
# File 'lib/head_music/time/clock_position.rb', line 35 def initialize(nanoseconds) @nanoseconds = nanoseconds end |
Instance Attribute Details
#nanoseconds ⇒ Integer (readonly)
Returns the number of nanoseconds since the reference point.
30 31 32 |
# File 'lib/head_music/time/clock_position.rb', line 30 def nanoseconds @nanoseconds end |
Instance Method Details
#+(other) ⇒ ClockPosition
Add another clock position to this one
71 72 73 |
# File 'lib/head_music/time/clock_position.rb', line 71 def +(other) self.class.new(nanoseconds + other.to_i) end |
#<=>(other) ⇒ Integer
Compare this position to another
79 80 81 |
# File 'lib/head_music/time/clock_position.rb', line 79 def <=>(other) nanoseconds <=> other.to_i end |
#to_i ⇒ Integer
Convert to integer representation (nanoseconds)
42 43 44 |
# File 'lib/head_music/time/clock_position.rb', line 42 def to_i nanoseconds end |
#to_microseconds ⇒ Float
Convert nanoseconds to microseconds
49 50 51 |
# File 'lib/head_music/time/clock_position.rb', line 49 def to_microseconds nanoseconds / 1_000.0 end |
#to_milliseconds ⇒ Float
Convert nanoseconds to milliseconds
56 57 58 |
# File 'lib/head_music/time/clock_position.rb', line 56 def to_milliseconds nanoseconds / 1_000_000.0 end |
#to_seconds ⇒ Float
Convert nanoseconds to seconds
63 64 65 |
# File 'lib/head_music/time/clock_position.rb', line 63 def to_seconds nanoseconds / 1_000_000_000.0 end |