tsunami 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/.gitignore +3 -0
  2. data/VERSION +1 -1
  3. data/lib/tsunami.rb +31 -25
  4. data/tsunami.gemspec +1 -1
  5. metadata +1 -1
data/.gitignore CHANGED
@@ -19,3 +19,6 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ lib/*.mp3
23
+ lib/*.png
24
+ lib/test.rb
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/lib/tsunami.rb CHANGED
@@ -5,41 +5,41 @@ require 'narray'
5
5
 
6
6
  class Tsunami
7
7
 
8
- attr_accessor :size, :width, :height
9
- attr_accessor :audio_file, :image_file
10
- attr_accessor :bitrate
11
- attr_accessor :offset
12
-
13
- def initialize size, audio_file, image_file
8
+ def initialize audio_file, image_file
14
9
  # graph parameters
15
- @size = size
16
- @width = (@size).to_i
17
- @height = 130
18
10
  @bitrate = '500'
19
11
  @offset = 5
20
12
  @audio_file = audio_file
21
13
  @image_file = image_file
22
- #testing_parameters
23
14
  end
24
15
 
25
- def create_waveform_image
26
- buckets = fill_buckets width,@audio_file
27
- p buckets
28
- p size
29
- gc = build_graph buckets,size
16
+ def create_waveform_image(width, height)
17
+ @size = width
18
+ @width = width
19
+ @height = height
20
+
21
+ buckets = fill_buckets
22
+
23
+ gc = build_graph buckets
24
+
30
25
  #create new image canvas
31
- canvas = Magick::Image.new(@width + 20, @height) { self.background_color = 'transparent' }
26
+ canvas = Magick::Image.new(@width + 2*@offset, @height) { self.background_color = 'transparent' }
27
+
32
28
  # canvas = Magick::ImageList.new('images/waveform.png')
33
29
  gc.draw(canvas)
30
+
34
31
  canvas.write(@image_file)
35
32
  end
36
33
 
37
34
  #fill the buckets
38
- def fill_buckets width,file
39
- buckets = NArray.int(width,2)
35
+ def fill_buckets
36
+ buckets = NArray.int(@width,2)
37
+
40
38
  #let sox fetch the byte array
41
- bytes = sox_get_bytes file
42
- bucket_size = (((bytes.size-1).to_f / width)+0.5).to_i + 1
39
+ bytes = wave_bytes
40
+
41
+ bucket_size = (((bytes.size-1).to_f / @width)+0.5).to_i + 1
42
+
43
43
  (0..bytes.total-1).each do |i|
44
44
  value = bytes[i]
45
45
  index = i/bucket_size
@@ -56,14 +56,19 @@ class Tsunami
56
56
  #positive total
57
57
  #buckets[index,5] += value if value > 0
58
58
  end
59
+
59
60
  return buckets
60
61
  end
61
62
 
63
+ def wave_bytes
64
+ @wave_bytes ||= sox_get_bytes
65
+ end
66
+
62
67
  #open file with sox and return a byte array with sweet waveform information in it
63
- def sox_get_bytes file, channels = 1
68
+ def sox_get_bytes channels = 1
64
69
  x=nil
65
70
  # read a 16 bit linear raw PCM file
66
- sox_command = [ 'sox', file, '-t', 'raw', '-r', @bitrate, '-c', channels.to_s, '-s', '-L', '-' ]
71
+ sox_command = [ 'sox', @audio_file, '-t', 'raw', '-r', @bitrate, '-c', channels.to_s, '-s', '-L', '-' ]
67
72
  # we have to fork/exec to get a clean commandline
68
73
  IO.popen('-') { |p|
69
74
  if p.nil? then
@@ -81,11 +86,12 @@ class Tsunami
81
86
  end
82
87
 
83
88
  #build the waveform graph
84
- def build_graph buckets,size
89
+ def build_graph buckets
85
90
  gc = Magick::Draw.new
91
+
86
92
  scale = 32768/@height*2.75
87
93
  midpoint = @height/2
88
- p buckets.size
94
+
89
95
  (0..(buckets.size/2)-1).each do |i|
90
96
  low = buckets[i,0]
91
97
  high = buckets[i,1]
@@ -94,8 +100,8 @@ class Tsunami
94
100
  low_point = midpoint+low/scale
95
101
  high_point = midpoint+high/scale
96
102
  gc.line(i+@offset, low_point.to_i, i+@offset, high_point.to_i)
97
- puts "#{i+@offset}, #{low_point.to_i}, #{i+@offset}, #{high_point.to_i}"
98
103
  end
104
+
99
105
  return gc
100
106
  end
101
107
  end
data/tsunami.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tsunami}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrei Bocan"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tsunami
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Bocan