Skip to content

Commit c180443

Browse files
committed
Add some more methods to MeasuringStream
1 parent a2407de commit c180443

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/AudioTools/CoreAudio/AudioStreams.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ class MeasuringStream : public ModifyingStream {
711711

712712
/// Provides the data from all streams mixed together
713713
size_t readBytes(uint8_t* data, size_t len) override {
714+
total_bytes_since_begin += len;
714715
return measure(p_stream->readBytes(data, len));
715716
}
716717

@@ -720,6 +721,7 @@ class MeasuringStream : public ModifyingStream {
720721

721722
/// Writes raw PCM audio data, which will be the input for the volume control
722723
virtual size_t write(const uint8_t *data, size_t len) override {
724+
total_bytes_since_begin += len;
723725
return measure(p_print->write(data, len));
724726
}
725727

@@ -744,12 +746,14 @@ class MeasuringStream : public ModifyingStream {
744746
}
745747

746748
bool begin(){
749+
total_bytes_since_begin = 0;
750+
ms_at_begin = millis();
747751
return AudioStream::begin();
748752
}
749753

750754
bool begin(AudioInfo info){
751755
setAudioInfo(info);
752-
return true;
756+
return begin();
753757
}
754758

755759
/// Trigger reporting in frames (=samples) per second
@@ -766,6 +770,28 @@ class MeasuringStream : public ModifyingStream {
766770
this->name = name;
767771
}
768772

773+
/// Provides the time in ms since the last call of begin()
774+
uint32_t timeSinceBegin() {
775+
return millis() - ms_at_begin;
776+
}
777+
778+
/// Provides the total processed bytes since the last call of begin()
779+
uint32_t bytesSinceBegin() {
780+
return total_bytes_since_begin;
781+
}
782+
783+
/// Provides the estimated runtime in milliseconds for the indicated total
784+
uint32_t estimatedTotalTimeFor(uint32_t totalBytes) {
785+
if (bytesSinceBegin()==0) return 0;
786+
return timeSinceBegin() / bytesSinceBegin() * totalBytes;
787+
}
788+
789+
/// Provides the estimated time from now to the end in ms
790+
uint32_t estimatedOpenTimeFor(uint32_t totalBytes) {
791+
if (bytesSinceBegin()==0) return 0;
792+
return estimatedTotalTimeFor(totalBytes) -timeSinceBegin();
793+
}
794+
769795
protected:
770796
int max_count=0;
771797
int count=0;
@@ -779,6 +805,8 @@ class MeasuringStream : public ModifyingStream {
779805
Print *p_logout=nullptr;
780806
bool report_bytes = false;
781807
const char* name = "";
808+
uint32_t ms_at_begin = 0;
809+
uint32_t total_bytes_since_begin = 0;
782810

783811
size_t measure(size_t len) {
784812
count--;

0 commit comments

Comments
 (0)