Class: Zappa::Wave
- Inherits:
-
Object
- Object
- Zappa::Wave
- Defined in:
- lib/zappa/wave.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#header ⇒ Object
Returns the value of attribute header.
-
#wave_data ⇒ Object
Returns the value of attribute wave_data.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #data_size ⇒ Object
- #frame_size ⇒ Object
-
#initialize ⇒ Wave
constructor
A new instance of Wave.
- #pack ⇒ Object
-
#path_to(source) ⇒ Object
Private method?.
- #sample_count ⇒ Object
- #samples ⇒ Object
- #set_samples(samples) ⇒ Object
- #unpack(source) ⇒ Object
Constructor Details
Instance Attribute Details
#format ⇒ Object
Returns the value of attribute format.
10 11 12 |
# File 'lib/zappa/wave.rb', line 10 def format @format end |
#header ⇒ Object
Returns the value of attribute header.
10 11 12 |
# File 'lib/zappa/wave.rb', line 10 def header @header end |
#wave_data ⇒ Object
Returns the value of attribute wave_data.
10 11 12 |
# File 'lib/zappa/wave.rb', line 10 def wave_data @wave_data end |
Instance Method Details
#==(other) ⇒ Object
34 35 36 |
# File 'lib/zappa/wave.rb', line 34 def ==(other) other.wave_data == wave_data end |
#data_size ⇒ Object
22 23 24 |
# File 'lib/zappa/wave.rb', line 22 def data_size @wave_data.chunk_size end |
#frame_size ⇒ Object
26 27 28 |
# File 'lib/zappa/wave.rb', line 26 def frame_size @format.bits_per_sample * @format.channels / 8 end |
#pack ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/zappa/wave.rb', line 38 def pack pack = @header.pack + @format.pack pack += @wave_data.chunk_id pack += [@wave_data.chunk_size].pack('V') pack += pack_samples(@wave_data.samples) pack end |
#path_to(source) ⇒ Object
Private method?
70 71 72 73 74 |
# File 'lib/zappa/wave.rb', line 70 def path_to(source) # Private method? return source if source.class == String return source.path if source.class == File fail 'cannot unpack type: ' + source.class.to_s end |
#sample_count ⇒ Object
30 31 32 |
# File 'lib/zappa/wave.rb', line 30 def sample_count data_size / frame_size end |
#samples ⇒ Object
18 19 20 |
# File 'lib/zappa/wave.rb', line 18 def samples @wave_data.samples end |
#set_samples(samples) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/zappa/wave.rb', line 63 def set_samples(samples) samples_change = (samples.size - @wave_data.samples.size) size_change = samples_change * @format.channels * 2 @header.chunk_size += size_change @wave_data.set_samples(samples) end |
#unpack(source) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/zappa/wave.rb', line 46 def unpack(source) file = File.open(path_to(source), 'rb') rescue raise 'Unable to open WAV file' else @header = RiffHeader.new(file) @format = Format.new(file) while sc_header = file.read(8) s = SubChunkHeader.new(sc_header) if s.chunk_id == 'data' unpack_samples(file) else file.read(s.chunk_size) end end end |