Class: WaveToJson
- Inherits:
-
Object
show all
- Defined in:
- lib/wave_to_json.rb,
lib/wave_to_json/cli.rb,
lib/wave_to_json/audio.rb,
lib/wave_to_json/version.rb,
lib/wave_to_json/shell_command.rb
Defined Under Namespace
Classes: Audio, CLI, ShellCommand
Constant Summary
collapse
- DEFAULT_PIXEL_PER_SECOND =
1000 / 30.0
- VERSION =
"0.1.0"
Instance Method Summary
collapse
Constructor Details
#initialize(source, destination, options = {}) ⇒ WaveToJson
Returns a new instance of WaveToJson.
9
10
11
12
13
14
15
|
# File 'lib/wave_to_json.rb', line 9
def initialize(source, destination, options = {})
@filename = source
@output_path = destination
@pixel_per_second = options.fetch(:pixel_per_second, DEFAULT_PIXEL_PER_SECOND)
@channel = options.fetch(:channel, :both)
@audio = Audio.new(@filename)
end
|
Instance Method Details
#calculate_ratios(mins, maxs) ⇒ Object
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/wave_to_json.rb', line 55
def calculate_ratios(mins, maxs)
max = 0
results = (0..mins.length-1).map do |i|
height = maxs[i] - mins[i]
max = height > max ? height : max
height
end
results.map { |result| (result / max.to_f).round(6) }
end
|
#generate ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/wave_to_json.rb', line 66
def generate
File.open(@output_path, 'w') do |file|
values = raw_values
converted_values = calculate_ratios(*values)
file.write(Oj.dump(converted_values))
end
end
|
#raw_values ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/wave_to_json.rb', line 17
def raw_values
min_values = []
max_values = []
contents = @audio.raw_data(channel: @channel)
segment_size = (contents.length.to_f / width).to_i
current_index = 0
min = Audio::MAX_VALUE
max = Audio::MIN_VALUE
contents.each do |value|
if current_index == segment_size
max_values.push(max)
min_values.push(min)
current_index = 0
min = Audio::MAX_VALUE
max = Audio::MIN_VALUE
else
min = value < min ? value : min
max = value > max ? value : max
current_index+=1
end
end
if current_index != 0
max_values.push(max)
min_values.push(min)
end
[min_values, max_values]
end
|
#width ⇒ Object
50
51
52
53
|
# File 'lib/wave_to_json.rb', line 50
def width
duration_in_millisecond = @audio.duration * 1000
@width ||= ( duration_in_millisecond/ @pixel_per_second).round
end
|