Class: WSK::Model::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/WSK/Model/Header.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iAudioFormat, iNbrChannels, iSampleRate, iNbrBitsPerSample) ⇒ Header

Constructor

Parameters
  • iAudioFormat (Integer): Audio format

  • iNbrChannels (Integer): Number of channels

  • iSampleRate (Integer): Sample rate

  • iNbrBitsPerSample (Integer): Number of bits per channel’s sample



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/WSK/Model/Header.rb', line 37

def initialize(iAudioFormat, iNbrChannels, iSampleRate, iNbrBitsPerSample)
  @AudioFormat, @NbrChannels, @SampleRate, @NbrBitsPerSample = iAudioFormat, iNbrChannels, iSampleRate, iNbrBitsPerSample
  # The pack/unpack formula to use
  lStrChannelPackFormula = ''
  case @NbrBitsPerSample
  when 8
    lStrChannelPackFormula = 'C'
  when 16
    lStrChannelPackFormula = 's'
  when 24
    lStrChannelPackFormula = 'Cs'
  when 32
    lStrChannelPackFormula = 'l'
  else
    raise RuntimeError.new("#{@NbrBitsPerSample} bits PCM data not supported.")
  end
  @StrSamplePackFormula = lStrChannelPackFormula*@NbrChannels
end

Instance Attribute Details

#AudioFormatObject (readonly)

Audio format PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression.

Integer


16
17
18
# File 'lib/WSK/Model/Header.rb', line 16

def AudioFormat
  @AudioFormat
end

#NbrBitsPerSampleObject (readonly)

Bits per sample (per channel)

Integer


28
29
30
# File 'lib/WSK/Model/Header.rb', line 28

def NbrBitsPerSample
  @NbrBitsPerSample
end

#NbrChannelsObject (readonly)

Number of channels

Integer


20
21
22
# File 'lib/WSK/Model/Header.rb', line 20

def NbrChannels
  @NbrChannels
end

#SampleRateObject (readonly)

Sample Rate

Integer


24
25
26
# File 'lib/WSK/Model/Header.rb', line 24

def SampleRate
  @SampleRate
end

Instance Method Details

#==(iOther) ⇒ Object

Compare with a different object

Parameters
  • iOther (Object): Another object

Return
  • Boolean: Are the objects equal ?



119
120
121
122
123
124
125
126
127
# File 'lib/WSK/Model/Header.rb', line 119

def ==(iOther)
  return ((iOther.object_id == self.object_id) or
          ((iOther.is_a?(WSK::Model::Header)) and
           (iOther.AudioFormat == @AudioFormat) and
           (iOther.NbrChannels == @NbrChannels) and
           (iOther.SampleRate == @SampleRate) and
           (iOther.NbrBitsPerSample == @NbrBitsPerSample)))

end

#getDecodedSamples(iEncodedString, iNbrSamplesToDecode) ⇒ Object

Get decoded samples from an encoded PCM string.

Parameters
  • iEncodedString (String): The encoded string

  • iNbrSamplesToDecode (String): Number of samples to decode

Return
  • list<Integer>: The list of samples (there will be iNbrSamplesToDecode*@NbrChannels values)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/WSK/Model/Header.rb', line 63

def getDecodedSamples(iEncodedString, iNbrSamplesToDecode)
  rSamples = iEncodedString.unpack(@StrSamplePackFormula*iNbrSamplesToDecode)

  if (@NbrBitsPerSample == 8)
    # Values are read unsigned. Shift them.
    rSamples.map! do |iChannelValue|
      next iChannelValue-128
    end
  elsif (@NbrBitsPerSample == 24)
    # Each channel value has been decoded into 2 integers. We have to sum them.
    lRealSamples = []
    (iNbrSamplesToDecode*@NbrChannels).times do |iIdxChannelSample|
      lIdxSamples = iIdxChannelSample*2
      lRealSamples[iIdxChannelSample] = rSamples[lIdxSamples] + rSamples[lIdxSamples+1] * 256
    end
    rSamples = lRealSamples
  end

  return rSamples
end

#getEncodedString(iChannelSamples) ⇒ Object

Get encoded PCM string from decoded samples

Parameters
  • iChannelSamples (list<Integer>): The list of samples to encode

Return
  • String: Encoded PCM samples



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/WSK/Model/Header.rb', line 90

def getEncodedString(iChannelSamples)
  lRealChannelSamples = nil
  if (@NbrBitsPerSample == 8)
    # Values have to be stored unsigned. Shift them.
    lRealChannelSamples = []
    iChannelSamples.each do |iChannelValue|
      lRealChannelSamples << iChannelValue+128
    end
  elsif (@NbrBitsPerSample == 24)
    # Each channel must be split into 2 integer values before encoding.
    lRealChannelSamples = []
    iChannelSamples.size.times do |iIdxChannelSample|
      lIdxReal = iIdxChannelSample*2
      lRealChannelSamples[lIdxReal] = iChannelSamples[iIdxChannelSample] & 255
      lRealChannelSamples[lIdxReal+1] = iChannelSamples[iIdxChannelSample] / 256
    end
  else
    lRealChannelSamples = iChannelSamples
  end

  return lRealChannelSamples.pack(@StrSamplePackFormula*(iChannelSamples.size/@NbrChannels))
end