rsox-command 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sox/stats.rb CHANGED
@@ -2,8 +2,10 @@ module Sox
2
2
  class Stats
3
3
 
4
4
  attr_reader :command
5
+ attr_accessor :use_cache
5
6
 
6
- def initialize(file = nil)
7
+ def initialize(file = nil, options = {})
8
+ options.each { |k,v| send "#{k}=", v }
7
9
  @command = Sox::Command.new.tap do |command|
8
10
  command.effect "stats"
9
11
  command.input file if file
@@ -24,7 +26,37 @@ module Sox
24
26
  end
25
27
  end
26
28
 
27
- def attributes
29
+ class Cache
30
+
31
+ attr_accessor :sound_file
32
+
33
+ def initialize(sound_file)
34
+ @sound_file = sound_file
35
+ end
36
+
37
+ def cache_file
38
+ "#{sound_file}.stats"
39
+ end
40
+
41
+ def load
42
+ YAML.load(IO.read(cache_file)) rescue nil
43
+ end
44
+
45
+ def dump(attributes)
46
+ puts "Save in #{cache_file}: #{attributes.inspect}"
47
+ File.open(cache_file, "w") do |f|
48
+ f.write YAML.dump(attributes)
49
+ end
50
+ attributes
51
+ end
52
+
53
+ def fetch(&block)
54
+ load or dump(block.call)
55
+ end
56
+
57
+ end
58
+
59
+ def attributes_without_cache
28
60
  @attributes ||=
29
61
  begin
30
62
  paires = raw_values.collect do |key, _, value|
@@ -35,6 +67,20 @@ module Sox
35
67
  end
36
68
  end
37
69
 
70
+ def attributes_with_cache
71
+ Cache.new(command.inputs.first.filename).fetch do
72
+ attributes_without_cache
73
+ end
74
+ end
75
+
76
+ def attributes
77
+ if use_cache and command.inputs.size == 1
78
+ attributes_with_cache
79
+ else
80
+ attributes_without_cache
81
+ end
82
+ end
83
+
38
84
  def rms_level
39
85
  attributes['RMS lev dB']
40
86
  end
data/rsox-command.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "rsox-command"
6
- s.version = "0.0.1"
6
+ s.version = "0.0.2"
7
7
  s.authors = ["Alban Peignier"]
8
8
  s.email = ["alban@tryphon.eu"]
9
9
  s.homepage = "http://projects.tryphon.eu/rsox-command"
@@ -3,16 +3,38 @@ require 'spec_helper'
3
3
  describe Sox::Stats do
4
4
 
5
5
  subject { Sox::Stats.new }
6
+
7
+ let(:file) { "spec/fixtures/test.ogg" }
6
8
 
7
9
  describe "#raw_output" do
8
10
 
9
11
  it "should return sox command output" do
10
- subject.input("spec/fixtures/test.ogg")
12
+ subject.input(file)
11
13
  subject.raw_output.should == IO.read("spec/fixtures/test.stats")
12
14
  end
13
15
 
14
16
  end
15
17
 
18
+ describe "cache" do
19
+
20
+ it "should return sox command output" do
21
+ # Fill cache
22
+ Sox::Stats.new(file, :use_cache => true).attributes
23
+
24
+ subject.use_cache = true
25
+ subject.input(file)
26
+
27
+ subject.command.should_not_receive(:run!)
28
+
29
+ subject.attributes
30
+ end
31
+
32
+ after(:each) do
33
+ FileUtils.rm_f "spec/fixtures/test.ogg.stats"
34
+ end
35
+
36
+ end
37
+
16
38
  describe "#attributes" do
17
39
 
18
40
  it "should read name and first value from raw output" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsox-command
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alban Peignier
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-18 00:00:00 Z
18
+ date: 2012-05-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  prerelease: false