Class: WSK::Model::InputData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iFile, iHeader) ⇒ InputData

Constructor

Parameters
  • iFile (IO): The file descriptor. Don’t use it externally as long as it is used by this class.

  • iHeader (WSK::Model::Header): Corresponding file header



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

def initialize(iFile, iHeader)
  @File, @Header = iFile, iHeader
  @NbrSamples = nil
end

Instance Attribute Details

#HeaderObject (readonly)

Header of the input data

WSK::Model::Header


21
22
23
# File 'lib/WSK/Model/InputData.rb', line 21

def Header
  @Header
end

#NbrSamplesObject (readonly)

Number of samples in the data

Integer


17
18
19
# File 'lib/WSK/Model/InputData.rb', line 17

def NbrSamples
  @NbrSamples
end

Instance Method Details

#each(iIdxBeginSample = 0) ⇒ Object

Iterate through the samples

Parameters
  • iIdxBeginSample (Integer): Index of the first sample to begin with [optional = 0]

  • CodeBlock: The code called for each iteration:

    • iInputSampleData (list<Integer>): The list of values (1 per channel)



66
67
68
69
70
71
72
# File 'lib/WSK/Model/InputData.rb', line 66

def each(iIdxBeginSample = 0)
  each_buffer(iIdxBeginSample) do |iBuffer, iNbrSamples|
    iBuffer.size.times do |iIdxSample|
      yield(iBuffer[iIdxSample*@Header.NbrChannels..(iIdxSample+1)*@Header.NbrChannels-1])
    end
  end
end

#each_buffer(iIdxBeginSample = 0) ⇒ Object

Iterate through the buffers. This is far more efficient than iterating over samples.

Parameters
  • iIdxBeginSample (Integer): Index of the first sample to begin with [optional = 0]

  • CodeBlock: The code called for each iteration:

    • iInputBuffer (list<Integer>): The list of channel values

    • iNbrSamples (Integer): The number of samples in this buffer

    • iNbrChannels (Integer): The number of channels in this buffer



82
83
84
85
86
# File 'lib/WSK/Model/InputData.rb', line 82

def each_buffer(iIdxBeginSample = 0)
  @WaveReader.each_buffer(iIdxBeginSample) do |iBuffer, iNbrSamples|
    yield(iBuffer, iNbrSamples, @Header.NbrChannels)
  end
end

#each_raw_buffer(iIdxBeginSample = 0, iIdxLastSample = @NbrSamples-1, iOptions = {}) ⇒ Object

Iterate through the buffers in raw mode (strings read directly without unpacking). This is far more efficient than iterating over samples or unpacked buffers.

Parameters
  • iIdxBeginSample (Integer): Index of the first sample to begin with [optional = 0]

  • iIdxLastSample (Integer): Index of the last sample to end with [optional = @NbrSamples-1]

  • iOptions (map<Symbol,Object>): Additional options. See CachedBufferReader for documentation. [optional = {}]

  • CodeBlock: The code called for each iteration:

    • iInputRawBuffer (String): The raw buffer

    • iNbrSamples (Integer): The number of samples in this buffer

    • iNbrChannels (Integer): The number of channels in this buffer



139
140
141
142
143
# File 'lib/WSK/Model/InputData.rb', line 139

def each_raw_buffer(iIdxBeginSample = 0, iIdxLastSample = @NbrSamples-1, iOptions = {})
  @RawReader.each_buffer(iIdxBeginSample, iIdxLastSample, iOptions) do |iBuffer, iNbrSamples|
    yield(iBuffer, iNbrSamples, @Header.NbrChannels)
  end
end

#each_reverse_buffer(iIdxEndSample = @NbrSamples-1) ⇒ Object

Iterate through the buffers in the reverse order. This is far more efficient than iterating over samples.

Parameters
  • iIdxEndSample (Integer): Index of the first sample to begin with [optional = @NbrSamples-1]

  • CodeBlock: The code called for each iteration:

    • iInputBuffer (list<Integer>): The list of channel values

    • iNbrSamples (Integer): The number of samples in this buffer

    • iNbrChannels (Integer): The number of channels in this buffer



122
123
124
125
126
# File 'lib/WSK/Model/InputData.rb', line 122

def each_reverse_buffer(iIdxEndSample = @NbrSamples-1)
  @WaveReader.each_reverse_buffer(0, iIdxEndSample) do |iBuffer, iNbrSamples|
    yield(iBuffer, iNbrSamples, @Header.NbrChannels)
  end
end

