Class: WaveInfo

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

Defined Under Namespace

Classes: FileFormatError

Constant Summary

VERSION =
"0.0.3"

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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/waveinfo.rb', line 7

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

Instance Method Details

#audio_formatObject

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/waveinfo.rb', line 43

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).



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

def audio_format_id
  @audio_format_id
end

#bits_per_sampleObject

Get the number of bits per sample.



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

def bits_per_sample
  @bits_per_sample
end

#block_alignObject

Get the number of bytes per sample slice.



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

def block_align
  @block_align
end

#byte_rateObject

Get the average number of bytes per second.



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

def byte_rate
  @byte_rate
end

#channelsObject

Get the number of channels.



73
74
75
# File 'lib/waveinfo.rb', line 73

def channels
  @channels
end

#durationObject

Get the duration of the audio (in seconds).



112
113
114
# File 'lib/waveinfo.rb', line 112

def duration
  samples.to_f / sample_rate.to_f
end

#filenameObject

Return the name of the input file.



33
34
35
# File 'lib/waveinfo.rb', line 33

def filename
  File.basename(@filepath)
end

#sample_rateObject

Get the sample rate (in Hz).



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

def sample_rate
  @sample_rate
end

#samplesObject

Get the total number of samples.



98
99
100
101
102
103
104
# File 'lib/waveinfo.rb', line 98

def samples
  if @samples
    @samples
  else
    @data_size / @block_align
  end
end

#sizeObject

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



107
108
109
# File 'lib/waveinfo.rb', line 107

def size
  @data_size
end