Fix crash on save of MIDI data.
[ardour.git] / libs / ardour / session_time.cc
index a388e31d80f3056822f6c2c2b8a3ddfad3d11ebe..ae8d07e9eea328640f68be9b9401386c91ce9366 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
-  Copyright (C) 1999-2002 Paul Davis 
+  Copyright (C) 1999-2002 Paul Davis
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 
 */
 
+#ifdef WAF_BUILD
+#include "libardour-config.h"
+#endif
+
 #include <iostream>
 #include <cmath>
 #include <unistd.h>
 
-#include <ardour/timestamps.h>
+#include "ardour/timestamps.h"
 
-#include <pbd/error.h>
-#include <pbd/enumwriter.h>
-#include <pbd/stacktrace.h>
+#include "pbd/error.h"
+#include "pbd/enumwriter.h"
+#include "pbd/stacktrace.h"
 
-#include <ardour/ardour.h>
-#include <ardour/configuration.h>
-#include <ardour/audioengine.h>
-#include <ardour/session.h>
-#include <ardour/tempo.h>
+#include "ardour/ardour.h"
+#include "ardour/configuration.h"
+#include "ardour/audioengine.h"
+#include "ardour/session.h"
+#include "ardour/tempo.h"
 
 #include "i18n.h"
 
+using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
@@ -51,44 +56,44 @@ Session::bbt_time (nframes_t when, BBT_Time& bbt)
 float
 Session::smpte_frames_per_second() const
 {
-       switch (Config->get_smpte_format()) {
-               case smpte_23976: 
+       switch (config.get_smpte_format()) {
+               case smpte_23976:
                        return 23.976;
 
                        break;
-               case smpte_24: 
+               case smpte_24:
                        return 24;
 
                        break;
-               case smpte_24976: 
+               case smpte_24976:
                        return 24.976;
 
                        break;
-               case smpte_25: 
+               case smpte_25:
                        return 25;
 
                        break;
-               case smpte_2997: 
+               case smpte_2997:
                        return 29.97;
 
                        break;
-               case smpte_2997drop: 
+               case smpte_2997drop:
                        return 29.97;
 
                        break;
-               case smpte_30: 
+               case smpte_30:
                        return 30;
 
                        break;
-               case smpte_30drop: 
+               case smpte_30drop:
                        return 30;
 
                        break;
-               case smpte_5994: 
+               case smpte_5994:
                        return 59.94;
 
                        break;
-               case smpte_60: 
+               case smpte_60:
                        return 60;
 
                        break;
@@ -100,44 +105,44 @@ Session::smpte_frames_per_second() const
 bool
 Session::smpte_drop_frames() const
 {
-       switch (Config->get_smpte_format()) {
-               case smpte_23976: 
+       switch (config.get_smpte_format()) {
+               case smpte_23976:
                        return false;
 
                        break;
-               case smpte_24: 
+               case smpte_24:
                        return false;
 
                        break;
-               case smpte_24976: 
+               case smpte_24976:
                        return false;
 
                        break;
-               case smpte_25: 
+               case smpte_25:
                        return false;
 
                        break;
-               case smpte_2997: 
+               case smpte_2997:
                        return false;
 
                        break;
-               case smpte_2997drop: 
+               case smpte_2997drop:
                        return true;
 
                        break;
-               case smpte_30: 
+               case smpte_30:
                        return false;
 
                        break;
-               case smpte_30drop: 
+               case smpte_30drop:
                        return true;
 
                        break;
-               case smpte_5994: 
+               case smpte_5994:
                        return false;
 
                        break;
-               case smpte_60: 
+               case smpte_60:
                        return false;
 
                        break;
@@ -149,7 +154,7 @@ Session::smpte_drop_frames() const
 void
 Session::sync_time_vars ()
 {
-       _current_frame_rate = (nframes_t) round (_base_frame_rate * (1.0 + (Config->get_video_pullup()/100.0)));
+       _current_frame_rate = (nframes_t) round (_base_frame_rate * (1.0 + (config.get_video_pullup()/100.0)));
        _frames_per_smpte_frame = (double) _current_frame_rate / (double) smpte_frames_per_second();
        if (smpte_drop_frames()) {
          _frames_per_hour = (long)(107892 * _frames_per_smpte_frame);
@@ -180,14 +185,6 @@ Session::sync_time_vars ()
        };
 }
 
-int
-Session::set_smpte_format (SmpteFormat format)
-{
-       /* this will trigger any other changes needed */
-       Config->set_smpte_format (format);
-       return 0;
-}
-
 void
 Session::set_smpte_offset (nframes_t off)
 {
@@ -222,9 +219,9 @@ Session::smpte_to_sample( SMPTE::Time& smpte, nframes_t& sample, bool use_offset
                // approx. 0.2 frames too early. This adds up with 0.2 too early for each minute until we are 1.8
                // frames too early at 0:9:0:2 (9 * 0.2 = 1.8). The 10th minute brings us 1.8 frames later again
                // (at end of 0:9:59:29), which sums up to 0 (we are back to zero at 0:10:0:0 :-).
-               // 
+               //
                // In table form:
-               // 
+               //
                // SMPTE value    frames offset   subframes offset   seconds (rounded)  44100 sample (rounded)
                //  0:00:00:00        0.0             0                     0.000                0 (accurate)
                //  0:00:59:29        1.8           144                    60.027          2647177
@@ -260,20 +257,20 @@ Session::smpte_to_sample( SMPTE::Time& smpte, nframes_t& sample, bool use_offset
                nframes_t exceeding_samples = (nframes_t) rint(exceeding_df_frames * _frames_per_smpte_frame);
                sample = base_samples + exceeding_samples;
        } else {
-               /* 
-                  Non drop is easy.. just note the use of 
+               /*
+                  Non drop is easy.. just note the use of
                   rint(smpte.rate) * _frames_per_smpte_frame
-                  (frames per SMPTE second), which is larger than  
+                  (frames per SMPTE second), which is larger than
                   frame_rate() in the non-integer SMPTE rate case.
                */
 
                sample = (nframes_t)rint((((smpte.hours * 60 * 60) + (smpte.minutes * 60) + smpte.seconds) * (rint(smpte.rate) * _frames_per_smpte_frame)) + (smpte.frames * _frames_per_smpte_frame));
        }
-  
+
        if (use_subframes) {
-               sample += (long) (((double)smpte.subframes * _frames_per_smpte_frame) / Config->get_subframes_per_frame());
+               sample += (long) (((double)smpte.subframes * _frames_per_smpte_frame) / config.get_subframes_per_frame());
        }
-  
+
        if (use_offset) {
                if (smpte_offset_negative()) {
                        if (sample >= smpte_offset()) {
@@ -320,11 +317,11 @@ Session::sample_to_smpte( nframes_t sample, SMPTE::Time& smpte, bool use_offset,
                        }
                }
        }
-  
+
        double smpte_frames_left_exact;
        double smpte_frames_fraction;
        unsigned long smpte_frames_left;
-  
+
        // Extract whole hours. Do this to prevent rounding errors with
        // high sample numbers in the calculations that follow.
        smpte.hours = offset_sample / _frames_per_hour;
@@ -333,10 +330,10 @@ Session::sample_to_smpte( nframes_t sample, SMPTE::Time& smpte, bool use_offset,
        // Calculate exact number of (exceeding) smpte frames and fractional frames
        smpte_frames_left_exact = (double) offset_sample / _frames_per_smpte_frame;
        smpte_frames_fraction = smpte_frames_left_exact - floor( smpte_frames_left_exact );
-       smpte.subframes = (long) rint(smpte_frames_fraction * Config->get_subframes_per_frame());
-  
+       smpte.subframes = (long) rint(smpte_frames_fraction * config.get_subframes_per_frame());
+
        // XXX Not sure if this is necessary anymore...
-       if (smpte.subframes == Config->get_subframes_per_frame()) {
+       if (smpte.subframes == config.get_subframes_per_frame()) {
                // This can happen with 24 fps (and 29.97 fps ?)
                smpte_frames_left_exact = ceil( smpte_frames_left_exact );
                smpte.subframes = 0;
@@ -360,7 +357,7 @@ Session::sample_to_smpte( nframes_t sample, SMPTE::Time& smpte, bool use_offset,
                        exceeding_df_frames -= extra_minutes_minus_1 * 1798; // take away the (extra) minutes just found
                        smpte.minutes += extra_minutes_minus_1 + 1; // update with exceeding minutes
                }
-    
+
                // Adjust frame numbering for dropped frames (frame 0 and 1 skipped at start of every minute except every 10th)
                if (smpte.minutes % 10) {
                        // Every minute except every 10th
@@ -417,7 +414,7 @@ Session::smpte_time_subframes (nframes_t when, SMPTE::Time& smpte)
                smpte = last_smpte;
                return;
        }
-  
+
        sample_to_smpte( when, smpte, true /* use_offset */, true /* use_subframes */ );
 
        last_smpte_when = when;
@@ -456,13 +453,13 @@ Session::jack_sync_callback (jack_transport_state_t state,
        switch (state) {
        case JackTransportStopped:
                if (slave && _transport_frame != pos->frame && post_transport_work == 0) {
-                       request_locate (pos->frame, false);
+                       request_locate (pos->frame, false);
                        // cerr << "SYNC: stopped, locate to " << pos->frame << " from " << _transport_frame << endl;
                        return false;
                } else {
                        return true;
                }
-               
+
        case JackTransportStarting:
                // cerr << "SYNC: starting @ " << pos->frame << " a@ " << _transport_frame << " our work = " <<  post_transport_work << " pos matches ? " << (_transport_frame == pos->frame) << endl;
                if (slave) {
@@ -482,16 +479,16 @@ Session::jack_sync_callback (jack_transport_state_t state,
        default:
                error << string_compose (_("Unknown JACK transport state %1 in sync callback"), state)
                      << endmsg;
-       } 
+       }
 
        return true;
 }
 
 void
-Session::jack_timebase_callback (jack_transport_state_t state,
-                                nframes_t nframes,
+Session::jack_timebase_callback (jack_transport_state_t /*state*/,
+                                nframes_t /*nframes*/,
                                 jack_position_t* pos,
-                                int new_position)
+                                int /*new_position*/)
 {
        BBT_Time bbt;
 
@@ -501,12 +498,12 @@ Session::jack_timebase_callback (jack_transport_state_t state,
        pos->valid = JackPositionTimecode;
 
        /* BBT info */
-       
+
        if (_tempo_map) {
 
                TempoMap::Metric metric (_tempo_map->metric_at (_transport_frame));
                _tempo_map->bbt_time_with_metric (_transport_frame, bbt, metric);
-               
+
                pos->bar = bbt.bars;
                pos->beat = bbt.beats;
                pos->tick = bbt.ticks;
@@ -562,16 +559,16 @@ Session::jack_timebase_callback (jack_transport_state_t state,
 
                }
 
-       } 
+       }
 
-#endif         
+#endif
 }
 
 ARDOUR::nframes_t
-Session::convert_to_frames_at (nframes_t position, AnyTime& any)
+Session::convert_to_frames_at (nframes_t /*position*/, AnyTime const & any)
 {
        double secs;
-       
+
        switch (any.type) {
        case AnyTime::BBT:
                return _tempo_map->frame_time ( any.bbt);
@@ -583,7 +580,7 @@ Session::convert_to_frames_at (nframes_t position, AnyTime& any)
                secs += any.smpte.minutes * 60;
                secs += any.smpte.seconds;
                secs += any.smpte.frames / smpte_frames_per_second();
-               if (_smpte_offset_negative) 
+               if (_smpte_offset_negative)
                {
                        return (nframes_t) floor (secs * frame_rate()) - _smpte_offset;
                }