Skip to content

Commit f86334f

Browse files
committed
VolumeMeter: avgVolume()
1 parent 5f94df5 commit f86334f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/AudioTools/CoreAudio/AudioStreams.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,12 +1769,29 @@ class VolumeMeter : public ModifyingStream {
17691769
/// Volume of indicated channel in %: max amplitude is 100
17701770
float volumePercent(int channel) { return 100.0f * volumeRatio(channel);}
17711771

1772+
/// Average volume of all channels
1773+
float volumeAvg() {
1774+
float total = 0;
1775+
size_t count = 0;
1776+
for (int j=0;j<info.channels;j++){
1777+
total += sum[j];
1778+
count += sample_count_per_channel;
1779+
}
1780+
return total / count;
1781+
}
1782+
1783+
/// Average volume of indicated channel
1784+
float volumeAvg(int channel) {
1785+
return sum[channel] / sample_count_per_channel;
1786+
}
1787+
17721788

17731789
/// Resets the actual volume
17741790
void clear() {
17751791
f_volume_tmp = 0;
17761792
for (int j = 0; j < info.channels; j++) {
17771793
volumes_tmp[j] = 0;
1794+
sum_tmp[j] = 0;
17781795
}
17791796
}
17801797

@@ -1791,12 +1808,18 @@ class VolumeMeter : public ModifyingStream {
17911808
float f_volume = 0;
17921809
Vector<float> volumes{0};
17931810
Vector<float> volumes_tmp{0};
1811+
Vector<float> sum{0};
1812+
Vector<float> sum_tmp{0};
17941813
Print* p_out = nullptr;
17951814
Stream* p_stream = nullptr;
1815+
size_t sample_count_per_channel = 0;
17961816

17971817
void updateVolumes(const uint8_t *data, size_t len){
17981818
clear();
17991819
switch (info.bits_per_sample) {
1820+
case 8:
1821+
updateVolumesT<int8_t>(data, len);
1822+
break;
18001823
case 16:
18011824
updateVolumesT<int16_t>(data, len);
18021825
break;
@@ -1815,6 +1838,7 @@ class VolumeMeter : public ModifyingStream {
18151838
template <typename T> void updateVolumesT(const uint8_t *buffer, size_t size) {
18161839
T *bufferT = (T *)buffer;
18171840
int samplesCount = size / sizeof(T);
1841+
sample_count_per_channel = samplesCount / info.channels;
18181842
for (int j = 0; j < samplesCount; j++) {
18191843
float tmp = abs(static_cast<float>(bufferT[j]));
18201844
updateVolume(tmp, j);
@@ -1830,6 +1854,7 @@ class VolumeMeter : public ModifyingStream {
18301854
int ch = j % info.channels;
18311855
if (tmp > volumes_tmp[ch]) {
18321856
volumes_tmp[ch] = tmp;
1857+
sum_tmp[ch] = tmp;
18331858
}
18341859
}
18351860
}
@@ -1838,6 +1863,7 @@ class VolumeMeter : public ModifyingStream {
18381863
f_volume = f_volume_tmp;
18391864
for (int j = 0; j < info.channels; j++) {
18401865
volumes[j] = volumes_tmp[j];
1866+
sum[j] = sum_tmp[j];
18411867
}
18421868
}
18431869
};

0 commit comments

Comments
 (0)