Class: WaveInfo
- Inherits:
-
Object
- Object
- WaveInfo
- Defined in:
- lib/waveinfo.rb,
lib/waveinfo/version.rb
Constant Summary
- VERSION =
"0.0.2"
Instance Method Summary (collapse)
-
- (Object) audio_format
Get the name of the audio codec (for example 'PCM').
-
- (Object) audio_format_id
Get the identifier of the audio codec (for example PCM would be 1).
-
- (Object) bits_per_sample
Get the number of bits per sample.
-
- (Object) block_align
Get the number of bytes per sample slice.
-
- (Object) byte_rate
Get the average number of bytes per second.
-
- (Object) channels
Get the number of channels.
-
- (Object) duration
Get the duration of the audio (in seconds).
-
- (Object) filename
Return the name of the input file.
-
- (WaveInfo) initialize(file)
constructor
Create a new WaveInfo object to get information and metadata about a Wave file (.wav).
-
- (Object) sample_rate
Get the sample rate (in Hz).
-
- (Object) samples
Get the total number of samples.
-
- (Object) size
Get the length of the audio data (in bytes).
Constructor Details
- (WaveInfo) initialize(file)
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
- (Object) audio_format
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 sprintf("Unknown (0x%2.2x)",@audio_format_id) end end |
- (Object) audio_format_id
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 |
- (Object) bits_per_sample
Get the number of bits per sample.
91 92 93 |
# File 'lib/waveinfo.rb', line 91 def bits_per_sample @bits_per_sample end |
- (Object) block_align
Get the number of bytes per sample slice.
86 87 88 |
# File 'lib/waveinfo.rb', line 86 def block_align @block_align end |
- (Object) byte_rate
Get the average number of bytes per second.
81 82 83 |
# File 'lib/waveinfo.rb', line 81 def byte_rate @byte_rate end |
- (Object) channels
Get the number of channels.
71 72 73 |
# File 'lib/waveinfo.rb', line 71 def channels @channels end |
- (Object) duration
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 |
- (Object) filename
Return the name of the input file.
33 34 35 |
# File 'lib/waveinfo.rb', line 33 def filename File.basename(@filepath) end |
- (Object) sample_rate
Get the sample rate (in Hz).
76 77 78 |
# File 'lib/waveinfo.rb', line 76 def sample_rate @sample_rate end |
- (Object) samples
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 |
- (Object) size
Get the length of the audio data (in bytes).
105 106 107 |
# File 'lib/waveinfo.rb', line 105 def size @data_size end |