arduino-audio-tools
Loading...
Searching...
No Matches
MeasuringStream.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace audio_tools {
6
14 public:
15 MeasuringStream(int count = 10, Print *logOut = nullptr) {
16 this->count = count;
17 this->max_count = count;
18 p_io = &null;
19 p_out = &null;
21 p_logout = logOut;
22 }
23
24 MeasuringStream(Print &print, int count = 10, Print *logOut = nullptr) {
25 this->count = count;
26 this->max_count = count;
27 setOutput(print);
29 p_logout = logOut;
30 }
31
32 MeasuringStream(Stream &stream, int count = 10, Print *logOut = nullptr) {
33 this->count = count;
34 this->max_count = count;
35 setStream(stream);
37 p_logout = logOut;
38 }
39
41 void setLogOutput(Print &out) { p_logout = &out; }
42
44 void setStream(Stream &io) override {
45 p_out = &io;
46 p_io = &io;
47 };
48
50 void setOutput(Print &out) override { p_out = &out; }
51
53 size_t readBytes(uint8_t *data, size_t len) override {
55 return measure(p_io->readBytes(data, len));
56 }
57
58 int available() override { return p_io->available(); }
59
61 virtual size_t write(const uint8_t *data, size_t len) override {
63 return measure(p_out->write(data, len));
64 }
65
67 virtual int availableForWrite() override {
68 return p_out->availableForWrite();
69 }
70
71 void flush() override {
72 if (p_out != nullptr) p_out->flush();
73 }
74
77
80 if (frame_size == 0) return 0;
82 }
83
85 uint32_t startTime() { return start_time; }
86
87 void setAudioInfo(AudioInfo info) override {
88 LOGI("MeasuringStream::setAudioInfo: %d bits, %d channels", info.bits_per_sample, info.channels);
91 }
92
93 bool begin() override {
97 }
98
101 return begin();
102 }
103
105 void setFrameSize(int size) { frame_size = size; }
106
108 void setReportBytes(bool flag) { report_bytes = flag; }
109
110 void setName(const char *name) { this->name = name; }
111
113 uint32_t timeSinceBegin() { return millis() - ms_at_begin; }
114
117
119 uint32_t estimatedTotalTimeFor(uint32_t totalBytes) {
120 if (bytesSinceBegin() == 0) return 0;
121 return static_cast<float>(timeSinceBegin()) / bytesSinceBegin() *
122 totalBytes;
123 }
124
126 uint32_t estimatedOpenTimeFor(uint32_t totalBytes) {
127 if (bytesSinceBegin() == 0) return 0;
128 return estimatedTotalTimeFor(totalBytes) - timeSinceBegin();
129 }
130
133 bool setProcessedBytes(uint32_t pos) {
134 bool is_regular_update = true;
135 if (pos < total_bytes_since_begin) {
136 begin();
137 is_regular_update = false;
138 }
140 return is_regular_update;
141 }
142
143 protected:
144 int max_count = 0;
145 int count = 0;
146 Stream *p_io = nullptr;
147 Print *p_out = nullptr;
148 uint32_t start_time;
149 int total_bytes = 0;
151 int frame_size = 0;
153 Print *p_logout = nullptr;
154 bool report_bytes = false;
155 const char *name = "";
156 uint32_t ms_at_begin = 0;
158
159 size_t measure(size_t len) {
160 count--;
161 total_bytes += len;
162
163 if (count <= 0) {
164 uint32_t end_time = millis();
165 int time_diff = end_time - start_time; // in ms
166 if (time_diff > 0) {
167 bytes_per_second = total_bytes / time_diff * 1000;
168 printResult();
170 total_bytes = 0;
171 start_time = end_time;
172 }
173 }
174 return len;
175 }
176
177 void printResult() {
178 char msg[70];
179 if (report_bytes || frame_size == 0) {
180 snprintf(msg, 70, "%s ==> Bytes per second: %d", name, bytes_per_second);
181 } else {
182 snprintf(msg, 70, "%s ==> Samples per second: %d", name,
184 }
185 if (p_logout != nullptr) {
186 p_logout->println(msg);
187 } else {
188 LOGI("%s", msg);
189 }
190 }
191};
192
193} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
Definition Arduino.h:56
virtual int availableForWrite()
Definition Arduino.h:128
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
virtual void flush()
Definition Arduino.h:130
Definition Arduino.h:136
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140
virtual int available()
Definition Arduino.h:139
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
AudioInfo info
Definition BaseStream.h:171
virtual bool begin()
Definition BaseStream.h:40
Class which measures the thruput.
Definition MeasuringStream.h:13
virtual size_t write(const uint8_t *data, size_t len) override
Writes raw PCM audio data, which will be the input for the volume control.
Definition MeasuringStream.h:61
void flush() override
Definition MeasuringStream.h:71
void setOutput(Print &out) override
Defines/Changes the output target.
Definition MeasuringStream.h:50
uint32_t estimatedTotalTimeFor(uint32_t totalBytes)
Provides the estimated runtime in milliseconds for the indicated total.
Definition MeasuringStream.h:119
bool report_bytes
Definition MeasuringStream.h:154
uint32_t ms_at_begin
Definition MeasuringStream.h:156
uint32_t start_time
Definition MeasuringStream.h:148
uint32_t timeSinceBegin()
Provides the time in ms since the last call of begin()
Definition MeasuringStream.h:113
size_t readBytes(uint8_t *data, size_t len) override
Provides the data from all streams mixed together.
Definition MeasuringStream.h:53
MeasuringStream(Stream &stream, int count=10, Print *logOut=nullptr)
Definition MeasuringStream.h:32
int framesPerSecond()
Returns the actual thrughput in frames (samples) per second.
Definition MeasuringStream.h:79
uint32_t bytesSinceBegin()
Provides the total processed bytes since the last call of begin()
Definition MeasuringStream.h:116
int available() override
Definition MeasuringStream.h:58
virtual int availableForWrite() override
Provides the nubmer of bytes we can write.
Definition MeasuringStream.h:67
MeasuringStream(int count=10, Print *logOut=nullptr)
Definition MeasuringStream.h:15
uint32_t total_bytes_since_begin
Definition MeasuringStream.h:157
size_t measure(size_t len)
Definition MeasuringStream.h:159
int total_bytes
Definition MeasuringStream.h:149
void setFrameSize(int size)
Trigger reporting in frames (=samples) per second.
Definition MeasuringStream.h:105
int max_count
Definition MeasuringStream.h:144
void setReportBytes(bool flag)
Report in bytes instead of samples.
Definition MeasuringStream.h:108
uint32_t estimatedOpenTimeFor(uint32_t totalBytes)
Provides the estimated time from now to the end in ms.
Definition MeasuringStream.h:126
const char * name
Definition MeasuringStream.h:155
void setLogOutput(Print &out)
Defines the logging output.
Definition MeasuringStream.h:41
int bytes_per_second
Definition MeasuringStream.h:150
MeasuringStream(Print &print, int count=10, Print *logOut=nullptr)
Definition MeasuringStream.h:24
bool setProcessedBytes(uint32_t pos)
Definition MeasuringStream.h:133
Print * p_out
Definition MeasuringStream.h:147
Print * p_logout
Definition MeasuringStream.h:153
void printResult()
Definition MeasuringStream.h:177
void setName(const char *name)
Definition MeasuringStream.h:110
uint32_t startTime()
Provides the time when the last measurement was started.
Definition MeasuringStream.h:85
bool begin() override
Definition MeasuringStream.h:93
void setAudioInfo(AudioInfo info) override
Defines the input AudioInfo.
Definition MeasuringStream.h:87
int count
Definition MeasuringStream.h:145
int frame_size
Definition MeasuringStream.h:151
void setStream(Stream &io) override
Defines/Changes the input & output.
Definition MeasuringStream.h:44
bool begin(AudioInfo info)
Definition MeasuringStream.h:99
NullStream null
Definition MeasuringStream.h:152
int bytesPerSecond()
Returns the actual thrughput in bytes per second.
Definition MeasuringStream.h:76
Stream * p_io
Definition MeasuringStream.h:146
Abstract class: Objects can be put into a pipleline.
Definition AudioStreams.h:68
The Arduino Stream which provides silence and simulates a null device when used as audio target or au...
Definition BaseStream.h:340
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57