Class: Tsunami

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(audio_file) ⇒ Tsunami

Returns a new instance of Tsunami.

Raises:

  • (Errno::ENOENT)


7
8
9
10
11
# File 'lib/tsunami.rb', line 7

def initialize audio_file
  raise Errno::ENOENT.new(audio_file) unless File.exist? audio_file

  @file_name = audio_file
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/tsunami.rb', line 5

def height
  @height
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/tsunami.rb', line 5

def width
  @width
end

Instance Method Details

#build_graph(values) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tsunami.rb', line 34

def build_graph(values)
  gc = Magick::Draw.new
  gc.stroke('red')
  gc.stroke_width(1)

  mid = height / 2

  values.each_with_index do |values, i|
    low = values[:min]
    high = values[:max]

    low_point = mid * ( 1 - low )
    high_point = mid * ( 1 - high )

    gc.line(i, low_point.to_i, i, high_point.to_i)
  end

  return gc
end

#create_waveform(image_file, width, height) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/tsunami.rb', line 13

def create_waveform(image_file, width, height)
  self.width = width
  self.height = height

  values = TsunamiC.get_stats(@file_name, width)

  canvas = draw_waveform(values)

  canvas.write(image_file)
end

#draw_waveform(values) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/tsunami.rb', line 24

def draw_waveform(values)
  gc = build_graph(values)

  canvas = Magick::Image.new(width, height) { self.background_color = 'transparent' }

  gc.draw(canvas)

  canvas
end