summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2015-12-06 00:21:02 +0000
committerCarl Hetherington <cth@carlh.net>2015-12-06 00:21:02 +0000
commit3bdf34970b512a6421d3251f8c66f285aee0e1d0 (patch)
treeb005c202f14801b9f01ff718186993e46a34487a /src
parent18875fc4ff9f04f57b035a51f151de9920a54a6a (diff)
Clip audio nicely rather than allowing out-of-range samples to cause discontinuities.
Diffstat (limited to 'src')
-rw-r--r--src/sound_asset_writer.cc7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/sound_asset_writer.cc b/src/sound_asset_writer.cc
index 02029f18..f1e17615 100644
--- a/src/sound_asset_writer.cc
+++ b/src/sound_asset_writer.cc
@@ -24,6 +24,8 @@
#include "compose.hpp"
#include "AS_DCP.h"
+using std::min;
+using std::max;
using namespace dcp;
struct SoundAssetWriter::ASDCPState
@@ -63,6 +65,8 @@ SoundAssetWriter::write (float const * const * data, int frames)
{
DCP_ASSERT (!_finalized);
+ static float const clip = 1.0f - (1.0f / pow (2, 23));
+
if (!_started) {
Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc);
if (ASDCP_FAILURE (r)) {
@@ -79,7 +83,8 @@ SoundAssetWriter::write (float const * const * data, int frames)
/* Write one sample per channel */
for (int j = 0; j < _sound_asset->channels(); ++j) {
- int32_t const s = data[j][i] * (1 << 23);
+ /* Convert sample to 24-bit int, clipping if necessary. */
+ int32_t const s = min (clip, max (-clip, data[j][i])) * (1 << 23);
*out++ = (s & 0xff);
*out++ = (s & 0xff00) >> 8;
*out++ = (s & 0xff0000) >> 16;