summaryrefslogtreecommitdiff
path: root/libs/ardour/sndfilesource.cc
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2016-12-27 18:33:41 +0100
committerRobin Gareus <robin@gareus.org>2016-12-27 18:33:41 +0100
commitfe01666475187e996bc1c37f69c691b433984211 (patch)
treeb00fd0f0b152f6ecd706c7c0a64cc7c3c7322695 /libs/ardour/sndfilesource.cc
parent54a79639df0a72afaf26ed914901688ecdac8ed4 (diff)
Normalize audio when archiving to fixed-point format.
Diffstat (limited to 'libs/ardour/sndfilesource.cc')
-rw-r--r--libs/ardour/sndfilesource.cc30
1 files changed, 28 insertions, 2 deletions
diff --git a/libs/ardour/sndfilesource.cc b/libs/ardour/sndfilesource.cc
index 2e7df7f918..89b497a248 100644
--- a/libs/ardour/sndfilesource.cc
+++ b/libs/ardour/sndfilesource.cc
@@ -36,6 +36,7 @@
#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h>
+#include "ardour/runtime_functions.h"
#include "ardour/sndfilesource.h"
#include "ardour/sndfile_helpers.h"
#include "ardour/utils.h"
@@ -275,16 +276,41 @@ SndFileSource::SndFileSource (Session& s, const AudioFileSource& other, const st
throw failed_constructor();
}
- /* copy file */
Sample buf[8192];
framecnt_t off = 0;
+ float peak = 0;
+ float norm = 1.f;
+
+ /* normalize before converting to fixed point, calc gain factor */
framecnt_t len = other.read (buf, off, 8192, /*channel*/0);
while (len > 0) {
+ peak = compute_peak (buf, len, peak);
+ off += len;
+ len = other.read (buf, off, 8192, /*channel*/0);
+ if (progress) {
+ progress->set_progress (0.5f * (float) off / other.readable_length ());
+ }
+ }
+
+ if (peak > 0) {
+ _gain *= peak;
+ norm = 1.f / peak;
+ }
+
+ /* copy file */
+ off = 0;
+ len = other.read (buf, off, 8192, /*channel*/0);
+ while (len > 0) {
+ if (norm != 1.f) {
+ for (framecnt_t i = 0; i < len; ++i) {
+ buf[i] *= norm;
+ }
+ }
write (buf, len);
off += len;
len = other.read (buf, off, 8192, /*channel*/0);
if (progress) {
- progress->set_progress ((float) off / other.readable_length ());
+ progress->set_progress (0.5f + 0.5f * (float) off / other.readable_length ());
}
}
}