2 Copyright (C) 2000-2002 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <glibmm/threads.h>
28 #include "pbd/enumwriter.h"
29 #include "pbd/xml++.h"
30 #include "evoral/Beats.hpp"
32 #include "ardour/debug.h"
33 #include "ardour/lmath.h"
34 #include "ardour/tempo.h"
40 using namespace ARDOUR;
43 using Timecode::BBT_Time;
45 /* _default tempo is 4/4 qtr=120 */
47 Meter TempoMap::_default_meter (4.0, 4.0);
48 Tempo TempoMap::_default_tempo (120.0, 4.0);
51 MetricSection::frame_at_minute (const double& time) const
53 return (framepos_t) floor ((time * 60.0 * _sample_rate) + 0.5);
57 MetricSection::minute_at_frame (const framepos_t& frame) const
59 return (frame / (double) _sample_rate) / 60.0;
62 /***********************************************************************/
65 Meter::frames_per_grid (const Tempo& tempo, framecnt_t sr) const
67 /* This is tempo- and meter-sensitive. The number it returns
68 is based on the interval between any two lines in the
69 grid that is constructed from tempo and meter sections.
71 The return value IS NOT interpretable in terms of "beats".
74 return (60.0 * sr) / (tempo.note_types_per_minute() * (_note_type/tempo.note_type()));
78 Meter::frames_per_bar (const Tempo& tempo, framecnt_t sr) const
80 return frames_per_grid (tempo, sr) * _divisions_per_bar;
83 /***********************************************************************/
85 const string TempoSection::xml_state_node_name = "Tempo";
87 TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
88 : MetricSection (0.0, 0, MusicTime, true, sample_rate)
89 , Tempo (TempoMap::default_tempo())
92 , _locked_to_meter (false)
94 XMLProperty const * prop;
100 _legacy_bbt = BBT_Time (0, 0, 0);
102 if ((prop = node.property ("start")) != 0) {
103 if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
107 /* legacy session - start used to be in bbt*/
110 info << _("Legacy session detected. TempoSection XML node will be altered.") << endmsg;
114 if ((prop = node.property ("pulse")) != 0) {
115 if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1) {
116 error << _("TempoSection XML node has an illegal \"pulse\" value") << endmsg;
122 if ((prop = node.property ("frame")) != 0) {
123 if (sscanf (prop->value().c_str(), "%" PRIu32, &frame) != 1) {
124 error << _("TempoSection XML node has an illegal \"frame\" value") << endmsg;
125 throw failed_constructor();
127 set_minute (minute_at_frame (frame));
131 /* XX replace old beats-per-minute name with note-types-per-minute */
132 if ((prop = node.property ("beats-per-minute")) != 0) {
133 if (sscanf (prop->value().c_str(), "%lf", &_note_types_per_minute) != 1 || _note_types_per_minute < 0.0) {
134 error << _("TempoSection XML node has an illegal \"beats-per-minutee\" value") << endmsg;
135 throw failed_constructor();
139 if ((prop = node.property ("note-type")) == 0) {
140 /* older session, make note type be quarter by default */
143 if (sscanf (prop->value().c_str(), "%lf", &_note_type) != 1 || _note_type < 1.0) {
144 error << _("TempoSection XML node has an illegal \"note-type\" value") << endmsg;
145 throw failed_constructor();
149 if ((prop = node.property ("movable")) == 0) {
150 error << _("TempoSection XML node has no \"movable\" property") << endmsg;
151 throw failed_constructor();
154 set_movable (string_is_affirmative (prop->value()));
156 if ((prop = node.property ("active")) == 0) {
157 warning << _("TempoSection XML node has no \"active\" property") << endmsg;
160 set_active (string_is_affirmative (prop->value()));
163 if ((prop = node.property ("tempo-type")) == 0) {
166 _type = Type (string_2_enum (prop->value(), _type));
169 if ((prop = node.property ("lock-style")) == 0) {
171 set_position_lock_style (MusicTime);
173 set_position_lock_style (AudioTime);
176 set_position_lock_style (PositionLockStyle (string_2_enum (prop->value(), position_lock_style())));
179 if ((prop = node.property ("locked-to-meter")) == 0) {
180 set_locked_to_meter (false);
182 set_locked_to_meter (string_is_affirmative (prop->value()));
187 TempoSection::get_state() const
189 XMLNode *root = new XMLNode (xml_state_node_name);
193 snprintf (buf, sizeof (buf), "%lf", pulse());
194 root->add_property ("pulse", buf);
195 snprintf (buf, sizeof (buf), "%li", frame());
196 root->add_property ("frame", buf);
197 snprintf (buf, sizeof (buf), "%lf", _note_types_per_minute);
198 root->add_property ("beats-per-minute", buf);
199 snprintf (buf, sizeof (buf), "%lf", _note_type);
200 root->add_property ("note-type", buf);
201 snprintf (buf, sizeof (buf), "%s", movable()?"yes":"no");
202 root->add_property ("movable", buf);
203 snprintf (buf, sizeof (buf), "%s", active()?"yes":"no");
204 root->add_property ("active", buf);
205 root->add_property ("tempo-type", enum_2_string (_type));
206 root->add_property ("lock-style", enum_2_string (position_lock_style()));
207 root->add_property ("locked-to-meter", locked_to_meter()?"yes":"no");
213 TempoSection::set_type (Type type)
218 /** returns the Tempo at the session-relative minute.
221 TempoSection::tempo_at_minute (const double& m) const
224 if (_type == Constant || _c_func == 0.0) {
225 return Tempo (note_types_per_minute(), note_type());
228 return Tempo (_tempo_at_time (m - minute()), _note_type);
231 /** returns the session relative minute where the supplied tempo in note types per minute occurs.
232 * @param ntpm the tempo in mote types per minute used to calculate the returned minute
233 * @param p the pulse used to calculate the returned minute for constant tempi
234 * @return the minute at the supplied tempo
236 * note that the note_type is currently ignored in this function. see below.
240 /** if tempoA (120, 4.0) precedes tempoB (120, 8.0),
241 * there should be no ramp between the two even if we are ramped.
242 * in other words a ramp should only place a curve on note_types_per_minute.
243 * we should be able to use Tempo note type here, but the above
244 * complicates things a bit.
247 TempoSection::minute_at_ntpm (const double& ntpm, const double& p) const
249 if (_type == Constant || _c_func == 0.0) {
250 return ((p - pulse()) / pulses_per_minute()) + minute();
253 return _time_at_tempo (ntpm) + minute();
256 /** returns the Tempo at the supplied whole-note pulse.
259 TempoSection::tempo_at_pulse (const double& p) const
262 if (_type == Constant || _c_func == 0.0) {
263 return Tempo (note_types_per_minute(), note_type());
266 return Tempo (_tempo_at_pulse (p - pulse()), _note_type);
269 /** returns the whole-note pulse where a tempo in note types per minute occurs.
270 * constant tempi require minute m.
271 * @param ntpm the note types per minute value used to calculate the returned pulse
272 * @param m the minute used to calculate the returned pulse if the tempo is constant
273 * @return the whole-note pulse at the supplied tempo
275 * note that note_type is currently ignored in this function. see minute_at_tempo().
277 * for constant tempi, this is anaologous to pulse_at_minute().
280 TempoSection::pulse_at_ntpm (const double& ntpm, const double& m) const
282 if (_type == Constant || _c_func == 0.0) {
283 return ((m - minute()) * pulses_per_minute()) + pulse();
286 return _pulse_at_tempo (ntpm) + pulse();
289 /** returns the whole-note pulse at the supplied session-relative minute.
292 TempoSection::pulse_at_minute (const double& m) const
294 if (_type == Constant || _c_func == 0.0) {
295 return ((m - minute()) * pulses_per_minute()) + pulse();
298 return _pulse_at_time (m - minute()) + pulse();
301 /** returns the session-relative minute at the supplied whole-note pulse.
304 TempoSection::minute_at_pulse (const double& p) const
306 if (_type == Constant || _c_func == 0.0) {
307 return ((p - pulse()) / pulses_per_minute()) + minute();
310 return _time_at_pulse (p - pulse()) + minute();
313 /** returns thw whole-note pulse at session frame position f.
314 * @param f the frame position.
315 * @return the position in whole-note pulses corresponding to f
317 * for use with musical units whose granularity is coarser than frames (e.g. ticks)
320 TempoSection::pulse_at_frame (const framepos_t& f) const
322 if (_type == Constant || _c_func == 0.0) {
323 return (minute_at_frame (f - frame()) * pulses_per_minute()) + pulse();
326 return _pulse_at_time (minute_at_frame (f - frame())) + pulse();
330 TempoSection::frame_at_pulse (const double& p) const
332 if (_type == Constant || _c_func == 0.0) {
333 return frame_at_minute (((p - pulse()) / pulses_per_minute()) + minute());
336 return frame_at_minute (_time_at_pulse (p - pulse()) + minute());
344 Tt----|-----------------*|
345 Ta----|--------------|* |
351 _______________|___|____
352 time a t (next tempo)
355 Duration in beats at time a is the integral of some Tempo function.
356 In our case, the Tempo function (Tempo at time t) is
359 with function constant
364 The integral over t of our Tempo function (the beat function, which is the duration in beats at some time t) is:
365 b(t) = T0(e^(ct) - 1) / c
367 To find the time t at beat duration b, we use the inverse function of the beat function (the time function) which can be shown to be:
368 t(b) = log((c.b / T0) + 1) / c
370 The time t at which Tempo T occurs is a as above:
371 t(T) = log(T / T0) / c
373 The beat at which a Tempo T occurs is:
376 The Tempo at which beat b occurs is:
379 We define c for this tempo ramp by placing a new tempo section at some time t after this one.
380 Our problem is that we usually don't know t.
381 We almost always know the duration in beats between this and the new section, so we need to find c in terms of the beat function.
382 Where a = t (i.e. when a is equal to the time of the next tempo section), the beat function reveals:
383 t = b log (Ta / T0) / (T0 (e^(log (Ta / T0)) - 1))
385 By substituting our expanded t as a in the c function above, our problem is reduced to:
386 c = T0 (e^(log (Ta / T0)) - 1) / b
388 Of course the word 'beat' has been left loosely defined above.
389 In music, a beat is defined by the musical pulse (which comes from the tempo)
390 and the meter in use at a particular time (how many pulse divisions there are in one bar).
391 It would be more accurate to substitute the work 'pulse' for 'beat' above.
395 We can now store c for future time calculations.
396 If the following tempo section (the one that defines c in conjunction with this one)
397 is changed or moved, c is no longer valid.
399 The public methods are session-relative.
401 Most of this stuff is taken from this paper:
404 TOOLS FOR DYNAMIC TEMPO CALCULATIONS
407 Zurich University of Arts
408 Institute for Computer Music and Sound Technology
410 https://www.zhdk.ch/fileadmin/data_subsites/data_icst/Downloads/Timegrid/ICST_Tempopolyphony_ICMC07.pdf
414 /** compute this ramp's function constant from some tempo-pulse point
415 * @param end_npm end tempo (in note types per minute)
416 * @param end_pulse duration (pulses into global start) of some other position.
417 * @return the calculated function constant
420 TempoSection::compute_c_func_pulse (const double& end_npm, const double& end_pulse) const
422 double const log_tempo_ratio = log (end_npm / note_types_per_minute());
423 return (note_types_per_minute() * expm1 (log_tempo_ratio)) / ((end_pulse - pulse()) * _note_type);
426 /** compute the function constant from some tempo-time point.
427 * @param end_npm tempo (note types/min.)
428 * @param end_minute distance (in minutes) from session origin
429 * @return the calculated function constant
432 TempoSection::compute_c_func_minute (const double& end_npm, const double& end_minute) const
434 return c_func (end_npm, end_minute - minute());
437 /* position function */
439 TempoSection::a_func (double end_npm, double c_func) const
441 return log (end_npm / note_types_per_minute()) / c_func;
444 /*function constant*/
446 TempoSection::c_func (double end_npm, double end_time) const
448 return log (end_npm / note_types_per_minute()) / end_time;
451 /* tempo in note types per minute at time in minutes */
453 TempoSection::_tempo_at_time (const double& time) const
455 return exp (_c_func * time) * note_types_per_minute();
458 /* time in minutes at tempo in note types per minute */
460 TempoSection::_time_at_tempo (const double& npm) const
462 return log (npm / note_types_per_minute()) / _c_func;
465 /* pulse at tempo in note types per minute */
467 TempoSection::_pulse_at_tempo (const double& npm) const
469 return ((npm - note_types_per_minute()) / _c_func) / _note_type;
472 /* tempo in note types per minute at pulse */
474 TempoSection::_tempo_at_pulse (const double& pulse) const
476 return (pulse * _note_type * _c_func) + note_types_per_minute();
479 /* pulse at time in minutes */
481 TempoSection::_pulse_at_time (const double& time) const
483 return (expm1 (_c_func * time) * (note_types_per_minute() / _c_func)) / _note_type;
486 /* time in minutes at pulse */
488 TempoSection::_time_at_pulse (const double& pulse) const
490 return log1p ((_c_func * pulse * _note_type) / note_types_per_minute()) / _c_func;
493 /***********************************************************************/
495 const string MeterSection::xml_state_node_name = "Meter";
497 MeterSection::MeterSection (const XMLNode& node, const framecnt_t sample_rate)
498 : MetricSection (0.0, 0, MusicTime, false, sample_rate), Meter (TempoMap::default_meter())
500 XMLProperty const * prop;
505 framepos_t frame = 0;
506 pair<double, BBT_Time> start;
508 if ((prop = node.property ("start")) != 0) {
509 if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
513 error << _("MeterSection XML node has an illegal \"start\" value") << endmsg;
515 /* legacy session - start used to be in bbt*/
516 info << _("Legacy session detected - MeterSection XML node will be altered.") << endmsg;
521 if ((prop = node.property ("pulse")) != 0) {
522 if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1) {
523 error << _("MeterSection XML node has an illegal \"pulse\" value") << endmsg;
528 if ((prop = node.property ("beat")) != 0) {
529 if (sscanf (prop->value().c_str(), "%lf", &beat) != 1) {
530 error << _("MeterSection XML node has an illegal \"beat\" value") << endmsg;
536 if ((prop = node.property ("bbt")) == 0) {
537 warning << _("MeterSection XML node has no \"bbt\" property") << endmsg;
538 } else if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
542 error << _("MeterSection XML node has an illegal \"bbt\" value") << endmsg;
543 throw failed_constructor();
549 if ((prop = node.property ("frame")) != 0) {
550 if (sscanf (prop->value().c_str(), "%li", &frame) != 1) {
551 error << _("MeterSection XML node has an illegal \"frame\" value") << endmsg;
552 throw failed_constructor();
554 set_minute (minute_at_frame (frame));
558 /* beats-per-bar is old; divisions-per-bar is new */
560 if ((prop = node.property ("divisions-per-bar")) == 0) {
561 if ((prop = node.property ("beats-per-bar")) == 0) {
562 error << _("MeterSection XML node has no \"beats-per-bar\" or \"divisions-per-bar\" property") << endmsg;
563 throw failed_constructor();
566 if (sscanf (prop->value().c_str(), "%lf", &_divisions_per_bar) != 1 || _divisions_per_bar < 0.0) {
567 error << _("MeterSection XML node has an illegal \"divisions-per-bar\" value") << endmsg;
568 throw failed_constructor();
571 if ((prop = node.property ("note-type")) == 0) {
572 error << _("MeterSection XML node has no \"note-type\" property") << endmsg;
573 throw failed_constructor();
575 if (sscanf (prop->value().c_str(), "%lf", &_note_type) != 1 || _note_type < 0.0) {
576 error << _("MeterSection XML node has an illegal \"note-type\" value") << endmsg;
577 throw failed_constructor();
580 if ((prop = node.property ("movable")) == 0) {
581 error << _("MeterSection XML node has no \"movable\" property") << endmsg;
582 throw failed_constructor();
585 set_movable (string_is_affirmative (prop->value()));
587 if ((prop = node.property ("lock-style")) == 0) {
588 warning << _("MeterSection XML node has no \"lock-style\" property") << endmsg;
590 set_position_lock_style (MusicTime);
592 set_position_lock_style (AudioTime);
595 set_position_lock_style (PositionLockStyle (string_2_enum (prop->value(), position_lock_style())));
600 MeterSection::get_state() const
602 XMLNode *root = new XMLNode (xml_state_node_name);
606 snprintf (buf, sizeof (buf), "%lf", pulse());
607 root->add_property ("pulse", buf);
608 snprintf (buf, sizeof (buf), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
612 root->add_property ("bbt", buf);
613 snprintf (buf, sizeof (buf), "%lf", beat());
614 root->add_property ("beat", buf);
615 snprintf (buf, sizeof (buf), "%lf", _note_type);
616 root->add_property ("note-type", buf);
617 snprintf (buf, sizeof (buf), "%li", frame());
618 root->add_property ("frame", buf);
619 root->add_property ("lock-style", enum_2_string (position_lock_style()));
620 snprintf (buf, sizeof (buf), "%lf", _divisions_per_bar);
621 root->add_property ("divisions-per-bar", buf);
622 snprintf (buf, sizeof (buf), "%s", movable()?"yes":"no");
623 root->add_property ("movable", buf);
628 /***********************************************************************/
632 Tempo determines the rate of musical pulse determined by its components
633 note types per minute - the rate per minute of the whole note divisor _note_type
634 note type - the division of whole notes (pulses) which occur at the rate of note types per minute.
635 Meter divides the musical pulse into measures and beats according to its components
639 TempoSection - translates between time, musical pulse and tempo.
640 has a musical location in whole notes (pulses).
641 has a time location in minutes.
642 Note that 'beats' in Tempo::note_types_per_minute() are in fact note types per minute.
643 (In the rest of tempo map,'beat' usually refers to accumulated BBT beats (pulse and meter based).
645 MeterSection - translates between BBT, meter-based beat and musical pulse.
646 has a musical location in whole notes (pulses)
647 has a musical location in meter-based beats
648 has a musical location in BBT time
649 has a time location expressed in minutes.
651 TempoSection and MeterSection may be locked to either audio or music (position lock style).
652 The lock style determines the location type to be kept as a reference when location is recalculated.
654 The first tempo and meter are special. they must move together, and are locked to audio.
655 Audio locked tempi which lie before the first meter are made inactive.
657 Recomputing the map is the process where the 'missing' location types are calculated.
658 We construct the tempo map by first using the locked location type of each section
659 to determine non-locked location types (pulse or minute position).
660 We then use this map to find the pulse or minute position of each meter (again depending on lock style).
662 Having done this, we can now traverse the Metrics list by pulse or minute
663 to query its relevant meter/tempo.
665 It is important to keep the _metrics in an order that makes sense.
666 Because ramped MusicTime and AudioTime tempos can interact with each other,
667 reordering is frequent. Care must be taken to keep _metrics in a solved state.
668 Solved means ordered by frame or pulse with frame-accurate precision (see check_solved()).
672 Music and audio-locked objects may seem interchangeable on the surface, but when translating
673 between audio samples and beat, remember that a sample is only a quantised approximation
674 of the actual time (in minutes) of a beat.
675 Thus if a gui user points to the frame occupying the start of a music-locked object on 1|3|0, it does not
676 mean that this frame is the actual location in time of 1|3|0.
678 You cannot use a frame measurement to determine beat distance except under special circumstances
679 (e.g. where the user has requested that a beat lie on a SMPTE frame or if the tempo is known to be constant over the duration).
681 This means is that a user operating on a musical grid must supply the desired beat position and/or current beat quantization in order for the
682 sample space the user is operating at to be translated correctly to the object.
684 The current approach is to interpret the supplied frame using the grid division the user has currently selected.
685 If the user has no musical grid set, they are actually operating in sample space (even SMPTE frames are rounded to audio frame), so
686 the supplied audio frame is interpreted as the desired musical location (beat_at_frame()).
688 tldr: Beat, being a function of time, has nothing to do with sample rate, but time quantization can get in the way of precision.
690 When frame_at_beat() is called, the position calculation is performed in pulses and minutes.
691 The result is rounded to audio frames.
692 When beat_at_frame() is called, the frame is converted to minutes, with no rounding performed on the result.
695 frame_at_beat (beat_at_frame (frame)) == frame
697 beat_at_frame (frame_at_beat (beat)) != beat due to the time quantization of frame_at_beat().
699 Doing the second one will result in a beat distance error of up to 0.5 audio samples.
700 frames_between_quarter_notes () eliminats this effect when determining time duration
701 from Beats distance, or instead work in quarter-notes and/or beats and convert to frames last.
703 The above pointless example could instead do:
704 beat_at_quarter_note (quarter_note_at_beat (beat)) to avoid rounding.
706 The Shaggs - Things I Wonder
707 https://www.youtube.com/watch?v=9wQK6zMJOoQ
710 struct MetricSectionSorter {
711 bool operator() (const MetricSection* a, const MetricSection* b) {
712 return a->pulse() < b->pulse();
716 struct MetricSectionFrameSorter {
717 bool operator() (const MetricSection* a, const MetricSection* b) {
718 return a->frame() < b->frame();
722 TempoMap::TempoMap (framecnt_t fr)
725 BBT_Time start (1, 1, 0);
727 TempoSection *t = new TempoSection (0.0, 0.0, _default_tempo.note_types_per_minute(), _default_tempo.note_type(), TempoSection::Ramp, AudioTime, fr);
728 MeterSection *m = new MeterSection (0.0, 0.0, 0.0, start, _default_meter.divisions_per_bar(), _default_meter.note_divisor(), AudioTime, fr);
730 t->set_movable (false);
731 m->set_movable (false);
733 /* note: frame time is correct (zero) for both of these */
735 _metrics.push_back (t);
736 _metrics.push_back (m);
740 TempoMap::~TempoMap ()
742 Metrics::const_iterator d = _metrics.begin();
743 while (d != _metrics.end()) {
751 TempoMap::frame_at_minute (const double time) const
753 return (framepos_t) floor ((time * 60.0 * _frame_rate) + 0.5);
757 TempoMap::minute_at_frame (const framepos_t frame) const
759 return (frame / (double) _frame_rate) / 60.0;
763 TempoMap::remove_tempo (const TempoSection& tempo, bool complete_operation)
765 bool removed = false;
768 Glib::Threads::RWLock::WriterLock lm (lock);
769 if ((removed = remove_tempo_locked (tempo))) {
770 if (complete_operation) {
771 recompute_map (_metrics);
776 if (removed && complete_operation) {
777 PropertyChanged (PropertyChange ());
782 TempoMap::remove_tempo_locked (const TempoSection& tempo)
786 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
787 if (dynamic_cast<TempoSection*> (*i) != 0) {
788 if (tempo.frame() == (*i)->frame()) {
789 if ((*i)->movable()) {
802 TempoMap::remove_meter (const MeterSection& tempo, bool complete_operation)
804 bool removed = false;
807 Glib::Threads::RWLock::WriterLock lm (lock);
808 if ((removed = remove_meter_locked (tempo))) {
809 if (complete_operation) {
810 recompute_map (_metrics);
815 if (removed && complete_operation) {
816 PropertyChanged (PropertyChange ());
821 TempoMap::remove_meter_locked (const MeterSection& meter)
824 if (meter.position_lock_style() == AudioTime) {
825 /* remove meter-locked tempo */
826 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
828 if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
829 if (t->locked_to_meter() && meter.frame() == (*i)->frame()) {
838 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
839 if (dynamic_cast<MeterSection*> (*i) != 0) {
840 if (meter.frame() == (*i)->frame()) {
841 if ((*i)->movable()) {
854 TempoMap::do_insert (MetricSection* section)
856 bool need_add = true;
857 /* we only allow new meters to be inserted on beat 1 of an existing
861 if ((m = dynamic_cast<MeterSection*>(section)) != 0) {
863 if ((m->bbt().beats != 1) || (m->bbt().ticks != 0)) {
865 pair<double, BBT_Time> corrected = make_pair (m->beat(), m->bbt());
866 corrected.second.beats = 1;
867 corrected.second.ticks = 0;
868 corrected.first = beat_at_bbt_locked (_metrics, corrected.second);
869 warning << string_compose (_("Meter changes can only be positioned on the first beat of a bar. Moving from %1 to %2"),
870 m->bbt(), corrected.second) << endmsg;
871 //m->set_pulse (corrected);
875 /* Look for any existing MetricSection that is of the same type and
876 in the same bar as the new one, and remove it before adding
877 the new one. Note that this means that if we find a matching,
878 existing section, we can break out of the loop since we're
879 guaranteed that there is only one such match.
882 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
884 TempoSection* const tempo = dynamic_cast<TempoSection*> (*i);
885 TempoSection* const insert_tempo = dynamic_cast<TempoSection*> (section);
886 MeterSection* const meter = dynamic_cast<MeterSection*> (*i);
887 MeterSection* const insert_meter = dynamic_cast<MeterSection*> (section);
889 if (tempo && insert_tempo) {
892 bool const ipm = insert_tempo->position_lock_style() == MusicTime;
893 if ((ipm && tempo->pulse() == insert_tempo->pulse()) || (!ipm && tempo->frame() == insert_tempo->frame())) {
895 if (!tempo->movable()) {
897 /* can't (re)move this section, so overwrite
898 * its data content (but not its properties as
902 *(dynamic_cast<Tempo*>(*i)) = *(dynamic_cast<Tempo*>(insert_tempo));
903 (*i)->set_position_lock_style (AudioTime);
905 if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
906 t->set_type (insert_tempo->type());
916 } else if (meter && insert_meter) {
920 bool const ipm = insert_meter->position_lock_style() == MusicTime;
922 if ((ipm && meter->beat() == insert_meter->beat()) || (!ipm && meter->frame() == insert_meter->frame())) {
924 if (!meter->movable()) {
926 /* can't (re)move this section, so overwrite
927 * its data content (but not its properties as
931 *(dynamic_cast<Meter*>(*i)) = *(dynamic_cast<Meter*>(insert_meter));
932 (*i)->set_position_lock_style (AudioTime);
942 /* non-matching types, so we don't care */
946 /* Add the given MetricSection, if we didn't just reset an existing
951 MeterSection* const insert_meter = dynamic_cast<MeterSection*> (section);
952 TempoSection* const insert_tempo = dynamic_cast<TempoSection*> (section);
955 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
956 MeterSection* const meter = dynamic_cast<MeterSection*> (*i);
959 bool const ipm = insert_meter->position_lock_style() == MusicTime;
960 if ((ipm && meter->beat() > insert_meter->beat()) || (!ipm && meter->frame() > insert_meter->frame())) {
965 } else if (insert_tempo) {
966 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
967 TempoSection* const tempo = dynamic_cast<TempoSection*> (*i);
970 bool const ipm = insert_tempo->position_lock_style() == MusicTime;
971 if ((ipm && tempo->pulse() > insert_tempo->pulse()) || (!ipm && tempo->frame() > insert_tempo->frame())) {
978 _metrics.insert (i, section);
979 //dump (_metrics, std::cout);
982 /* user supplies the exact pulse if pls == MusicTime */
984 TempoMap::add_tempo (const Tempo& tempo, const double& pulse, const framepos_t& frame, ARDOUR::TempoSection::Type type, PositionLockStyle pls)
986 TempoSection* ts = 0;
988 Glib::Threads::RWLock::WriterLock lm (lock);
989 ts = add_tempo_locked (tempo, pulse, minute_at_frame (frame), type, pls, true);
993 PropertyChanged (PropertyChange ());
999 TempoMap::replace_tempo (const TempoSection& ts, const Tempo& tempo, const double& pulse, const framepos_t& frame, TempoSection::Type type, PositionLockStyle pls)
1001 const bool locked_to_meter = ts.locked_to_meter();
1004 Glib::Threads::RWLock::WriterLock lm (lock);
1005 TempoSection& first (first_tempo());
1006 if (ts.frame() != first.frame()) {
1007 remove_tempo_locked (ts);
1008 add_tempo_locked (tempo, pulse, minute_at_frame (frame), type, pls, true, locked_to_meter);
1010 first.set_type (type);
1011 first.set_pulse (0.0);
1012 first.set_minute (minute_at_frame (frame));
1013 first.set_position_lock_style (AudioTime);
1015 /* cannot move the first tempo section */
1016 *static_cast<Tempo*>(&first) = tempo;
1017 recompute_map (_metrics);
1022 PropertyChanged (PropertyChange ());
1026 TempoMap::add_tempo_locked (const Tempo& tempo, double pulse, double minute
1027 , TempoSection::Type type, PositionLockStyle pls, bool recompute, bool locked_to_meter)
1029 TempoSection* t = new TempoSection (pulse, minute, tempo.note_types_per_minute(), tempo.note_type(), type, pls, _frame_rate);
1030 t->set_locked_to_meter (locked_to_meter);
1031 bool solved = false;
1036 if (pls == AudioTime) {
1037 solved = solve_map_minute (_metrics, t, t->minute());
1039 solved = solve_map_pulse (_metrics, t, t->pulse());
1041 recompute_meters (_metrics);
1044 if (!solved && recompute) {
1045 recompute_map (_metrics);
1052 TempoMap::add_meter (const Meter& meter, const double& beat, const Timecode::BBT_Time& where, PositionLockStyle pls)
1054 MeterSection* m = 0;
1056 Glib::Threads::RWLock::WriterLock lm (lock);
1057 m = add_meter_locked (meter, beat, where, pls, true);
1062 if (DEBUG_ENABLED(DEBUG::TempoMap)) {
1063 dump (_metrics, std::cerr);
1067 PropertyChanged (PropertyChange ());
1072 TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_Time& where, PositionLockStyle pls)
1075 Glib::Threads::RWLock::WriterLock lm (lock);
1076 const double beat = beat_at_bbt_locked (_metrics, where);
1079 remove_meter_locked (ms);
1080 add_meter_locked (meter, beat, where, pls, true);
1082 MeterSection& first (first_meter());
1083 TempoSection& first_t (first_tempo());
1084 /* cannot move the first meter section */
1085 *static_cast<Meter*>(&first) = meter;
1086 first.set_position_lock_style (AudioTime);
1087 first.set_pulse (0.0);
1088 //first.set_minute (minute_at_frame (frame));
1089 pair<double, BBT_Time> beat = make_pair (0.0, BBT_Time (1, 1, 0));
1090 first.set_beat (beat);
1091 first_t.set_minute (first.minute());
1092 first_t.set_pulse (0.0);
1093 first_t.set_position_lock_style (AudioTime);
1094 recompute_map (_metrics);
1098 PropertyChanged (PropertyChange ());
1102 TempoMap::add_meter_locked (const Meter& meter, double beat, const BBT_Time& where, PositionLockStyle pls, bool recompute)
1104 const MeterSection& prev_m = meter_section_at_minute_locked (_metrics, minute_at_beat_locked (_metrics, beat) - minute_at_frame (1));
1105 const double pulse = ((where.bars - prev_m.bbt().bars) * (prev_m.divisions_per_bar() / prev_m.note_divisor())) + prev_m.pulse();
1106 const double time_minutes = minute_at_pulse_locked (_metrics, pulse);
1107 TempoSection* mlt = 0;
1109 if (pls == AudioTime) {
1110 /* add meter-locked tempo */
1111 mlt = add_tempo_locked (tempo_at_minute_locked (_metrics, time_minutes), pulse, time_minutes, TempoSection::Ramp, AudioTime, true, true);
1119 MeterSection* new_meter = new MeterSection (pulse, time_minutes, beat, where, meter.divisions_per_bar(), meter.note_divisor(), pls, _frame_rate);
1120 bool solved = false;
1122 do_insert (new_meter);
1126 if (pls == AudioTime) {
1127 solved = solve_map_minute (_metrics, new_meter, time_minutes);
1129 solved = solve_map_bbt (_metrics, new_meter, where);
1130 /* required due to resetting the pulse of meter-locked tempi above.
1131 Arguably solve_map_bbt() should use solve_map_pulse (_metrics, TempoSection) instead,
1132 but afaict this cannot cause the map to be left unsolved (these tempi are all audio locked).
1134 recompute_map (_metrics);
1138 if (!solved && recompute) {
1139 /* if this has failed to solve, there is little we can do other than to ensure that
1140 the new map is recalculated.
1142 warning << "Adding meter may have left the tempo map unsolved." << endmsg;
1143 recompute_map (_metrics);
1150 TempoMap::change_initial_tempo (double note_types_per_minute, double note_type)
1152 Tempo newtempo (note_types_per_minute, note_type);
1155 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1156 if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
1161 Glib::Threads::RWLock::WriterLock lm (lock);
1162 *((Tempo*) t) = newtempo;
1163 recompute_map (_metrics);
1165 PropertyChanged (PropertyChange ());
1172 TempoMap::change_existing_tempo_at (framepos_t where, double note_types_per_minute, double note_type)
1174 Tempo newtempo (note_types_per_minute, note_type);
1177 TempoSection* first;
1178 Metrics::iterator i;
1180 /* find the TempoSection immediately preceding "where"
1183 for (first = 0, i = _metrics.begin(), prev = 0; i != _metrics.end(); ++i) {
1185 if ((*i)->frame() > where) {
1191 if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
1204 error << string_compose (_("no tempo sections defined in tempo map - cannot change tempo @ %1"), where) << endmsg;
1214 Glib::Threads::RWLock::WriterLock lm (lock);
1215 /* cannot move the first tempo section */
1216 *((Tempo*)prev) = newtempo;
1217 recompute_map (_metrics);
1220 PropertyChanged (PropertyChange ());
1224 TempoMap::first_meter () const
1226 const MeterSection *m = 0;
1228 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1229 if ((m = dynamic_cast<const MeterSection *> (*i)) != 0) {
1234 fatal << _("programming error: no meter section in tempo map!") << endmsg;
1235 abort(); /*NOTREACHED*/
1240 TempoMap::first_meter ()
1242 MeterSection *m = 0;
1244 /* CALLER MUST HOLD LOCK */
1246 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1247 if ((m = dynamic_cast<MeterSection *> (*i)) != 0) {
1252 fatal << _("programming error: no tempo section in tempo map!") << endmsg;
1253 abort(); /*NOTREACHED*/
1258 TempoMap::first_tempo () const
1260 const TempoSection *t = 0;
1262 /* CALLER MUST HOLD LOCK */
1264 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1265 if ((t = dynamic_cast<const TempoSection *> (*i)) != 0) {
1269 if (!t->movable()) {
1275 fatal << _("programming error: no tempo section in tempo map!") << endmsg;
1276 abort(); /*NOTREACHED*/
1281 TempoMap::first_tempo ()
1283 TempoSection *t = 0;
1285 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1286 if ((t = dynamic_cast<TempoSection *> (*i)) != 0) {
1290 if (!t->movable()) {
1296 fatal << _("programming error: no tempo section in tempo map!") << endmsg;
1297 abort(); /*NOTREACHED*/
1301 TempoMap::recompute_tempi (Metrics& metrics)
1303 TempoSection* prev_t = 0;
1305 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1308 if ((*i)->is_tempo()) {
1309 t = static_cast<TempoSection*> (*i);
1313 if (!t->movable()) {
1321 if (t->position_lock_style() == AudioTime) {
1322 prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
1323 if (!t->locked_to_meter()) {
1324 t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
1328 prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
1329 t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
1336 prev_t->set_c_func (0.0);
1339 /* tempos must be positioned correctly.
1340 the current approach is to use a meter's bbt time as its base position unit.
1341 an audio-locked meter requires a recomputation of pulse and beat (but not bbt),
1342 while a music-locked meter requires recomputations of frame pulse and beat (but not bbt)
1345 TempoMap::recompute_meters (Metrics& metrics)
1347 MeterSection* meter = 0;
1348 MeterSection* prev_m = 0;
1350 for (Metrics::const_iterator mi = metrics.begin(); mi != metrics.end(); ++mi) {
1351 if (!(*mi)->is_tempo()) {
1352 meter = static_cast<MeterSection*> (*mi);
1353 if (meter->position_lock_style() == AudioTime) {
1355 pair<double, BBT_Time> b_bbt;
1356 TempoSection* meter_locked_tempo = 0;
1357 for (Metrics::const_iterator ii = metrics.begin(); ii != metrics.end(); ++ii) {
1359 if ((*ii)->is_tempo()) {
1360 t = static_cast<TempoSection*> (*ii);
1361 if ((t->locked_to_meter() || !t->movable()) && t->frame() == meter->frame()) {
1362 meter_locked_tempo = t;
1369 const double beats = (meter->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar();
1370 if (beats + prev_m->beat() != meter->beat()) {
1371 /* reordering caused a bbt change */
1372 b_bbt = make_pair (beats + prev_m->beat()
1373 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
1374 pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
1376 } else if (meter->movable()) {
1377 b_bbt = make_pair (meter->beat(), meter->bbt());
1378 pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
1381 b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
1383 if (meter_locked_tempo) {
1384 meter_locked_tempo->set_pulse (pulse);
1386 meter->set_beat (b_bbt);
1387 meter->set_pulse (pulse);
1392 pair<double, BBT_Time> b_bbt;
1394 const double beats = (meter->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar();
1395 if (beats + prev_m->beat() != meter->beat()) {
1396 /* reordering caused a bbt change */
1397 b_bbt = make_pair (beats + prev_m->beat()
1398 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
1400 b_bbt = make_pair (beats + prev_m->beat(), meter->bbt());
1402 pulse = (beats / prev_m->note_divisor()) + prev_m->pulse();
1404 /* shouldn't happen - the first is audio-locked */
1405 pulse = pulse_at_beat_locked (metrics, meter->beat());
1406 b_bbt = make_pair (meter->beat(), meter->bbt());
1409 meter->set_beat (b_bbt);
1410 meter->set_pulse (pulse);
1411 meter->set_minute (minute_at_pulse_locked (metrics, pulse));
1420 TempoMap::recompute_map (Metrics& metrics, framepos_t end)
1422 /* CALLER MUST HOLD WRITE LOCK */
1424 DEBUG_TRACE (DEBUG::TempoMath, string_compose ("recomputing tempo map, zero to %1\n", end));
1427 /* silly call from Session::process() during startup
1432 recompute_tempi (metrics);
1433 recompute_meters (metrics);
1437 TempoMap::metric_at (framepos_t frame, Metrics::const_iterator* last) const
1439 Glib::Threads::RWLock::ReaderLock lm (lock);
1440 TempoMetric m (first_meter(), first_tempo());
1442 /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
1443 at something, because we insert the default tempo and meter during
1444 TempoMap construction.
1446 now see if we can find better candidates.
1449 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1451 if ((*i)->frame() > frame) {
1465 /* XX meters only */
1467 TempoMap::metric_at (BBT_Time bbt) const
1469 Glib::Threads::RWLock::ReaderLock lm (lock);
1470 TempoMetric m (first_meter(), first_tempo());
1472 /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
1473 at something, because we insert the default tempo and meter during
1474 TempoMap construction.
1476 now see if we can find better candidates.
1479 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1481 if (!(*i)->is_tempo()) {
1482 mw = static_cast<MeterSection*> (*i);
1483 BBT_Time section_start (mw->bbt());
1485 if (section_start.bars > bbt.bars || (section_start.bars == bbt.bars && section_start.beats > bbt.beats)) {
1496 /** Returns the BBT (meter-based) beat corresponding to the supplied frame, possibly returning a negative value.
1497 * @param frame The session frame position.
1498 * @return The beat duration according to the tempo map at the supplied frame.
1500 * If the supplied frame lies before the first meter, the returned beat duration will be negative.
1501 * The returned beat is obtained using the first meter and the continuation of the tempo curve (backwards).
1503 * This function uses both tempo and meter.
1506 TempoMap::beat_at_frame (const framecnt_t& frame) const
1508 Glib::Threads::RWLock::ReaderLock lm (lock);
1510 return beat_at_minute_locked (_metrics, minute_at_frame (frame));
1513 /* This function uses both tempo and meter.*/
1515 TempoMap::beat_at_minute_locked (const Metrics& metrics, const double& minute) const
1517 const TempoSection& ts = tempo_section_at_minute_locked (metrics, minute);
1518 MeterSection* prev_m = 0;
1519 MeterSection* next_m = 0;
1521 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1522 if (!(*i)->is_tempo()) {
1523 if (prev_m && (*i)->minute() > minute) {
1524 next_m = static_cast<MeterSection*> (*i);
1527 prev_m = static_cast<MeterSection*> (*i);
1531 const double beat = prev_m->beat() + (ts.pulse_at_minute (minute) - prev_m->pulse()) * prev_m->note_divisor();
1533 /* audio locked meters fake their beat */
1534 if (next_m && next_m->beat() < beat) {
1535 return next_m->beat();
1541 /** Returns the frame corresponding to the supplied BBT (meter-based) beat.
1542 * @param beat The BBT (meter-based) beat.
1543 * @return The frame duration according to the tempo map at the supplied BBT (meter-based) beat.
1545 * This function uses both tempo and meter.
1548 TempoMap::frame_at_beat (const double& beat) const
1550 Glib::Threads::RWLock::ReaderLock lm (lock);
1552 return frame_at_minute (minute_at_beat_locked (_metrics, beat));
1555 /* meter & tempo section based */
1557 TempoMap::minute_at_beat_locked (const Metrics& metrics, const double& beat) const
1559 MeterSection* prev_m = 0;
1560 TempoSection* prev_t = 0;
1564 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1565 if (!(*i)->is_tempo()) {
1566 m = static_cast<MeterSection*> (*i);
1567 if (prev_m && m->beat() > beat) {
1576 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1577 if ((*i)->is_tempo()) {
1578 t = static_cast<TempoSection*> (*i);
1579 if (prev_t && ((t->pulse() - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat() > beat) {
1587 return prev_t->minute_at_pulse (((beat - prev_m->beat()) / prev_m->note_divisor()) + prev_m->pulse());
1590 /** Returns a Tempo corresponding to the supplied frame position.
1591 * @param frame The audio frame.
1592 * @return a Tempo according to the tempo map at the supplied frame.
1596 TempoMap::tempo_at_frame (const framepos_t& frame) const
1598 Glib::Threads::RWLock::ReaderLock lm (lock);
1600 return tempo_at_minute_locked (_metrics, minute_at_frame (frame));
1604 TempoMap::tempo_at_minute_locked (const Metrics& metrics, const double& minute) const
1606 TempoSection* prev_t = 0;
1610 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1611 if ((*i)->is_tempo()) {
1612 t = static_cast<TempoSection*> (*i);
1616 if ((prev_t) && t->minute() > minute) {
1617 /* t is the section past frame */
1618 return prev_t->tempo_at_minute (minute);
1624 return Tempo (prev_t->note_types_per_minute(), prev_t->note_type());
1627 /** returns the frame at which the supplied tempo occurs, or
1628 * the frame of the last tempo section (search exhausted)
1629 * only the position of the first occurence will be returned
1633 TempoMap::frame_at_tempo (const Tempo& tempo) const
1635 Glib::Threads::RWLock::ReaderLock lm (lock);
1637 return frame_at_minute (minute_at_tempo_locked (_metrics, tempo));
1641 TempoMap::minute_at_tempo_locked (const Metrics& metrics, const Tempo& tempo) const
1643 TempoSection* prev_t = 0;
1644 const double tempo_bpm = tempo.note_types_per_minute();
1646 Metrics::const_iterator i;
1648 for (i = metrics.begin(); i != metrics.end(); ++i) {
1650 if ((*i)->is_tempo()) {
1651 t = static_cast<TempoSection*> (*i);
1657 const double t_bpm = t->note_types_per_minute();
1659 if (t_bpm == tempo_bpm) {
1664 const double prev_t_bpm = prev_t->note_types_per_minute();
1666 if ((t_bpm > tempo_bpm && prev_t_bpm < tempo_bpm) || (t_bpm < tempo_bpm && prev_t_bpm > tempo_bpm)) {
1667 return prev_t->minute_at_ntpm (prev_t->note_types_per_minute(), prev_t->pulse());
1674 return prev_t->minute();
1678 TempoMap::tempo_at_pulse_locked (const Metrics& metrics, const double& pulse) const
1680 TempoSection* prev_t = 0;
1684 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1685 if ((*i)->is_tempo()) {
1686 t = static_cast<TempoSection*> (*i);
1690 if ((prev_t) && t->pulse() > pulse) {
1691 /* t is the section past frame */
1692 return prev_t->tempo_at_pulse (pulse);
1698 return Tempo (prev_t->note_types_per_minute(), prev_t->note_type());
1702 TempoMap::pulse_at_tempo_locked (const Metrics& metrics, const Tempo& tempo) const
1704 TempoSection* prev_t = 0;
1705 const double tempo_bpm = tempo.note_types_per_minute();
1707 Metrics::const_iterator i;
1709 for (i = metrics.begin(); i != metrics.end(); ++i) {
1711 if ((*i)->is_tempo()) {
1712 t = static_cast<TempoSection*> (*i);
1718 const double t_bpm = t->note_types_per_minute();
1720 if (t_bpm == tempo_bpm) {
1725 const double prev_t_bpm = prev_t->note_types_per_minute();
1727 if ((t_bpm > tempo_bpm && prev_t_bpm < tempo_bpm) || (t_bpm < tempo_bpm && prev_t_bpm > tempo_bpm)) {
1728 return prev_t->pulse_at_ntpm (prev_t->note_types_per_minute(), prev_t->minute());
1735 return prev_t->pulse();
1738 /** Returns a Tempo corresponding to the supplied position in quarter-note beats.
1739 * @param qn the position in quarter note beats.
1740 * @return the Tempo at the supplied quarter-note.
1743 TempoMap::tempo_at_quarter_note (const double& qn) const
1745 Glib::Threads::RWLock::ReaderLock lm (lock);
1747 return tempo_at_pulse_locked (_metrics, qn / 4.0);
1750 /** Returns the position in quarter-note beats corresponding to the supplied Tempo.
1751 * @param tempo the tempo.
1752 * @return the position in quarter-note beats where the map bpm
1753 * is equal to that of the Tempo. currently ignores note_type.
1756 TempoMap::quarter_note_at_tempo (const Tempo& tempo) const
1758 Glib::Threads::RWLock::ReaderLock lm (lock);
1760 return pulse_at_tempo_locked (_metrics, tempo) * 4.0;;
1763 /** Returns the whole-note pulse corresponding to the supplied BBT (meter-based) beat.
1764 * @param metrics the list of metric sections used to calculate the pulse.
1765 * @param beat The BBT (meter-based) beat.
1766 * @return the whole-note pulse at the supplied BBT (meter-based) beat.
1768 * a pulse or whole note is the base musical position of a MetricSection.
1769 * it is equivalent to four quarter notes.
1773 TempoMap::pulse_at_beat_locked (const Metrics& metrics, const double& beat) const
1775 const MeterSection* prev_m = &meter_section_at_beat_locked (metrics, beat);
1777 return prev_m->pulse() + ((beat - prev_m->beat()) / prev_m->note_divisor());
1780 /** Returns the BBT (meter-based) beat corresponding to the supplied whole-note pulse .
1781 * @param metrics the list of metric sections used to calculate the beat.
1782 * @param pulse the whole-note pulse.
1783 * @return the meter-based beat at the supplied whole-note pulse.
1785 * a pulse or whole note is the base musical position of a MetricSection.
1786 * it is equivalent to four quarter notes.
1789 TempoMap::beat_at_pulse_locked (const Metrics& metrics, const double& pulse) const
1791 MeterSection* prev_m = 0;
1793 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1795 if (!(*i)->is_tempo()) {
1796 m = static_cast<MeterSection*> (*i);
1797 if (prev_m && m->pulse() > pulse) {
1804 double const ret = ((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat();
1808 /* tempo section based */
1810 TempoMap::pulse_at_minute_locked (const Metrics& metrics, const double& minute) const
1812 /* HOLD (at least) THE READER LOCK */
1813 TempoSection* prev_t = 0;
1815 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1817 if ((*i)->is_tempo()) {
1818 t = static_cast<TempoSection*> (*i);
1822 if (prev_t && t->minute() > minute) {
1823 /*the previous ts is the one containing the frame */
1824 const double ret = prev_t->pulse_at_minute (minute);
1825 /* audio locked section in new meter*/
1826 if (t->pulse() < ret) {
1835 /* treated as constant for this ts */
1836 const double pulses_in_section = ((minute - prev_t->minute()) * prev_t->note_types_per_minute()) / prev_t->note_type();
1838 return pulses_in_section + prev_t->pulse();
1841 /* tempo section based */
1843 TempoMap::minute_at_pulse_locked (const Metrics& metrics, const double& pulse) const
1845 /* HOLD THE READER LOCK */
1847 const TempoSection* prev_t = 0;
1849 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1852 if ((*i)->is_tempo()) {
1853 t = static_cast<TempoSection*> (*i);
1857 if (prev_t && t->pulse() > pulse) {
1858 return prev_t->minute_at_pulse (pulse);
1864 /* must be treated as constant, irrespective of _type */
1865 double const dtime = ((pulse - prev_t->pulse()) * prev_t->note_type()) / prev_t->note_types_per_minute();
1867 return dtime + prev_t->minute();
1870 /** Returns the BBT (meter-based) beat corresponding to the supplied BBT time.
1871 * @param bbt The BBT time (meter-based).
1872 * @return bbt The BBT beat (meter-based) at the supplied BBT time.
1876 TempoMap::beat_at_bbt (const Timecode::BBT_Time& bbt)
1878 Glib::Threads::RWLock::ReaderLock lm (lock);
1879 return beat_at_bbt_locked (_metrics, bbt);
1884 TempoMap::beat_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const
1886 /* CALLER HOLDS READ LOCK */
1888 MeterSection* prev_m = 0;
1890 /* because audio-locked meters have 'fake' integral beats,
1891 there is no pulse offset here.
1895 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1896 if (!(*i)->is_tempo()) {
1897 m = static_cast<MeterSection*> (*i);
1899 const double bars_to_m = (m->beat() - prev_m->beat()) / prev_m->divisions_per_bar();
1900 if ((bars_to_m + (prev_m->bbt().bars - 1)) > (bbt.bars - 1)) {
1908 const double remaining_bars = bbt.bars - prev_m->bbt().bars;
1909 const double remaining_bars_in_beats = remaining_bars * prev_m->divisions_per_bar();
1910 const double ret = remaining_bars_in_beats + prev_m->beat() + (bbt.beats - 1) + (bbt.ticks / BBT_Time::ticks_per_beat);
1915 /** Returns the BBT time corresponding to the supplied BBT (meter-based) beat.
1916 * @param beat The BBT (meter-based) beat.
1917 * @return The BBT time (meter-based) at the supplied meter-based beat.
1921 TempoMap::bbt_at_beat (const double& beat)
1923 Glib::Threads::RWLock::ReaderLock lm (lock);
1924 return bbt_at_beat_locked (_metrics, beat);
1928 TempoMap::bbt_at_beat_locked (const Metrics& metrics, const double& b) const
1930 /* CALLER HOLDS READ LOCK */
1931 MeterSection* prev_m = 0;
1932 const double beats = max (0.0, b);
1934 MeterSection* m = 0;
1936 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1937 if (!(*i)->is_tempo()) {
1938 m = static_cast<MeterSection*> (*i);
1940 if (m->beat() > beats) {
1941 /* this is the meter after the one our beat is on*/
1950 const double beats_in_ms = beats - prev_m->beat();
1951 const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
1952 const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
1953 const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
1954 const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
1958 ret.ticks = (uint32_t) floor (remaining_ticks + 0.5);
1959 ret.beats = (uint32_t) floor (remaining_beats);
1960 ret.bars = total_bars;
1962 /* 0 0 0 to 1 1 0 - based mapping*/
1966 if (ret.ticks >= BBT_Time::ticks_per_beat) {
1968 ret.ticks -= BBT_Time::ticks_per_beat;
1971 if (ret.beats >= prev_m->divisions_per_bar() + 1) {
1979 /** Returns the quarter-note beat corresponding to the supplied BBT time (meter-based).
1980 * @param bbt The BBT time (meter-based).
1981 * @return the quarter note beat at the supplied BBT time
1983 * quarter-notes ignore meter and are based on pulse (the musical unit of MetricSection).
1985 * while the input uses meter, the output does not.
1988 TempoMap::quarter_note_at_bbt (const Timecode::BBT_Time& bbt)
1990 Glib::Threads::RWLock::ReaderLock lm (lock);
1992 return pulse_at_bbt_locked (_metrics, bbt) * 4.0;
1996 TempoMap::quarter_note_at_bbt_rt (const Timecode::BBT_Time& bbt)
1998 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
2001 throw std::logic_error ("TempoMap::quarter_note_at_bbt_rt() could not lock tempo map");
2004 return pulse_at_bbt_locked (_metrics, bbt) * 4.0;
2008 TempoMap::pulse_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const
2010 /* CALLER HOLDS READ LOCK */
2012 MeterSection* prev_m = 0;
2014 /* because audio-locked meters have 'fake' integral beats,
2015 there is no pulse offset here.
2019 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2020 if (!(*i)->is_tempo()) {
2021 m = static_cast<MeterSection*> (*i);
2023 if (m->bbt().bars > bbt.bars) {
2031 const double remaining_bars = bbt.bars - prev_m->bbt().bars;
2032 const double remaining_pulses = remaining_bars * prev_m->divisions_per_bar() / prev_m->note_divisor();
2033 const double ret = remaining_pulses + prev_m->pulse() + (((bbt.beats - 1) + (bbt.ticks / BBT_Time::ticks_per_beat)) / prev_m->note_divisor());
2038 /** Returns the BBT time corresponding to the supplied quarter-note beat.
2039 * @param qn the quarter-note beat.
2040 * @return The BBT time (meter-based) at the supplied meter-based beat.
2042 * quarter-notes ignore meter and are based on pulse (the musical unit of MetricSection).
2046 TempoMap::bbt_at_quarter_note (const double& qn)
2048 Glib::Threads::RWLock::ReaderLock lm (lock);
2050 return bbt_at_pulse_locked (_metrics, qn / 4.0);
2053 /** Returns the BBT time (meter-based) corresponding to the supplied whole-note pulse position.
2054 * @param metrics The list of metric sections used to determine the result.
2055 * @param pulse The whole-note pulse.
2056 * @return The BBT time at the supplied whole-note pulse.
2058 * a pulse or whole note is the basic musical position of a MetricSection.
2059 * it is equivalent to four quarter notes.
2060 * while the output uses meter, the input does not.
2063 TempoMap::bbt_at_pulse_locked (const Metrics& metrics, const double& pulse) const
2065 MeterSection* prev_m = 0;
2067 MeterSection* m = 0;
2069 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2071 if (!(*i)->is_tempo()) {
2072 m = static_cast<MeterSection*> (*i);
2075 double const pulses_to_m = m->pulse() - prev_m->pulse();
2076 if (prev_m->pulse() + pulses_to_m > pulse) {
2077 /* this is the meter after the one our beat is on*/
2086 const double beats_in_ms = (pulse - prev_m->pulse()) * prev_m->note_divisor();
2087 const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
2088 const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
2089 const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
2090 const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
2094 ret.ticks = (uint32_t) floor (remaining_ticks + 0.5);
2095 ret.beats = (uint32_t) floor (remaining_beats);
2096 ret.bars = total_bars;
2098 /* 0 0 0 to 1 1 0 mapping*/
2102 if (ret.ticks >= BBT_Time::ticks_per_beat) {
2104 ret.ticks -= BBT_Time::ticks_per_beat;
2107 if (ret.beats >= prev_m->divisions_per_bar() + 1) {
2115 /** Returns the BBT time corresponding to the supplied frame position.
2116 * @param frame the position in audio samples.
2117 * @return the BBT time at the frame position .
2121 TempoMap::bbt_at_frame (framepos_t frame)
2128 warning << string_compose (_("tempo map was asked for BBT time at frame %1\n"), frame) << endmsg;
2131 Glib::Threads::RWLock::ReaderLock lm (lock);
2133 return bbt_at_minute_locked (_metrics, minute_at_frame (frame));
2137 TempoMap::bbt_at_frame_rt (framepos_t frame)
2139 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
2142 throw std::logic_error ("TempoMap::bbt_at_frame_rt() could not lock tempo map");
2145 return bbt_at_minute_locked (_metrics, minute_at_frame (frame));
2149 TempoMap::bbt_at_minute_locked (const Metrics& metrics, const double& minute) const
2159 const TempoSection& ts = tempo_section_at_minute_locked (metrics, minute);
2160 MeterSection* prev_m = 0;
2161 MeterSection* next_m = 0;
2165 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2166 if (!(*i)->is_tempo()) {
2167 m = static_cast<MeterSection*> (*i);
2168 if (prev_m && m->minute() > minute) {
2176 double beat = prev_m->beat() + (ts.pulse_at_minute (minute) - prev_m->pulse()) * prev_m->note_divisor();
2178 /* handle frame before first meter */
2179 if (minute < prev_m->minute()) {
2182 /* audio locked meters fake their beat */
2183 if (next_m && next_m->beat() < beat) {
2184 beat = next_m->beat();
2187 beat = max (0.0, beat);
2189 const double beats_in_ms = beat - prev_m->beat();
2190 const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
2191 const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
2192 const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
2193 const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
2197 ret.ticks = (uint32_t) floor (remaining_ticks + 0.5);
2198 ret.beats = (uint32_t) floor (remaining_beats);
2199 ret.bars = total_bars;
2201 /* 0 0 0 to 1 1 0 - based mapping*/
2205 if (ret.ticks >= BBT_Time::ticks_per_beat) {
2207 ret.ticks -= BBT_Time::ticks_per_beat;
2210 if (ret.beats >= prev_m->divisions_per_bar() + 1) {
2218 /** Returns the frame position corresponding to the supplied BBT time.
2219 * @param bbt the position in BBT time.
2220 * @return the frame position at bbt.
2224 TempoMap::frame_at_bbt (const BBT_Time& bbt)
2227 warning << string_compose (_("tempo map asked for frame time at bar < 1 (%1)\n"), bbt) << endmsg;
2231 if (bbt.beats < 1) {
2232 throw std::logic_error ("beats are counted from one");
2234 Glib::Threads::RWLock::ReaderLock lm (lock);
2236 return frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
2239 /* meter & tempo section based */
2241 TempoMap::minute_at_bbt_locked (const Metrics& metrics, const BBT_Time& bbt) const
2243 /* HOLD THE READER LOCK */
2245 const double ret = minute_at_beat_locked (metrics, beat_at_bbt_locked (metrics, bbt));
2250 * Returns the quarter-note beat position corresponding to the supplied frame.
2252 * @param frame the position in frames.
2253 * @return The quarter-note position of the supplied frame. Ignores meter.
2257 TempoMap::quarter_note_at_frame (const framepos_t frame) const
2259 Glib::Threads::RWLock::ReaderLock lm (lock);
2261 const double ret = quarter_note_at_minute_locked (_metrics, minute_at_frame (frame));
2267 TempoMap::quarter_note_at_minute_locked (const Metrics& metrics, const double minute) const
2269 const double ret = pulse_at_minute_locked (metrics, minute) * 4.0;
2275 TempoMap::quarter_note_at_frame_rt (const framepos_t frame) const
2277 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
2280 throw std::logic_error ("TempoMap::quarter_note_at_frame_rt() could not lock tempo map");
2283 const double ret = pulse_at_minute_locked (_metrics, minute_at_frame (frame)) * 4.0;
2289 * Returns the frame position corresponding to the supplied quarter-note beat.
2291 * @param quarter_note the quarter-note position.
2292 * @return the frame position of the supplied quarter-note. Ignores meter.
2297 TempoMap::frame_at_quarter_note (const double quarter_note) const
2299 Glib::Threads::RWLock::ReaderLock lm (lock);
2301 const framepos_t ret = frame_at_minute (minute_at_quarter_note_locked (_metrics, quarter_note));
2307 TempoMap::minute_at_quarter_note_locked (const Metrics& metrics, const double quarter_note) const
2309 const double ret = minute_at_pulse_locked (metrics, quarter_note / 4.0);
2314 /** Returns the quarter-note beats corresponding to the supplied BBT (meter-based) beat.
2315 * @param beat The BBT (meter-based) beat.
2316 * @return The quarter-note position of the supplied BBT (meter-based) beat.
2318 * a quarter-note may be compared with and assigned to Evoral::Beats.
2322 TempoMap::quarter_note_at_beat (const double beat)
2324 Glib::Threads::RWLock::ReaderLock lm (lock);
2326 const double ret = quarter_note_at_beat_locked (_metrics, beat);
2332 TempoMap::quarter_note_at_beat_locked (const Metrics& metrics, const double beat) const
2334 const double ret = pulse_at_beat_locked (metrics, beat) * 4.0;
2339 /** Returns the BBT (meter-based) beat position corresponding to the supplied quarter-note beats.
2340 * @param quarter_note The position in quarter-note beats.
2341 * @return the BBT (meter-based) beat position of the supplied quarter-note beats.
2343 * a quarter-note is the musical unit of Evoral::Beats.
2347 TempoMap::beat_at_quarter_note (const double quarter_note)
2349 Glib::Threads::RWLock::ReaderLock lm (lock);
2351 const double ret = beat_at_quarter_note_locked (_metrics, quarter_note);
2357 TempoMap::beat_at_quarter_note_locked (const Metrics& metrics, const double quarter_note) const
2360 return beat_at_pulse_locked (metrics, quarter_note / 4.0);
2363 /** Returns the duration in frames between two supplied quarter-note beat positions.
2364 * @param start the first position in quarter-note beats.
2365 * @param end the end position in quarter-note beats.
2366 * @return the frame distance ober the quarter-note beats duration.
2368 * use this rather than e.g.
2369 * frame_at-quarter_note (end_beats) - frame_at_quarter_note (start_beats).
2370 * frames_between_quarter_notes() doesn't round to audio frames as an intermediate step,
2374 TempoMap::frames_between_quarter_notes (const double start, const double end) const
2376 Glib::Threads::RWLock::ReaderLock lm (lock);
2378 return frame_at_minute (minutes_between_quarter_notes_locked (_metrics, start, end));
2382 TempoMap::minutes_between_quarter_notes_locked (const Metrics& metrics, const double start, const double end) const
2385 return minute_at_pulse_locked (metrics, end / 4.0) - minute_at_pulse_locked (metrics, start / 4.0);
2389 TempoMap::quarter_notes_between_frames (const framecnt_t start, const framecnt_t end) const
2391 Glib::Threads::RWLock::ReaderLock lm (lock);
2393 return quarter_notes_between_frames_locked (_metrics, start, end);
2397 TempoMap::quarter_notes_between_frames_locked (const Metrics& metrics, const framecnt_t start, const framecnt_t end) const
2399 const TempoSection* prev_t = 0;
2401 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2404 if ((*i)->is_tempo()) {
2405 t = static_cast<TempoSection*> (*i);
2409 if (prev_t && t->frame() > start) {
2415 const double start_qn = prev_t->pulse_at_frame (start);
2417 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2420 if ((*i)->is_tempo()) {
2421 t = static_cast<TempoSection*> (*i);
2425 if (prev_t && t->frame() > end) {
2431 const double end_qn = prev_t->pulse_at_frame (end);
2433 return (end_qn - start_qn) * 4.0;
2437 TempoMap::check_solved (const Metrics& metrics) const
2439 TempoSection* prev_t = 0;
2440 MeterSection* prev_m = 0;
2442 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2445 if ((*i)->is_tempo()) {
2446 t = static_cast<TempoSection*> (*i);
2451 /* check ordering */
2452 if ((t->minute() <= prev_t->minute()) || (t->pulse() <= prev_t->pulse())) {
2456 /* precision check ensures tempo and frames align.*/
2457 if (t->frame() != frame_at_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()))) {
2458 if (!t->locked_to_meter()) {
2463 /* gradient limit - who knows what it should be?
2464 things are also ok (if a little chaotic) without this
2466 if (fabs (prev_t->c_func()) > 1000.0) {
2467 //std::cout << "c : " << prev_t->c_func() << std::endl;
2474 if (!(*i)->is_tempo()) {
2475 m = static_cast<MeterSection*> (*i);
2476 if (prev_m && m->position_lock_style() == AudioTime) {
2477 const TempoSection* t = &tempo_section_at_minute_locked (metrics, minute_at_frame (m->frame() - 1));
2478 const double nascent_m_minute = t->minute_at_pulse (m->pulse());
2479 /* Here we check that a preceding section of music doesn't overlap a subsequent one.
2481 if (t && (nascent_m_minute > m->minute() || nascent_m_minute < 0.0)) {
2495 TempoMap::set_active_tempos (const Metrics& metrics, const framepos_t& frame)
2497 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2499 if ((*i)->is_tempo()) {
2500 t = static_cast<TempoSection*> (*i);
2501 if (!t->movable()) {
2502 t->set_active (true);
2505 if (t->movable() && t->active () && t->position_lock_style() == AudioTime && t->frame() < frame) {
2506 t->set_active (false);
2508 } else if (t->movable() && t->position_lock_style() == AudioTime && t->frame() > frame) {
2509 t->set_active (true);
2510 } else if (t->movable() && t->position_lock_style() == AudioTime && t->frame() == frame) {
2519 TempoMap::solve_map_minute (Metrics& imaginary, TempoSection* section, const double& minute)
2521 TempoSection* prev_t = 0;
2522 TempoSection* section_prev = 0;
2523 double first_m_minute = 0.0;
2525 /* can't move a tempo before the first meter */
2526 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2528 if (!(*i)->is_tempo()) {
2529 m = static_cast<MeterSection*> (*i);
2530 if (!m->movable()) {
2531 first_m_minute = m->minute();
2536 if (section->movable() && minute <= first_m_minute) {
2540 section->set_active (true);
2541 section->set_minute (minute);
2543 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2545 if ((*i)->is_tempo()) {
2546 t = static_cast<TempoSection*> (*i);
2553 section_prev = prev_t;
2554 if (t->locked_to_meter()) {
2559 if (t->position_lock_style() == MusicTime) {
2560 prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
2561 t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
2563 prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
2564 if (!t->locked_to_meter()) {
2565 t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
2574 section_prev->set_c_func (section_prev->compute_c_func_minute (section->note_types_per_minute(), minute));
2575 if (!section->locked_to_meter()) {
2576 section->set_pulse (section_prev->pulse_at_ntpm (section->note_types_per_minute(), minute));
2581 recompute_tempi (imaginary);
2583 if (check_solved (imaginary)) {
2586 dunp (imaginary, std::cout);
2590 MetricSectionFrameSorter fcmp;
2591 imaginary.sort (fcmp);
2593 recompute_tempi (imaginary);
2595 if (check_solved (imaginary)) {
2603 TempoMap::solve_map_pulse (Metrics& imaginary, TempoSection* section, const double& pulse)
2605 TempoSection* prev_t = 0;
2606 TempoSection* section_prev = 0;
2608 section->set_pulse (pulse);
2610 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2612 if ((*i)->is_tempo()) {
2613 t = static_cast<TempoSection*> (*i);
2617 if (!t->movable()) {
2624 section_prev = prev_t;
2627 if (t->position_lock_style() == MusicTime) {
2628 prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
2629 t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
2631 prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
2632 if (!t->locked_to_meter()) {
2633 t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
2642 section_prev->set_c_func (section_prev->compute_c_func_pulse (section->note_types_per_minute(), pulse));
2643 section->set_minute (section_prev->minute_at_ntpm (section->note_types_per_minute(), pulse));
2647 recompute_tempi (imaginary);
2649 if (check_solved (imaginary)) {
2652 dunp (imaginary, std::cout);
2656 MetricSectionSorter cmp;
2657 imaginary.sort (cmp);
2659 recompute_tempi (imaginary);
2661 * XX need a restriction here, but only for this case,
2662 * as audio locked tempos don't interact in the same way.
2664 * With music-locked tempos, the solution to cross-dragging can fly off the screen
2666 * |50 bpm |250 bpm |60 bpm
2667 * drag 250 to the pulse after 60->
2668 * a clue: dragging the second 60 <- past the 250 would cause no such problem.
2670 if (check_solved (imaginary)) {
2678 TempoMap::solve_map_minute (Metrics& imaginary, MeterSection* section, const double& minute)
2680 /* disallow moving first meter past any subsequent one, and any movable meter before the first one */
2681 const MeterSection* other = &meter_section_at_minute_locked (imaginary, minute);
2682 if ((!section->movable() && other->movable()) || (!other->movable() && section->movable() && other->minute() >= minute)) {
2686 if (!section->movable()) {
2687 /* lock the first tempo to our first meter */
2688 if (!set_active_tempos (imaginary, section->frame_at_minute (minute))) {
2693 TempoSection* meter_locked_tempo = 0;
2695 for (Metrics::const_iterator ii = imaginary.begin(); ii != imaginary.end(); ++ii) {
2697 if ((*ii)->is_tempo()) {
2698 t = static_cast<TempoSection*> (*ii);
2699 if ((t->locked_to_meter() || !t->movable()) && t->minute() == section->minute()) {
2700 meter_locked_tempo = t;
2706 if (!meter_locked_tempo) {
2710 MeterSection* prev_m = 0;
2712 TempoSection* tempo_copy = copy_metrics_and_point (imaginary, future_map, meter_locked_tempo);
2713 bool solved = false;
2715 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2717 if (!(*i)->is_tempo()) {
2718 m = static_cast<MeterSection*> (*i);
2720 if (prev_m && section->movable()) {
2721 const double beats = (pulse_at_minute_locked (imaginary, minute) - prev_m->pulse()) * prev_m->note_divisor();
2722 if (beats + prev_m->beat() < section->beat()) {
2723 /* set the section pulse according to its musical position,
2724 * as an earlier time than this has been requested.
2726 const double new_pulse = ((section->beat() - prev_m->beat())
2727 / prev_m->note_divisor()) + prev_m->pulse();
2729 tempo_copy->set_position_lock_style (MusicTime);
2730 if ((solved = solve_map_pulse (future_map, tempo_copy, new_pulse))) {
2731 meter_locked_tempo->set_position_lock_style (MusicTime);
2732 section->set_position_lock_style (MusicTime);
2733 section->set_pulse (new_pulse);
2734 solve_map_pulse (imaginary, meter_locked_tempo, new_pulse);
2735 meter_locked_tempo->set_position_lock_style (AudioTime);
2736 section->set_position_lock_style (AudioTime);
2737 section->set_minute (meter_locked_tempo->minute());
2743 Metrics::const_iterator d = future_map.begin();
2744 while (d != future_map.end()) {
2753 /* all is ok. set section's locked tempo if allowed.
2754 possibly disallowed if there is an adjacent audio-locked tempo.
2755 XX this check could possibly go. its never actually happened here.
2757 MeterSection* meter_copy = const_cast<MeterSection*>
2758 (&meter_section_at_minute_locked (future_map, section->minute()));
2760 meter_copy->set_minute (minute);
2762 if ((solved = solve_map_minute (future_map, tempo_copy, minute))) {
2763 section->set_minute (minute);
2764 meter_locked_tempo->set_pulse (((section->beat() - prev_m->beat())
2765 / prev_m->note_divisor()) + prev_m->pulse());
2766 solve_map_minute (imaginary, meter_locked_tempo, minute);
2771 Metrics::const_iterator d = future_map.begin();
2772 while (d != future_map.end()) {
2782 /* not movable (first meter atm) */
2784 tempo_copy->set_minute (minute);
2785 tempo_copy->set_pulse (0.0);
2787 if ((solved = solve_map_minute (future_map, tempo_copy, minute))) {
2788 section->set_minute (minute);
2789 meter_locked_tempo->set_minute (minute);
2790 meter_locked_tempo->set_pulse (0.0);
2791 solve_map_minute (imaginary, meter_locked_tempo, minute);
2796 Metrics::const_iterator d = future_map.begin();
2797 while (d != future_map.end()) {
2806 pair<double, BBT_Time> b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
2807 section->set_beat (b_bbt);
2808 section->set_pulse (0.0);
2818 MetricSectionFrameSorter fcmp;
2819 imaginary.sort (fcmp);
2821 recompute_meters (imaginary);
2827 TempoMap::solve_map_bbt (Metrics& imaginary, MeterSection* section, const BBT_Time& when)
2829 /* disallow setting section to an existing meter's bbt */
2830 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2832 if (!(*i)->is_tempo()) {
2833 m = static_cast<MeterSection*> (*i);
2834 if (m != section && m->bbt().bars == when.bars) {
2840 MeterSection* prev_m = 0;
2841 MeterSection* section_prev = 0;
2843 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2845 if (!(*i)->is_tempo()) {
2846 m = static_cast<MeterSection*> (*i);
2847 pair<double, BBT_Time> b_bbt;
2848 double new_pulse = 0.0;
2850 if (prev_m && m->bbt().bars > when.bars && !section_prev){
2851 section_prev = prev_m;
2852 const double beats = (when.bars - section_prev->bbt().bars) * section_prev->divisions_per_bar();
2853 const double pulse = (beats / section_prev->note_divisor()) + section_prev->pulse();
2854 pair<double, BBT_Time> b_bbt = make_pair (beats + section_prev->beat(), when);
2856 section->set_beat (b_bbt);
2857 section->set_pulse (pulse);
2858 section->set_minute (minute_at_pulse_locked (imaginary, pulse));
2863 if (m->position_lock_style() == AudioTime) {
2864 TempoSection* meter_locked_tempo = 0;
2866 for (Metrics::const_iterator ii = imaginary.begin(); ii != imaginary.end(); ++ii) {
2868 if ((*ii)->is_tempo()) {
2869 t = static_cast<TempoSection*> (*ii);
2870 if ((t->locked_to_meter() || !t->movable()) && t->frame() == m->frame()) {
2871 meter_locked_tempo = t;
2877 if (!meter_locked_tempo) {
2882 const double beats = ((m->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar());
2884 if (beats + prev_m->beat() != m->beat()) {
2885 /* tempo/ meter change caused a change in beat (bar). */
2886 b_bbt = make_pair (beats + prev_m->beat()
2887 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
2888 new_pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
2889 } else if (m->movable()) {
2890 b_bbt = make_pair (m->beat(), m->bbt());
2891 new_pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
2894 b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
2897 meter_locked_tempo->set_pulse (new_pulse);
2898 m->set_beat (b_bbt);
2899 m->set_pulse (new_pulse);
2903 const double beats = ((m->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar());
2904 if (beats + prev_m->beat() != m->beat()) {
2905 /* tempo/ meter change caused a change in beat (bar). */
2906 b_bbt = make_pair (beats + prev_m->beat()
2907 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
2909 b_bbt = make_pair (beats + prev_m->beat()
2912 new_pulse = (beats / prev_m->note_divisor()) + prev_m->pulse();
2913 m->set_beat (b_bbt);
2914 m->set_pulse (new_pulse);
2915 m->set_minute (minute_at_pulse_locked (imaginary, new_pulse));
2922 if (!section_prev) {
2924 const double beats = (when.bars - prev_m->bbt().bars) * prev_m->divisions_per_bar();
2925 const double pulse = (beats / prev_m->note_divisor()) + prev_m->pulse();
2926 pair<double, BBT_Time> b_bbt = make_pair (beats + prev_m->beat(), when);
2928 section->set_beat (b_bbt);
2929 section->set_pulse (pulse);
2930 section->set_minute (minute_at_pulse_locked (imaginary, pulse));
2933 MetricSectionSorter cmp;
2934 imaginary.sort (cmp);
2936 recompute_meters (imaginary);
2941 /** places a copy of _metrics into copy and returns a pointer
2942 * to section's equivalent in copy.
2945 TempoMap::copy_metrics_and_point (const Metrics& metrics, Metrics& copy, TempoSection* section)
2947 TempoSection* ret = 0;
2949 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2952 if ((*i)->is_tempo()) {
2953 t = static_cast<TempoSection*> (*i);
2955 ret = new TempoSection (*t);
2956 copy.push_back (ret);
2960 TempoSection* cp = new TempoSection (*t);
2961 copy.push_back (cp);
2963 if (!(*i)->is_tempo()) {
2964 m = static_cast<MeterSection *> (*i);
2965 MeterSection* cp = new MeterSection (*m);
2966 copy.push_back (cp);
2974 TempoMap::copy_metrics_and_point (const Metrics& metrics, Metrics& copy, MeterSection* section)
2976 MeterSection* ret = 0;
2978 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2981 if ((*i)->is_tempo()) {
2982 t = static_cast<TempoSection*> (*i);
2983 TempoSection* cp = new TempoSection (*t);
2984 copy.push_back (cp);
2987 if (!(*i)->is_tempo()) {
2988 m = static_cast<MeterSection *> (*i);
2990 ret = new MeterSection (*m);
2991 copy.push_back (ret);
2994 MeterSection* cp = new MeterSection (*m);
2995 copy.push_back (cp);
3002 /** answers the question "is this a valid beat position for this tempo section?".
3003 * it returns true if the tempo section can be moved to the requested bbt position,
3004 * leaving the tempo map in a solved state.
3005 * @param ts the tempo section to be moved
3006 * @param bbt the requested new position for the tempo section
3007 * @return true if the tempo section can be moved to the position, otherwise false.
3010 TempoMap::can_solve_bbt (TempoSection* ts, const BBT_Time& bbt)
3013 TempoSection* tempo_copy = 0;
3016 Glib::Threads::RWLock::ReaderLock lm (lock);
3017 tempo_copy = copy_metrics_and_point (_metrics, copy, ts);
3023 const bool ret = solve_map_pulse (copy, tempo_copy, pulse_at_bbt_locked (copy, bbt));
3025 Metrics::const_iterator d = copy.begin();
3026 while (d != copy.end()) {
3035 * This is for a gui that needs to know the pulse or frame of a tempo section if it were to be moved to some bbt time,
3036 * taking any possible reordering as a consequence of this into account.
3037 * @param section - the section to be altered
3038 * @param bbt - the BBT time where the altered tempo will fall
3039 * @return returns - the position in pulses and frames (as a pair) where the new tempo section will lie.
3041 pair<double, framepos_t>
3042 TempoMap::predict_tempo_position (TempoSection* section, const BBT_Time& bbt)
3045 pair<double, framepos_t> ret = make_pair (0.0, 0);
3047 Glib::Threads::RWLock::ReaderLock lm (lock);
3049 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, section);
3051 const double beat = beat_at_bbt_locked (future_map, bbt);
3053 if (solve_map_pulse (future_map, tempo_copy, pulse_at_beat_locked (future_map, beat))) {
3054 ret.first = tempo_copy->pulse();
3055 ret.second = tempo_copy->frame();
3057 ret.first = section->pulse();
3058 ret.second = section->frame();
3061 Metrics::const_iterator d = future_map.begin();
3062 while (d != future_map.end()) {
3069 /** moves a TempoSection to a specified position.
3070 * @param ts - the section to be moved
3071 * @param frame - the new position in frames for the tempo
3072 * @param sub_num - the snap division to use if using musical time.
3074 * if sub_num is non-zero, the frame position is used to calculate an exact
3077 * -1 | snap to bars (meter-based)
3078 * 0 | no snap - use audio frame for musical position
3079 * 1 | snap to meter-based (BBT) beat
3080 * >1 | snap to quarter-note subdivision (i.e. 4 will snap to sixteenth notes)
3082 * this follows the snap convention in the gui.
3083 * if sub_num is zero, the musical position will be taken from the supplied frame.
3086 TempoMap::gui_move_tempo (TempoSection* ts, const framepos_t& frame, const int& sub_num)
3090 if (ts->position_lock_style() == MusicTime) {
3092 /* if we're snapping to a musical grid, set the pulse exactly instead of via the supplied frame. */
3093 Glib::Threads::RWLock::WriterLock lm (lock);
3094 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
3096 tempo_copy->set_position_lock_style (AudioTime);
3098 if (solve_map_minute (future_map, tempo_copy, minute_at_frame (frame))) {
3099 const double beat = exact_beat_at_frame_locked (future_map, frame, sub_num);
3100 const double pulse = pulse_at_beat_locked (future_map, beat);
3102 if (solve_map_pulse (future_map, tempo_copy, pulse)) {
3103 solve_map_pulse (_metrics, ts, pulse);
3104 recompute_meters (_metrics);
3112 Glib::Threads::RWLock::WriterLock lm (lock);
3113 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
3115 if (solve_map_minute (future_map, tempo_copy, minute_at_frame (frame))) {
3117 /* We're moving the object that defines the grid while snapping to it...
3118 * Placing the ts at the beat corresponding to the requested frame may shift the
3119 * grid in such a way that the mouse is left hovering over a completerly different division,
3120 * causing jittering when the mouse next moves (esp. large tempo deltas).
3121 * To avoid this, place the ts at the requested frame in a dummy map
3122 * then find the closest beat subdivision to that frame in the dummy.
3123 * This alters the snap behaviour slightly in that we snap to beat divisions
3124 * in the future map rather than the existing one.
3126 const double beat = exact_beat_at_frame_locked (future_map, frame, sub_num);
3127 const double pulse = pulse_at_beat_locked (future_map, beat);
3129 if (solve_map_pulse (future_map, tempo_copy, pulse)) {
3130 /* snapping to a grid. force MusicTime temporarily. */
3131 ts->set_position_lock_style (MusicTime);
3132 solve_map_pulse (_metrics, ts, pulse);
3133 ts->set_position_lock_style (AudioTime);
3135 recompute_meters (_metrics);
3138 solve_map_minute (_metrics, ts, minute_at_frame (frame));
3139 recompute_meters (_metrics);
3145 Metrics::const_iterator d = future_map.begin();
3146 while (d != future_map.end()) {
3151 MetricPositionChanged (); // Emit Signal
3154 /** moves a MeterSection to a specified position.
3155 * @param ms - the section to be moved
3156 * @param frame - the new position in frames for the meter
3158 * as a meter cannot snap to anything but bars,
3159 * the supplied frame is rounded to the nearest bar, possibly
3160 * leaving the meter position unchanged.
3163 TempoMap::gui_move_meter (MeterSection* ms, const framepos_t& frame)
3167 if (ms->position_lock_style() == AudioTime) {
3170 Glib::Threads::RWLock::WriterLock lm (lock);
3171 MeterSection* copy = copy_metrics_and_point (_metrics, future_map, ms);
3173 if (solve_map_minute (future_map, copy, minute_at_frame (frame))) {
3174 solve_map_minute (_metrics, ms, minute_at_frame (frame));
3175 recompute_tempi (_metrics);
3180 Glib::Threads::RWLock::WriterLock lm (lock);
3181 MeterSection* copy = copy_metrics_and_point (_metrics, future_map, ms);
3183 const double beat = beat_at_minute_locked (_metrics, minute_at_frame (frame));
3184 const Timecode::BBT_Time bbt = bbt_at_beat_locked (_metrics, beat);
3186 if (solve_map_bbt (future_map, copy, bbt)) {
3187 solve_map_bbt (_metrics, ms, bbt);
3188 recompute_tempi (_metrics);
3193 Metrics::const_iterator d = future_map.begin();
3194 while (d != future_map.end()) {
3199 MetricPositionChanged (); // Emit Signal
3203 TempoMap::gui_change_tempo (TempoSection* ts, const Tempo& bpm)
3206 bool can_solve = false;
3208 Glib::Threads::RWLock::WriterLock lm (lock);
3209 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
3210 tempo_copy->set_note_types_per_minute (bpm.note_types_per_minute());
3211 recompute_tempi (future_map);
3213 if (check_solved (future_map)) {
3214 ts->set_note_types_per_minute (bpm.note_types_per_minute());
3215 recompute_map (_metrics);
3220 Metrics::const_iterator d = future_map.begin();
3221 while (d != future_map.end()) {
3226 MetricPositionChanged (); // Emit Signal
3232 TempoMap::gui_dilate_tempo (TempoSection* ts, const framepos_t& frame, const framepos_t& end_frame)
3235 Ts (future prev_t) Tnext
3238 |----------|----------
3245 Glib::Threads::RWLock::WriterLock lm (lock);
3251 TempoSection* prev_t = copy_metrics_and_point (_metrics, future_map, ts);
3252 TempoSection* prev_to_prev_t = 0;
3253 const frameoffset_t fr_off = end_frame - frame;
3255 if (prev_t && prev_t->pulse() > 0.0) {
3256 prev_to_prev_t = const_cast<TempoSection*>(&tempo_section_at_minute_locked (future_map, minute_at_frame (prev_t->frame() - 1)));
3259 TempoSection* next_t = 0;
3260 for (Metrics::iterator i = future_map.begin(); i != future_map.end(); ++i) {
3261 TempoSection* t = 0;
3262 if ((*i)->is_tempo()) {
3263 t = static_cast<TempoSection*> (*i);
3264 if (t->frame() > ts->frame()) {
3270 /* minimum allowed measurement distance in frames */
3271 const framepos_t min_dframe = 2;
3273 /* the change in frames is the result of changing the slope of at most 2 previous tempo sections.
3274 constant to constant is straightforward, as the tempo prev to prev_t has constant slope.
3276 double contribution = 0.0;
3278 if (next_t && prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
3279 contribution = (prev_t->frame() - prev_to_prev_t->frame()) / (double) (next_t->frame() - prev_to_prev_t->frame());
3282 const frameoffset_t prev_t_frame_contribution = fr_off - (contribution * (double) fr_off);
3284 const double start_pulse = prev_t->pulse_at_minute (minute_at_frame (frame));
3285 const double end_pulse = prev_t->pulse_at_minute (minute_at_frame (end_frame));
3289 if (prev_t->type() == TempoSection::Constant || prev_t->c_func() == 0.0) {
3291 if (prev_t->position_lock_style() == MusicTime) {
3292 if (prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
3293 if (frame > prev_to_prev_t->frame() + min_dframe && (frame + prev_t_frame_contribution) > prev_to_prev_t->frame() + min_dframe) {
3295 new_bpm = prev_t->note_types_per_minute() * ((frame - prev_to_prev_t->frame())
3296 / (double) ((frame + prev_t_frame_contribution) - prev_to_prev_t->frame()));
3298 new_bpm = prev_t->note_types_per_minute();
3301 /* prev to prev is irrelevant */
3303 if (start_pulse > prev_t->pulse() && end_pulse > prev_t->pulse()) {
3304 new_bpm = prev_t->note_types_per_minute() * ((start_pulse - prev_t->pulse()) / (end_pulse - prev_t->pulse()));
3306 new_bpm = prev_t->note_types_per_minute();
3311 if (prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
3312 if (frame > prev_to_prev_t->frame() + min_dframe && end_frame > prev_to_prev_t->frame() + min_dframe) {
3314 new_bpm = prev_t->note_types_per_minute() * ((frame - prev_to_prev_t->frame())
3315 / (double) ((end_frame) - prev_to_prev_t->frame()));
3317 new_bpm = prev_t->note_types_per_minute();
3320 /* prev_to_prev_t is irrelevant */
3322 if (frame > prev_t->frame() + min_dframe && end_frame > prev_t->frame() + min_dframe) {
3323 new_bpm = prev_t->note_types_per_minute() * ((frame - prev_t->frame()) / (double) (end_frame - prev_t->frame()));
3325 new_bpm = prev_t->note_types_per_minute();
3331 double frame_ratio = 1.0;
3332 double pulse_ratio = 1.0;
3333 const double pulse_pos = frame;
3335 if (prev_to_prev_t) {
3336 if (pulse_pos > prev_to_prev_t->frame() + min_dframe && (pulse_pos - fr_off) > prev_to_prev_t->frame() + min_dframe) {
3337 frame_ratio = (((pulse_pos - fr_off) - prev_to_prev_t->frame()) / (double) ((pulse_pos) - prev_to_prev_t->frame()));
3339 if (end_pulse > prev_to_prev_t->pulse() && start_pulse > prev_to_prev_t->pulse()) {
3340 pulse_ratio = ((start_pulse - prev_to_prev_t->pulse()) / (end_pulse - prev_to_prev_t->pulse()));
3343 if (pulse_pos > prev_t->frame() + min_dframe && (pulse_pos - fr_off) > prev_t->frame() + min_dframe) {
3344 frame_ratio = (((pulse_pos - fr_off) - prev_t->frame()) / (double) ((pulse_pos) - prev_t->frame()));
3346 pulse_ratio = (start_pulse / end_pulse);
3348 new_bpm = prev_t->note_types_per_minute() * (pulse_ratio * frame_ratio);
3351 /* don't clamp and proceed here.
3352 testing has revealed that this can go negative,
3353 which is an entirely different thing to just being too low.
3355 if (new_bpm < 0.5) {
3358 new_bpm = min (new_bpm, (double) 1000.0);
3359 prev_t->set_note_types_per_minute (new_bpm);
3360 recompute_tempi (future_map);
3361 recompute_meters (future_map);
3363 if (check_solved (future_map)) {
3364 ts->set_note_types_per_minute (new_bpm);
3365 recompute_tempi (_metrics);
3366 recompute_meters (_metrics);
3370 Metrics::const_iterator d = future_map.begin();
3371 while (d != future_map.end()) {
3376 MetricPositionChanged (); // Emit Signal
3379 /** Returns the exact bbt-based beat corresponding to the bar, beat or quarter note subdivision nearest to
3380 * the supplied frame, possibly returning a negative value.
3382 * @param frame The session frame position.
3383 * @param sub_num The subdivision to use when rounding the beat.
3384 * A value of -1 indicates rounding to BBT bar. 1 indicates rounding to BBT beats.
3385 * Positive integers indicate quarter note (non BBT) divisions.
3386 * 0 indicates that the returned beat should not be rounded (equivalent to quarter_note_at_frame()).
3387 * @return The beat position of the supplied frame.
3389 * when working to a musical grid, the use of sub_nom indicates that
3390 * the position should be interpreted musically.
3392 * it effectively snaps to meter bars, meter beats or quarter note divisions
3393 * (as per current gui convention) and returns a musical position independent of frame rate.
3395 * If the supplied frame lies before the first meter, the return will be negative,
3396 * in which case the returned beat uses the first meter (for BBT subdivisions) and
3397 * the continuation of the tempo curve (backwards).
3399 * This function is sensitive to tempo and meter.
3402 TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num)
3404 Glib::Threads::RWLock::ReaderLock lm (lock);
3406 return exact_beat_at_frame_locked (_metrics, frame, sub_num);
3410 TempoMap::exact_beat_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t divisions)
3412 return beat_at_pulse_locked (_metrics, exact_qn_at_frame_locked (metrics, frame, divisions) / 4.0);
3415 /** Returns the exact quarter note corresponding to the bar, beat or quarter note subdivision nearest to
3416 * the supplied frame, possibly returning a negative value.
3418 * @param frame The session frame position.
3419 * @param sub_num The subdivision to use when rounding the quarter note.
3420 * A value of -1 indicates rounding to BBT bar. 1 indicates rounding to BBT beats.
3421 * Positive integers indicate quarter note (non BBT) divisions.
3422 * 0 indicates that the returned quarter note should not be rounded (equivalent to quarter_note_at_frame()).
3423 * @return The quarter note position of the supplied frame.
3425 * When working to a musical grid, the use of sub_nom indicates that
3426 * the frame position should be interpreted musically.
3428 * it effectively snaps to meter bars, meter beats or quarter note divisions
3429 * (as per current gui convention) and returns a musical position independent of frame rate.
3431 * If the supplied frame lies before the first meter, the return will be negative,
3432 * in which case the returned quarter note uses the first meter (for BBT subdivisions) and
3433 * the continuation of the tempo curve (backwards).
3435 * This function is tempo-sensitive.
3438 TempoMap::exact_qn_at_frame (const framepos_t& frame, const int32_t sub_num)
3440 Glib::Threads::RWLock::ReaderLock lm (lock);
3442 return exact_qn_at_frame_locked (_metrics, frame, sub_num);
3446 TempoMap::exact_qn_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t sub_num)
3448 double qn = quarter_note_at_minute_locked (metrics, minute_at_frame (frame));
3451 qn = floor (qn) + (floor (((qn - floor (qn)) * (double) sub_num) + 0.5) / sub_num);
3452 } else if (sub_num == 1) {
3453 /* the gui requested exact musical (BBT) beat */
3454 qn = quarter_note_at_beat_locked (metrics, floor (beat_at_minute_locked (metrics, minute_at_frame (frame)) + 0.5));
3455 } else if (sub_num == -1) {
3457 Timecode::BBT_Time bbt = bbt_at_pulse_locked (metrics, qn / 4.0);
3461 const double prev_b = pulse_at_bbt_locked (metrics, bbt) * 4.0;
3463 const double next_b = pulse_at_bbt_locked (metrics, bbt) * 4.0;
3465 if ((qn - prev_b) > (next_b - prev_b) / 2.0) {
3475 /** returns the frame duration of the supplied BBT time at a specified frame position in the tempo map.
3476 * @param pos the frame position in the tempo map.
3477 * @param bbt the distance in BBT time from pos to calculate.
3478 * @param dir the rounding direction..
3479 * @return the duration in frames between pos and bbt
3482 TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir)
3484 Glib::Threads::RWLock::ReaderLock lm (lock);
3486 BBT_Time pos_bbt = bbt_at_minute_locked (_metrics, minute_at_frame (pos));
3487 const framecnt_t offset = frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
3488 const double divisions = meter_section_at_minute_locked (_metrics, minute_at_frame (pos)).divisions_per_bar();
3491 pos_bbt.bars += bbt.bars;
3493 pos_bbt.ticks += bbt.ticks;
3494 if ((double) pos_bbt.ticks > BBT_Time::ticks_per_beat) {
3496 pos_bbt.ticks -= BBT_Time::ticks_per_beat;
3499 pos_bbt.beats += bbt.beats;
3500 if ((double) pos_bbt.beats > divisions) {
3502 pos_bbt.beats -= divisions;
3505 return frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt)) - offset;
3507 pos_bbt.bars -= bbt.bars;
3509 if (pos_bbt.ticks < bbt.ticks) {
3510 if (pos_bbt.beats == 1) {
3512 pos_bbt.beats = divisions;
3516 pos_bbt.ticks = BBT_Time::ticks_per_beat - (bbt.ticks - pos_bbt.ticks);
3518 pos_bbt.ticks -= bbt.ticks;
3521 if (pos_bbt.beats <= bbt.beats) {
3523 pos_bbt.beats = divisions - (bbt.beats - pos_bbt.beats);
3525 pos_bbt.beats -= bbt.beats;
3528 return offset - frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
3535 TempoMap::round_to_bar (framepos_t fr, RoundMode dir)
3537 return round_to_type (fr, dir, Bar);
3541 TempoMap::round_to_beat (framepos_t fr, RoundMode dir)
3543 return round_to_type (fr, dir, Beat);
3547 TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, RoundMode dir)
3549 Glib::Threads::RWLock::ReaderLock lm (lock);
3550 uint32_t ticks = (uint32_t) floor (max (0.0, beat_at_minute_locked (_metrics, minute_at_frame (fr))) * BBT_Time::ticks_per_beat);
3551 uint32_t beats = (uint32_t) floor (ticks / BBT_Time::ticks_per_beat);
3552 uint32_t ticks_one_subdivisions_worth = (uint32_t) BBT_Time::ticks_per_beat / sub_num;
3554 ticks -= beats * BBT_Time::ticks_per_beat;
3557 /* round to next (or same iff dir == RoundUpMaybe) */
3559 uint32_t mod = ticks % ticks_one_subdivisions_worth;
3561 if (mod == 0 && dir == RoundUpMaybe) {
3562 /* right on the subdivision, which is fine, so do nothing */
3564 } else if (mod == 0) {
3565 /* right on the subdivision, so the difference is just the subdivision ticks */
3566 ticks += ticks_one_subdivisions_worth;
3569 /* not on subdivision, compute distance to next subdivision */
3571 ticks += ticks_one_subdivisions_worth - mod;
3574 if (ticks >= BBT_Time::ticks_per_beat) {
3575 ticks -= BBT_Time::ticks_per_beat;
3577 } else if (dir < 0) {
3579 /* round to previous (or same iff dir == RoundDownMaybe) */
3581 uint32_t difference = ticks % ticks_one_subdivisions_worth;
3583 if (difference == 0 && dir == RoundDownAlways) {
3584 /* right on the subdivision, but force-rounding down,
3585 so the difference is just the subdivision ticks */
3586 difference = ticks_one_subdivisions_worth;
3589 if (ticks < difference) {
3590 ticks = BBT_Time::ticks_per_beat - ticks;
3592 ticks -= difference;
3596 /* round to nearest */
3599 /* compute the distance to the previous and next subdivision */
3601 if ((rem = fmod ((double) ticks, (double) ticks_one_subdivisions_worth)) > ticks_one_subdivisions_worth/2.0) {
3603 /* closer to the next subdivision, so shift forward */
3605 ticks = lrint (ticks + (ticks_one_subdivisions_worth - rem));
3607 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved forward to %1\n", ticks));
3609 if (ticks > BBT_Time::ticks_per_beat) {
3611 ticks -= BBT_Time::ticks_per_beat;
3612 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("fold beat to %1\n", beats));
3615 } else if (rem > 0) {
3617 /* closer to previous subdivision, so shift backward */
3621 /* can't go backwards past zero, so ... */
3624 /* step back to previous beat */
3626 ticks = lrint (BBT_Time::ticks_per_beat - rem);
3627 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("step back beat to %1\n", beats));
3629 ticks = lrint (ticks - rem);
3630 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved backward to %1\n", ticks));
3633 /* on the subdivision, do nothing */
3637 const framepos_t ret_frame = frame_at_minute (minute_at_beat_locked (_metrics, beats + (ticks / BBT_Time::ticks_per_beat)));
3643 TempoMap::round_to_quarter_note_subdivision (framepos_t fr, int sub_num, RoundMode dir)
3645 Glib::Threads::RWLock::ReaderLock lm (lock);
3646 uint32_t ticks = (uint32_t) floor (max (0.0, quarter_note_at_minute_locked (_metrics, minute_at_frame (fr))) * BBT_Time::ticks_per_beat);
3647 uint32_t beats = (uint32_t) floor (ticks / BBT_Time::ticks_per_beat);
3648 uint32_t ticks_one_subdivisions_worth = (uint32_t) BBT_Time::ticks_per_beat / sub_num;
3650 ticks -= beats * BBT_Time::ticks_per_beat;
3653 /* round to next (or same iff dir == RoundUpMaybe) */
3655 uint32_t mod = ticks % ticks_one_subdivisions_worth;
3657 if (mod == 0 && dir == RoundUpMaybe) {
3658 /* right on the subdivision, which is fine, so do nothing */
3660 } else if (mod == 0) {
3661 /* right on the subdivision, so the difference is just the subdivision ticks */
3662 ticks += ticks_one_subdivisions_worth;
3665 /* not on subdivision, compute distance to next subdivision */
3667 ticks += ticks_one_subdivisions_worth - mod;
3670 if (ticks >= BBT_Time::ticks_per_beat) {
3671 ticks -= BBT_Time::ticks_per_beat;
3673 } else if (dir < 0) {
3675 /* round to previous (or same iff dir == RoundDownMaybe) */
3677 uint32_t difference = ticks % ticks_one_subdivisions_worth;
3679 if (difference == 0 && dir == RoundDownAlways) {
3680 /* right on the subdivision, but force-rounding down,
3681 so the difference is just the subdivision ticks */
3682 difference = ticks_one_subdivisions_worth;
3685 if (ticks < difference) {
3686 ticks = BBT_Time::ticks_per_beat - ticks;
3688 ticks -= difference;
3692 /* round to nearest */
3695 /* compute the distance to the previous and next subdivision */
3697 if ((rem = fmod ((double) ticks, (double) ticks_one_subdivisions_worth)) > ticks_one_subdivisions_worth/2.0) {
3699 /* closer to the next subdivision, so shift forward */
3701 ticks = lrint (ticks + (ticks_one_subdivisions_worth - rem));
3703 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved forward to %1\n", ticks));
3705 if (ticks > BBT_Time::ticks_per_beat) {
3707 ticks -= BBT_Time::ticks_per_beat;
3708 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("fold beat to %1\n", beats));
3711 } else if (rem > 0) {
3713 /* closer to previous subdivision, so shift backward */
3717 /* can't go backwards past zero, so ... */
3720 /* step back to previous beat */
3722 ticks = lrint (BBT_Time::ticks_per_beat - rem);
3723 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("step back beat to %1\n", beats));
3725 ticks = lrint (ticks - rem);
3726 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved backward to %1\n", ticks));
3729 /* on the subdivision, do nothing */
3733 const framepos_t ret_frame = frame_at_minute (minute_at_quarter_note_locked (_metrics, beats + (ticks / BBT_Time::ticks_per_beat)));
3739 TempoMap::round_to_type (framepos_t frame, RoundMode dir, BBTPointType type)
3741 Glib::Threads::RWLock::ReaderLock lm (lock);
3743 const double beat_at_framepos = max (0.0, beat_at_minute_locked (_metrics, minute_at_frame (frame)));
3744 BBT_Time bbt (bbt_at_beat_locked (_metrics, beat_at_framepos));
3749 /* find bar previous to 'frame' */
3752 return frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3754 } else if (dir > 0) {
3755 /* find bar following 'frame' */
3759 return frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3761 /* true rounding: find nearest bar */
3762 framepos_t raw_ft = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3765 framepos_t prev_ft = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3767 framepos_t next_ft = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3769 if ((raw_ft - prev_ft) > (next_ft - prev_ft) / 2) {
3780 return frame_at_minute (minute_at_beat_locked (_metrics, floor (beat_at_framepos)));
3781 } else if (dir > 0) {
3782 return frame_at_minute (minute_at_beat_locked (_metrics, ceil (beat_at_framepos)));
3784 return frame_at_minute (minute_at_beat_locked (_metrics, floor (beat_at_framepos + 0.5)));
3793 TempoMap::get_grid (vector<TempoMap::BBTPoint>& points,
3794 framepos_t lower, framepos_t upper, uint32_t bar_mod)
3796 Glib::Threads::RWLock::ReaderLock lm (lock);
3797 int32_t cnt = ceil (beat_at_minute_locked (_metrics, minute_at_frame (lower)));
3799 /* although the map handles negative beats, bbt doesn't. */
3804 if (minute_at_beat_locked (_metrics, cnt) >= minute_at_frame (upper)) {
3808 while (pos >= 0 && pos < upper) {
3809 pos = frame_at_minute (minute_at_beat_locked (_metrics, cnt));
3810 const TempoSection tempo = tempo_section_at_minute_locked (_metrics, minute_at_frame (pos));
3811 const MeterSection meter = meter_section_at_minute_locked (_metrics, minute_at_frame (pos));
3812 const BBT_Time bbt = bbt_at_beat_locked (_metrics, cnt);
3813 points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, tempo.c_func()));
3817 BBT_Time bbt = bbt_at_minute_locked (_metrics, minute_at_frame (lower));
3822 bbt.bars -= bbt.bars % bar_mod;
3826 while (pos >= 0 && pos < upper) {
3827 pos = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3828 const TempoSection tempo = tempo_section_at_minute_locked (_metrics, minute_at_frame (pos));
3829 const MeterSection meter = meter_section_at_minute_locked (_metrics, minute_at_frame (pos));
3830 points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, tempo.c_func()));
3831 bbt.bars += bar_mod;
3837 TempoMap::tempo_section_at_frame (framepos_t frame) const
3839 Glib::Threads::RWLock::ReaderLock lm (lock);
3841 return tempo_section_at_minute_locked (_metrics, minute_at_frame (frame));
3845 TempoMap::tempo_section_at_minute_locked (const Metrics& metrics, double minute) const
3847 TempoSection* prev = 0;
3851 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
3853 if ((*i)->is_tempo()) {
3854 t = static_cast<TempoSection*> (*i);
3858 if (prev && t->minute() > minute) {
3868 abort(); /*NOTREACHED*/
3875 TempoMap::tempo_section_at_beat_locked (const Metrics& metrics, const double& beat) const
3877 TempoSection* prev_t = 0;
3878 const MeterSection* prev_m = &meter_section_at_beat_locked (metrics, beat);
3882 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
3883 if ((*i)->is_tempo()) {
3884 t = static_cast<TempoSection*> (*i);
3885 if (prev_t && ((t->pulse() - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat() > beat) {
3895 /* don't use this to calculate length (the tempo is only correct for this frame).
3896 do that stuff based on the beat_at_frame and frame_at_beat api
3899 TempoMap::frames_per_quarter_note_at (const framepos_t& frame, const framecnt_t& sr) const
3901 Glib::Threads::RWLock::ReaderLock lm (lock);
3903 const TempoSection* ts_at = 0;
3904 const TempoSection* ts_after = 0;
3905 Metrics::const_iterator i;
3908 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
3910 if ((*i)->is_tempo()) {
3911 t = static_cast<TempoSection*> (*i);
3915 if (ts_at && (*i)->frame() > frame) {
3924 return (60.0 * _frame_rate) / ts_at->tempo_at_minute (minute_at_frame (frame)).quarter_notes_per_minute();
3926 /* must be treated as constant tempo */
3927 return ts_at->frames_per_quarter_note (_frame_rate);
3931 TempoMap::meter_section_at_frame (framepos_t frame) const
3933 Glib::Threads::RWLock::ReaderLock lm (lock);
3934 return meter_section_at_minute_locked (_metrics, minute_at_frame (frame));
3938 TempoMap::meter_section_at_minute_locked (const Metrics& metrics, double minute) const
3940 Metrics::const_iterator i;
3941 MeterSection* prev = 0;
3945 for (i = metrics.begin(); i != metrics.end(); ++i) {
3947 if (!(*i)->is_tempo()) {
3948 m = static_cast<MeterSection*> (*i);
3950 if (prev && (*i)->minute() > minute) {
3960 abort(); /*NOTREACHED*/
3967 TempoMap::meter_section_at_beat_locked (const Metrics& metrics, const double& beat) const
3969 MeterSection* prev_m = 0;
3971 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
3973 if (!(*i)->is_tempo()) {
3974 m = static_cast<MeterSection*> (*i);
3975 if (prev_m && m->beat() > beat) {
3986 TempoMap::meter_section_at_beat (double beat) const
3988 Glib::Threads::RWLock::ReaderLock lm (lock);
3989 return meter_section_at_beat_locked (_metrics, beat);
3993 TempoMap::meter_at_frame (framepos_t frame) const
3995 TempoMetric m (metric_at (frame));
4000 TempoMap::fix_legacy_session ()
4002 MeterSection* prev_m = 0;
4003 TempoSection* prev_t = 0;
4005 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4009 if ((m = dynamic_cast<MeterSection*>(*i)) != 0) {
4010 if (!m->movable()) {
4011 pair<double, BBT_Time> bbt = make_pair (0.0, BBT_Time (1, 1, 0));
4014 m->set_minute (0.0);
4015 m->set_position_lock_style (AudioTime);
4020 pair<double, BBT_Time> start = make_pair (((m->bbt().bars - 1) * prev_m->note_divisor())
4021 + (m->bbt().beats - 1)
4022 + (m->bbt().ticks / BBT_Time::ticks_per_beat)
4024 m->set_beat (start);
4025 const double start_beat = ((m->bbt().bars - 1) * prev_m->note_divisor())
4026 + (m->bbt().beats - 1)
4027 + (m->bbt().ticks / BBT_Time::ticks_per_beat);
4028 m->set_pulse (start_beat / prev_m->note_divisor());
4031 } else if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
4037 if (!t->movable()) {
4039 t->set_minute (0.0);
4040 t->set_position_lock_style (AudioTime);
4046 const double beat = ((t->legacy_bbt().bars - 1) * ((prev_m) ? prev_m->note_divisor() : 4.0))
4047 + (t->legacy_bbt().beats - 1)
4048 + (t->legacy_bbt().ticks / BBT_Time::ticks_per_beat);
4050 t->set_pulse (beat / prev_m->note_divisor());
4052 /* really shouldn't happen but.. */
4053 t->set_pulse (beat / 4.0);
4062 TempoMap::get_state ()
4064 Metrics::const_iterator i;
4065 XMLNode *root = new XMLNode ("TempoMap");
4068 Glib::Threads::RWLock::ReaderLock lm (lock);
4069 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
4070 root->add_child_nocopy ((*i)->get_state());
4078 TempoMap::set_state (const XMLNode& node, int /*version*/)
4081 Glib::Threads::RWLock::WriterLock lm (lock);
4084 XMLNodeConstIterator niter;
4085 Metrics old_metrics (_metrics);
4088 nlist = node.children();
4090 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
4091 XMLNode* child = *niter;
4093 if (child->name() == TempoSection::xml_state_node_name) {
4096 TempoSection* ts = new TempoSection (*child, _frame_rate);
4097 _metrics.push_back (ts);
4100 catch (failed_constructor& err){
4101 error << _("Tempo map: could not set new state, restoring old one.") << endmsg;
4102 _metrics = old_metrics;
4103 old_metrics.clear();
4107 } else if (child->name() == MeterSection::xml_state_node_name) {
4110 MeterSection* ms = new MeterSection (*child, _frame_rate);
4111 _metrics.push_back (ms);
4114 catch (failed_constructor& err) {
4115 error << _("Tempo map: could not set new state, restoring old one.") << endmsg;
4116 _metrics = old_metrics;
4117 old_metrics.clear();
4123 if (niter == nlist.end()) {
4124 MetricSectionSorter cmp;
4125 _metrics.sort (cmp);
4128 /* check for legacy sessions where bbt was the base musical unit for tempo */
4129 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4131 if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
4132 if (t->legacy_bbt().bars != 0) {
4133 fix_legacy_session();
4140 /* check for multiple tempo/meters at the same location, which
4141 ardour2 somehow allowed.
4144 Metrics::iterator prev = _metrics.end();
4145 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4146 if (prev != _metrics.end()) {
4148 MeterSection* prev_m;
4150 TempoSection* prev_t;
4151 if ((prev_m = dynamic_cast<MeterSection*>(*prev)) != 0 && (ms = dynamic_cast<MeterSection*>(*i)) != 0) {
4152 if (prev_m->pulse() == ms->pulse()) {
4153 cerr << string_compose (_("Multiple meter definitions found at %1"), prev_m->pulse()) << endmsg;
4154 error << string_compose (_("Multiple meter definitions found at %1"), prev_m->pulse()) << endmsg;
4157 } else if ((prev_t = dynamic_cast<TempoSection*>(*prev)) != 0 && (ts = dynamic_cast<TempoSection*>(*i)) != 0) {
4158 if (prev_t->pulse() == ts->pulse()) {
4159 cerr << string_compose (_("Multiple tempo definitions found at %1"), prev_t->pulse()) << endmsg;
4160 error << string_compose (_("Multiple tempo definitions found at %1"), prev_t->pulse()) << endmsg;
4168 recompute_map (_metrics);
4170 Metrics::const_iterator d = old_metrics.begin();
4171 while (d != old_metrics.end()) {
4175 old_metrics.clear ();
4178 PropertyChanged (PropertyChange ());
4184 TempoMap::dump (const Metrics& metrics, std::ostream& o) const
4186 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
4187 const MeterSection* m;
4188 const TempoSection* t;
4189 const TempoSection* prev_t = 0;
4191 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
4193 if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
4194 o << "Tempo @ " << *i << t->note_types_per_minute() << " BPM (pulse = 1/" << t->note_type()
4195 << " type= " << enum_2_string (t->type()) << ") " << " at pulse= " << t->pulse()
4196 << " minute= " << t->minute() << " frame= " << t->frame() << " (movable? " << t->movable() << ')'
4197 << " pos lock: " << enum_2_string (t->position_lock_style()) << std::endl;
4199 o << std::setprecision (17) << " current : " << t->note_types_per_minute()
4200 << " | " << t->pulse() << " | " << t->frame() << " | " << t->minute() << std::endl;
4201 o << " previous : " << prev_t->note_types_per_minute()
4202 << " | " << prev_t->pulse() << " | " << prev_t->frame() << " | " << prev_t->minute() << std::endl;
4203 o << " calculated : " << prev_t->tempo_at_pulse (t->pulse())
4204 << " | " << prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute())
4205 << " | " << frame_at_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()))
4206 << " | " << prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()) << std::endl;
4209 } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
4210 o << "Meter @ " << *i << ' ' << m->divisions_per_bar() << '/' << m->note_divisor() << " at " << m->bbt()
4211 << " frame= " << m->frame() << " pulse: " << m->pulse() << " beat : " << m->beat()
4212 << " pos lock: " << enum_2_string (m->position_lock_style()) << " (movable? " << m->movable() << ')' << endl;
4215 o << "------" << std::endl;
4219 TempoMap::n_tempos() const
4221 Glib::Threads::RWLock::ReaderLock lm (lock);
4224 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4225 if ((*i)->is_tempo()) {
4234 TempoMap::n_meters() const
4236 Glib::Threads::RWLock::ReaderLock lm (lock);
4239 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4240 if (!(*i)->is_tempo()) {
4249 TempoMap::insert_time (framepos_t where, framecnt_t amount)
4251 for (Metrics::reverse_iterator i = _metrics.rbegin(); i != _metrics.rend(); ++i) {
4252 if ((*i)->frame() >= where && (*i)->movable ()) {
4256 if ((ms = dynamic_cast <MeterSection*>(*i)) != 0) {
4257 gui_move_meter (ms, (*i)->frame() + amount);
4260 if ((ts = dynamic_cast <TempoSection*>(*i)) != 0) {
4261 gui_move_tempo (ts, (*i)->frame() + amount, 0);
4266 PropertyChanged (PropertyChange ());
4270 TempoMap::remove_time (framepos_t where, framecnt_t amount)
4274 std::list<MetricSection*> metric_kill_list;
4276 TempoSection* last_tempo = NULL;
4277 MeterSection* last_meter = NULL;
4278 bool tempo_after = false; // is there a tempo marker at the first sample after the removed range?
4279 bool meter_after = false; // is there a meter marker likewise?
4281 Glib::Threads::RWLock::WriterLock lm (lock);
4282 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4283 if ((*i)->frame() >= where && (*i)->frame() < where+amount) {
4284 metric_kill_list.push_back(*i);
4285 TempoSection *lt = dynamic_cast<TempoSection*> (*i);
4288 MeterSection *lm = dynamic_cast<MeterSection*> (*i);
4292 else if ((*i)->frame() >= where) {
4293 // TODO: make sure that moved tempo/meter markers are rounded to beat/bar boundaries
4294 (*i)->set_minute ((*i)->minute() - minute_at_frame (amount));
4295 if ((*i)->frame() == where) {
4296 // marker was immediately after end of range
4297 tempo_after = dynamic_cast<TempoSection*> (*i);
4298 meter_after = dynamic_cast<MeterSection*> (*i);
4304 //find the last TEMPO and METER metric (if any) and move it to the cut point so future stuff is correct
4305 if (last_tempo && !tempo_after) {
4306 metric_kill_list.remove(last_tempo);
4307 last_tempo->set_minute (minute_at_frame (where));
4310 if (last_meter && !meter_after) {
4311 metric_kill_list.remove(last_meter);
4312 last_meter->set_minute (minute_at_frame (where));
4316 //remove all the remaining metrics
4317 for (std::list<MetricSection*>::iterator i = metric_kill_list.begin(); i != metric_kill_list.end(); ++i) {
4318 _metrics.remove(*i);
4323 recompute_map (_metrics);
4326 PropertyChanged (PropertyChange ());
4330 /** Add some (fractional) Beats to a session frame position, and return the result in frames.
4331 * pos can be -ve, if required.
4334 TempoMap::framepos_plus_qn (framepos_t frame, Evoral::Beats beats) const
4336 Glib::Threads::RWLock::ReaderLock lm (lock);
4337 const double frame_qn = quarter_notes_between_frames_locked (_metrics, 0, frame);
4339 return frame_at_minute (minute_at_quarter_note_locked (_metrics, frame_qn + beats.to_double()));
4343 TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
4345 Glib::Threads::RWLock::ReaderLock lm (lock);
4347 BBT_Time pos_bbt = bbt_at_beat_locked (_metrics, beat_at_minute_locked (_metrics, minute_at_frame (pos)));
4348 pos_bbt.ticks += op.ticks;
4349 if (pos_bbt.ticks >= BBT_Time::ticks_per_beat) {
4351 pos_bbt.ticks -= BBT_Time::ticks_per_beat;
4353 pos_bbt.beats += op.beats;
4354 /* the meter in effect will start on the bar */
4355 double divisions_per_bar = meter_section_at_beat (beat_at_bbt_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
4356 while (pos_bbt.beats >= divisions_per_bar + 1) {
4358 divisions_per_bar = meter_section_at_beat (beat_at_bbt_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
4359 pos_bbt.beats -= divisions_per_bar;
4361 pos_bbt.bars += op.bars;
4363 return frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
4366 /** Count the number of beats that are equivalent to distance when going forward,
4370 TempoMap::framewalk_to_qn (framepos_t pos, framecnt_t distance) const
4372 Glib::Threads::RWLock::ReaderLock lm (lock);
4374 return Evoral::Beats (quarter_notes_between_frames_locked (_metrics, pos, pos + distance));
4378 bool operator() (const BBT_Time& a, const BBT_Time& b) {
4384 operator<< (std::ostream& o, const Meter& m) {
4385 return o << m.divisions_per_bar() << '/' << m.note_divisor();
4389 operator<< (std::ostream& o, const Tempo& t) {
4390 return o << t.note_types_per_minute() << " 1/" << t.note_type() << "'s per minute";
4394 operator<< (std::ostream& o, const MetricSection& section) {
4396 o << "MetricSection @ " << section.frame() << ' ';
4398 const TempoSection* ts;
4399 const MeterSection* ms;
4401 if ((ts = dynamic_cast<const TempoSection*> (§ion)) != 0) {
4402 o << *((const Tempo*) ts);
4403 } else if ((ms = dynamic_cast<const MeterSection*> (§ion)) != 0) {
4404 o << *((const Meter*) ms);