albanpeignier-alsa-backup 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.0.5
2
+
3
+ * Fixe half buffer writing in recording file
4
+
1
5
  == 0.0.4
2
6
 
3
7
  * Replace :int by :off_t in Sndfile#write_int prototype
data/alsa-backup.gemspec CHANGED
@@ -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.4"
5
+ s.version = "0.0.5"
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-22}
9
+ s.date = %q{2009-05-27}
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"]
data/lib/alsa.rb CHANGED
@@ -17,7 +17,7 @@ module ALSA
17
17
  def self.logger=(logger); @logger = logger; end
18
18
 
19
19
  def self.try_to(message, &block)
20
- logger.debug('alsa') { message }
20
+ logger.debug { message }
21
21
  if ALSA::Native::error_code?(response = yield)
22
22
  raise "cannot #{message} (#{ALSA::Native::strerror(response)})"
23
23
  else
@@ -91,7 +91,7 @@ module ALSA
91
91
  end
92
92
 
93
93
  def read
94
- ALSA.logger.debug('alsa') { "start read with #{hw_params.sample_rate}, #{hw_params.channels} channels"}
94
+ ALSA.logger.debug { "start read with #{hw_params.sample_rate}, #{hw_params.channels} channels"}
95
95
 
96
96
  # use an 500ms buffer
97
97
  frame_count = hw_params.sample_rate / 2 * hw_params.channels
@@ -107,7 +107,7 @@ module ALSA
107
107
  read_count = ALSA::try_to "read from audio interface" do
108
108
  response = ALSA::PCM::Native::readi(self.handle, buffer, frame_count)
109
109
  if ALSA::Native::error_code?(response)
110
- ALSA.logger.debug('alsa') { "try to recover '#{ALSA::Native::strerror(response)}' on read"}
110
+ ALSA.logger.debug { "try to recover '#{ALSA::Native::strerror(response)}' on read"}
111
111
  ALSA::PCM::Native::pcm_recover(self.handle, response, 1)
112
112
  else
113
113
  response
@@ -116,7 +116,7 @@ module ALSA
116
116
 
117
117
  missing_frame_count = frame_count - read_count
118
118
  if missing_frame_count > 0
119
- ALSA.logger.debug('alsa') { "re-read missing frame count: #{missing_frame_count}"}
119
+ ALSA.logger.debug { "re-read missing frame count: #{missing_frame_count}"}
120
120
  read_buffer_size = hw_params.buffer_size_for(read_count)
121
121
  # buffer[read_buffer_size] doesn't return a MemoryPointer
122
122
  read_buffer(buffer + read_buffer_size, missing_frame_count)
data/lib/alsa_backup.rb CHANGED
@@ -10,7 +10,7 @@ require 'activesupport'
10
10
  require 'logger'
11
11
 
12
12
  module AlsaBackup
13
- VERSION = '0.0.4'
13
+ VERSION = '0.0.5'
14
14
 
15
15
  def self.recorder
16
16
  @recorder ||= AlsaBackup::Recorder.new
@@ -9,14 +9,6 @@ end
9
9
 
10
10
  class File
11
11
 
12
- def self.extension(file)
13
- if file =~ /(\.[^.]*)$/
14
- $1
15
- else
16
- ""
17
- end
18
- end
19
-
20
12
  def self.suffix_basename(file, suffix)
21
13
  dirname = File.dirname(file)
22
14
 
@@ -28,7 +20,7 @@ class File
28
20
  dirname + "/"
29
21
  end
30
22
 
31
- extension = File.extension(file)
23
+ extension = File.extname(file)
32
24
  dirname +
33
25
  File.basename(file, extension) +
34
26
  suffix +
@@ -15,22 +15,37 @@ module AlsaBackup
15
15
  def start(seconds_to_record = nil)
16
16
  length_controller = self.length_controller(seconds_to_record)
17
17
 
18
- Writer.open(directory, file, format(:format => "wav pcm_16")) do |writer|
19
- ALSA::PCM::Capture.open("hw:0", self.format(:sample_format => :s16_le)) do |capture|
18
+ open_writer do |writer|
19
+ open_capture do |capture|
20
20
  capture.read do |buffer, frame_count|
