Class: WSK::Model::CachedBufferReader

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

Overview

Class to be inherited to define the following virtual methods:

  • read_buffer(iIdxStart, iIdxEnd) -> Buffer

  • extract_sub_buffer(iBuffer, iIdxStart, iIdxEnd) -> Buffer

  • get_nbr_samples_per_buffer -> Integer (number of samples in 1 buffer)

  • get_nbr_samples -> Integer (total number of samples)

Direct Known Subclasses

RawReader, WaveReader

Instance Method Summary collapse

Constructor Details

#initializeCachedBufferReader

Constructor



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/WSK/Model/CachedBufferReader.rb', line 18

def initialize
  @NbrSamples = get_nbr_samples
  @NbrSamplesPerBuffer = get_nbr_samples_per_buffer
  # The position of the first sample of the buffer
  # Integer
  @IdxStartBufferSample = nil
  # The position of the last sample of the buffer
  # Integer
  @IdxEndBufferSample = nil
  # The buffer itself
  @Buffer = nil
end

Instance Method Details

#each_buffer(iIdxStartSample = 0, iIdxEndSample = @NbrSamples-1, iOptions = {}) ⇒ Object

Iterate through the buffers.

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

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

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

    • :nbr_samples_prefetch (Integer): Specify a number of samples to effectively read if the data needs to be accessed. This number will always be minored by the number of samples to read and majored by the number of samples per buffer. [optional = 0]

  • CodeBlock: The code called for each iteration:

    • iBuffer (String): The buffer

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



41
42
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
69
70
71
72
# File 'lib/WSK/Model/CachedBufferReader.rb', line 41

def each_buffer(iIdxStartSample = 0, iIdxEndSample = @NbrSamples-1, iOptions = {})
  lNbrSamplesPrefetch = iOptions[:nbr_samples_prefetch]
  if (lNbrSamplesPrefetch == nil)
    lNbrSamplesPrefetch = 0
  end
  lIdxFirstSample = iIdxStartSample
  while (lIdxFirstSample <= iIdxEndSample)
    lIdxLastSample = lIdxFirstSample+@NbrSamplesPerBuffer
    if (lIdxLastSample > iIdxEndSample)
      lIdxLastSample = iIdxEndSample
    end
    # Compute the last sample to prefetch
    lIdxLastSamplePrefetch = lIdxFirstSample + lNbrSamplesPrefetch
    if (lIdxLastSamplePrefetch < lIdxLastSample)
      lIdxLastSamplePrefetch = lIdxLastSample
    elsif (lIdxLastSamplePrefetch > lIdxFirstSample+@NbrSamplesPerBuffer)
      lIdxLastSamplePrefetch = lIdxFirstSample+@NbrSamplesPerBuffer
    end
    prepare_buffer(lIdxFirstSample, lIdxLastSample, lIdxFirstSample, lIdxLastSamplePrefetch)
    # Check if we need to return a sub-copy of the buffer
    lBuffer = []
    if ((lIdxFirstSample == @IdxStartBufferSample) and
        (lIdxLastSample == @IdxEndBufferSample))
      lBuffer = @Buffer
    else
      lBuffer = extract_sub_buffer(@Buffer, lIdxFirstSample - @IdxStartBufferSample, lIdxLastSample - @IdxStartBufferSample)
    end
    # Call client code
    yield(lBuffer, lIdxLastSample - lIdxFirstSample + 1)
    lIdxFirstSample = lIdxLastSample+1
  end
end

#each_reverse_buffer(iIdxStartSample = 0, iIdxEndSample = @NbrSamples-1, iOptions = {}) ⇒ Object

Iterate through the buffers in reverse order.

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

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

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

    • :nbr_samples_prefetch (Integer): Specify a number of samples to effectively read if the data needs to be accessed. This number will always be minored by the number of samples to read and majored by the number of samples per buffer. [optional = 0]

  • CodeBlock: The code called for each iteration:

    • iBuffer (String): The buffer

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/WSK/Model/CachedBufferReader.rb', line 84

def each_reverse_buffer(iIdxStartSample = 0, iIdxEndSample = @NbrSamples-1, iOptions = {})
  lNbrSamplesPrefetch = iOptions[:nbr_samples_prefetch]
  if (lNbrSamplesPrefetch == nil)
    lNbrSamplesPrefetch = 0
  end
  lIdxLastSample = iIdxEndSample
  while (lIdxLastSample >= iIdxStartSample)
    lIdxFirstSample = lIdxLastSample-@NbrSamplesPerBuffer
    if (lIdxFirstSample < iIdxStartSample)
      lIdxFirstSample = iIdxStartSample
    end
    # Compute the first sample to prefetch
    lIdxFirstSamplePrefetch = lIdxLastSample - lNbrSamplesPrefetch
    if (lIdxFirstSamplePrefetch > lIdxFirstSample)
      lIdxFirstSamplePrefetch = lIdxFirstSample
    elsif (lIdxFirstSamplePrefetch < lIdxLastSample-@NbrSamplesPerBuffer)
      lIdxFirstSamplePrefetch = lIdxLastSample-@NbrSamplesPerBuffer
    end
    prepare_buffer(lIdxFirstSample, lIdxLastSample, lIdxFirstSamplePrefetch, lIdxLastSample)
    # Check if we need to return a sub-copy of the buffer
    lBuffer = []
    if ((lIdxFirstSample == @IdxStartBufferSample) and
        (lIdxLastSample == @IdxEndBufferSample))
      lBuffer = @Buffer
    else
      lBuffer = extract_sub_buffer(@Buffer, lIdxFirstSample - @IdxStartBufferSample, lIdxLastSample - @IdxStartBufferSample)
    end
    # Call client code
    yield(lBuffer, lIdxLastSample - lIdxFirstSample + 1)
    lIdxLastSample = lIdxFirstSample-1
  end
end

#get_current_bufferObject

Get the current buffer

Return
  • Object: The current buffer

  • Integer: The first sample of the buffer

  • Integer: The last sample of the buffer



143
144
145
# File 'lib/WSK/Model/CachedBufferReader.rb', line 143

def get_current_buffer
  return @Buffer, @IdxStartBufferSample, @IdxEndBufferSample
end

#prepare_buffer(iIdxStartSample, iIdxEndSample, iIdxStartSamplePrefetch = iIdxStartSample, iIdxEndSamplePrefetch = iIdxEndSample) ⇒ Object

Ensure that a given samples range is loaded in the buffer. Use the caching mechanism if needed. The buffer might contain more samples than desired.

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

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

  • iIdxStartSamplePrefetch (Integer): Specify the first sample to effectively read if the data needs to be accessed. [optional = iIdxStartSample]

  • iIdxEndSamplePrefetch (Integer): Specify the last sample to effectively read if the data needs to be accessed. [optional = iIdxEndSample]



126
127
128
129
130
131
132
133
134
135
# File 'lib/WSK/Model/CachedBufferReader.rb', line 126

def prepare_buffer(iIdxStartSample, iIdxEndSample, iIdxStartSamplePrefetch = iIdxStartSample, iIdxEndSamplePrefetch = iIdxEndSample)
  if ((@Buffer == nil) or
      (iIdxStartSample < @IdxStartBufferSample) or
      (iIdxEndSample > @IdxEndBufferSample))
    # Read all from the data
    @Buffer = read_buffer(iIdxStartSamplePrefetch, iIdxEndSamplePrefetch)
    @IdxStartBufferSample = iIdxStartSamplePrefetch
    @IdxEndBufferSample = iIdxEndSamplePrefetch
  end
end