Module: HeadMusic::Time::RadixCarry

Included in:
MusicalPosition, SmpteTimecode
Defined in:
lib/head_music/time/radix_carry.rb

Overview

Shared helper for positional/timecode components that carry between radix-bounded fields (e.g. bars:beats:ticks, hours:minutes:seconds:frames).

Instance Method Summary collapse

Instance Method Details

#carry(component, radix, first: 0) ⇒ Integer (private)

Divide the named component by its radix, store the remainder back, and return the amount to carry into the next-higher component.

A 1-indexed component (a beat, whose values run 1..radix) is shifted into 0-indexed space before the divmod and back afterward, so that the last value in the range stays put instead of carrying.

Parameters:

  • first (Integer) (defaults to: 0) —

    the component's lowest valid value

Returns:

  • (Integer) —

    the carry (may be negative when borrowing)



19
20
21
22
23
24
# File 'lib/head_music/time/radix_carry.rb', line 19

def carry(component, radix, first: 0)
  ivar = :"@#{component}"
  delta, remainder = (instance_variable_get(ivar) - first).divmod(radix)
  instance_variable_set(ivar, remainder + first)
  delta
end