Class: WaveFile::Duration

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

Overview

Public: Calculates playback time given the number of sample frames and the sample rate. For example, you can use this to calculate how long a given Wave file is.

The hours, minutes, seconds, and milliseconds fields return values like you would see on a stopwatch, and not the total amount of time in that unit. For example, a stopwatch running for exactly 2 hours would show something like “2:00:00.000”. Accordingly, if the given sample frame count and sample rate add up to exactly 2 hours, then hours will be 2, and minutes, seconds, and milliseconds will all be 0.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample_frame_count, sample_rate) ⇒ Duration

Public: Constructs a new immutable Duration.

sample_frame_count - The number of sample frames, i.e. the number

samples in each channel.

sample_rate - The number of samples per second, such as 44100

Examples:

duration = Duration.new(400_000_000, 44100)
duration.hours         # => 2
duration.minutes       # => 31
duration.seconds       # => 10
duration.milliseconds  # => 294

The hours, minutes, seconds, and milliseconds fields return values like you would see on a stopwatch, and not the total amount of time in that unit. For example, a stopwatch running for exactly 2 hours would show something like “2:00:00.000”. Accordingly, if the given sample frame count and sample rate add up to exactly 2 hours, then hours will be 2, and minutes, seconds, and milliseconds will all be 0.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wavefile/duration.rb', line 30

def initialize(sample_frame_count, sample_rate)
  @sample_frame_count = sample_frame_count
  @sample_rate = sample_rate

  sample_frames_per_millisecond = sample_rate / 1000.0
  sample_frames_per_second = sample_rate
  sample_frames_per_minute = sample_frames_per_second * 60
  sample_frames_per_hour = sample_frames_per_minute * 60
  @hours, @minutes, @seconds, @milliseconds = 0, 0, 0, 0

  if sample_frame_count >= sample_frames_per_hour
    @hours = sample_frame_count / sample_frames_per_hour
    sample_frame_count -= sample_frames_per_hour * @hours
  end

  if sample_frame_count >= sample_frames_per_minute
    @minutes = sample_frame_count / sample_frames_per_minute
    sample_frame_count -= sample_frames_per_minute * @minutes
  end

  if sample_frame_count >= sample_frames_per_second
    @seconds = sample_frame_count / sample_frames_per_second
    sample_frame_count -= sample_frames_per_second * @seconds
  end

  @milliseconds = (sample_frame_count / sample_frames_per_millisecond).floor
end

Instance Attribute Details

#hoursObject (readonly)

Public



87
88
89
# File 'lib/wavefile/duration.rb', line 87

def hours
  @hours
end

#millisecondsObject (readonly)

Public



96
97
98
# File 'lib/wavefile/duration.rb', line 96

def milliseconds
  @milliseconds
end

#minutesObject (readonly)

Public



90
91
92
# File 'lib/wavefile/duration.rb', line 90

def minutes
  @minutes
end

#sample_frame_countObject (readonly)

Public



81
82
83
# File 'lib/wavefile/duration.rb', line 81

def sample_frame_count
  @sample_frame_count
end

#sample_rateObject (readonly)

Public



84
85
86
# File 'lib/wavefile/duration.rb', line 84

def sample_rate
  @sample_rate
end

#secondsObject (readonly)

Public



93
94
95
# File 'lib/wavefile/duration.rb', line 93

def seconds
  @seconds
end

Instance Method Details

#==(other_duration) ⇒ Object

Public: Returns true if this Duration represents that same amount of time as other_duration.

Two Duration instances will evaluate as == if they correspond to the same “stopwatch time”. This means that two Durations constructed from a different number of sample frames or different sample rates can be considered equal if they correspond to the same amount of time. For example, a Duration from 44,100 sample frames at 44,100 samples/sec will be considered equal to a Duration from 22,050 sample frames at 22,050 samples/sec, because both correspond to 1 second of audio.

Since the finest resolution of a duration is 1 millisecond, two Durations that represent different amounts of time but differ by less than 1 millisecond will be considered equal.



73
74
75
76
77
78
# File 'lib/wavefile/duration.rb', line 73

def ==(other_duration)
  @hours == other_duration.hours &&
  @minutes == other_duration.minutes &&
  @seconds == other_duration.seconds &&
  @milliseconds == other_duration.milliseconds
end