From b29dcd04cad08902bde5696131906184a440d647 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 27 Dec 2018 14:25:25 +0100 Subject: Reduce volume with number of samples played to fix clipping noises. --- src/soundplayer.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/soundplayer.cc') diff --git a/src/soundplayer.cc b/src/soundplayer.cc index c170ff2..d24eec5 100644 --- a/src/soundplayer.cc +++ b/src/soundplayer.cc @@ -30,6 +30,7 @@ #include #include #include +#include #define BUFSZ 512 @@ -62,6 +63,8 @@ void SoundPlayer::run() running = true; short s[BUFSZ]; + float _sf[BUFSZ]; + double scale = 1.0; while(running) { { // Check for new Selection. @@ -74,6 +77,7 @@ void SoundPlayer::run() } memset(s, 0, BUFSZ * sizeof(short)); + memset(_sf, 0, BUFSZ * sizeof(float)); QList::iterator it = active.begin(); while(it != active.end()) @@ -87,8 +91,20 @@ void SoundPlayer::run() it = active.erase(it); goto nextitem; } - s[i] += item.samples[item.pos] * 32536 * 0.5; + + //while(std::abs(_sf[i] + item.samples[item.pos] * scale) > 1.0) + //{ + // scale *= 0.999999; + //} + scale = 1.0 / active.size(); + _sf[i] += item.samples[item.pos] * scale; + item.pos++; + scale += 0.00000001; + if(scale > 1.0) + { + scale = 1.0; + } } it++; @@ -97,6 +113,10 @@ void SoundPlayer::run() int a = 1;(void)a; } + for(size_t i = 0; i < BUFSZ; i++) + { + s[i] = _sf[i] * 32000.0; + } ao_play(dev, (char*)s, BUFSZ * sizeof(short)); } -- cgit v1.2.3