Pico-Arduino
Buffers.h
1 #pragma once
2 #include "PicoQueue.h"
3 
4 namespace pico_arduino {
5 
13 template <class T>
14 class Buffers {
15  public:
24  Buffers(int buffer_count, int buffer_length, bool blocking=true, bool synchronized = true){
25  this->buffer_count = buffer_count;
26  this->buffer_length = buffer_length;
27 
28  emptyBuffers = new Queue<T*>(buffer_count, blocking, synchronized);
29  filledBuffers = new Queue<T*>(buffer_count, blocking, synchronized);
30 
31  // allocate buffers
32  for (int j=0;j<buffer_count;j++) {
33  T* new_buffer = new T[buffer_length];
34  emptyBuffers->push(new_buffer);
35  }
36  }
37 
40  if (emptyBuffers!=nullptr) delete emptyBuffers;
41  if (filledBuffers!=nullptr) delete filledBuffers;
42  }
43 
45  virtual void addEmpty(T* buffer){
46  if (buffer!=nullptr)
47  emptyBuffers->push(buffer);
48  }
49 
51  virtual T* getEmpty(){
52  T* buffer;
53  emptyBuffers->pop(buffer);
54  return buffer;
55  }
56 
58  virtual void addFull(T* buffer){
59  if (buffer!=nullptr)
60  filledBuffers->push(buffer);
61  }
62 
64  virtual T* getFull(){
65  T* buffer;
66  filledBuffers->pop(buffer);
67  return buffer;
68  }
69 
71  virtual int bufferLength(){
72  return buffer_length;
73  }
74 
75  protected:
76  int buffer_count;
77  int buffer_length;
78  float amplifier;
79  T clip;
80  Queue<T*> *emptyBuffers;
81  Queue<T*> *filledBuffers;
82 
83 };
84 
92 template <class T>
93 class SoundBuffer : public Buffers<T> {
94  public:
103  SoundBuffer(int buffer_count, int buffer_length, double amplifier = 1.0, T clip=0) : Buffers<T>(buffer_count, buffer_length, true, true){
104  this->amplifier = amplifier;
105  this->clip = abs(clip);
106  }
107 
109  T* getFull(){
110  T* buffer = Buffers<T>::getFull();
111  int len = Buffers<T>::bufferLength();
112  // we make sure that the avarage is 0
113  T avg_value = avg(buffer,len);
114  // make sure that the avg is at 0 and amplify the values
115  standardize(buffer,avg_value, len);
116  return buffer;
117  }
118 
119  protected:
120  double amplifier;
121  T clip;
122 
124  double avg( T* buffer, int len){
125  double total = 0;
126  for (int j=0;j<len;j++){
127  total += buffer[j];
128  }
129  return total / len;
130  }
131 
133  void standardize(T* buffer,double avg_value , int len){
134  if (clip>=0){
135  for (int j=0;j<len;j++){
136  T value = (static_cast<double>(buffer[j]) - avg_value) * amplifier;
137  if (value>clip){
138  value = clip;
139  }
140  if (value < -clip){
141  value = -clip;
142  }
143  buffer[j] = value;
144  }
145  } else {
146  for (int j=0;j<len;j++){
147  buffer[j] = (static_cast<double>(buffer[j]) - avg_value) * amplifier;
148  }
149  }
150  }
151 
152 };
153 
154 } // namespace
We are managing a 2 collections of memory arrays: One for the available buffers which can be requeste...
Definition: Buffers.h:14
virtual void addEmpty(T *buffer)
Makes the buffer available as empty.
Definition: Buffers.h:45
virtual T * getFull()
Retrieves the next available data.
Definition: Buffers.h:64
Buffers(int buffer_count, int buffer_length, bool blocking=true, bool synchronized=true)
Construct a new Buffers object.
Definition: Buffers.h:24
virtual void addFull(T *buffer)
Adds the buffer to the list of available data.
Definition: Buffers.h:58
virtual int bufferLength()
Provides the length of each buffer.
Definition: Buffers.h:71
~Buffers()
Destructor.
Definition: Buffers.h:39
virtual T * getEmpty()
Retrieves an empty buffer.
Definition: Buffers.h:51
bool pop(T &data)
Gets the next element (from the head) and removes it from the queue.
Definition: PicoQueue.h:75
bool push(T &data)
Adds an element (at the end) of the queue.
Definition: PicoQueue.h:63
SoundBuffer where the data is standardized, amplified and clipped.
Definition: Buffers.h:93
double avg(T *buffer, int len)
calculate the average over all samples
Definition: Buffers.h:124
T * getFull()
provides the buffer which contains the sound samples - the recorded values are standardized
Definition: Buffers.h:109
void standardize(T *buffer, double avg_value, int len)
make sure that the center is at 0 and that the peaks are clipped
Definition: Buffers.h:133
SoundBuffer(int buffer_count, int buffer_length, double amplifier=1.0, T clip=0)
Construct a new Sound Buffer object.
Definition: Buffers.h:103
Pico Arduino Framework.
Definition: Arduino.cpp:26