#each_reverse_raw_buffer(iIdxBeginSample = 0, iIdxLastSample = @NbrSamples-1, iOptions = {}) ⇒ Object

Iterate through the buffers in the reverse order in raw mode (strings read directly without unpacking). This is far more efficient than iterating over samples or unpacked buffers.

Parameters
  • iIdxBeginSample (Integer): Index of the first sample to begin with [optional = 0]

  • iIdxLastSample (Integer): Index of the last sample to end with [optional = @NbrSamples-1]

  • iOptions (map<Symbol,Object>): Additional options. See CachedBufferReader for documentation. [optional = {}]

  • CodeBlock: The code called for each iteration:

    • iInputRawBuffer (String): The raw buffer

    • iNbrSamples (Integer): The number of samples in this buffer

    • iNbrChannels (Integer): The number of channels in this buffer



156
157
158
159
160
# File 'lib/WSK/Model/InputData.rb', line 156

def each_reverse_raw_buffer(iIdxBeginSample = 0, iIdxLastSample = @NbrSamples-1, iOptions = {})
  @RawReader.each_reverse_buffer(iIdxBeginSample, iIdxLastSample, iOptions) do |iBuffer, iNbrSamples|
    yield(iBuffer, iNbrSamples, @Header.NbrChannels)
  end
end

#get_current_bufferObject

Get the current buffer. !!! This must be called only if the buffer was previously initialized (call getSampleSata to do so).

Return
  • list<Integer>: The list of channel values

  • Integer: The number of samples in this buffer

  • Integer: The number of channels in this buffer



95
96
97
98
99
# File 'lib/WSK/Model/InputData.rb', line 95

def get_current_buffer
  rBuffer, lIdxStartSample, lIdxEndSample = @WaveReader.get_current_buffer

  return rBuffer, lIdxEndSample-lIdxStartSample+1, @Header.NbrChannels
end

#get_current_raw_bufferObject

Get the current raw buffer. !!! This must be called only if the buffer was previously initialized (call each_raw_buffer to do so).

Return
  • String: The raw buffer

  • Integer: The number of samples in this buffer

  • Integer: The number of channels in this buffer



108
109
110
111
112
# File 'lib/WSK/Model/InputData.rb', line 108

def get_current_raw_buffer
  rBuffer, lIdxStartSample, lIdxEndSample = @RawReader.get_current_buffer

  return rBuffer, lIdxEndSample-lIdxStartSample+1, @Header.NbrChannels
end

#get_sample_data(iIdxSample, iOptions = {}) ⇒ Object

Get a sample’s data

Parameters
  • iIdxSample (Integer): Index of the sample to retrieve

  • iOptions (map<Symbol,Object>): Additional options [optional = {}]

    • :reverse_buffer (Boolean): Do we load the previous buffer containing this sample if needed ? [optional = false]

Return
  • list<Integer>: The list of values (1 per channel), or nil in case of error



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/WSK/Model/InputData.rb', line 170

def get_sample_data(iIdxSample, iOptions = {})
  rSampleData = nil

  lReverseBuffer = (iOptions[:reverse_buffer] != nil) ? iOptions[:reverse_buffer] : false
  if (lReverseBuffer)
    @WaveReader.each_reverse_buffer(0, iIdxSample) do |iBuffer, iNbrSamples|
      rSampleData = iBuffer[-@Header.NbrChannels..-1]
      break
    end
  else
    @WaveReader.each_buffer(iIdxSample) do |iBuffer, iNbrSamples|
      rSampleData = iBuffer[0..@Header.NbrChannels-1]
      break
    end
  end

  return rSampleData
end

#init_cursorObject

Check that data seems coherent, and initialize the cursor

Return
  • Exception: Error, or nil in case of success



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

def init_cursor
  rError = nil

  # Size of a sample
  # Integer
  lSampleSize = (@Header.NbrChannels*@Header.NbrBitsPerSample)/8
  # Read the size of the data
  rError, lDataSize = RIFFReader.new(@File).setFilePos('data')
  if (rError == nil)
    # Check that the data size is coherent
    if (lDataSize % lSampleSize == 0)
      @NbrSamples = lDataSize / lSampleSize
      @RawReader = RawReader.new(@File, @File.pos, lSampleSize, @NbrSamples)
      @WaveReader = WaveReader.new(@RawReader, @Header)
      log_debug "Number of samples: #{@NbrSamples}"
    else
      rError = RuntimeError.new("Data size (#{lDataSize} should be a multiple of #{lSampleSize} according to header.")
    end
  end

  return rError
end