Class: NuWav::DataChunk

Inherits:
Chunk
  • Object
show all
Defined in:
lib/nu_wav/chunk.rb

Instance Attribute Summary collapse

Attributes inherited from Chunk

#id, #pad_byte, #raw, #size

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chunk

#parse, #read_char, #read_dword, #read_word, #write_char, #write_dword, #write_word

Constructor Details

#initialize(id = nil, size = nil, tmp_data_file = nil) ⇒ DataChunk

Returns a new instance of DataChunk.



301
302
303
# File 'lib/nu_wav/chunk.rb', line 301

def initialize(id=nil, size=nil, tmp_data_file=nil)
  @id, @size, @tmp_data_file = id, size, tmp_data_file
end

Instance Attribute Details

#tmp_data_fileObject

Returns the value of attribute tmp_data_file.



273
274
275
# File 'lib/nu_wav/chunk.rb', line 273

def tmp_data_file
  @tmp_data_file
end

Class Method Details

.new_from_file(file) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/nu_wav/chunk.rb', line 293

def self.new_from_file(file)
  tmp_data = NuWav.temp_file('data_chunk', true)
  tmp_data.binmode
  FileUtils.cp(file.path, tmp_data.path)
  tmp_data.rewind
  self.new('data', File.size(tmp_data.path).to_s, tmp_data)
end

.parse(id, size, file) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/nu_wav/chunk.rb', line 275

def self.parse(id, size, file)

  # tmp_data = File.open('./data_chunk.mp2', 'wb')
  tmp_data = NuWav.temp_file('data_chunk', true)
  tmp_data.binmode
  
  remaining = size
  while (remaining > 0 && !file.eof?)
    read_bytes = [128, remaining].min
    tmp_data << file.read(read_bytes)
    remaining -= read_bytes
  end
  tmp_data.rewind
  chunk = self.new(id, size, tmp_data)

  return chunk
end

Instance Method Details

#dataObject



305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/nu_wav/chunk.rb', line 305

def data
  f = ''
  if self.tmp_data_file
    NuWav::WaveFile.log "we have a tmp_data_file!"
    self.tmp_data_file.rewind
    f = self.tmp_data_file.read
    self.tmp_data_file.rewind
  else
    NuWav::WaveFile.log "we have NO tmp_data_file!"
  end
  f
end

#to_binary(options = {}) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/nu_wav/chunk.rb', line 322

def to_binary(options={})
  NuWav::WaveFile.log "data chunk to_binary"
  d = self.data
  NuWav::WaveFile.log "got data size = #{d.size} #{d[0,10]}"
  out = "data" + write_dword(d.size) + d
  if d.size.odd? && !options[:no_pad_byte]
    NuWav::WaveFile.log "odd, adding a pad byte"
    out += "\0"
  end
  out
end

#to_sObject



318
319
320
# File 'lib/nu_wav/chunk.rb', line 318

def to_s
  "<chunk type:data (size:#{data.size})/>"
end