Frames_tascam

This was made fast yesterday. Just wanted a way to split up recordings into bars and beats. I haven’t even got the machine (Tascam DP-006) yet.

Not sure if it’s possible to edit tracks the way I imagine.

FRAME_RATE = 30 # according to tascam.eu

module Smpte
  refine Integer do
    def frames      = ( self )
    def seconds     = ( self * FRAME_RATE )
    def minutes     = ( self.seconds * 60 )
    def hours       = ( self.minutes * 60 )
   
    {frames: [:frame, :fr], seconds: [:secs, :sec],
    minutes: [:mins, :min], hours: [:hrs, :hr]}.each {|k, v|
      v.each {|al| define_method(al) { send(k) } }
    }
  end
end

class Frames

  attr_accessor :frames
  def initialize(frames=0)
    @frames = frames
    @smpte = Struct.new(:hr, :min, :sec, :frames) {
      def to_s = ('%.2i:%.2i:%.2i (%.2i)' %
        [hr, min, sec, frames])
    }
  end

  def reset = ( @frames = 0 )

  def smpte(frames = @frames)
    smpte = @smpte.new(0,0,0,0)
    smpte.sec, smpte.frames = frames.divmod(FRAME_RATE)
    smpte.min, smpte.sec = smpte.sec.divmod(60) if smpte.sec > 0
    smpte.hr, smpte.min = smpte.min.divmod(60) if smpte.min > 0
    smpte
  end

  def bpm(b)
    mult, mult_rest = b.divmod(4) # one measure
    p '  >> REST: %s' % mult_rest if mult_rest > 0
    smpte((FRAME_RATE * 60).divmod(mult).first)
  end
end

Usage :

using Smpte

fr = Frames.new
fr.frames = 1.min + 30.sec
p fr.frames # => "2700"
puts fr.smpte # => "00:01:30 (00)"

# With 120BPM.. one meassure will take 2 seconds.
puts fr.bpm(120) # => "00:00:02 (00)"

# 2 seconds and 4 frames with 112 BPM
puts fr.bpm(112) # => "00:00:02 (04)"

# 1 second, 26 frames
puts fr.bpm(128)

# how many frames is that
fr.reset
fr.frames = 1.sec + 26.frames
p fr.frames # => 56

# I want four bars
fr.frames *= 4
p fr.frames # => 224

puts fr.smpte # => "00:00:07 (14)"