Give SndfileContent a video frame rate so that it can be specified by the user.
authorCarl Hetherington <cth@carlh.net>
Sat, 22 Mar 2014 16:51:05 +0000 (16:51 +0000)
committerCarl Hetherington <cth@carlh.net>
Sat, 22 Mar 2014 16:51:05 +0000 (16:51 +0000)
ChangeLog
src/lib/sndfile_content.cc
src/lib/sndfile_content.h
src/wx/timing_panel.cc

index f44c248704192eeecfeb4966e899c49df879d50e..23b2b23c4d04146889430dd459c03c2b8c634b51 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2014-03-22  Carl Hetherington  <cth@carlh.net>
 
+       * Allow specification of the video frame rate that a sound file
+       was prepared for.
+
        * Another attempt to fix colour conversion dialog strange behaviour
        on OS X.
 
index 98171a8433ec768fda8ce9bd6bed99ebd8fe9d9f..a8388ab77b24cb1441e1c19a0905e01ec775a193 100644 (file)
@@ -33,6 +33,8 @@ using std::cout;
 using boost::shared_ptr;
 using boost::lexical_cast;
 
+int const SndfileContentProperty::VIDEO_FRAME_RATE = 600;
+
 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
        : Content (f, p)
        , AudioContent (f, p)
@@ -147,13 +149,11 @@ SndfileContent::full_length () const
        shared_ptr<const Film> film = _film.lock ();
        assert (film);
 
-       OutputAudioFrame const len = divide_with_round (audio_length() * output_audio_frame_rate(), content_audio_frame_rate ());
-       
-       /* XXX: this depends on whether, alongside this audio, we are running video slower or faster than
-          it should be.  The calculation above works out the output audio frames assuming that we are just
-          resampling the audio: it would be incomplete if, for example, we were running this audio alongside
-          25fps video that was being run at 24fps.
-       */
+       float const rate = _video_frame_rate.get_value_or (film->video_frame_rate ());
+       OutputAudioFrame const len = divide_with_round (
+               audio_length() * output_audio_frame_rate() * rate,
+               content_audio_frame_rate() * film->video_frame_rate()
+               );
        
        return film->audio_frames_to_time (len);
 }
@@ -177,3 +177,18 @@ SndfileContent::set_audio_mapping (AudioMapping m)
 
        signal_changed (AudioContentProperty::AUDIO_MAPPING);
 }
+
+float
+SndfileContent::video_frame_rate () const
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               if (_video_frame_rate) {
+                       return _video_frame_rate.get ();
+               }
+       }
+
+       shared_ptr<const Film> film = _film.lock ();
+       assert (film);
+       return film->video_frame_rate ();
+}
index 701ff16b24bd0dbbdf2d75e5708e2be3c7ce9e45..a79fb99b6bd01f6497d6e3dd2d2184c2c57d5ec1 100644 (file)
@@ -29,6 +29,12 @@ namespace cxml {
        class Node;
 }
 
+class SndfileContentProperty
+{
+public:
+       static int const VIDEO_FRAME_RATE;
+};
+
 class SndfileContent : public AudioContent
 {
 public:
@@ -69,6 +75,17 @@ public:
                return _audio_mapping;
        }
 
+       void set_video_frame_rate (float r) {
+               {
+                       boost::mutex::scoped_lock lm (_mutex);
+                       _video_frame_rate = r;
+               }
+
+               signal_changed (SndfileContentProperty::VIDEO_FRAME_RATE);
+       }
+
+       float video_frame_rate () const;
+
        void set_audio_mapping (AudioMapping);
        
        static bool valid_file (boost::filesystem::path);
@@ -78,6 +95,10 @@ private:
        AudioContent::Frame _audio_length;
        int _audio_frame_rate;
        AudioMapping _audio_mapping;
+       /** Video frame rate that this audio has been prepared for,
+           if specified.
+       */
+       boost::optional<float> _video_frame_rate;
 };
 
 #endif
index 2321fd0dfdbfb7ac84ce8f1d12b603d9064adcf9..5cefe318af333d35ff68da2c02482325d57721c2 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "lib/content.h"
 #include "lib/image_content.h"
