@@ -1769,12 +1769,29 @@ class VolumeMeter : public ModifyingStream {
1769
1769
// / Volume of indicated channel in %: max amplitude is 100
1770
1770
float volumePercent (int channel) { return 100 .0f * volumeRatio (channel);}
1771
1771
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
+
1772
1788
1773
1789
// / Resets the actual volume
1774
1790
void clear () {
1775
1791
f_volume_tmp = 0 ;
1776
1792
for (int j = 0 ; j < info.channels ; j++) {
1777
1793
volumes_tmp[j] = 0 ;
1794
+ sum_tmp[j] = 0 ;
1778
1795
}
1779
1796
}
1780
1797
@@ -1791,12 +1808,18 @@ class VolumeMeter : public ModifyingStream {
1791
1808
float f_volume = 0 ;
1792
1809
Vector<float > volumes{0 };
1793
1810
Vector<float > volumes_tmp{0 };
1811
+ Vector<float > sum{0 };
1812
+ Vector<float > sum_tmp{0 };
1794
1813
Print* p_out = nullptr ;
1795
1814
Stream* p_stream = nullptr ;
1815
+ size_t sample_count_per_channel = 0 ;
1796
1816
1797
1817
void updateVolumes (const uint8_t *data, size_t len){
1798
1818
clear ();
1799
1819
switch (info.bits_per_sample ) {
1820
+ case 8 :
1821
+ updateVolumesT<int8_t >(data, len);
1822
+ break ;
1800
1823
case 16 :
1801
1824
updateVolumesT<int16_t >(data, len);
1802
1825
break ;
@@ -1815,6 +1838,7 @@ class VolumeMeter : public ModifyingStream {
1815
1838
template <typename T> void updateVolumesT (const uint8_t *buffer, size_t size) {
1816
1839
T *bufferT = (T *)buffer;
1817
1840
int samplesCount = size / sizeof (T);
1841
+ sample_count_per_channel = samplesCount / info.channels ;
1818
1842
for (int j = 0 ; j < samplesCount; j++) {
1819
1843
float tmp = abs (static_cast <float >(bufferT[j]));
1820
1844
updateVolume (tmp, j);
@@ -1830,6 +1854,7 @@ class VolumeMeter : public ModifyingStream {
1830
1854
int ch = j % info.channels ;
1831
1855
if (tmp > volumes_tmp[ch]) {
1832
1856
volumes_tmp[ch] = tmp;
1857
+ sum_tmp[ch] = tmp;
1833
1858
}
1834
1859
}
1835
1860
}
@@ -1838,6 +1863,7 @@ class VolumeMeter : public ModifyingStream {
1838
1863
f_volume = f_volume_tmp;
1839
1864
for (int j = 0 ; j < info.channels ; j++) {
1840
1865
volumes[j] = volumes_tmp[j];
1866
+ sum[j] = sum_tmp[j];
1841
1867
}
1842
1868
}
1843
1869
};
0 commit comments