Class: OSC::TimeTag

Inherits:
Object
  • Object
show all
Defined in:
lib/osc.rb

Overview

64-bit big-endian fixed-point time tag

Constant Summary collapse

JAN_1970 =
0x83aa7e80

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TimeTag

nil

immediately

Numeric

seconds since January 1, 1900 00:00

Numeric,Numeric

int,frac parts of a TimeTag.

Time

convert from Time object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/osc.rb', line 42

def initialize(*args)
  t = args
  t = t.first if t and t.size == 1
  case t
  when NIL # immediately
	@int = 0
	@frac = 1
  when Numeric
	@int, fr = t.divmod(1)
	@frac = (fr * (2**32)).to_i
  when Array
	@int,@frac = t
  when Time
	@int, fr = (t.to_f+JAN_1970).divmod(1)
	@frac = (fr * (2**32)).to_i
  else
	raise ArgumentError
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



74
75
76
# File 'lib/osc.rb', line 74

def method_missing(sym, *args)
  time.__send__(sym, *args)
end

Instance Attribute Details

#fracObject

Returns the value of attribute frac.



61
62
63
# File 'lib/osc.rb', line 61

def frac
  @frac
end

#intObject

Returns the value of attribute int.



61
62
63
# File 'lib/osc.rb', line 61

def int
  @int
end

Class Method Details

.nowObject



73
# File 'lib/osc.rb', line 73

def self.now; TimeTag.new(Time.now); end

Instance Method Details

#to_aObject

int,frac


67
# File 'lib/osc.rb', line 67

def to_a; [@int,@frac]; end

#to_fObject

Ruby’s Float can handle the 64 bits so we have the luxury of dealing with Float directly



65
# File 'lib/osc.rb', line 65

def to_f; @int.to_f + @frac.to_f/(2**32); end

#to_iObject



62
# File 'lib/osc.rb', line 62

def to_i; to_f.to_i; end

#to_sObject

Human-readable, like the output of Time#to_s



69
# File 'lib/osc.rb', line 69

def to_s; to_time.to_s; end

#to_timeObject Also known as: time

Ruby Time object



71
# File 'lib/osc.rb', line 71

def to_time; Time.at(to_f-JAN_1970); end

#to_yamlObject



77
78
79
# File 'lib/osc.rb', line 77

def to_yaml
  to_a.to_yaml
end