arduino-audio-tools
Loading...
Searching...
No Matches
ObjectStream.h
1#include "AudioTools/CoreAudio/BaseStream.h"
2namespace audio_tools {
9class ObjectStream : public BaseStream {
10 ObjectStream(Stream &stream) {
11 p_in = &stream;
12 p_out = &stream;
13 }
14 ObjectStream(Print &stream) { p_out = &stream; }
15
18 size_t readBytes(uint8_t *data, size_t len) {
19 if (p_in == nullptr) return 0;
20 // read the size prefix if necessary
21 int to_read = available();
22 size_t result = 0;
23 if (to_read > 0) {
24 if (to_read > len) to_read = len;
25 // read the data
26 result = p_in->readBytes(data, to_read);
27 // determe the open number of bytes
28 n_open_read -= result;
29 is_complete = n_open_read == 0;
30 // if we have read all data we need to read the size prefix again
31 if (is_complete) n_open_read = -1;
32 }
33 return result;
34 }
35
36 size_t write(const uint8_t *data, size_t len) {
37 if (p_out == nullptr) return 0;
38 // write the size prefix
39 p_out->write((uint8_t *)&len, sizeof(size_t));
40 // write the data
41 return p_out->write(data, len);
42 }
43
44 int available() {
45 if (p_in == nullptr) return 0;
46 if (n_open_read >= 0) return n_open_read;
47 // make sure that we can read the size prefix
48 if (p_in->available() < sizeof(size_t)) return 0;
49 // read the size prefix
50 p_in->readBytes((uint8_t *)&n_open_read, sizeof(size_t));
51 return n_open_read;
52 }
53
54 // not supported
55 virtual size_t write(uint8_t ch) { return 0; }
56
57 int availableForWrite() override {
58 if (max_object_size > 0) return max_object_size;
59 if (p_out == nullptr) return DEFAULT_BUFFER_SIZE;
60 return p_out->availableForWrite();
61 }
62
65 void setMaxObjectSize(size_t size) { max_object_size = size; }
66
68 bool isObjectComplete() { return is_complete; }
69
70 protected:
71 Stream *p_in = nullptr;
72 Print *p_out = nullptr;
73 int n_open_read = -1;
74 int max_object_size = 0; // 0 u
75 bool is_complete = true;
76};
77} // namespace audio_tools
Base class for all Streams. It relies on write(const uint8_t *buffer, size_t size) and readBytes(uint...
Definition BaseStream.h:36
A Arduino Stream which makes sure that we read back the same size as we wrote. It adds a size prefix ...
Definition ObjectStream.h:9
Definition NoArduino.h:62
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10