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 {
54 size_t result = p_io->readBytes(data, len);
56 return measure(result);
57 }
58
59 int available() override { return p_io->available(); }
60
62 virtual size_t write(const uint8_t *data, size_t len) override {
63 size_t result = p_out->write(data, len);
65 return measure(result);
66 }
67
69 virtual int availableForWrite() override {
70 return p_out->availableForWrite();
71 }
72
73 void flush() override {
74 if (p_out != nullptr) p_out->flush();
75 }
76
79
82 if (frame_size == 0) return 0;
84 }
85
87 uint32_t startTime() { return start_time; }
88
89 void setAudioInfo(AudioInfo info) override {
90 LOGI("MeasuringStream::setAudioInfo: %d bits, %d channels", info.bits_per_sample, info.channels);
92 setFrameSize(info.bits_per_sample / 8 * info.channels);
93 }
94
95 bool begin() override {
99 }
100
103 return begin();
104 }
105
107 void setFrameSize(int size) { frame_size = size; }
108
110 void setReportBytes(bool flag) { report_bytes = flag; }
111
112 void setName(const char *name) { this->name = name; }
113
115 uint32_t timeSinceBegin() { return millis() - ms_at_begin; }
116
119
121 uint32_t estimatedTotalTimeFor(uint32_t totalBytes) {
122 if (bytesSinceBegin() == 0) return 0;
123 return static_cast<float>(timeSinceBegin()) / bytesSinceBegin() *
124 totalBytes;
125 }
126
128 uint32_t estimatedOpenTimeFor(uint32_t totalBytes) {
129 if (bytesSinceBegin() == 0) return 0;
130 return estimatedTotalTimeFor(totalBytes) - timeSinceBegin();
131 }
132
135 bool setProcessedBytes(uint32_t pos) {
136 bool is_regular_update = true;
137 if (pos < total_bytes_since_begin) {
138 begin();
139 is_regular_update = false;
140 }
142 return is_regular_update;
143 }
144
145 protected:
146 int max_count = 0;
147 int count = 0;
148 Stream *p_io = nullptr;
149 Print *p_out = nullptr;
150 uint32_t start_time;
151 int total_bytes = 0;
153 int frame_size = 0;
155 Print *p_logout = nullptr;
156 bool report_bytes = false;
157 const char *name = "";
158 uint32_t ms_at_begin = 0;
160
161 size_t measure(size_t len) {
162 count--;
163 total_bytes += len;
164
165 if (count <= 0) {
166 uint32_t end_time = millis();
167 int time_diff = end_time - start_time; // in ms
168 if (time_diff > 0) {
169 bytes_per_second = (int64_t)total_bytes * 1000 / time_diff;
170 printResult();
172 total_bytes = 0;
173 start_time = end_time;
174 }
175 }
176 return len;
177 }
178
179 void printResult() {
180 char msg[70];
181 if (report_bytes || frame_size == 0) {
182 snprintf(msg, 70, "%s ==> Bytes per second: %d", name, bytes_per_second);
183 } else {
184 snprintf(msg, 70, "%s ==> Samples per second: %d", name,
186 }
187 if (p_logout != nullptr) {
188 p_logout->println(msg);
189 } else {
190 LOGI("%s", msg);
191 }
192 }
193};
194
195} // namespace audio_tools
#define LOGI(...)
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:62
void flush() override
Definition MeasuringStream.h:73
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:121
bool report_bytes
Definition MeasuringStream.h:156
uint32_t ms_at_begin
Definition MeasuringStream.h:158
uint32_t start_time
Definition MeasuringStream.h:150
uint32_t timeSinceBegin()
Provides the time in ms since the last call of begin().
Definition MeasuringStream.h:115
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:81
uint32_t bytesSinceBegin()
Provides the total processed bytes since the last call of begin().
Definition MeasuringStream.h:118
int available() override
Definition MeasuringStream.h:59
virtual int availableForWrite() override
Provides the nubmer of bytes we can write.
Definition MeasuringStream.h:69
MeasuringStream(int count=10, Print *logOut=nullptr)
Definition MeasuringStream.h:15
uint32_t total_bytes_since_begin
Definition MeasuringStream.h:159
size_t measure(size_t len)
Definition MeasuringStream.h:161
int total_bytes
Definition MeasuringStream.h:151
void setFrameSize(int size)
Trigger reporting in frames (=samples) per second.
Definition MeasuringStream.h:107
int max_count
Definition MeasuringStream.h:146
void setReportBytes(bool flag)
Report in bytes instead of samples.
Definition MeasuringStream.h:110
uint32_t estimatedOpenTimeFor(uint32_t totalBytes)
Provides the estimated time from now to the end in ms.
Definition MeasuringStream.h:128
const char * name
Definition MeasuringStream.h:157
void setLogOutput(Print &out)
Defines the logging output.
Definition MeasuringStream.h:41
int bytes_per_second
Definition MeasuringStream.h:152
MeasuringStream(Print &print, int count=10, Print *logOut=nullptr)
Definition MeasuringStream.h:24
bool setProcessedBytes(uint32_t pos)
Definition MeasuringStream.h:135
Print * p_out
Definition MeasuringStream.h:149
Print * p_logout
Definition MeasuringStream.h:155
void printResult()
Definition MeasuringStream.h:179
void setName(const char *name)
Definition MeasuringStream.h:112
uint32_t startTime()
Provides the time when the last measurement was started.
Definition MeasuringStream.h:87
bool begin() override
Definition MeasuringStream.h:95
void setAudioInfo(AudioInfo info) override
Definition MeasuringStream.h:89
int count
Definition MeasuringStream.h:147
int frame_size
Definition MeasuringStream.h:153
void setStream(Stream &io) override
Defines/Changes the input & output.
Definition MeasuringStream.h:44
bool begin(AudioInfo info)
Definition MeasuringStream.h:101
NullStream null
Definition MeasuringStream.h:154
int bytesPerSecond()
Returns the actual thrughput in bytes per second.
Definition MeasuringStream.h:78
Stream * p_io
Definition MeasuringStream.h:148
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
Definition CodecOpusOgg.h:57
Definition CodecOpusOgg.h:137
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
AudioInfo info
Definition CodecOpusOgg.h:171
virtual bool begin()
Definition CodecOpusOgg.h:40
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:56