Class: AlsaBackup::Writer
- Inherits:
-
Object
- Object
- AlsaBackup::Writer
- Defined in:
- lib/alsa_backup/writer.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#file ⇒ Object
Returns the value of attribute file.
-
#format ⇒ Object
Returns the value of attribute format.
-
#on_close_callbacks ⇒ Object
Returns the value of attribute on_close_callbacks.
Class Method Summary collapse
- .default_format ⇒ Object
- .delete_empty_file(file) ⇒ Object
- .open(options, &block) ⇒ Object
- .rename_existing_file(file) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #close_file ⇒ Object
-
#initialize(options = {}) ⇒ Writer
constructor
A new instance of Writer.
- #on_close(file) ⇒ Object
- #prepare ⇒ Object
- #sndfile ⇒ Object
- #target_file ⇒ Object
- #write(*arguments) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Writer
Returns a new instance of Writer.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/alsa_backup/writer.rb', line 13 def initialize( = {}) = { :format => Writer.default_format }.update() @directory = [:directory] @file = [:file] @format = [:format] @on_close_callbacks = [] @on_close_callbacks << [:on_close] if [:on_close] end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
7 8 9 |
# File 'lib/alsa_backup/writer.rb', line 7 def directory @directory end |
#file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/alsa_backup/writer.rb', line 7 def file @file end |
#format ⇒ Object
Returns the value of attribute format.
7 8 9 |
# File 'lib/alsa_backup/writer.rb', line 7 def format @format end |
#on_close_callbacks ⇒ Object
Returns the value of attribute on_close_callbacks.
7 8 9 |
# File 'lib/alsa_backup/writer.rb', line 7 def on_close_callbacks @on_close_callbacks end |
Class Method Details
.default_format ⇒ Object
9 10 11 |
# File 'lib/alsa_backup/writer.rb', line 9 def self.default_format {:sample_rate => 44100, :channels => 2, :format => "wav pcm_16"} end |
.delete_empty_file(file) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/alsa_backup/writer.rb', line 102 def self.delete_empty_file(file) if File.exists?(file) and File.size(file) <= 44 AlsaBackup.logger.warn("remove empty file #{file}") File.delete file end end |
.open(options, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/alsa_backup/writer.rb', line 26 def self.open(, &block) writer = Writer.new() begin writer.prepare yield writer ensure writer.close end end |
.rename_existing_file(file) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/alsa_backup/writer.rb', line 109 def self.rename_existing_file(file) if File.exists?(file) index = 1 while File.exists?(new_file = File.suffix_basename(file, "-#{index}")) index += 1 raise "can't find a free file for #{file}" if index > 1000 end AlsaBackup.logger.warn "rename existing file #{File.basename(file)} into #{new_file}" File.rename(file, new_file) end end |
Instance Method Details
#close ⇒ Object
46 47 48 |
# File 'lib/alsa_backup/writer.rb', line 46 def close close_file end |
#close_file ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/alsa_backup/writer.rb', line 50 def close_file if @sndfile on_close(@sndfile.path) @sndfile.close end @sndfile = nil end |
#on_close(file) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/alsa_backup/writer.rb', line 58 def on_close(file) AlsaBackup.logger.info('close current file') return if Writer.delete_empty_file(file) AlsaBackup.logger.debug("invoke #{@on_close_callbacks.size} callback(s)") @on_close_callbacks.each do |callback| begin callback.call(file) rescue Exception => e AlsaBackup.logger.error("error in on_close callback : #{e}") AlsaBackup.logger.debug { e.backtrace.join("\n") } end end end |
#prepare ⇒ Object
36 37 38 39 40 |
# File 'lib/alsa_backup/writer.rb', line 36 def prepare # prepare sndfile self.sndfile self end |
#sndfile ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/alsa_backup/writer.rb', line 86 def sndfile target_file = self.target_file raise "no recording file" unless target_file unless @sndfile and @sndfile.path == target_file close_file Writer.rename_existing_file(target_file) AlsaBackup.logger.info{"new file #{File. target_file}"} FileUtils.mkdir_p File.dirname(target_file) @sndfile = Sndfile::File.new(target_file, "w", self.format) end @sndfile end |
#target_file ⇒ Object
82 83 84 |
# File 'lib/alsa_backup/writer.rb', line 82 def target_file File.join self.directory, self.file end |
#write(*arguments) ⇒ Object
42 43 44 |
# File 'lib/alsa_backup/writer.rb', line 42 def write(*arguments) self.sndfile.write *arguments end |