Class: WaveFile::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefile/format.rb

Overview

Public: Represents information about the data format for a Wave file, such as number of channels, bits per sample, sample rate, and so forth. A Format instance is used by Reader to indicate what format to read samples out as, and by Writer to indicate what format to write samples as.

This class is immutable - once a new Format is constructed, it can’t be modified.

Direct Known Subclasses

UnvalidatedFormat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channels, format_code, sample_rate) ⇒ Format

Public: Constructs a new immutable Format.

channels - The number of channels in the format. Can either be an Integer

(e.g. 1, 2, 3) or the symbols :mono (equivalent to 1) or
:stereo (equivalent to 2).

format_code - A symbol indicating the format of each sample. Consists of

two parts: a format code, and the bits per sample. The valid
values are :pcm_8, :pcm_16, :pcm_24, :pcm_32, :float_32,
:float_64, and :float (equivalent to :float_32)

sample_rate - The number of samples per second, such as 44100

Examples

format = Format.new(1, :pcm_16, 44100)
format = Format.new(:mono, :pcm_16, 44100)  # Equivalent to above

format = Format.new(:stereo, :float_32, 44100)
format = Format.new(:stereo, :float, 44100)  # Equivalent to above


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wavefile/format.rb', line 41

def initialize(channels, format_code, sample_rate)
  channels = normalize_channels(channels)
  sample_format, bits_per_sample = normalize_format_code(format_code)
  validate_channels(channels)
  validate_sample_format(sample_format)
  validate_bits_per_sample(sample_format, bits_per_sample)
  validate_sample_rate(sample_rate)

  @channels = channels
  @sample_format = sample_format
  @bits_per_sample = bits_per_sample
  @sample_rate = sample_rate
  @block_align = (@bits_per_sample / 8) * @channels
  @byte_rate = @block_align * @sample_rate
end

Instance Attribute Details

#bits_per_sampleObject (readonly)

Public: Returns the number of bits per sample, such as 8, 16, 24, 32, or 64.



76
77
78
# File 'lib/wavefile/format.rb', line 76

def bits_per_sample
  @bits_per_sample
end

#block_alignObject (readonly)

Public: Returns the number of bytes in each sample frame. For example, in a 16-bit stereo file, this will be 4 (2 bytes for each 16-bit sample, times 2 channels).



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

def block_align
  @block_align
end

#byte_rateObject (readonly)

Public: Returns the number of bytes contained in 1 second of sample data. Is equivalent to block_align * sample_rate.



87
88
89
# File 'lib/wavefile/format.rb', line 87

def byte_rate
  @byte_rate
end

#channelsObject (readonly)

Public: Returns the number of channels, such as 1 or 2. This will always return a Integer, even if the number of channels is specified with a symbol (e.g. :mono) in the constructor.



70
71
72
# File 'lib/wavefile/format.rb', line 70

def channels
  @channels
end

#sample_formatObject (readonly)

Public: Returns a symbol indicating the sample format, such as :pcm or :float



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

def sample_format
  @sample_format
end

#sample_rateObject (readonly)

Public: Returns the number of samples per second, such as 44100.



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

def sample_rate
  @sample_rate
end

Instance Method Details

#mono?Boolean

Public: Returns true if the format has 1 channel, false otherwise.

Returns:

  • (Boolean)


58
59
60
# File 'lib/wavefile/format.rb', line 58

def mono?
  @channels == 1
end

#stereo?Boolean

Public: Returns true if the format has 2 channels, false otherwise.

Returns:

  • (Boolean)


63
64
65
# File 'lib/wavefile/format.rb', line 63

def stereo?
  @channels == 2
end