arduino-audio-tools
ReedSolomonFEC.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/BaseStream.h"
5 #include "FEC/ReedSolomon/rs.hpp"
6 
7 namespace audio_tools {
8 
16 template <int bytecount, int additional_bytes>
17 class ReedSolomonFEC : public BaseStream {
18  public:
19 
20  ReedSolomonFEC(Stream &stream){
21  p_stream = &stream;
22  p_print = &stream;
23  }
24 
25  ReedSolomonFEC(Print &print){
26  p_print = &print;
27  }
28 
29  int availableForWrite() override {
30  return bytecount;
31  }
32 
33  size_t write(const uint8_t* data, size_t len) override {
34  if (p_print==nullptr) return 0;
35  for (int j=0;j<len;j++){
36  raw.write(data[j]);
37  if(raw.availableForWrite()==0){
38  rs.Encode(raw.data(), encoded.data());
39  p_print->write(encoded.data(), bytecount+additional_bytes);
40  raw.reset();
41  }
42  }
43  return len;
44  }
45 
46  int available()override {
47  return bytecount;
48  }
49 
50  size_t readBytes(uint8_t* data, size_t len) override{
51  if (p_stream==nullptr) return 0;
52  if (encoded.isEmpty()){
53  int read_len = bytecount + additional_bytes;
54  p_stream->readBytes(encoded.data(), read_len);
55  encoded.setAvailable(read_len);
56  }
57  return encoded.readArray(data, len);
58  }
59 
60 
61  protected:
62  SingleBuffer<uint8_t> raw{bytecount};
63  SingleBuffer<uint8_t> encoded{bytecount+additional_bytes};
65  Stream* p_stream = nullptr;
66  Print* p_print = nullptr;
67 
68 };
69 
70 }
virtual int readArray(T data[], int len)
reads multiple values
Definition: Buffers.h:41
Base class for all Streams. It relies on write(const uint8_t *buffer, size_t size) and readBytes(uint...
Definition: BaseStream.h:34
Definition: NoArduino.h:58
Forward error correction using Reed-Solomon: write is encoding and readBytes does the decoding.
Definition: ReedSolomonFEC.h:17
T * data()
Provides address of actual data.
Definition: Buffers.h:252
size_t setAvailable(size_t available_size)
Definition: Buffers.h:264
bool write(T sample) override
write add an entry to the buffer
Definition: Buffers.h:194
int availableForWrite() override
provides the number of entries that are available to write
Definition: Buffers.h:224
void reset() override
clears the buffer
Definition: Buffers.h:254
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823