21
- writer.write buffer, frame_count
21
+ writer.write buffer, frame_count*format[:channels]
22
22
  length_controller.continue_after? frame_count
23
23
  end
24
24
  end
25
25
  end
26
- rescue Interrupt
27
- AlsaBackup.logger.debug('recorder interrupted')
28
26
  rescue Exception => e
27
+ retry if handle_error(e, seconds_to_record.nil?)
28
+ end
29
+
30
+ def open_writer(&block)
31
+ Writer.open(directory, file, format(:format => "wav pcm_16"), &block)
32
+ end
33
+
34
+ def open_capture(&block)
35
+ ALSA::PCM::Capture.open("hw:0", self.format(:sample_format => :s16_le), &block)
36
+ end
37
+
38
+ def handle_error(e, try_to_continue = true)
39
+ if Interrupt === e
40
+ AlsaBackup.logger.debug('recorder interrupted')
41
+ return false
42
+ end
43
+
29
44
  AlsaBackup.logger.error(e)
30
45
  AlsaBackup.logger.debug { e.backtrace.join("\n") }
31
46
 
32
- if seconds_to_record.nil? and continue_on_error?(e)
33
- retry
47
+ if try_to_continue and continue_on_error?(e)
48
+ return true
34
49
  else
35
50
  raise e
36
51
  end
data/lib/sndfile.rb CHANGED
@@ -40,7 +40,7 @@ module Sndfile
40
40
  end
41
41
 
42
42
  def write(buffer, frame_count)
43
- ALSA.logger.debug('sndfile') { "write #{frame_count} frames in #{path}"}
43
+ ALSA.logger.debug { "write #{frame_count} frames in #{path}"}
44
44
  write_count = Sndfile::Native::write_int(@handle, buffer, frame_count)
45
45
 
46
46
  unless write_count == frame_count
@@ -15,11 +15,10 @@ describe AlsaBackup::CLI, "execute" do
15
15
  def execute_cli(options = {})
16
16
  options = { :file => @file, :length => 2 }.update(options)
17
17
  arguments = options.collect do |key,value|
18
- case value
19
- when true: "--#{key}"
20
- when nil: nil
21
- else
22
- "--#{key}=#{value}"
18
+ if value
19
+ returning "--#{key}" do |argument|
20
+ argument << "=#{value}" unless value == true
21
+ end
23
22
  end
24
23
  end.compact
25
24
 
@@ -71,7 +70,7 @@ describe AlsaBackup::CLI, "execute" do
71
70
  IO.read(pid_file).strip.should == $$.to_s
72
71
  end
73
72
 
74
- it "should write pid in specified file" do
73
+ it "should daemonize the process with option background" do
75
74
  Daemonize.should_receive(:daemonize)
76
75
  execute_cli :background => true
77
76
  end
@@ -14,22 +14,6 @@ describe Time do
14
14
  end
15
15
 
16
16
  describe File do
17
-
18
- describe "extension" do
19
-
20
- it "should be .rb for test.rb" do
21
- File.extension('test.rb').should == '.rb'
22
- end
23
-
24
- it "should be '.' for 'test.'" do
25
- File.extension('test.').should == '.'
26
- end
27
-
28
- it "should be blank for 'test'" do
29
- File.extension('test').should be_blank
30
- end
31
-
32
- end
33
17
 
34
18
  describe "suffix_basename" do
35
19
 
data/spec/spec_helper.rb CHANGED
@@ -10,7 +10,9 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
10
  require 'alsa_backup'
11
11
 
12
12
  def test_directory
13
- File.dirname(__FILE__) + '/../tmp'
13
+ directory = File.dirname(__FILE__) + '/../tmp'
14
+ Dir.mkdir(directory) unless File.exists?(directory)
15
+ directory
14
16
  end
15
17
 
16
18
  def test_file(name = 'test.wav')
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.4
4
+ version: 0.0.5
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-22 00:00:00 -07:00
12
+ date: 2009-05-27 00:00:00 -07:00
13
13
  default_executable: alsa.backup
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency