Class: WavFile
- Inherits:
-
Object
- Object
- WavFile
- Defined in:
- lib/wav.rb
Overview
WAV file parser
Copyright (C) 2014 Hiroshi Kuwagata <[email protected]>
Instance Attribute Summary collapse
-
#block_size ⇒ Object
readonly
Returns the value of attribute block_size.
-
#bytes_per_sec ⇒ Object
readonly
Returns the value of attribute bytes_per_sec.
-
#channel_num ⇒ Object
readonly
Returns the value of attribute channel_num.
-
#data_size ⇒ Object
readonly
Returns the value of attribute data_size.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
-
#sample_size ⇒ Object
readonly
Returns the value of attribute sample_size.
Instance Method Summary collapse
- #close ⇒ Object
- #eof? ⇒ Boolean
-
#initialize(file) ⇒ WavFile
constructor
A new instance of WavFile.
- #read(n = nil) ⇒ Object
- #seek(pos) ⇒ Object
Constructor Details
#initialize(file) ⇒ WavFile
Returns a new instance of WavFile.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wav.rb', line 15 def initialize(file) @io = File.open(file, "rb") tmp = @io.read(12).unpack('a4Va4') @magic = tmp[0] @data_size = tmp[1] @type = tmp[2] if not (@magic == "RIFF" and @type == "WAVE") raise('Illeagal magic number.') end if @data_size != (File.size(file) - 8) raise('Data size is not match.') end tmp = @io.read(8).unpack('a4V') @id = tmp[0] if @id != 'fmt ' raise('ID "%s" is not supported.' % @id) end tmp = @io.read(tmp[1]).unpack('vvVVvv') @format_id = tmp[0] @channel_num = tmp[1] @sample_rate = tmp[2] @bytes_per_sec = tmp[3] @block_size = tmp[4] @sample_size = tmp[5] loop { tmp = @io.read(8).unpack('a4V') break if tmp[0] == "data" @io.seek(tmp[1], IO::SEEK_CUR) } @data_offset = @io.pos @data_size = tmp[1] end |
Instance Attribute Details
#block_size ⇒ Object (readonly)
Returns the value of attribute block_size.
60 61 62 |
# File 'lib/wav.rb', line 60 def block_size @block_size end |
#bytes_per_sec ⇒ Object (readonly)
Returns the value of attribute bytes_per_sec.
60 61 62 |
# File 'lib/wav.rb', line 60 def bytes_per_sec @bytes_per_sec end |
#channel_num ⇒ Object (readonly)
Returns the value of attribute channel_num.
60 61 62 |
# File 'lib/wav.rb', line 60 def channel_num @channel_num end |
#data_size ⇒ Object (readonly)
Returns the value of attribute data_size.
60 61 62 |
# File 'lib/wav.rb', line 60 def data_size @data_size end |
#sample_rate ⇒ Object (readonly)
Returns the value of attribute sample_rate.
60 61 62 |
# File 'lib/wav.rb', line 60 def sample_rate @sample_rate end |
#sample_size ⇒ Object (readonly)
Returns the value of attribute sample_size.
60 61 62 |
# File 'lib/wav.rb', line 60 def sample_size @sample_size end |
Instance Method Details
#close ⇒ Object
79 80 81 |
# File 'lib/wav.rb', line 79 def close @io.close end |
#eof? ⇒ Boolean
75 76 77 |
# File 'lib/wav.rb', line 75 def eof? @io.eof? end |
#read(n = nil) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/wav.rb', line 63 def read(n = nil) if not n.nil? @io.read(@block_size * n) else @io.read end end |
#seek(pos) ⇒ Object
71 72 73 |
# File 'lib/wav.rb', line 71 def seek(pos) @io.seek(@block_size * pos, IO::SEEK_SET) end |