+#include "lib/sndfile_content.h"
 #include "timing_panel.h"
 #include "wx_util.h"
 #include "timecode.h"
@@ -80,37 +81,40 @@ TimingPanel::film_content_changed (int property)
        if (cl.size() == 1) {
                content = cl.front ();
        }
+
+       int const film_video_frame_rate = _editor->film()->video_frame_rate ();
        
        if (property == ContentProperty::POSITION) {
                if (content) {
-                       _position->set (content->position (), _editor->film()->video_frame_rate ());
+                       _position->set (content->position (), film_video_frame_rate);
                } else {
                        _position->set (0, 24);
                }
        } else if (
                property == ContentProperty::LENGTH ||
                property == VideoContentProperty::VIDEO_FRAME_RATE ||
-               property == VideoContentProperty::VIDEO_FRAME_TYPE
+               property == VideoContentProperty::VIDEO_FRAME_TYPE ||
+               property == SndfileContentProperty::VIDEO_FRAME_RATE
                ) {
                if (content) {
-                       _full_length->set (content->full_length (), _editor->film()->video_frame_rate ());
-                       _play_length->set (content->length_after_trim (), _editor->film()->video_frame_rate ());
+                       _full_length->set (content->full_length (), film_video_frame_rate);
+                       _play_length->set (content->length_after_trim (), film_video_frame_rate);
                } else {
                        _full_length->set (0, 24);
                        _play_length->set (0, 24);
                }
        } else if (property == ContentProperty::TRIM_START) {
                if (content) {
-                       _trim_start->set (content->trim_start (), _editor->film()->video_frame_rate ());
-                       _play_length->set (content->length_after_trim (), _editor->film()->video_frame_rate ());
+                       _trim_start->set (content->trim_start (), film_video_frame_rate);
+                       _play_length->set (content->length_after_trim (), film_video_frame_rate);
                } else {
                        _trim_start->set (0, 24);
                        _play_length->set (0, 24);
                }
        } else if (property == ContentProperty::TRIM_END) {
                if (content) {
-                       _trim_end->set (content->trim_end (), _editor->film()->video_frame_rate ());
-                       _play_length->set (content->length_after_trim (), _editor->film()->video_frame_rate ());
+                       _trim_end->set (content->trim_end (), film_video_frame_rate);
+                       _play_length->set (content->length_after_trim (), film_video_frame_rate);
                } else {
                        _trim_end->set (0, 24);
                        _play_length->set (0, 24);
@@ -120,8 +124,11 @@ TimingPanel::film_content_changed (int property)
        if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
                if (content) {
                        shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (content);
+                       shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (content);
                        if (vc) {
                                _video_frame_rate->SetValue (std_to_wx (lexical_cast<string> (vc->video_frame_rate ())));
+                       } else if (sc) {
+                               _video_frame_rate->SetValue (std_to_wx (lexical_cast<string> (sc->video_frame_rate ())));
                        } else {
                                _video_frame_rate->SetValue ("24");
                        }
@@ -131,9 +138,10 @@ TimingPanel::film_content_changed (int property)
        }
 
        shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (content);
+       shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (content);
        _full_length->set_editable (ic && ic->still ());
        _play_length->set_editable (!ic || !ic->still ());
-       _video_frame_rate->Enable (ic && !ic->still ());
+       _video_frame_rate->Enable ((ic && !ic->still ()) || sc);
        _set_video_frame_rate->Enable (false);
 }
 
@@ -200,8 +208,12 @@ TimingPanel::set_video_frame_rate ()
                shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (c.front ());
                if (ic) {
                        ic->set_video_frame_rate (lexical_cast<float> (wx_to_std (_video_frame_rate->GetValue ())));
-                       _set_video_frame_rate->Enable (false);
                }
+               shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (c.front ());
+               if (sc) {
+                       sc->set_video_frame_rate (lexical_cast<float> (wx_to_std (_video_frame_rate->GetValue ())));
+               }
+               _set_video_frame_rate->Enable (false);
        }
 }