albanpeignier-alsa-backup 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ == 0.0.7
2
+
3
+ * Make configurable channels, sample rate and alsa device
4
+
1
5
  == 0.0.6
2
6
 
3
7
  * Fix file writing problem
data/TODO CHANGED
@@ -1,4 +1 @@
1
- * configurable channels
2
- * configurable sample rate
3
- * configurable alsa device
4
1
  * improve spec coverage
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{alsa-backup}
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Alban Peignier"]
9
- s.date = %q{2009-05-28}
9
+ s.date = %q{2009-05-31}
10
10
  s.default_executable = %q{alsa.backup}
11
11
  s.description = %q{ALSA client to perform continuous recording}
12
12
  s.email = ["alban.peignier@free.fr"]
@@ -21,6 +21,23 @@
21
21
  AlsaBackup.logger.info("load config #{__FILE__}")
22
22
 
23
23
  AlsaBackup.config do |recorder|
24
+
25
+ #
26
+ # To specify the alsa device to be used
27
+ #
28
+ #recorder.device = "plughw:0"
29
+
30
+ #
31
+ # To specify the channel count to be used
32
+ #
33
+ #recorder.channels = 1
34
+ #recorder.channels = 4
35
+
36
+ #
37
+ # To specify the sample_rate to be used
38
+ #
39
+ #recorder.sample_rate = 48000
40
+
24
41
  #
25
42
  # To use files named like this :
26
43
  # - 2009/05-May/17-Sun/19h.wav
@@ -10,7 +10,7 @@ require 'activesupport'
10
10
  require 'logger'
11
11
 
12
12
  module AlsaBackup
13
- VERSION = '0.0.6'
13
+ VERSION = '0.0.7'
14
14
 
15
15
  def self.recorder
16
16
  @recorder ||= AlsaBackup::Recorder.new
@@ -7,10 +7,15 @@ module AlsaBackup
7
7
  @file = File.basename(file)
8
8
  @directory = File.dirname(file)
9
9
 
10
+ @device = "hw:0"
11
+ @sample_rate = 44100
12
+ @channels = 2
13
+
10
14
  @error_handler = Proc.new { |e| true }
11
15
  end
12
16
 
13
17
  attr_accessor :file, :directory, :error_handler
18
+ attr_accessor :device, :sample_rate, :channels
14
19
 
15
20
  def start(seconds_to_record = nil)
16
21
  length_controller = self.length_controller(seconds_to_record)
@@ -32,7 +37,7 @@ module AlsaBackup
32
37
  end
33
38
 
34
39
  def open_capture(&block)
35
- ALSA::PCM::Capture.open("hw:0", self.format(:sample_format => :s16_le), &block)
40
+ ALSA::PCM::Capture.open(device, self.format(:sample_format => :s16_le), &block)
36
41
  end
37
42
 
38
43
  def handle_error(e, try_to_continue = true)
@@ -64,7 +69,7 @@ module AlsaBackup
64
69
  end
65
70
 
66
71
  def format(additional_parameters = {})
67
- {:sample_rate => 44100, :channels => 2}.merge(additional_parameters)
72
+ {:sample_rate => sample_rate, :channels => channels}.merge(additional_parameters)
68
73
  end
69
74
 
70
75
  def length_controller(seconds_to_record)
@@ -39,7 +39,13 @@ module AlsaBackup
39
39
  def close
40
40
  if @sndfile
41
41
  AlsaBackup.logger.info('close current file')
42
- @sndfile.close
42
+ @sndfile.close
43
+
44
+ closed_file = @sndfile.path
45
+ if File.zero?(closed_file)
46
+ AlsaBackup.logger.warn('remove empty file #{closed_file}')
47
+ File.delete closed_file
48
+ end
43
49
  end
44
50
  @sndfile = nil
45
51
  end
@@ -13,6 +13,34 @@ describe AlsaBackup::Recorder do
13
13
  lambda { @recorder.start(2) }.should_not raise_error
14
14
  end
15
15
 
16
+ it "should use the specified alsa device" do
17
+ @recorder.device = alsa_device = "dummy"
18
+ ALSA::PCM::Capture.should_receive(:open).with(alsa_device, anything)
19
+ @recorder.open_capture
20
+ end
21
+
22
+ it "should use the specified sample rate" do
23
+ @recorder.sample_rate = 48000
24
+ @recorder.format[:sample_rate].should == @recorder.sample_rate
25
+ end
26
+
27
+ it "should use the specified channels" do
28
+ @recorder.channels = 4
29
+ @recorder.format[:channels].should == @recorder.channels
30
+ end
31
+
32
+ it "should use 44100 as default sample rate" do
33
+ @recorder.sample_rate.should == 44100
34
+ end
35
+
36
+ it "should use 2 as default channels" do
37
+ @recorder.channels.should == 2
38
+ end
39
+
40
+ it "should use hw:0 as default device" do
41
+ @recorder.device.should == "hw:0"
42
+ end
43
+
16
44
  describe "error handler" do
17
45
 
18
46
  class TestErrorHandler
@@ -5,20 +5,20 @@ describe AlsaBackup::Writer do
5
5
  before(:each) do
6
6
  @file = "test.wav"
7
7
  @directory = test_directory
8
- @recorder = AlsaBackup::Writer.new(@file, @directory)
8
+ @writer = AlsaBackup::Writer.new(@file, @directory)
9
9
  end
10
10
 
11
11
  describe "file" do
12
12
 
13
13
  it "should accept file as string" do
14
- @recorder.file = file_name = "dummy"
15
- @recorder.file.should == file_name
14
+ @writer.file = file_name = "dummy"
15
+ @writer.file.should == file_name
16
16
  end
17
17
 
18
18
  it "should accept file as Proc" do
19
19
  file_name = "dummy"
20
- @recorder.file = Proc.new { file_name }
21
- @recorder.file.should == file_name
20
+ @writer.file = Proc.new { file_name }
21
+ @writer.file.should == file_name
22
22
  end
23
23
 
24
24
  end
@@ -50,4 +50,23 @@ describe AlsaBackup::Writer do
50
50
 
51
51
  end
52
52
 
53
+ describe "close" do
54
+
55
+ it "should close current sndfile" do
56
+ sndfile = @writer.sndfile
57
+ sndfile.should_receive(:close)
58
+ @writer.close
59
+ end
60
+
61
+ it "should remove closed file if empty" do
62
+ @writer.sndfile
63
+
64
+ File.stub!(:zero?).and_return(true)
65
+ File.should_receive(:delete).with(@writer.target_file)
66
+
67
+ @writer.close
68
+ end
69
+
70
+ end
71
+
53
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: albanpeignier-alsa-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alban Peignier
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-28 00:00:00 -07:00
12
+ date: 2009-05-31 00:00:00 -07:00
13
13
  default_executable: alsa.backup
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency