summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-07-11 15:50:18 +0100
committerCarl Hetherington <cth@carlh.net>2013-07-11 15:50:18 +0100
commit4122f932b1dc25f7a4592f49f2c9ab19d63b3a4e (patch)
treeda592cad1e8e7293d22dcc45839de2a0f11a1d68 /src
parentbba1bd9b08eb78bda8cdf7fa1393f3eeb2a504d9 (diff)
Add test for audio delay, and do it in the player rather than the decoder.
Diffstat (limited to 'src')
-rw-r--r--src/lib/audio_buffers.cc12
-rw-r--r--src/lib/audio_buffers.h1
-rw-r--r--src/lib/audio_decoder.cc7
-rw-r--r--src/lib/audio_decoder.h3
-rw-r--r--src/lib/player.cc36
5 files changed, 45 insertions, 14 deletions
diff --git a/src/lib/audio_buffers.cc b/src/lib/audio_buffers.cc
index 403babaf7..6d4eb8514 100644
--- a/src/lib/audio_buffers.cc
+++ b/src/lib/audio_buffers.cc
@@ -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.
diff --git a/src/lib/audio_buffers.h b/src/lib/audio_buffers.h
index 47b8145a1..b450b83ec 100644
--- a/src/lib/audio_buffers.h
+++ b/src/lib/audio_buffers.h
@@ -50,6 +50,7 @@ public:
void make_silent ();
void make_silent (int c);
+ void make_silent (int from, int frames);
void copy_from (AudioBuffers const * from, int frames_to_copy, int read_offset, int write_offset);
void move (int from, int to, int frames);
diff --git a/src/lib/audio_decoder.cc b/src/lib/audio_decoder.cc
index 2fe347cf2..3b97cc16f 100644
--- a/src/lib/audio_decoder.cc
+++ b/src/lib/audio_decoder.cc
@@ -35,17 +35,12 @@ AudioDecoder::AudioDecoder (shared_ptr<const Film> f, shared_ptr<const AudioCont
: Decoder (f)
, _audio_position (0)
{
- _delay_frames = c->audio_delay() * c->content_audio_frame_rate() * f->dcp_audio_frame_rate() / (c->output_audio_frame_rate() * 1000);
+
}
void
AudioDecoder::audio (shared_ptr<const AudioBuffers> data, AudioContent::Frame frame)
{
- frame += _delay_frames;
-
Audio (data, frame);
_audio_position = frame + data->frames ();
- if (_audio_position < 0) {
- _audio_position = 0;
- }
}
diff --git a/src/lib/audio_decoder.h b/src/lib/audio_decoder.h
index a7849b9cc..d3f12ab84 100644
--- a/src/lib/audio_decoder.h
+++ b/src/lib/audio_decoder.h
@@ -44,9 +44,6 @@ protected:
void audio (boost::shared_ptr<const AudioBuffers>, AudioContent::Frame);
AudioContent::Frame _audio_position;
-
-private:
- AudioContent::Frame _delay_frames;
};
#endif
diff --git a/src/lib/player.cc b/src/lib/player.cc
index 18c42296f..6db7ff693 100644
--- a/src/lib/player.cc
+++ b/src/lib/player.cc
@@ -260,6 +260,7 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
shared_ptr<AudioContent> content = dynamic_pointer_cast<AudioContent> (piece->content);
assert (content);
+ /* Resample */
if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
shared_ptr<Resampler> r = resampler (content);
audio = r->run (audio);
@@ -278,15 +279,40 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
audio = dcp_mapped;
+ Time time = content->start() + (frame * TIME_HZ / _film->dcp_audio_frame_rate()) + (content->audio_delay() * TIME_HZ / 1000);
+
+ /* We must cut off anything that comes before the start of all time */
+ if (time < 0) {
+ int const frames = - time * _film->dcp_audio_frame_rate() / TIME_HZ;
+ if (frames >= audio->frames ()) {
+ return;
+ }
+
+ shared_ptr<AudioBuffers> trimmed (new AudioBuffers (audio->channels(), audio->frames() - frames));
+ trimmed->copy_from (audio.get(), audio->frames() - frames, frames, 0);
+
+ audio = trimmed;
+ time = 0;
+ }
+
/* The time of this audio may indicate that some of our buffered audio is not going to
be added to any more, so it can be emitted.
*/
- Time const time = content->start() + (frame * TIME_HZ / _film->dcp_audio_frame_rate());
-
if (time > _audio_position) {
/* We can emit some audio from our buffers */
OutputAudioFrame const N = _film->time_to_audio_frames (time - _audio_position);
+ if (N > _audio_buffers.frames()) {
+ /* We need some extra silence before whatever is in the buffers */
+ _audio_buffers.ensure_size (N);
+ _audio_buffers.move (0, N - _audio_buffers.frames(), _audio_buffers.frames ());
+ _audio_buffers.make_silent (0, _audio_buffers.frames());
+ _audio_buffers.set_frames (N);
+ }
+
+ if (N > _audio_buffers.frames()) {
+ cout << "N=" << N << ", ab=" << _audio_buffers.frames() << "\n";
+ }
assert (N <= _audio_buffers.frames());
shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
emit->copy_from (&_audio_buffers, N, 0, 0);
@@ -318,9 +344,9 @@ Player::flush ()
}
if (_last_resampler) {
- shared_ptr<const AudioBuffers> resamp = _last_resampler->flush ();
- Audio (resamp, _audio_position);
- _audio_position += _film->audio_frames_to_time (resamp->frames ());
+// shared_ptr<const AudioBuffers> resamp = _last_resampler->flush ();
+// Audio (resamp, _audio_position);
+// _audio_position += _film->audio_frames_to_time (resamp->frames ());
}
while (_video_position < _audio_position) {