arduino-audio-tools
Slice.h
1 #pragma once
2 #include <stddef.h>
3 #include <stdint.h>
4 
5 namespace audio_tools {
6 
15 template <class T>
16 class Slice {
17  public:
18  Slice(T* start, size_t len) {
19  this->start = start;
20  this->len = len;
21  }
22 
24  T* data() { return start; }
25 
27  size_t size() { return len; }
28 
30  size_t slices(int sliceSize){
31  int result = len / sliceSize;
32  return len % sliceSize == 0 ? result : result+1;
33  }
34 
36  operator bool() { return len > 0 && start!=nullptr; }
37 
39  Slice slice(int sliceSize, int idx) {
40  int start_pos = idx * sliceSize;
41  int end_pos = start_pos + sliceSize;
42  if (end_pos > len) {
43  end_pos = len;
44  }
45 
46  if (start_pos < len) {
47  assert(start!=nullptr);
48  return Slice(start + start_pos, end_pos - start_pos);
49  } else {
50  return Slice(nullptr, 0);
51  }
52  }
53 
54  protected:
55  T* start = nullptr;
56  size_t len = 0;
57 };
58 
59 } // namespace audio_tools
Helps to split up a big memory array into smaller slices. There are no additinal heap allocations!...
Definition: Slice.h:16
size_t size()
Returns the (result) data size is number of entries.
Definition: Slice.h:27
size_t slices(int sliceSize)
Returns the number of slices of the indicated size.
Definition: Slice.h:30
T * data()
Returns the data.
Definition: Slice.h:24
Slice slice(int sliceSize, int idx)
Returns the slice at the indicated index for the indicated slize size;.
Definition: Slice.h:39
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823