tsunami 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/VERSION +1 -1
- data/lib/tsunami.rb +31 -25
- data/tsunami.gemspec +1 -1
- metadata +1 -1
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
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
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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 +
|
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
|
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 =
|
42
|
-
|
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
|
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',
|
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
|
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
|
-
|
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