Fix glitchness at the start by not seeking to -ve times.
[dcpomatic.git] / src / lib / audio_buffers.cc
index cd8fcd35be45ac3f7ecb1d4cee272f58f453f2eb..7b3af91e0069324efb4ca69f5e7c41a19ead512a 100644 (file)
@@ -144,6 +144,18 @@ AudioBuffers::make_silent (int c)
        }
 }
 
+void
+AudioBuffers::make_silent (int from, int frames)
+{
+       assert ((from + frames) <= _allocated_frames);
+
+       for (int c = 0; c < _channels; ++c) {
+               for (int i = from; i < (from + frames); ++i) {
+                       _data[c][i] = 0;
+               }
+       }
+}
+
 /** Copy data from another AudioBuffers to this one.  All channels are copied.
  *  @param from AudioBuffers to copy from; must have the same number of channels as this.
  *  @param frames_to_copy Number of frames to copy.
@@ -193,10 +205,11 @@ AudioBuffers::move (int from, int to, int frames)
 
 /** Add data from from `from', `from_channel' to our channel `to_channel' */
 void
-AudioBuffers::accumulate (AudioBuffers const * from, int from_channel, int to_channel)
+AudioBuffers::accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel)
 {
        int const N = frames ();
        assert (from->frames() == N);
+       assert (to_channel <= _channels);
 
        float* s = from->data (from_channel);
        float* d = _data[to_channel];
@@ -206,6 +219,9 @@ AudioBuffers::accumulate (AudioBuffers const * from, int from_channel, int to_ch
        }
 }
 
+/** Ensure we have space for at least a certain number of frames.  If we extend
+ *  the buffers, fill the new space with silence.
+ */
 void
 AudioBuffers::ensure_size (int frames)
 {
@@ -214,9 +230,27 @@ AudioBuffers::ensure_size (int frames)
        }
 
        for (int i = 0; i < _channels; ++i) {
-               _data[i] = static_cast<float*> (realloc (_data[i], _frames * sizeof (float)));
+               _data[i] = static_cast<float*> (realloc (_data[i], frames * sizeof (float)));
                if (!_data[i]) {
                        throw bad_alloc ();
                }
+               for (int j = _allocated_frames; j < frames; ++j) {
+                       _data[i][j] = 0;
+               }
        }
+
+       _allocated_frames = frames;
 }
+
+void
+AudioBuffers::accumulate_frames (AudioBuffers const * from, int read_offset, int write_offset, int frames)
+{
+       assert (_channels == from->channels ());
+
+       for (int i = 0; i < _channels; ++i) {
+               for (int j = 0; j < frames; ++j) {
+                       _data[i][j + write_offset] += from->data()[i][j + read_offset];
+               }
+       }
+}
+