Skip to content

Commit 1ff5231

Browse files
committed
ProgressStream provide processedSecs()
1 parent 7d9c3d5 commit 1ff5231

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/AudioTools/AudioStreams.h

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,16 @@ class MeasuringStream : public AudioStream {
11001100
}
11011101
};
11021102

1103+
/**
1104+
* @brief Configuration for ProgressStream
1105+
* @author Phil Schatzmann
1106+
* @copyright GPLv3
1107+
*
1108+
*/
1109+
class ProgressStreamInfo : public AudioInfo {
1110+
public:
1111+
size_t total_size = 0;
1112+
};
11031113
/**
11041114
* @brief Generic calss to measure the the total bytes which were processed in order to
11051115
* calculate the progress as a percentage of the total size.
@@ -1119,6 +1129,15 @@ class ProgressStream : public AudioStream {
11191129
setStream(stream);
11201130
}
11211131

1132+
ProgressStreamInfo& defaultConfig() {
1133+
return progress_info;
1134+
}
1135+
1136+
void setAudioInfo(AudioInfo info) override {
1137+
AudioStream::setAudioInfo(info);
1138+
progress_info.copyFrom(info);
1139+
}
1140+
11221141
void setStream(Stream &stream){
11231142
p_stream =&stream;
11241143
p_print = &stream;
@@ -1138,26 +1157,43 @@ class ProgressStream : public AudioStream {
11381157
return AudioStream::begin();
11391158
}
11401159

1160+
bool begin(ProgressStreamInfo info){
1161+
progress_info = info;
1162+
setAudioInfo(info);
1163+
return AudioStream::begin();
1164+
}
1165+
11411166
/// Updates the total size and restarts the percent calculation
11421167
void setSize(size_t len){
11431168
total_processed = 0;
1144-
total_size = len;
1169+
progress_info.total_size = len;
11451170
}
11461171

11471172
/// Provides the current total size (defined by setSize)
11481173
size_t size(){
1149-
return total_size;
1174+
return progress_info.total_size;
11501175
}
11511176

11521177
/// Provides the number of processed bytes
1153-
size_t processed() {
1178+
size_t processedBytes() {
11541179
return total_processed;
11551180
}
11561181

1182+
/// Provides the number of processed seconds
1183+
size_t processedSecs() {
1184+
AudioInfo info = audioInfo();
1185+
int byte_rate = info.sample_rate * info.bits_per_sample * info.channels / 8;
1186+
if (byte_rate==0){
1187+
LOGE("Audio Info not defined");
1188+
return 0;
1189+
}
1190+
return total_processed / byte_rate;
1191+
}
1192+
11571193
/// Provides the processed percentage: If no size has been defined we return 0
11581194
float percentage() {
1159-
if (total_size==0) return 0;
1160-
return 100.0 * total_processed / total_size;
1195+
if (progress_info.total_size==0) return 0;
1196+
return 100.0 * total_processed / progress_info.total_size;
11611197
}
11621198

11631199
/// Provides the data from all streams mixed together
@@ -1184,10 +1220,10 @@ class ProgressStream : public AudioStream {
11841220
}
11851221

11861222
protected:
1223+
ProgressStreamInfo progress_info;
11871224
Stream *p_stream=nullptr;
11881225
Print *p_print=nullptr;
11891226
size_t total_processed = 0;
1190-
size_t total_size = 0;
11911227

11921228
size_t measure(size_t len) {
11931229
total_processed += len;

0 commit comments

Comments
 (0)