X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_buffers.cc;h=8d00fa8ba5a2bec7d0d5894cc1ea730d4beedb21;hb=8c11b87490f0e0e75487c856b4f8584472376d26;hp=d387304142462c6245b70aace709c730deb669ae;hpb=e16c8ed02a0cb1f733a990d75a9de1bf50cf89bd;p=dcpomatic.git diff --git a/src/lib/audio_buffers.cc b/src/lib/audio_buffers.cc index d38730414..8d00fa8ba 100644 --- a/src/lib/audio_buffers.cc +++ b/src/lib/audio_buffers.cc @@ -19,6 +19,7 @@ #include #include +#include #include #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 other) { allocate (other->_channels, other->_frames); @@ -75,6 +73,9 @@ AudioBuffers::~AudioBuffers () void AudioBuffers::allocate (int channels, int frames) { + assert (frames >= 0); + assert (channels >= 0); + _channels = channels; _frames = frames; _allocated_frames = frames; @@ -113,13 +114,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; } @@ -197,16 +206,18 @@ AudioBuffers::move (int from, int to, int frames) assert (frames > 0); assert (frames <= _frames); assert ((from + frames) <= _frames); - assert ((to + frames) <= _frames); + assert ((to + frames) <= _allocated_frames); for (int i = 0; i < _channels; ++i) { memmove (_data[i] + to, _data[i] + from, frames * sizeof(float)); } } -/** Add data from from `from', `from_channel' to our channel `to_channel' */ +/** Add data from from `from', `from_channel' to our channel `to_channel'. + * @param gain Linear gain to apply to the data before it is added. + */ void -AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel) +AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain) { int const N = frames (); assert (from->frames() == N); @@ -216,7 +227,7 @@ AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, i float* d = _data[to_channel]; for (int i = 0; i < N; ++i) { - *d++ += *s++; + *d++ += (*s++) * gain; } } @@ -255,3 +266,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; + } + } +}