Class: WaveInfo
- Inherits:
-
Object
- Object
- WaveInfo
- Defined in:
- lib/waveinfo.rb
Defined Under Namespace
Classes: FileFormatError
Instance Method Summary collapse
-
#audio_format ⇒ Object
Get the name of the audio codec (for example ‘PCM’).
-
#audio_format_id ⇒ Object
Get the identifier of the audio codec (for example PCM would be 1).
-
#bits_per_sample ⇒ Object
Get the number of bits per sample.
-
#block_align ⇒ Object
Get the number of bytes per sample slice.
-
#byte_rate ⇒ Object
Get the average number of bytes per second.
-
#channels ⇒ Object
Get the number of channels.
-
#duration ⇒ Object
Get the duration of the audio (in seconds).
-
#filename ⇒ Object
Return the name of the input file.
-
#initialize(file) ⇒ WaveInfo
constructor
Create a new WaveInfo object to get information and metadata about a Wave file (.wav).
-
#sample_rate ⇒ Object
Get the sample rate (in Hz).
-
#samples ⇒ Object
Get the total number of samples.
-
#size ⇒ Object
Get the length of the audio data (in bytes).
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 read_headers end end |
Instance Method Details
#audio_format ⇒ Object
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 |
# 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" else "Unknown (#{@audio_format_id})" end end |
#audio_format_id ⇒ Object
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_sample ⇒ Object
Get the number of bits per sample.
91 92 93 |
# File 'lib/waveinfo.rb', line 91 def bits_per_sample @bits_per_sample end |
#block_align ⇒ Object
Get the number of bytes per sample slice.
86 87 88 |
# File 'lib/waveinfo.rb', line 86 def block_align @block_align end |
#byte_rate ⇒ Object
Get the average number of bytes per second.
81 82 83 |
# File 'lib/waveinfo.rb', line 81 def byte_rate @byte_rate end |
#channels ⇒ Object
Get the number of channels.
71 72 73 |
# File 'lib/waveinfo.rb', line 71 def channels @channels end |
#duration ⇒ Object
Get the duration of the audio (in seconds).
110 111 112 |
# File 'lib/waveinfo.rb', line 110 def duration samples.to_f / sample_rate.to_f end |
#filename ⇒ Object
Return the name of the input file.
33 34 35 |
# File 'lib/waveinfo.rb', line 33 def filename File.basename(@filepath) end |
#sample_rate ⇒ Object
Get the sample rate (in Hz).
76 77 78 |
# File 'lib/waveinfo.rb', line 76 def sample_rate @sample_rate end |
#samples ⇒ Object
Get the total number of samples.
96 97 98 99 100 101 102 |
# File 'lib/waveinfo.rb', line 96 def samples if @samples @samples else @data_size / @block_align end end |
#size ⇒ Object
Get the length of the audio data (in bytes).
105 106 107 |
# File 'lib/waveinfo.rb', line 105 def size @data_size end |