Class: RubyAudio::SoundInfo

Inherits:
CSoundInfo show all
Defined in:
lib/ruby-audio/sound_info.rb

Overview

Class SoundInfo provides information about open sound files’ format, length, samplerate, channels, and other things.

Example:

snd = RubyAudio::Sound.open("snd.wav")
puts snd.info.channels                  #=> 2
puts snd.info.samplerate                #=> 48000
snd.close

In addition it can be used to specify the format of new sound files:

info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
snd = RubyAudio::Sound.open("new.wav", 'w', info)

Instance Method Summary collapse

Methods inherited from CSoundInfo

#channels, #channels=, #format, #format=, #frames, #samplerate, #samplerate=, #sections, #seekable, #valid?

Constructor Details

#initialize(options = {}) ⇒ SoundInfo

Creates a new SoundInfo object and populates it using the given data

Example:

info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16


20
21
22
23
24
25
# File 'lib/ruby-audio/sound_info.rb', line 20

def initialize options={}
  # Populate from options if given
  unless options.empty?
    options.each {|key,value| send("#{key}=", value)}
  end
end

Instance Method Details

#cloneObject

Returns a new SoundInfo object that has the same channel count, sample rate, and format. This is useful in creating a new sound with the same format as an already existing sound.

Example:

snd1 = RubyAudio::Sound.open("snd.wav")
snd2 = RubyAudio::Sound.open("snd2.wav", 'w', snd1.info.clone)


34
35
36
# File 'lib/ruby-audio/sound_info.rb', line 34

def clone
  SoundInfo.new(:channels => channels, :samplerate => samplerate, :format => format)
end

#lengthObject

Returns the length of the audio file in seconds



63
64
65
# File 'lib/ruby-audio/sound_info.rb', line 63

def length
  frames / samplerate.to_f
end

#main_formatObject

Returns the main format constant as a string

Example:

info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
info.main_format
#=> "FORMAT_WAV"


46
47
48
49
# File 'lib/ruby-audio/sound_info.rb', line 46

def main_format
  calculate_format if @main_format.nil?
  @main_format
end

#sub_formatObject

Returns the sub format constant as a string

Example:

info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
info.sub_format
#=> "FORMAT_PCM_16"


57
58
59
60
# File 'lib/ruby-audio/sound_info.rb', line 57

def sub_format
  calculate_format if @sub_format.nil?
  @sub_format
end