Class: Deftones::Music::Time
- Inherits:
-
Object
- Object
- Deftones::Music::Time
- Defined in:
- lib/deftones/music/time.rb
Constant Summary collapse
- SYMBOL_MAP =
{ whole: "1n", half: "2n", quarter: "4n", eighth: "8n", sixteenth: "16n", quarter_triplet: "4t", eighth_triplet: "8t", dotted_quarter: "4n.", dotted_eighth: "8n.", measure: "1m" }.freeze
Instance Attribute Summary collapse
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .arithmetic_expression?(value) ⇒ Boolean private
- .beat_duration(bpm) ⇒ Object private
- .compute_rpn(tokens, bpm:, time_signature:, ppq:) ⇒ Object private
- .evaluate_expression(value, bpm:, time_signature:, ppq:) ⇒ Object private
- .normalize_unary_minus(tokens) ⇒ Object private
- .operator?(token) ⇒ Boolean private
- .parse(value, bpm: Deftones.transport.bpm, time_signature: Deftones.transport.time_signature, ppq: Deftones.transport.ppq) ⇒ Object
- .parse_literal(value, bpm:, time_signature:, ppq:) ⇒ Object private
- .precedence(token) ⇒ Object private
- .to_rpn(tokens) ⇒ Object private
- .tokenize(expression) ⇒ Object private
Instance Method Summary collapse
- #dispose ⇒ Object
- #disposed? ⇒ Boolean
- #from_type(type) ⇒ Object
-
#initialize(value, transport: Deftones.transport) ⇒ Time
constructor
A new instance of Time.
- #quantize(subdiv, percent = 1.0) ⇒ Object
- #to_bars_beats_sixteenths ⇒ Object
- #to_frequency ⇒ Object
- #to_midi ⇒ Object
- #to_milliseconds ⇒ Object
- #to_notation ⇒ Object
- #to_s ⇒ Object (also: #toString)
- #to_samples(sample_rate = Deftones.context.sample_rate) ⇒ Object
- #to_seconds ⇒ Object
- #to_ticks ⇒ Object
- #value_of ⇒ Object
Constructor Details
Instance Attribute Details
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
6 7 8 |
# File 'lib/deftones/music/time.rb', line 6 def transport @transport end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/deftones/music/time.rb', line 6 def value @value end |
Class Method Details
.arithmetic_expression?(value) ⇒ Boolean (private)
149 150 151 |
# File 'lib/deftones/music/time.rb', line 149 def arithmetic_expression?(value) value.match?(/[+\-*\/()]/) && !value.match?(/\A-?\d+(?:\.\d+)?\z/) end |
.beat_duration(bpm) ⇒ Object (private)
153 154 155 |
# File 'lib/deftones/music/time.rb', line 153 def beat_duration(bpm) 60.0 / bpm.to_f end |
.compute_rpn(tokens, bpm:, time_signature:, ppq:) ⇒ Object (private)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/deftones/music/time.rb', line 204 def compute_rpn(tokens, bpm:, time_signature:, ppq:) stack = [] tokens.each do |token| if operator?(token) raise Deftones::InvalidTimeError, "Invalid time expression" if stack.length < 2 right = stack.pop left = stack.pop stack << left.public_send(token, right) else stack << parse_literal(token, bpm: bpm, time_signature: time_signature, ppq: ppq) end end raise Deftones::InvalidTimeError, "Invalid time expression" unless stack.length == 1 stack.first end |
.evaluate_expression(value, bpm:, time_signature:, ppq:) ⇒ Object (private)
157 158 159 |
# File 'lib/deftones/music/time.rb', line 157 def evaluate_expression(value, bpm:, time_signature:, ppq:) compute_rpn(to_rpn(tokenize(value)), bpm: bpm, time_signature: time_signature, ppq: ppq) end |
.normalize_unary_minus(tokens) ⇒ Object (private)
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/deftones/music/time.rb', line 224 def normalize_unary_minus(tokens) normalized = [] tokens.each_with_index do |token, index| if token == "-" && (index.zero? || operator?(tokens[index - 1]) || tokens[index - 1] == "(") normalized << "0" end normalized << token end normalized end |
.operator?(token) ⇒ Boolean (private)
237 238 239 |
# File 'lib/deftones/music/time.rb', line 237 def operator?(token) %w[+ - * /].include?(token) end |
.parse(value, bpm: Deftones.transport.bpm, time_signature: Deftones.transport.time_signature, ppq: Deftones.transport.ppq) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/deftones/music/time.rb', line 93 def parse(value, bpm: Deftones.transport.bpm, time_signature: Deftones.transport.time_signature, ppq: Deftones.transport.ppq) case value when Numeric value.to_f when Symbol parse(SYMBOL_MAP.fetch(value), bpm: bpm, time_signature: time_signature, ppq: ppq) when String return evaluate_expression(value, bpm: bpm, time_signature: time_signature, ppq: ppq) if arithmetic_expression?(value) parse_literal(value, bpm: bpm, time_signature: time_signature, ppq: ppq) when /\A(\d+)n\z/ parse_literal(value, bpm: bpm, time_signature: time_signature, ppq: ppq) else return value.to_seconds if value.respond_to?(:to_seconds) end rescue KeyError, ArgumentError => error raise error if error.is_a?(Deftones::InvalidTimeError) raise Deftones::InvalidTimeError, "Unknown time format: #{value}" end |
.parse_literal(value, bpm:, time_signature:, ppq:) ⇒ Object (private)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/deftones/music/time.rb', line 117 def parse_literal(value, bpm:, time_signature:, ppq:) case value when /\A(\d+)n\z/ beats = 4.0 / Regexp.last_match(1).to_f beats * beat_duration(bpm) when /\A(\d+)t\z/ beats = (4.0 / Regexp.last_match(1).to_f) * (2.0 / 3.0) beats * beat_duration(bpm) when /\A(\d+)n\.\z/ parse("#{Regexp.last_match(1)}n", bpm: bpm, time_signature: time_signature, ppq: ppq) * 1.5 when /\A(\d+)m\z/i measures = Regexp.last_match(1).to_f beats_per_measure = Array(time_signature).first || 4 measures * beats_per_measure * beat_duration(bpm) when /\A(\d+):(\d+):(\d+)\z/ = Regexp.last_match(1).to_f beats = Regexp.last_match(2).to_f sixteenths = Regexp.last_match(3).to_f beats_per_measure = Array(time_signature).first || 4 (( * beats_per_measure) + beats + (sixteenths * 0.25)) * beat_duration(bpm) when /\A(\d+(?:\.\d+)?)hz\z/i frequency = Regexp.last_match(1).to_f raise Deftones::InvalidTimeError, "Hz time values must be positive" unless frequency.positive? 1.0 / frequency when /\A(-?\d+(?:\.\d+)?)i\z/i (Regexp.last_match(1).to_f / ppq.to_f) * beat_duration(bpm) else Float(value) end end |
.precedence(token) ⇒ Object (private)
241 242 243 |
# File 'lib/deftones/music/time.rb', line 241 def precedence(token) %w[* /].include?(token) ? 2 : 1 end |
.to_rpn(tokens) ⇒ Object (private)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/deftones/music/time.rb', line 177 def to_rpn(tokens) output = [] operators = [] tokens.each do |token| if operator?(token) while operators.any? && operator?(operators.last) && precedence(operators.last) >= precedence(token) output << operators.pop end operators << token elsif token == "(" operators << token elsif token == ")" raise Deftones::InvalidTimeError, "Mismatched parentheses" unless operators.include?("(") output << operators.pop until operators.last == "(" operators.pop else output << token end end raise Deftones::InvalidTimeError, "Mismatched parentheses" if operators.any? { |operator| ["(", ")"].include?(operator) } output.concat(operators.reverse) end |
.tokenize(expression) ⇒ Object (private)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/deftones/music/time.rb', line 161 def tokenize(expression) tokens = [] offset = 0 pattern = /\G\s*(\d+:\d+:\d+|\d+(?:\.\d+)?hz|-?\d+(?:\.\d+)?i|\d+n\.?|\d+t|\d+m|[()+\-*\/]|-?\d+(?:\.\d+)?)/ while offset < expression.length match = expression.match(pattern, offset) raise Deftones::InvalidTimeError, "Invalid time expression: #{expression}" unless match tokens << match[1] offset = match.end(0) end normalize_unary_minus(tokens) end |
Instance Method Details
#dispose ⇒ Object
60 61 62 63 |
# File 'lib/deftones/music/time.rb', line 60 def dispose @disposed = true self end |
#disposed? ⇒ Boolean
65 66 67 |
# File 'lib/deftones/music/time.rb', line 65 def disposed? @disposed end |
#from_type(type) ⇒ Object
55 56 57 58 |
# File 'lib/deftones/music/time.rb', line 55 def from_type(type) @value = type.respond_to?(:value_of) ? type.value_of : type self end |
#quantize(subdiv, percent = 1.0) ⇒ Object
51 52 53 |
# File 'lib/deftones/music/time.rb', line 51 def quantize(subdiv, percent = 1.0) UnitHelpers.quantize_seconds(to_seconds, subdiv, transport: transport, percent: percent) end |
#to_bars_beats_sixteenths ⇒ Object
27 28 29 |
# File 'lib/deftones/music/time.rb', line 27 def transport.seconds_to_position(to_seconds) end |
#to_frequency ⇒ Object
31 32 33 |
# File 'lib/deftones/music/time.rb', line 31 def to_frequency 1.0 / [to_seconds, 1.0e-6].max end |
#to_midi ⇒ Object
35 36 37 |
# File 'lib/deftones/music/time.rb', line 35 def to_midi Note.to_midi(Note.from_frequency(to_frequency)) end |
#to_milliseconds ⇒ Object
39 40 41 |
# File 'lib/deftones/music/time.rb', line 39 def to_milliseconds to_seconds * 1000.0 end |
#to_notation ⇒ Object
47 48 49 |
# File 'lib/deftones/music/time.rb', line 47 def to_notation UnitHelpers.closest_notation(to_seconds, transport: transport) end |
#to_s ⇒ Object Also known as: toString
69 70 71 |
# File 'lib/deftones/music/time.rb', line 69 def to_s value.to_s end |
#to_samples(sample_rate = Deftones.context.sample_rate) ⇒ Object
43 44 45 |
# File 'lib/deftones/music/time.rb', line 43 def to_samples(sample_rate = Deftones.context.sample_rate) UnitHelpers.samples_for_seconds(to_seconds, sample_rate) end |
#to_seconds ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/deftones/music/time.rb', line 14 def to_seconds self.class.parse( value, bpm: transport.bpm, time_signature: transport.time_signature, ppq: transport.ppq ) end |
#to_ticks ⇒ Object
23 24 25 |
# File 'lib/deftones/music/time.rb', line 23 def to_ticks transport.seconds_to_ticks(to_seconds) end |
#value_of ⇒ Object
75 76 77 |
# File 'lib/deftones/music/time.rb', line 75 def value_of to_seconds end |