Class: WaveFile::CachingWriter
- Inherits:
-
Writer
- Object
- Writer
- WaveFile::CachingWriter
- Defined in:
- lib/wavefile/cachingwriter.rb
Overview
Implementation of Writer that caches the raw wave data for each buffer that it has written. If the Buffer is written again, it will write the version from cache instead of re-doing a String.pack() call.
Instance Method Summary collapse
-
#initialize(file_name, format) ⇒ CachingWriter
constructor
A new instance of CachingWriter.
- #write(buffer) ⇒ Object
Constructor Details
#initialize(file_name, format) ⇒ CachingWriter
Returns a new instance of CachingWriter.
6 7 8 9 10 |
# File 'lib/wavefile/cachingwriter.rb', line 6 def initialize(file_name, format) super @buffer_cache = {} end |
Instance Method Details
#write(buffer) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wavefile/cachingwriter.rb', line 12 def write(buffer) packed_buffer_data = {} key = buffer.hash if @buffer_cache.member?(key) packed_buffer_data = @buffer_cache[key] else samples = buffer.convert(@format).samples if @format.channels > 1 data = samples.flatten.pack(@pack_code) else # Flattening an already flat array is a waste of time. data = samples.pack(@pack_code) end packed_buffer_data = { :data => data, :sample_count => samples.length } @buffer_cache[key] = packed_buffer_data end @file.syswrite(packed_buffer_data[:data]) @total_sample_frames += packed_buffer_data[:sample_count] end |