#include "audio_buffers.h"
#include "exceptions.h"
#include "log.h"
+#include "resampler.h"
#include "i18n.h"
using boost::optional;
using boost::shared_ptr;
-AudioDecoder::AudioDecoder (shared_ptr<const Film> f)
- : Decoder (f)
+AudioDecoder::AudioDecoder (shared_ptr<const Film> film, shared_ptr<const AudioContent> content)
+ : Decoder (film)
, _audio_position (0)
{
-
+ if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
+ _resampler.reset (
+ new Resampler (
+ content->content_audio_frame_rate(),
+ content->output_audio_frame_rate(),
+ content->audio_channels()
+ )
+ );
+ }
}
void
AudioDecoder::audio (shared_ptr<const AudioBuffers> data, AudioContent::Frame frame)
{
- Audio (data, frame);
+ /* XXX: no-one's calling _resampler->flush() again */
+
+ if (_resampler) {
+ data = _resampler->run (data);
+ }
+
+ Audio (data, _audio_position);
_audio_position = frame + data->frames ();
}
#include "audio_content.h"
class AudioBuffers;
+class Resampler;
/** @class AudioDecoder.
* @brief Parent class for audio decoders.
class AudioDecoder : public virtual Decoder
{
public:
- AudioDecoder (boost::shared_ptr<const Film>);
+ AudioDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const AudioContent>);
/** Emitted when some audio data is ready */
boost::signals2::signal<void (boost::shared_ptr<const AudioBuffers>, AudioContent::Frame)> Audio;
protected:
void audio (boost::shared_ptr<const AudioBuffers>, AudioContent::Frame);
+ /** Frame index of next emission (post resampling) */
AudioContent::Frame _audio_position;
+ boost::shared_ptr<Resampler> _resampler;
};
#endif
#include "job.h"
#include "image.h"
#include "ratio.h"
-#include "resampler.h"
#include "log.h"
#include "scaler.h"
cout << "Pass " << *earliest << "\n";
#endif
earliest->decoder->pass ();
-
- if (earliest->decoder->done()) {
- shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (earliest->content);
- assert (ac);
- shared_ptr<Resampler> re = resampler (ac, false);
- if (re) {
- shared_ptr<const AudioBuffers> b = re->flush ();
- if (b->frames ()) {
- process_audio (earliest, b, ac->audio_length ());
- }
- }
- }
}
break;
}
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, true);
- audio = r->run (audio);
- }
-
/* Remap channels */
shared_ptr<AudioBuffers> dcp_mapped (new AudioBuffers (_film->audio_channels(), audio->frames()));
dcp_mapped->make_silent ();
audio = dcp_mapped;
- /* Convert frame to time. After resampling, the frame time (in the DCP rate) will be T_D where
- T_D = frame * DCP_rate / original_rate. Hence the time in seconds is T_D / DCP_rate.
- */
- Time time = content->start() + (frame * TIME_HZ / _film->audio_frame_rate()) + (content->audio_delay() * TIME_HZ / 1000);
+ Time time = content->start()
+ + _film->audio_frames_to_time (frame)
+ + (content->audio_delay() * TIME_HZ / 1000);
/* We must cut off anything that comes before the start of all time */
if (time < 0) {
*/
if (time > _audio_position) {
- /* We can emit some audio from our buffers */
+ /* We can emit some audio from our buffers; this is how many frames */
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.set_frames (N);
}
assert (N <= _audio_buffers.frames());
+
+ /* XXX: not convinced that a copy is necessary here */
shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
emit->copy_from (&_audio_buffers, N, 0, 0);
Audio (emit, _audio_position);
- _audio_position = piece->audio_position = time + _film->audio_frames_to_time (N);
+ _audio_position = piece->audio_position = _audio_position + _film->audio_frames_to_time (N);
/* And remove it from our buffers */
if (_audio_buffers.frames() > N) {
_black_frame->make_black ();
}
-shared_ptr<Resampler>
-Player::resampler (shared_ptr<AudioContent> c, bool create)
-{
- map<shared_ptr<AudioContent>, shared_ptr<Resampler> >::iterator i = _resamplers.find (c);
- if (i != _resamplers.end ()) {
- return i->second;
- }
-
- if (!create) {
- return shared_ptr<Resampler> ();
- }
-
- shared_ptr<Resampler> r (new Resampler (c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()));
- _resamplers[c] = r;
- return r;
-}
-
void
Player::emit_black ()
{
class AudioContent;
class Piece;
class Image;
-class Resampler;
/** @class Player
* @brief A class which can `play' a Playlist; emitting its audio and video.
void flush ();
void emit_black ();
void emit_silence (OutputAudioFrame);
- boost::shared_ptr<Resampler> resampler (boost::shared_ptr<AudioContent>, bool);
void film_changed (Film::Property);
void update_subtitle ();
libdcp::Size _video_container_size;
boost::shared_ptr<Image> _black_frame;
- std::map<boost::shared_ptr<AudioContent>, boost::shared_ptr<Resampler> > _resamplers;
- boost::shared_ptr<Resampler> _last_resampler;
struct {
boost::weak_ptr<Piece> piece;