summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2018-12-27 14:25:25 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2018-12-27 14:25:25 +0100
commitb29dcd04cad08902bde5696131906184a440d647 (patch)
tree87458f6dc7c0a21cd2f3acdbb182ced36350d4d7
parent5864285c0b987f97b632d1acfcc7cc49439e985d (diff)
Reduce volume with number of samples played to fix clipping noises.
-rw-r--r--src/soundplayer.cc22
1 files changed, 21 insertions, 1 deletions
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 <ao/ao.h>
#include <sndfile.h>
#include <samplerate.h>
+#include <cmath>
#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<QueueItem>::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));
}