Class: WaveInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/waveinfo.rb,
lib/waveinfo/version.rb

Defined Under Namespace

Classes: FileFormatError

Constant Summary collapse

VERSION =
"0.0.5"

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ WaveInfo

Create a new WaveInfo object to get information and metadata about a Wave file (.wav). ‘file’ can either be a filename or an IO object.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/waveinfo.rb', line 13

def initialize(file)

  # Set default values
  @audio_format_id = 0
  @bits_per_sample = nil
  @block_align = nil
  @byte_rate = nil
  @channels = nil
  @data_size = nil
  @sample_rate = nil
  @samples = nil

  # What was passed in to us?
  if file.is_a?(String)
    @io = File.new(file, 'rb')
    @filepath = @io.path
    read_headers
    @io.close
  else
    @io = file
    @filepath = @io.path if @io.respond_to?(:path)
    read_headers
  end
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



7
8
9
# File 'lib/waveinfo.rb', line 7

def debug
  @debug
end

Instance Method Details

#audio_formatObject

Get the name of the audio codec (for example ‘PCM’).



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/waveinfo.rb', line 49

def audio_format
  case @audio_format_id
    when 0x01 then
      "PCM"
    when 0x02 then
      "Microsoft ADPCM"
    when 0x06 then
      "a-law"
    when 0x07 then
      "u-law"
    when 0x11 then
      "IMA ADPCM"
    when 0x14 then
      "G.723"
    when 0x31 then
      "GSM"
    when 0x40 then
      "G.721"
    when 0x50 then
      "MPEG-1 Audio"
    when 0x55 then
      "MPEG Audio Layer 3"
    when 0xFFFE
      "Extensible wave format"
    else
      sprintf("Unknown (0x%2.2x)",@audio_format_id)
  end
end

#audio_format_idObject

Get the identifier of the audio codec (for example PCM would be 1).



44
45
46
# File 'lib/waveinfo.rb', line 44

def audio_format_id
  @audio_format_id
end

#bits_per_sampleObject

Get the number of bits per sample.



99
100
101
# File 'lib/waveinfo.rb', line 99

def bits_per_sample
  @bits_per_sample
end

#block_alignObject

Get the number of bytes per sample slice.



94
95
96
# File 'lib/waveinfo.rb', line 94

def block_align
  @block_align
end

#byte_rateObject

Get the average number of bytes per second.



89
90
91
# File 'lib/waveinfo.rb', line 89

def byte_rate
  @byte_rate
end

#channelsObject

Get the number of channels.



79
80
81
# File 'lib/waveinfo.rb', line 79

def channels
  @channels
end

#durationObject

Get the duration of the audio (in seconds).



120
121
122
123
124
125
126
# File 'lib/waveinfo.rb', line 120

def duration
  if sample_rate.to_f.nonzero?
    sample_rate ? samples.to_f / sample_rate.to_f : 0.0
  else
    0.0
  end
end

#filenameObject

Return the name of the input file.



39
40
41
# File 'lib/waveinfo.rb', line 39

def filename
  File.basename(@filepath)
end

#sample_rateObject

Get the sample rate (in Hz).



84
85
86
# File 'lib/waveinfo.rb', line 84

def sample_rate
  @sample_rate
end

#samplesObject

Get the total number of samples.



104
105
106
107
108
109
110
111
112
# File 'lib/waveinfo.rb', line 104

def samples
  if @samples
    @samples
  elsif @block_align.to_f.nonzero?
    @data_size && @block_align ? @data_size / @block_align : 0.0
  else 
    0.0
  end
end

#sizeObject

Get the length of the audio data (in bytes).



115
116
117
# File 'lib/waveinfo.rb', line 115

def size
  @data_size
end