File: samplebuffer_iterators.h

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (190 lines) | stat: -rw-r--r-- 4,310 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
73
74
75
76
77
78
79
80
81
82
83
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef INCLUDED_SAMPLEBUFFER_ITERATORS_H
#define INCLUDED_SAMPLEBUFFER_ITERATORS_H

#include "samplebuffer.h"

/**
 * Iterate through all samples. No specific order.
 *
 * Related design patterns:
 *     - Iterator (GoF257)
 */
class SAMPLE_ITERATOR {

 private:

  SAMPLE_BUFFER* target;
  SAMPLE_BUFFER::buf_size_t index;     // index of the current sample
  SAMPLE_BUFFER::channel_size_t channel_index;  // index of current channel

 public:

  /**
   * Prepare iterator for processing.
   */
  void init(SAMPLE_BUFFER* buf) { target = buf; }

  /**
   * Start iteration from the first audio item;
   */
  void begin(void);

  /**
   * True if iterator is past the last audio item.
   */
  inline bool end(void) { return(channel_index >= static_cast<int>(target->channel_count_rep)); }

  /**
   * Move iterator to the next audio item.
   */
  void next(void);

  /**
   * Returns a pointer to the current sample.
   */
  inline SAMPLE_SPECS::sample_t* current(void) { return(&(target->buffer[channel_index][index])); }
};

/**
 * Iterate through all samples of one channel. 
 *
 * Notice! This iterator can be used to add extra
 * channels to the sample data.
 *
 * Related design patterns:
 *     - Iterator (GoF257)
 */
class SAMPLE_ITERATOR_CHANNEL {

 private:

  SAMPLE_BUFFER* target;
  SAMPLE_BUFFER::buf_size_t index;     // index of the current sample
  SAMPLE_BUFFER::channel_size_t channel_index;  // index of current channel

 public:

  /**
   * Prepare iterator for processing.
   */
  void init(SAMPLE_BUFFER* buf, int channel = 0);

  /**
   * Start iteration from the first sample of 'channel'. More channels 
   * are allocated, if sample buffer has fewer channels than asked for.
   *
   * @param channel number of iterated channel (0, 1, ... , n)
   */
  void begin(int channel);

  /**
   * Start iteration from the first audio item (using the previously
   * set channel).
   */
  void begin(void) { index = 0; }
  
  /**
   * Move iterator to the next audio item.
   */
  void next(void) { ++index; }

  /**
   * True if iterator is past the last audio item.
   */
  inline bool end(void) { return(static_cast<long int>(index) >= target->buffersize_rep); }

  /**
   * Returns a pointer to the current sample.
   */
  inline SAMPLE_SPECS::sample_t* current(void) { return(&target->buffer[channel_index][index]); }
};

/**
 * Iterate through all samples, one channel at a time.
 *
 * Related design patterns:
 *     - Iterator (GoF257)
 */
class SAMPLE_ITERATOR_CHANNELS {

 private:

  SAMPLE_BUFFER* target;
  SAMPLE_BUFFER::buf_size_t index;     // index of the current sample
  SAMPLE_BUFFER::channel_size_t channel_index;  // index of current channel

 public:

  /**
   * Prepare iterator for processing.
   */
  void init(SAMPLE_BUFFER* buf) { target = buf; }

  /**
   * Start iteration from the first audio item;
   */
  void begin(void);

  /**
   * Move iterator to the next audio item.
   */
  void next(void);

  /**
   * True if iterator is past the last audio item.
   */
  inline bool end(void) { return(channel_index >= static_cast<int>(target->channel_count_rep)); }

  /**
   * Returns a pointer to the current sample.
   */
  inline SAMPLE_SPECS::sample_t* current(void) { return(&(target->buffer[channel_index][index])); }

  /**
   * Returns current channel index (starting from 0)
   */
  inline int channel(void) const { return(channel_index); }
};

/**
 * Iterate through all samples, one sample frame (interleaved) at a time.
 *
 * Related design patterns:
 *     - Iterator (GoF257)
 */
class SAMPLE_ITERATOR_INTERLEAVED {

 private:

  SAMPLE_BUFFER* target;
  SAMPLE_BUFFER::buf_size_t  index;     // index of the current sample

 public:

  /**
   * Prepare iterator for processing.
   */
  void init(SAMPLE_BUFFER* buf) { target = buf; }

  /**
   * Start iteration from the first audio item;
   */
  void begin(void) { index = 0; }

  /**
   * Move iterator to the next audio item.
   */
  inline void next(void) { ++index; }

  /**
   * True if iterator is past the last audio item.
   */
  inline bool end(void) { return(static_cast<long int>(index) >= target->buffersize_rep); }

  /**
   * Returns a pointer to the current sample.
   */
  inline SAMPLE_SPECS::sample_t* current(int channel) { return(&target->buffer[channel][index]); }
};

#endif