Some boost::filesystem::path cleanups; tweak for changes to libdcp.
[dcpomatic.git] / src / lib / audio_buffers.cc
index 7590f02d2ed35c49b247864464104c5bbfde4496..e80142b8e518ad2efe66f5f1c1ee4b90f6dcafa6 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <cassert>
 #include <cstring>
+#include <cmath>
 #include <stdexcept>
 #include "audio_buffers.h"
 
@@ -43,9 +44,6 @@ AudioBuffers::AudioBuffers (AudioBuffers const & other)
        copy_from (&other, other._frames, 0, 0);
 }
 
-/* XXX: it's a shame that this is a copy-and-paste of the above;
-   probably fixable with c++0x.
-*/
 AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
 {
        allocate (other->_channels, other->_frames);
@@ -113,13 +111,21 @@ AudioBuffers::data (int c) const
 }
 
 /** Set the number of frames that these AudioBuffers will report themselves
- *  as having.
+ *  as having.  If we reduce the number of frames, the `lost' frames will
+ *  be silenced.
  *  @param f Frames; must be less than or equal to the number of allocated frames.
  */
 void
 AudioBuffers::set_frames (int f)
 {
        assert (f <= _allocated_frames);
+
+       for (int c = 0; c < _channels; ++c) {
+               for (int i = f; i < _frames; ++i) {
+                       _data[c][i] = 0;
+               }
+       }
+       
        _frames = f;
 }
 
@@ -255,3 +261,15 @@ AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int
        }
 }
 
+/** @param dB gain in dB */
+void
+AudioBuffers::apply_gain (float dB)
+{
+       float const linear = pow (10, dB / 20);
+       
+       for (int i = 0; i < _channels; ++i) {
+               for (int j = 0; j < _frames; ++j) {
+                       _data[i][j] *= linear;
+               }
+       }
+}