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-minute\" 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()));
1337 prev_t->set_c_func (0.0);
1340 /* tempos must be positioned correctly.
1341 the current approach is to use a meter's bbt time as its base position unit.
1342 an audio-locked meter requires a recomputation of pulse and beat (but not bbt),
1343 while a music-locked meter requires recomputations of frame pulse and beat (but not bbt)
1346 TempoMap::recompute_meters (Metrics& metrics)
1348 MeterSection* meter = 0;
1349 MeterSection* prev_m = 0;
1351 for (Metrics::const_iterator mi = metrics.begin(); mi != metrics.end(); ++mi) {
1352 if (!(*mi)->is_tempo()) {
1353 meter = static_cast<MeterSection*> (*mi);
1354 if (meter->position_lock_style() == AudioTime) {
1356 pair<double, BBT_Time> b_bbt;
1357 TempoSection* meter_locked_tempo = 0;
1358 for (Metrics::const_iterator ii = metrics.begin(); ii != metrics.end(); ++ii) {
1360 if ((*ii)->is_tempo()) {
1361 t = static_cast<TempoSection*> (*ii);
1362 if ((t->locked_to_meter() || !t->movable()) && t->frame() == meter->frame()) {
1363 meter_locked_tempo = t;
1370 const double beats = (meter->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar();
1371 if (beats + prev_m->beat() != meter->beat()) {
1372 /* reordering caused a bbt change */
1373 b_bbt = make_pair (beats + prev_m->beat()
1374 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
1375 pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
1377 } else if (meter->movable()) {
1378 b_bbt = make_pair (meter->beat(), meter->bbt());
1379 pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
1382 b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
1384 if (meter_locked_tempo) {
1385 meter_locked_tempo->set_pulse (pulse);
1387 meter->set_beat (b_bbt);
1388 meter->set_pulse (pulse);
1393 pair<double, BBT_Time> b_bbt;
1395 const double beats = (meter->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar();
1396 if (beats + prev_m->beat() != meter->beat()) {
1397 /* reordering caused a bbt change */
1398 b_bbt = make_pair (beats + prev_m->beat()
1399 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
1401 b_bbt = make_pair (beats + prev_m->beat(), meter->bbt());
1403 pulse = (beats / prev_m->note_divisor()) + prev_m->pulse();
1405 /* shouldn't happen - the first is audio-locked */
1406 pulse = pulse_at_beat_locked (metrics, meter->beat());
1407 b_bbt = make_pair (meter->beat(), meter->bbt());
1410 meter->set_beat (b_bbt);
1411 meter->set_pulse (pulse);
1412 meter->set_minute (minute_at_pulse_locked (metrics, pulse));
1421 TempoMap::recompute_map (Metrics& metrics, framepos_t end)
1423 /* CALLER MUST HOLD WRITE LOCK */
1425 DEBUG_TRACE (DEBUG::TempoMath, string_compose ("recomputing tempo map, zero to %1\n", end));
1428 /* silly call from Session::process() during startup
1433 recompute_tempi (metrics);
1434 recompute_meters (metrics);
1438 TempoMap::metric_at (framepos_t frame, Metrics::const_iterator* last) const
1440 Glib::Threads::RWLock::ReaderLock lm (lock);
1441 TempoMetric m (first_meter(), first_tempo());
1443 /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
1444 at something, because we insert the default tempo and meter during
1445 TempoMap construction.
1447 now see if we can find better candidates.
1450 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1452 if ((*i)->frame() > frame) {
1466 /* XX meters only */
1468 TempoMap::metric_at (BBT_Time bbt) const
1470 Glib::Threads::RWLock::ReaderLock lm (lock);
1471 TempoMetric m (first_meter(), first_tempo());
1473 /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
1474 at something, because we insert the default tempo and meter during
1475 TempoMap construction.
1477 now see if we can find better candidates.
1480 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
1482 if (!(*i)->is_tempo()) {
1483 mw = static_cast<MeterSection*> (*i);
1484 BBT_Time section_start (mw->bbt());
1486 if (section_start.bars > bbt.bars || (section_start.bars == bbt.bars && section_start.beats > bbt.beats)) {
1497 /** Returns the BBT (meter-based) beat corresponding to the supplied frame, possibly returning a negative value.
1498 * @param frame The session frame position.
1499 * @return The beat duration according to the tempo map at the supplied frame.
1501 * If the supplied frame lies before the first meter, the returned beat duration will be negative.
1502 * The returned beat is obtained using the first meter and the continuation of the tempo curve (backwards).
1504 * This function uses both tempo and meter.
1507 TempoMap::beat_at_frame (const framecnt_t& frame) const
1509 Glib::Threads::RWLock::ReaderLock lm (lock);
1511 return beat_at_minute_locked (_metrics, minute_at_frame (frame));
1514 /* This function uses both tempo and meter.*/
1516 TempoMap::beat_at_minute_locked (const Metrics& metrics, const double& minute) const
1518 const TempoSection& ts = tempo_section_at_minute_locked (metrics, minute);
1519 MeterSection* prev_m = 0;
1520 MeterSection* next_m = 0;
1522 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1523 if (!(*i)->is_tempo()) {
1524 if (prev_m && (*i)->minute() > minute) {
1525 next_m = static_cast<MeterSection*> (*i);
1528 prev_m = static_cast<MeterSection*> (*i);
1532 const double beat = prev_m->beat() + (ts.pulse_at_minute (minute) - prev_m->pulse()) * prev_m->note_divisor();
1534 /* audio locked meters fake their beat */
1535 if (next_m && next_m->beat() < beat) {
1536 return next_m->beat();
1542 /** Returns the frame corresponding to the supplied BBT (meter-based) beat.
1543 * @param beat The BBT (meter-based) beat.
1544 * @return The frame duration according to the tempo map at the supplied BBT (meter-based) beat.
1546 * This function uses both tempo and meter.
1549 TempoMap::frame_at_beat (const double& beat) const
1551 Glib::Threads::RWLock::ReaderLock lm (lock);
1553 return frame_at_minute (minute_at_beat_locked (_metrics, beat));
1556 /* meter & tempo section based */
1558 TempoMap::minute_at_beat_locked (const Metrics& metrics, const double& beat) const
1560 MeterSection* prev_m = 0;
1561 TempoSection* prev_t = 0;
1565 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1566 if (!(*i)->is_tempo()) {
1567 m = static_cast<MeterSection*> (*i);
1568 if (prev_m && m->beat() > beat) {
1578 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1579 if ((*i)->is_tempo()) {
1580 t = static_cast<TempoSection*> (*i);
1581 if (prev_t && ((t->pulse() - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat() > beat) {
1590 return prev_t->minute_at_pulse (((beat - prev_m->beat()) / prev_m->note_divisor()) + prev_m->pulse());
1593 /** Returns a Tempo corresponding to the supplied frame position.
1594 * @param frame The audio frame.
1595 * @return a Tempo according to the tempo map at the supplied frame.
1599 TempoMap::tempo_at_frame (const framepos_t& frame) const
1601 Glib::Threads::RWLock::ReaderLock lm (lock);
1603 return tempo_at_minute_locked (_metrics, minute_at_frame (frame));
1607 TempoMap::tempo_at_minute_locked (const Metrics& metrics, const double& minute) const
1609 TempoSection* prev_t = 0;
1613 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1614 if ((*i)->is_tempo()) {
1615 t = static_cast<TempoSection*> (*i);
1619 if ((prev_t) && t->minute() > minute) {
1620 /* t is the section past frame */
1621 return prev_t->tempo_at_minute (minute);
1627 return Tempo (prev_t->note_types_per_minute(), prev_t->note_type());
1630 /** returns the frame at which the supplied tempo occurs, or
1631 * the frame of the last tempo section (search exhausted)
1632 * only the position of the first occurence will be returned
1636 TempoMap::frame_at_tempo (const Tempo& tempo) const
1638 Glib::Threads::RWLock::ReaderLock lm (lock);
1640 return frame_at_minute (minute_at_tempo_locked (_metrics, tempo));
1644 TempoMap::minute_at_tempo_locked (const Metrics& metrics, const Tempo& tempo) const
1646 TempoSection* prev_t = 0;
1647 const double tempo_bpm = tempo.note_types_per_minute();
1649 Metrics::const_iterator i;
1651 for (i = metrics.begin(); i != metrics.end(); ++i) {
1653 if ((*i)->is_tempo()) {
1654 t = static_cast<TempoSection*> (*i);
1660 const double t_bpm = t->note_types_per_minute();
1662 if (t_bpm == tempo_bpm) {
1667 const double prev_t_bpm = prev_t->note_types_per_minute();
1669 if ((t_bpm > tempo_bpm && prev_t_bpm < tempo_bpm) || (t_bpm < tempo_bpm && prev_t_bpm > tempo_bpm)) {
1670 return prev_t->minute_at_ntpm (prev_t->note_types_per_minute(), prev_t->pulse());
1677 return prev_t->minute();
1681 TempoMap::tempo_at_pulse_locked (const Metrics& metrics, const double& pulse) const
1683 TempoSection* prev_t = 0;
1687 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1688 if ((*i)->is_tempo()) {
1689 t = static_cast<TempoSection*> (*i);
1693 if ((prev_t) && t->pulse() > pulse) {
1694 /* t is the section past frame */
1695 return prev_t->tempo_at_pulse (pulse);
1701 return Tempo (prev_t->note_types_per_minute(), prev_t->note_type());
1705 TempoMap::pulse_at_tempo_locked (const Metrics& metrics, const Tempo& tempo) const
1707 TempoSection* prev_t = 0;
1708 const double tempo_bpm = tempo.note_types_per_minute();
1710 Metrics::const_iterator i;
1712 for (i = metrics.begin(); i != metrics.end(); ++i) {
1714 if ((*i)->is_tempo()) {
1715 t = static_cast<TempoSection*> (*i);
1721 const double t_bpm = t->note_types_per_minute();
1723 if (t_bpm == tempo_bpm) {
1728 const double prev_t_bpm = prev_t->note_types_per_minute();
1730 if ((t_bpm > tempo_bpm && prev_t_bpm < tempo_bpm) || (t_bpm < tempo_bpm && prev_t_bpm > tempo_bpm)) {
1731 return prev_t->pulse_at_ntpm (prev_t->note_types_per_minute(), prev_t->minute());
1738 return prev_t->pulse();
1741 /** Returns a Tempo corresponding to the supplied position in quarter-note beats.
1742 * @param qn the position in quarter note beats.
1743 * @return the Tempo at the supplied quarter-note.
1746 TempoMap::tempo_at_quarter_note (const double& qn) const
1748 Glib::Threads::RWLock::ReaderLock lm (lock);
1750 return tempo_at_pulse_locked (_metrics, qn / 4.0);
1753 /** Returns the position in quarter-note beats corresponding to the supplied Tempo.
1754 * @param tempo the tempo.
1755 * @return the position in quarter-note beats where the map bpm
1756 * is equal to that of the Tempo. currently ignores note_type.
1759 TempoMap::quarter_note_at_tempo (const Tempo& tempo) const
1761 Glib::Threads::RWLock::ReaderLock lm (lock);
1763 return pulse_at_tempo_locked (_metrics, tempo) * 4.0;;
1766 /** Returns the whole-note pulse corresponding to the supplied BBT (meter-based) beat.
1767 * @param metrics the list of metric sections used to calculate the pulse.
1768 * @param beat The BBT (meter-based) beat.
1769 * @return the whole-note pulse at the supplied BBT (meter-based) beat.
1771 * a pulse or whole note is the base musical position of a MetricSection.
1772 * it is equivalent to four quarter notes.
1776 TempoMap::pulse_at_beat_locked (const Metrics& metrics, const double& beat) const
1778 const MeterSection* prev_m = &meter_section_at_beat_locked (metrics, beat);
1780 return prev_m->pulse() + ((beat - prev_m->beat()) / prev_m->note_divisor());
1783 /** Returns the BBT (meter-based) beat corresponding to the supplied whole-note pulse .
1784 * @param metrics the list of metric sections used to calculate the beat.
1785 * @param pulse the whole-note pulse.
1786 * @return the meter-based beat at the supplied whole-note pulse.
1788 * a pulse or whole note is the base musical position of a MetricSection.
1789 * it is equivalent to four quarter notes.
1792 TempoMap::beat_at_pulse_locked (const Metrics& metrics, const double& pulse) const
1794 MeterSection* prev_m = 0;
1796 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1798 if (!(*i)->is_tempo()) {
1799 m = static_cast<MeterSection*> (*i);
1800 if (prev_m && m->pulse() > pulse) {
1808 double const ret = ((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat();
1812 /* tempo section based */
1814 TempoMap::pulse_at_minute_locked (const Metrics& metrics, const double& minute) const
1816 /* HOLD (at least) THE READER LOCK */
1817 TempoSection* prev_t = 0;
1819 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1821 if ((*i)->is_tempo()) {
1822 t = static_cast<TempoSection*> (*i);
1826 if (prev_t && t->minute() > minute) {
1827 /*the previous ts is the one containing the frame */
1828 const double ret = prev_t->pulse_at_minute (minute);
1829 /* audio locked section in new meter*/
1830 if (t->pulse() < ret) {
1839 /* treated as constant for this ts */
1840 const double pulses_in_section = ((minute - prev_t->minute()) * prev_t->note_types_per_minute()) / prev_t->note_type();
1842 return pulses_in_section + prev_t->pulse();
1845 /* tempo section based */
1847 TempoMap::minute_at_pulse_locked (const Metrics& metrics, const double& pulse) const
1849 /* HOLD THE READER LOCK */
1851 const TempoSection* prev_t = 0;
1853 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1856 if ((*i)->is_tempo()) {
1857 t = static_cast<TempoSection*> (*i);
1861 if (prev_t && t->pulse() > pulse) {
1862 return prev_t->minute_at_pulse (pulse);
1868 /* must be treated as constant, irrespective of _type */
1869 double const dtime = ((pulse - prev_t->pulse()) * prev_t->note_type()) / prev_t->note_types_per_minute();
1871 return dtime + prev_t->minute();
1874 /** Returns the BBT (meter-based) beat corresponding to the supplied BBT time.
1875 * @param bbt The BBT time (meter-based).
1876 * @return bbt The BBT beat (meter-based) at the supplied BBT time.
1880 TempoMap::beat_at_bbt (const Timecode::BBT_Time& bbt)
1882 Glib::Threads::RWLock::ReaderLock lm (lock);
1883 return beat_at_bbt_locked (_metrics, bbt);
1888 TempoMap::beat_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const
1890 /* CALLER HOLDS READ LOCK */
1892 MeterSection* prev_m = 0;
1894 /* because audio-locked meters have 'fake' integral beats,
1895 there is no pulse offset here.
1899 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1900 if (!(*i)->is_tempo()) {
1901 m = static_cast<MeterSection*> (*i);
1903 const double bars_to_m = (m->beat() - prev_m->beat()) / prev_m->divisions_per_bar();
1904 if ((bars_to_m + (prev_m->bbt().bars - 1)) > (bbt.bars - 1)) {
1912 const double remaining_bars = bbt.bars - prev_m->bbt().bars;
1913 const double remaining_bars_in_beats = remaining_bars * prev_m->divisions_per_bar();
1914 const double ret = remaining_bars_in_beats + prev_m->beat() + (bbt.beats - 1) + (bbt.ticks / BBT_Time::ticks_per_beat);
1919 /** Returns the BBT time corresponding to the supplied BBT (meter-based) beat.
1920 * @param beat The BBT (meter-based) beat.
1921 * @return The BBT time (meter-based) at the supplied meter-based beat.
1925 TempoMap::bbt_at_beat (const double& beat)
1927 Glib::Threads::RWLock::ReaderLock lm (lock);
1928 return bbt_at_beat_locked (_metrics, beat);
1932 TempoMap::bbt_at_beat_locked (const Metrics& metrics, const double& b) const
1934 /* CALLER HOLDS READ LOCK */
1935 MeterSection* prev_m = 0;
1936 const double beats = max (0.0, b);
1938 MeterSection* m = 0;
1940 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
1941 if (!(*i)->is_tempo()) {
1942 m = static_cast<MeterSection*> (*i);
1944 if (m->beat() > beats) {
1945 /* this is the meter after the one our beat is on*/
1955 const double beats_in_ms = beats - prev_m->beat();
1956 const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
1957 const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
1958 const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
1959 const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
1963 ret.ticks = (uint32_t) floor (remaining_ticks + 0.5);
1964 ret.beats = (uint32_t) floor (remaining_beats);
1965 ret.bars = total_bars;
1967 /* 0 0 0 to 1 1 0 - based mapping*/
1971 if (ret.ticks >= BBT_Time::ticks_per_beat) {
1973 ret.ticks -= BBT_Time::ticks_per_beat;
1976 if (ret.beats >= prev_m->divisions_per_bar() + 1) {
1984 /** Returns the quarter-note beat corresponding to the supplied BBT time (meter-based).
1985 * @param bbt The BBT time (meter-based).
1986 * @return the quarter note beat at the supplied BBT time
1988 * quarter-notes ignore meter and are based on pulse (the musical unit of MetricSection).
1990 * while the input uses meter, the output does not.
1993 TempoMap::quarter_note_at_bbt (const Timecode::BBT_Time& bbt)
1995 Glib::Threads::RWLock::ReaderLock lm (lock);
1997 return pulse_at_bbt_locked (_metrics, bbt) * 4.0;
2001 TempoMap::quarter_note_at_bbt_rt (const Timecode::BBT_Time& bbt)
2003 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
2006 throw std::logic_error ("TempoMap::quarter_note_at_bbt_rt() could not lock tempo map");
2009 return pulse_at_bbt_locked (_metrics, bbt) * 4.0;
2013 TempoMap::pulse_at_bbt_locked (const Metrics& metrics, const Timecode::BBT_Time& bbt) const
2015 /* CALLER HOLDS READ LOCK */
2017 MeterSection* prev_m = 0;
2019 /* because audio-locked meters have 'fake' integral beats,
2020 there is no pulse offset here.
2024 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2025 if (!(*i)->is_tempo()) {
2026 m = static_cast<MeterSection*> (*i);
2028 if (m->bbt().bars > bbt.bars) {
2036 const double remaining_bars = bbt.bars - prev_m->bbt().bars;
2037 const double remaining_pulses = remaining_bars * prev_m->divisions_per_bar() / prev_m->note_divisor();
2038 const double ret = remaining_pulses + prev_m->pulse() + (((bbt.beats - 1) + (bbt.ticks / BBT_Time::ticks_per_beat)) / prev_m->note_divisor());
2043 /** Returns the BBT time corresponding to the supplied quarter-note beat.
2044 * @param qn the quarter-note beat.
2045 * @return The BBT time (meter-based) at the supplied meter-based beat.
2047 * quarter-notes ignore meter and are based on pulse (the musical unit of MetricSection).
2051 TempoMap::bbt_at_quarter_note (const double& qn)
2053 Glib::Threads::RWLock::ReaderLock lm (lock);
2055 return bbt_at_pulse_locked (_metrics, qn / 4.0);
2058 /** Returns the BBT time (meter-based) corresponding to the supplied whole-note pulse position.
2059 * @param metrics The list of metric sections used to determine the result.
2060 * @param pulse The whole-note pulse.
2061 * @return The BBT time at the supplied whole-note pulse.
2063 * a pulse or whole note is the basic musical position of a MetricSection.
2064 * it is equivalent to four quarter notes.
2065 * while the output uses meter, the input does not.
2068 TempoMap::bbt_at_pulse_locked (const Metrics& metrics, const double& pulse) const
2070 MeterSection* prev_m = 0;
2072 MeterSection* m = 0;
2074 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2076 if (!(*i)->is_tempo()) {
2077 m = static_cast<MeterSection*> (*i);
2080 double const pulses_to_m = m->pulse() - prev_m->pulse();
2081 if (prev_m->pulse() + pulses_to_m > pulse) {
2082 /* this is the meter after the one our beat is on*/
2093 const double beats_in_ms = (pulse - prev_m->pulse()) * prev_m->note_divisor();
2094 const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
2095 const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
2096 const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
2097 const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
2101 ret.ticks = (uint32_t) floor (remaining_ticks + 0.5);
2102 ret.beats = (uint32_t) floor (remaining_beats);
2103 ret.bars = total_bars;
2105 /* 0 0 0 to 1 1 0 mapping*/
2109 if (ret.ticks >= BBT_Time::ticks_per_beat) {
2111 ret.ticks -= BBT_Time::ticks_per_beat;
2114 if (ret.beats >= prev_m->divisions_per_bar() + 1) {
2122 /** Returns the BBT time corresponding to the supplied frame position.
2123 * @param frame the position in audio samples.
2124 * @return the BBT time at the frame position .
2128 TempoMap::bbt_at_frame (framepos_t frame)
2135 warning << string_compose (_("tempo map was asked for BBT time at frame %1\n"), frame) << endmsg;
2138 Glib::Threads::RWLock::ReaderLock lm (lock);
2140 return bbt_at_minute_locked (_metrics, minute_at_frame (frame));
2144 TempoMap::bbt_at_frame_rt (framepos_t frame)
2146 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
2149 throw std::logic_error ("TempoMap::bbt_at_frame_rt() could not lock tempo map");
2152 return bbt_at_minute_locked (_metrics, minute_at_frame (frame));
2156 TempoMap::bbt_at_minute_locked (const Metrics& metrics, const double& minute) const
2166 const TempoSection& ts = tempo_section_at_minute_locked (metrics, minute);
2167 MeterSection* prev_m = 0;
2168 MeterSection* next_m = 0;
2172 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2173 if (!(*i)->is_tempo()) {
2174 m = static_cast<MeterSection*> (*i);
2175 if (prev_m && m->minute() > minute) {
2183 double beat = prev_m->beat() + (ts.pulse_at_minute (minute) - prev_m->pulse()) * prev_m->note_divisor();
2185 /* handle frame before first meter */
2186 if (minute < prev_m->minute()) {
2189 /* audio locked meters fake their beat */
2190 if (next_m && next_m->beat() < beat) {
2191 beat = next_m->beat();
2194 beat = max (0.0, beat);
2196 const double beats_in_ms = beat - prev_m->beat();
2197 const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
2198 const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
2199 const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
2200 const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
2204 ret.ticks = (uint32_t) floor (remaining_ticks + 0.5);
2205 ret.beats = (uint32_t) floor (remaining_beats);
2206 ret.bars = total_bars;
2208 /* 0 0 0 to 1 1 0 - based mapping*/
2212 if (ret.ticks >= BBT_Time::ticks_per_beat) {
2214 ret.ticks -= BBT_Time::ticks_per_beat;
2217 if (ret.beats >= prev_m->divisions_per_bar() + 1) {
2225 /** Returns the frame position corresponding to the supplied BBT time.
2226 * @param bbt the position in BBT time.
2227 * @return the frame position at bbt.
2231 TempoMap::frame_at_bbt (const BBT_Time& bbt)
2234 warning << string_compose (_("tempo map asked for frame time at bar < 1 (%1)\n"), bbt) << endmsg;
2238 if (bbt.beats < 1) {
2239 throw std::logic_error ("beats are counted from one");
2241 Glib::Threads::RWLock::ReaderLock lm (lock);
2243 return frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
2246 /* meter & tempo section based */
2248 TempoMap::minute_at_bbt_locked (const Metrics& metrics, const BBT_Time& bbt) const
2250 /* HOLD THE READER LOCK */
2252 const double ret = minute_at_beat_locked (metrics, beat_at_bbt_locked (metrics, bbt));
2257 * Returns the quarter-note beat position corresponding to the supplied frame.
2259 * @param frame the position in frames.
2260 * @return The quarter-note position of the supplied frame. Ignores meter.
2264 TempoMap::quarter_note_at_frame (const framepos_t frame) const
2266 Glib::Threads::RWLock::ReaderLock lm (lock);
2268 const double ret = quarter_note_at_minute_locked (_metrics, minute_at_frame (frame));
2274 TempoMap::quarter_note_at_minute_locked (const Metrics& metrics, const double minute) const
2276 const double ret = pulse_at_minute_locked (metrics, minute) * 4.0;
2282 TempoMap::quarter_note_at_frame_rt (const framepos_t frame) const
2284 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
2287 throw std::logic_error ("TempoMap::quarter_note_at_frame_rt() could not lock tempo map");
2290 const double ret = pulse_at_minute_locked (_metrics, minute_at_frame (frame)) * 4.0;
2296 * Returns the frame position corresponding to the supplied quarter-note beat.
2298 * @param quarter_note the quarter-note position.
2299 * @return the frame position of the supplied quarter-note. Ignores meter.
2304 TempoMap::frame_at_quarter_note (const double quarter_note) const
2306 Glib::Threads::RWLock::ReaderLock lm (lock);
2308 const framepos_t ret = frame_at_minute (minute_at_quarter_note_locked (_metrics, quarter_note));
2314 TempoMap::minute_at_quarter_note_locked (const Metrics& metrics, const double quarter_note) const
2316 const double ret = minute_at_pulse_locked (metrics, quarter_note / 4.0);
2321 /** Returns the quarter-note beats corresponding to the supplied BBT (meter-based) beat.
2322 * @param beat The BBT (meter-based) beat.
2323 * @return The quarter-note position of the supplied BBT (meter-based) beat.
2325 * a quarter-note may be compared with and assigned to Evoral::Beats.
2329 TempoMap::quarter_note_at_beat (const double beat)
2331 Glib::Threads::RWLock::ReaderLock lm (lock);
2333 const double ret = quarter_note_at_beat_locked (_metrics, beat);
2339 TempoMap::quarter_note_at_beat_locked (const Metrics& metrics, const double beat) const
2341 const double ret = pulse_at_beat_locked (metrics, beat) * 4.0;
2346 /** Returns the BBT (meter-based) beat position corresponding to the supplied quarter-note beats.
2347 * @param quarter_note The position in quarter-note beats.
2348 * @return the BBT (meter-based) beat position of the supplied quarter-note beats.
2350 * a quarter-note is the musical unit of Evoral::Beats.
2354 TempoMap::beat_at_quarter_note (const double quarter_note)
2356 Glib::Threads::RWLock::ReaderLock lm (lock);
2358 const double ret = beat_at_quarter_note_locked (_metrics, quarter_note);
2364 TempoMap::beat_at_quarter_note_locked (const Metrics& metrics, const double quarter_note) const
2367 return beat_at_pulse_locked (metrics, quarter_note / 4.0);
2370 /** Returns the duration in frames between two supplied quarter-note beat positions.
2371 * @param start the first position in quarter-note beats.
2372 * @param end the end position in quarter-note beats.
2373 * @return the frame distance ober the quarter-note beats duration.
2375 * use this rather than e.g.
2376 * frame_at-quarter_note (end_beats) - frame_at_quarter_note (start_beats).
2377 * frames_between_quarter_notes() doesn't round to audio frames as an intermediate step,
2381 TempoMap::frames_between_quarter_notes (const double start, const double end) const
2383 Glib::Threads::RWLock::ReaderLock lm (lock);
2385 return frame_at_minute (minutes_between_quarter_notes_locked (_metrics, start, end));
2389 TempoMap::minutes_between_quarter_notes_locked (const Metrics& metrics, const double start, const double end) const
2392 return minute_at_pulse_locked (metrics, end / 4.0) - minute_at_pulse_locked (metrics, start / 4.0);
2396 TempoMap::quarter_notes_between_frames (const framecnt_t start, const framecnt_t end) const
2398 Glib::Threads::RWLock::ReaderLock lm (lock);
2400 return quarter_notes_between_frames_locked (_metrics, start, end);
2404 TempoMap::quarter_notes_between_frames_locked (const Metrics& metrics, const framecnt_t start, const framecnt_t end) const
2406 const TempoSection* prev_t = 0;
2408 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2411 if ((*i)->is_tempo()) {
2412 t = static_cast<TempoSection*> (*i);
2416 if (prev_t && t->frame() > start) {
2423 const double start_qn = prev_t->pulse_at_frame (start);
2425 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2428 if ((*i)->is_tempo()) {
2429 t = static_cast<TempoSection*> (*i);
2433 if (prev_t && t->frame() > end) {
2439 const double end_qn = prev_t->pulse_at_frame (end);
2441 return (end_qn - start_qn) * 4.0;
2445 TempoMap::check_solved (const Metrics& metrics) const
2447 TempoSection* prev_t = 0;
2448 MeterSection* prev_m = 0;
2450 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2453 if ((*i)->is_tempo()) {
2454 t = static_cast<TempoSection*> (*i);
2459 /* check ordering */
2460 if ((t->minute() <= prev_t->minute()) || (t->pulse() <= prev_t->pulse())) {
2464 /* precision check ensures tempo and frames align.*/
2465 if (t->frame() != frame_at_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()))) {
2466 if (!t->locked_to_meter()) {
2471 /* gradient limit - who knows what it should be?
2472 things are also ok (if a little chaotic) without this
2474 if (fabs (prev_t->c_func()) > 1000.0) {
2475 //std::cout << "c : " << prev_t->c_func() << std::endl;
2482 if (!(*i)->is_tempo()) {
2483 m = static_cast<MeterSection*> (*i);
2484 if (prev_m && m->position_lock_style() == AudioTime) {
2485 const TempoSection* t = &tempo_section_at_minute_locked (metrics, minute_at_frame (m->frame() - 1));
2486 const double nascent_m_minute = t->minute_at_pulse (m->pulse());
2487 /* Here we check that a preceding section of music doesn't overlap a subsequent one.
2489 if (t && (nascent_m_minute > m->minute() || nascent_m_minute < 0.0)) {
2503 TempoMap::set_active_tempos (const Metrics& metrics, const framepos_t& frame)
2505 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2507 if ((*i)->is_tempo()) {
2508 t = static_cast<TempoSection*> (*i);
2509 if (!t->movable()) {
2510 t->set_active (true);
2513 if (t->movable() && t->active () && t->position_lock_style() == AudioTime && t->frame() < frame) {
2514 t->set_active (false);
2516 } else if (t->movable() && t->position_lock_style() == AudioTime && t->frame() > frame) {
2517 t->set_active (true);
2518 } else if (t->movable() && t->position_lock_style() == AudioTime && t->frame() == frame) {
2527 TempoMap::solve_map_minute (Metrics& imaginary, TempoSection* section, const double& minute)
2529 TempoSection* prev_t = 0;
2530 TempoSection* section_prev = 0;
2531 double first_m_minute = 0.0;
2533 /* can't move a tempo before the first meter */
2534 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2536 if (!(*i)->is_tempo()) {
2537 m = static_cast<MeterSection*> (*i);
2538 if (!m->movable()) {
2539 first_m_minute = m->minute();
2544 if (section->movable() && minute <= first_m_minute) {
2548 section->set_active (true);
2549 section->set_minute (minute);
2551 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2553 if ((*i)->is_tempo()) {
2554 t = static_cast<TempoSection*> (*i);
2561 section_prev = prev_t;
2562 if (t->locked_to_meter()) {
2567 if (t->position_lock_style() == MusicTime) {
2568 prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
2569 t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
2571 prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
2572 if (!t->locked_to_meter()) {
2573 t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
2582 section_prev->set_c_func (section_prev->compute_c_func_minute (section->note_types_per_minute(), minute));
2583 if (!section->locked_to_meter()) {
2584 section->set_pulse (section_prev->pulse_at_ntpm (section->note_types_per_minute(), minute));
2589 recompute_tempi (imaginary);
2591 if (check_solved (imaginary)) {
2594 dunp (imaginary, std::cout);
2598 MetricSectionFrameSorter fcmp;
2599 imaginary.sort (fcmp);
2601 recompute_tempi (imaginary);
2603 if (check_solved (imaginary)) {
2611 TempoMap::solve_map_pulse (Metrics& imaginary, TempoSection* section, const double& pulse)
2613 TempoSection* prev_t = 0;
2614 TempoSection* section_prev = 0;
2616 section->set_pulse (pulse);
2618 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2620 if ((*i)->is_tempo()) {
2621 t = static_cast<TempoSection*> (*i);
2625 if (!t->movable()) {
2632 section_prev = prev_t;
2635 if (t->position_lock_style() == MusicTime) {
2636 prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
2637 t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
2639 prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
2640 if (!t->locked_to_meter()) {
2641 t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
2650 section_prev->set_c_func (section_prev->compute_c_func_pulse (section->note_types_per_minute(), pulse));
2651 section->set_minute (section_prev->minute_at_ntpm (section->note_types_per_minute(), pulse));
2655 recompute_tempi (imaginary);
2657 if (check_solved (imaginary)) {
2660 dunp (imaginary, std::cout);
2664 MetricSectionSorter cmp;
2665 imaginary.sort (cmp);
2667 recompute_tempi (imaginary);
2669 * XX need a restriction here, but only for this case,
2670 * as audio locked tempos don't interact in the same way.
2672 * With music-locked tempos, the solution to cross-dragging can fly off the screen
2674 * |50 bpm |250 bpm |60 bpm
2675 * drag 250 to the pulse after 60->
2676 * a clue: dragging the second 60 <- past the 250 would cause no such problem.
2678 if (check_solved (imaginary)) {
2686 TempoMap::solve_map_minute (Metrics& imaginary, MeterSection* section, const double& minute)
2688 /* disallow moving first meter past any subsequent one, and any movable meter before the first one */
2689 const MeterSection* other = &meter_section_at_minute_locked (imaginary, minute);
2690 if ((!section->movable() && other->movable()) || (!other->movable() && section->movable() && other->minute() >= minute)) {
2694 if (!section->movable()) {
2695 /* lock the first tempo to our first meter */
2696 if (!set_active_tempos (imaginary, section->frame_at_minute (minute))) {
2701 TempoSection* meter_locked_tempo = 0;
2703 for (Metrics::const_iterator ii = imaginary.begin(); ii != imaginary.end(); ++ii) {
2705 if ((*ii)->is_tempo()) {
2706 t = static_cast<TempoSection*> (*ii);
2707 if ((t->locked_to_meter() || !t->movable()) && t->minute() == section->minute()) {
2708 meter_locked_tempo = t;
2714 if (!meter_locked_tempo) {
2718 MeterSection* prev_m = 0;
2720 TempoSection* tempo_copy = copy_metrics_and_point (imaginary, future_map, meter_locked_tempo);
2721 bool solved = false;
2723 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2725 if (!(*i)->is_tempo()) {
2726 m = static_cast<MeterSection*> (*i);
2728 if (prev_m && section->movable()) {
2729 const double beats = (pulse_at_minute_locked (imaginary, minute) - prev_m->pulse()) * prev_m->note_divisor();
2730 if (beats + prev_m->beat() < section->beat()) {
2731 /* set the section pulse according to its musical position,
2732 * as an earlier time than this has been requested.
2734 const double new_pulse = ((section->beat() - prev_m->beat())
2735 / prev_m->note_divisor()) + prev_m->pulse();
2737 tempo_copy->set_position_lock_style (MusicTime);
2738 if ((solved = solve_map_pulse (future_map, tempo_copy, new_pulse))) {
2739 meter_locked_tempo->set_position_lock_style (MusicTime);
2740 section->set_position_lock_style (MusicTime);
2741 section->set_pulse (new_pulse);
2742 solve_map_pulse (imaginary, meter_locked_tempo, new_pulse);
2743 meter_locked_tempo->set_position_lock_style (AudioTime);
2744 section->set_position_lock_style (AudioTime);
2745 section->set_minute (meter_locked_tempo->minute());
2751 Metrics::const_iterator d = future_map.begin();
2752 while (d != future_map.end()) {
2761 /* all is ok. set section's locked tempo if allowed.
2762 possibly disallowed if there is an adjacent audio-locked tempo.
2763 XX this check could possibly go. its never actually happened here.
2765 MeterSection* meter_copy = const_cast<MeterSection*>
2766 (&meter_section_at_minute_locked (future_map, section->minute()));
2768 meter_copy->set_minute (minute);
2770 if ((solved = solve_map_minute (future_map, tempo_copy, minute))) {
2771 section->set_minute (minute);
2772 meter_locked_tempo->set_pulse (((section->beat() - prev_m->beat())
2773 / prev_m->note_divisor()) + prev_m->pulse());
2774 solve_map_minute (imaginary, meter_locked_tempo, minute);
2779 Metrics::const_iterator d = future_map.begin();
2780 while (d != future_map.end()) {
2790 /* not movable (first meter atm) */
2792 tempo_copy->set_minute (minute);
2793 tempo_copy->set_pulse (0.0);
2795 if ((solved = solve_map_minute (future_map, tempo_copy, minute))) {
2796 section->set_minute (minute);
2797 meter_locked_tempo->set_minute (minute);
2798 meter_locked_tempo->set_pulse (0.0);
2799 solve_map_minute (imaginary, meter_locked_tempo, minute);
2804 Metrics::const_iterator d = future_map.begin();
2805 while (d != future_map.end()) {
2814 pair<double, BBT_Time> b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
2815 section->set_beat (b_bbt);
2816 section->set_pulse (0.0);
2826 MetricSectionFrameSorter fcmp;
2827 imaginary.sort (fcmp);
2829 recompute_meters (imaginary);
2835 TempoMap::solve_map_bbt (Metrics& imaginary, MeterSection* section, const BBT_Time& when)
2837 /* disallow setting section to an existing meter's bbt */
2838 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2840 if (!(*i)->is_tempo()) {
2841 m = static_cast<MeterSection*> (*i);
2842 if (m != section && m->bbt().bars == when.bars) {
2848 MeterSection* prev_m = 0;
2849 MeterSection* section_prev = 0;
2851 for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
2853 if (!(*i)->is_tempo()) {
2854 m = static_cast<MeterSection*> (*i);
2855 pair<double, BBT_Time> b_bbt;
2856 double new_pulse = 0.0;
2858 if (prev_m && m->bbt().bars > when.bars && !section_prev){
2859 section_prev = prev_m;
2860 const double beats = (when.bars - section_prev->bbt().bars) * section_prev->divisions_per_bar();
2861 const double pulse = (beats / section_prev->note_divisor()) + section_prev->pulse();
2862 pair<double, BBT_Time> b_bbt = make_pair (beats + section_prev->beat(), when);
2864 section->set_beat (b_bbt);
2865 section->set_pulse (pulse);
2866 section->set_minute (minute_at_pulse_locked (imaginary, pulse));
2871 if (m->position_lock_style() == AudioTime) {
2872 TempoSection* meter_locked_tempo = 0;
2874 for (Metrics::const_iterator ii = imaginary.begin(); ii != imaginary.end(); ++ii) {
2876 if ((*ii)->is_tempo()) {
2877 t = static_cast<TempoSection*> (*ii);
2878 if ((t->locked_to_meter() || !t->movable()) && t->frame() == m->frame()) {
2879 meter_locked_tempo = t;
2885 if (!meter_locked_tempo) {
2890 const double beats = ((m->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar());
2892 if (beats + prev_m->beat() != m->beat()) {
2893 /* tempo/ meter change caused a change in beat (bar). */
2894 b_bbt = make_pair (beats + prev_m->beat()
2895 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
2896 new_pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
2897 } else if (m->movable()) {
2898 b_bbt = make_pair (m->beat(), m->bbt());
2899 new_pulse = prev_m->pulse() + (beats / prev_m->note_divisor());
2902 b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
2905 meter_locked_tempo->set_pulse (new_pulse);
2906 m->set_beat (b_bbt);
2907 m->set_pulse (new_pulse);
2911 const double beats = ((m->bbt().bars - prev_m->bbt().bars) * prev_m->divisions_per_bar());
2912 if (beats + prev_m->beat() != m->beat()) {
2913 /* tempo/ meter change caused a change in beat (bar). */
2914 b_bbt = make_pair (beats + prev_m->beat()
2915 , BBT_Time ((beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
2917 b_bbt = make_pair (beats + prev_m->beat()
2920 new_pulse = (beats / prev_m->note_divisor()) + prev_m->pulse();
2921 m->set_beat (b_bbt);
2922 m->set_pulse (new_pulse);
2923 m->set_minute (minute_at_pulse_locked (imaginary, new_pulse));
2930 if (!section_prev) {
2932 const double beats = (when.bars - prev_m->bbt().bars) * prev_m->divisions_per_bar();
2933 const double pulse = (beats / prev_m->note_divisor()) + prev_m->pulse();
2934 pair<double, BBT_Time> b_bbt = make_pair (beats + prev_m->beat(), when);
2936 section->set_beat (b_bbt);
2937 section->set_pulse (pulse);
2938 section->set_minute (minute_at_pulse_locked (imaginary, pulse));
2941 MetricSectionSorter cmp;
2942 imaginary.sort (cmp);
2944 recompute_meters (imaginary);
2949 /** places a copy of _metrics into copy and returns a pointer
2950 * to section's equivalent in copy.
2953 TempoMap::copy_metrics_and_point (const Metrics& metrics, Metrics& copy, TempoSection* section)
2955 TempoSection* ret = 0;
2957 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2960 if ((*i)->is_tempo()) {
2961 t = static_cast<TempoSection*> (*i);
2963 ret = new TempoSection (*t);
2964 copy.push_back (ret);
2968 TempoSection* cp = new TempoSection (*t);
2969 copy.push_back (cp);
2971 if (!(*i)->is_tempo()) {
2972 m = static_cast<MeterSection *> (*i);
2973 MeterSection* cp = new MeterSection (*m);
2974 copy.push_back (cp);
2982 TempoMap::copy_metrics_and_point (const Metrics& metrics, Metrics& copy, MeterSection* section)
2984 MeterSection* ret = 0;
2986 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
2989 if ((*i)->is_tempo()) {
2990 t = static_cast<TempoSection*> (*i);
2991 TempoSection* cp = new TempoSection (*t);
2992 copy.push_back (cp);
2995 if (!(*i)->is_tempo()) {
2996 m = static_cast<MeterSection *> (*i);
2998 ret = new MeterSection (*m);
2999 copy.push_back (ret);
3002 MeterSection* cp = new MeterSection (*m);
3003 copy.push_back (cp);
3010 /** answers the question "is this a valid beat position for this tempo section?".
3011 * it returns true if the tempo section can be moved to the requested bbt position,
3012 * leaving the tempo map in a solved state.
3013 * @param ts the tempo section to be moved
3014 * @param bbt the requested new position for the tempo section
3015 * @return true if the tempo section can be moved to the position, otherwise false.
3018 TempoMap::can_solve_bbt (TempoSection* ts, const BBT_Time& bbt)
3021 TempoSection* tempo_copy = 0;
3024 Glib::Threads::RWLock::ReaderLock lm (lock);
3025 tempo_copy = copy_metrics_and_point (_metrics, copy, ts);
3031 const bool ret = solve_map_pulse (copy, tempo_copy, pulse_at_bbt_locked (copy, bbt));
3033 Metrics::const_iterator d = copy.begin();
3034 while (d != copy.end()) {
3043 * 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,
3044 * taking any possible reordering as a consequence of this into account.
3045 * @param section - the section to be altered
3046 * @param bbt - the BBT time where the altered tempo will fall
3047 * @return returns - the position in pulses and frames (as a pair) where the new tempo section will lie.
3049 pair<double, framepos_t>
3050 TempoMap::predict_tempo_position (TempoSection* section, const BBT_Time& bbt)
3053 pair<double, framepos_t> ret = make_pair (0.0, 0);
3055 Glib::Threads::RWLock::ReaderLock lm (lock);
3057 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, section);
3059 const double beat = beat_at_bbt_locked (future_map, bbt);
3061 if (solve_map_pulse (future_map, tempo_copy, pulse_at_beat_locked (future_map, beat))) {
3062 ret.first = tempo_copy->pulse();
3063 ret.second = tempo_copy->frame();
3065 ret.first = section->pulse();
3066 ret.second = section->frame();
3069 Metrics::const_iterator d = future_map.begin();
3070 while (d != future_map.end()) {
3077 /** moves a TempoSection to a specified position.
3078 * @param ts - the section to be moved
3079 * @param frame - the new position in frames for the tempo
3080 * @param sub_num - the snap division to use if using musical time.
3082 * if sub_num is non-zero, the frame position is used to calculate an exact
3085 * -1 | snap to bars (meter-based)
3086 * 0 | no snap - use audio frame for musical position
3087 * 1 | snap to meter-based (BBT) beat
3088 * >1 | snap to quarter-note subdivision (i.e. 4 will snap to sixteenth notes)
3090 * this follows the snap convention in the gui.
3091 * if sub_num is zero, the musical position will be taken from the supplied frame.
3094 TempoMap::gui_move_tempo (TempoSection* ts, const framepos_t& frame, const int& sub_num)
3098 if (ts->position_lock_style() == MusicTime) {
3100 /* if we're snapping to a musical grid, set the pulse exactly instead of via the supplied frame. */
3101 Glib::Threads::RWLock::WriterLock lm (lock);
3102 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
3104 tempo_copy->set_position_lock_style (AudioTime);
3106 if (solve_map_minute (future_map, tempo_copy, minute_at_frame (frame))) {
3107 const double beat = exact_beat_at_frame_locked (future_map, frame, sub_num);
3108 const double pulse = pulse_at_beat_locked (future_map, beat);
3110 if (solve_map_pulse (future_map, tempo_copy, pulse)) {
3111 solve_map_pulse (_metrics, ts, pulse);
3112 recompute_meters (_metrics);
3120 Glib::Threads::RWLock::WriterLock lm (lock);
3121 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
3123 if (solve_map_minute (future_map, tempo_copy, minute_at_frame (frame))) {
3125 /* We're moving the object that defines the grid while snapping to it...
3126 * Placing the ts at the beat corresponding to the requested frame may shift the
3127 * grid in such a way that the mouse is left hovering over a completerly different division,
3128 * causing jittering when the mouse next moves (esp. large tempo deltas).
3129 * To avoid this, place the ts at the requested frame in a dummy map
3130 * then find the closest beat subdivision to that frame in the dummy.
3131 * This alters the snap behaviour slightly in that we snap to beat divisions
3132 * in the future map rather than the existing one.
3134 const double beat = exact_beat_at_frame_locked (future_map, frame, sub_num);
3135 const double pulse = pulse_at_beat_locked (future_map, beat);
3137 if (solve_map_pulse (future_map, tempo_copy, pulse)) {
3138 /* snapping to a grid. force MusicTime temporarily. */
3139 ts->set_position_lock_style (MusicTime);
3140 solve_map_pulse (_metrics, ts, pulse);
3141 ts->set_position_lock_style (AudioTime);
3143 recompute_meters (_metrics);
3146 solve_map_minute (_metrics, ts, minute_at_frame (frame));
3147 recompute_meters (_metrics);
3153 Metrics::const_iterator d = future_map.begin();
3154 while (d != future_map.end()) {
3159 MetricPositionChanged (); // Emit Signal
3162 /** moves a MeterSection to a specified position.
3163 * @param ms - the section to be moved
3164 * @param frame - the new position in frames for the meter
3166 * as a meter cannot snap to anything but bars,
3167 * the supplied frame is rounded to the nearest bar, possibly
3168 * leaving the meter position unchanged.
3171 TempoMap::gui_move_meter (MeterSection* ms, const framepos_t& frame)
3175 if (ms->position_lock_style() == AudioTime) {
3178 Glib::Threads::RWLock::WriterLock lm (lock);
3179 MeterSection* copy = copy_metrics_and_point (_metrics, future_map, ms);
3181 if (solve_map_minute (future_map, copy, minute_at_frame (frame))) {
3182 solve_map_minute (_metrics, ms, minute_at_frame (frame));
3183 recompute_tempi (_metrics);
3188 Glib::Threads::RWLock::WriterLock lm (lock);
3189 MeterSection* copy = copy_metrics_and_point (_metrics, future_map, ms);
3191 const double beat = beat_at_minute_locked (_metrics, minute_at_frame (frame));
3192 const Timecode::BBT_Time bbt = bbt_at_beat_locked (_metrics, beat);
3194 if (solve_map_bbt (future_map, copy, bbt)) {
3195 solve_map_bbt (_metrics, ms, bbt);
3196 recompute_tempi (_metrics);
3201 Metrics::const_iterator d = future_map.begin();
3202 while (d != future_map.end()) {
3207 MetricPositionChanged (); // Emit Signal
3211 TempoMap::gui_change_tempo (TempoSection* ts, const Tempo& bpm)
3214 bool can_solve = false;
3216 Glib::Threads::RWLock::WriterLock lm (lock);
3217 TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
3218 tempo_copy->set_note_types_per_minute (bpm.note_types_per_minute());
3219 recompute_tempi (future_map);
3221 if (check_solved (future_map)) {
3222 ts->set_note_types_per_minute (bpm.note_types_per_minute());
3223 recompute_map (_metrics);
3228 Metrics::const_iterator d = future_map.begin();
3229 while (d != future_map.end()) {
3234 MetricPositionChanged (); // Emit Signal
3240 TempoMap::gui_dilate_tempo (TempoSection* ts, const framepos_t& frame, const framepos_t& end_frame)
3243 Ts (future prev_t) Tnext
3246 |----------|----------
3253 Glib::Threads::RWLock::WriterLock lm (lock);
3259 TempoSection* prev_t = copy_metrics_and_point (_metrics, future_map, ts);
3260 TempoSection* prev_to_prev_t = 0;
3261 const frameoffset_t fr_off = end_frame - frame;
3265 if (prev_t->pulse() > 0.0) {
3266 prev_to_prev_t = const_cast<TempoSection*>(&tempo_section_at_minute_locked (future_map, minute_at_frame (prev_t->frame() - 1)));
3269 TempoSection* next_t = 0;
3270 for (Metrics::iterator i = future_map.begin(); i != future_map.end(); ++i) {
3271 TempoSection* t = 0;
3272 if ((*i)->is_tempo()) {
3273 t = static_cast<TempoSection*> (*i);
3274 if (t->frame() > ts->frame()) {
3280 /* minimum allowed measurement distance in frames */
3281 const framepos_t min_dframe = 2;
3283 /* the change in frames is the result of changing the slope of at most 2 previous tempo sections.
3284 constant to constant is straightforward, as the tempo prev to prev_t has constant slope.
3286 double contribution = 0.0;
3288 if (next_t && prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
3289 contribution = (prev_t->frame() - prev_to_prev_t->frame()) / (double) (next_t->frame() - prev_to_prev_t->frame());
3292 const frameoffset_t prev_t_frame_contribution = fr_off - (contribution * (double) fr_off);
3294 const double start_pulse = prev_t->pulse_at_minute (minute_at_frame (frame));
3295 const double end_pulse = prev_t->pulse_at_minute (minute_at_frame (end_frame));
3299 if (prev_t->type() == TempoSection::Constant || prev_t->c_func() == 0.0) {
3301 if (prev_t->position_lock_style() == MusicTime) {
3302 if (prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
3303 if (frame > prev_to_prev_t->frame() + min_dframe && (frame + prev_t_frame_contribution) > prev_to_prev_t->frame() + min_dframe) {
3305 new_bpm = prev_t->note_types_per_minute() * ((frame - prev_to_prev_t->frame())
3306 / (double) ((frame + prev_t_frame_contribution) - prev_to_prev_t->frame()));
3308 new_bpm = prev_t->note_types_per_minute();
3311 /* prev to prev is irrelevant */
3313 if (start_pulse > prev_t->pulse() && end_pulse > prev_t->pulse()) {
3314 new_bpm = prev_t->note_types_per_minute() * ((start_pulse - prev_t->pulse()) / (end_pulse - prev_t->pulse()));
3316 new_bpm = prev_t->note_types_per_minute();
3321 if (prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
3322 if (frame > prev_to_prev_t->frame() + min_dframe && end_frame > prev_to_prev_t->frame() + min_dframe) {
3324 new_bpm = prev_t->note_types_per_minute() * ((frame - prev_to_prev_t->frame())
3325 / (double) ((end_frame) - prev_to_prev_t->frame()));
3327 new_bpm = prev_t->note_types_per_minute();
3330 /* prev_to_prev_t is irrelevant */
3332 if (frame > prev_t->frame() + min_dframe && end_frame > prev_t->frame() + min_dframe) {
3333 new_bpm = prev_t->note_types_per_minute() * ((frame - prev_t->frame()) / (double) (end_frame - prev_t->frame()));
3335 new_bpm = prev_t->note_types_per_minute();
3341 double frame_ratio = 1.0;
3342 double pulse_ratio = 1.0;
3343 const double pulse_pos = frame;
3345 if (prev_to_prev_t) {
3346 if (pulse_pos > prev_to_prev_t->frame() + min_dframe && (pulse_pos - fr_off) > prev_to_prev_t->frame() + min_dframe) {
3347 frame_ratio = (((pulse_pos - fr_off) - prev_to_prev_t->frame()) / (double) ((pulse_pos) - prev_to_prev_t->frame()));
3349 if (end_pulse > prev_to_prev_t->pulse() && start_pulse > prev_to_prev_t->pulse()) {
3350 pulse_ratio = ((start_pulse - prev_to_prev_t->pulse()) / (end_pulse - prev_to_prev_t->pulse()));
3353 if (pulse_pos > prev_t->frame() + min_dframe && (pulse_pos - fr_off) > prev_t->frame() + min_dframe) {
3354 frame_ratio = (((pulse_pos - fr_off) - prev_t->frame()) / (double) ((pulse_pos) - prev_t->frame()));
3356 pulse_ratio = (start_pulse / end_pulse);
3358 new_bpm = prev_t->note_types_per_minute() * (pulse_ratio * frame_ratio);
3361 /* don't clamp and proceed here.
3362 testing has revealed that this can go negative,
3363 which is an entirely different thing to just being too low.
3365 if (new_bpm < 0.5) {
3368 new_bpm = min (new_bpm, (double) 1000.0);
3369 prev_t->set_note_types_per_minute (new_bpm);
3370 recompute_tempi (future_map);
3371 recompute_meters (future_map);
3373 if (check_solved (future_map)) {
3374 ts->set_note_types_per_minute (new_bpm);
3375 recompute_tempi (_metrics);
3376 recompute_meters (_metrics);
3380 Metrics::const_iterator d = future_map.begin();
3381 while (d != future_map.end()) {
3386 MetricPositionChanged (); // Emit Signal
3389 /** Returns the exact bbt-based beat corresponding to the bar, beat or quarter note subdivision nearest to
3390 * the supplied frame, possibly returning a negative value.
3392 * @param frame The session frame position.
3393 * @param sub_num The subdivision to use when rounding the beat.
3394 * A value of -1 indicates rounding to BBT bar. 1 indicates rounding to BBT beats.
3395 * Positive integers indicate quarter note (non BBT) divisions.
3396 * 0 indicates that the returned beat should not be rounded (equivalent to quarter_note_at_frame()).
3397 * @return The beat position of the supplied frame.
3399 * when working to a musical grid, the use of sub_nom indicates that
3400 * the position should be interpreted musically.
3402 * it effectively snaps to meter bars, meter beats or quarter note divisions
3403 * (as per current gui convention) and returns a musical position independent of frame rate.
3405 * If the supplied frame lies before the first meter, the return will be negative,
3406 * in which case the returned beat uses the first meter (for BBT subdivisions) and
3407 * the continuation of the tempo curve (backwards).
3409 * This function is sensitive to tempo and meter.
3412 TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num)
3414 Glib::Threads::RWLock::ReaderLock lm (lock);
3416 return exact_beat_at_frame_locked (_metrics, frame, sub_num);
3420 TempoMap::exact_beat_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t divisions)
3422 return beat_at_pulse_locked (_metrics, exact_qn_at_frame_locked (metrics, frame, divisions) / 4.0);
3425 /** Returns the exact quarter note corresponding to the bar, beat or quarter note subdivision nearest to
3426 * the supplied frame, possibly returning a negative value.
3428 * @param frame The session frame position.
3429 * @param sub_num The subdivision to use when rounding the quarter note.
3430 * A value of -1 indicates rounding to BBT bar. 1 indicates rounding to BBT beats.
3431 * Positive integers indicate quarter note (non BBT) divisions.
3432 * 0 indicates that the returned quarter note should not be rounded (equivalent to quarter_note_at_frame()).
3433 * @return The quarter note position of the supplied frame.
3435 * When working to a musical grid, the use of sub_nom indicates that
3436 * the frame position should be interpreted musically.
3438 * it effectively snaps to meter bars, meter beats or quarter note divisions
3439 * (as per current gui convention) and returns a musical position independent of frame rate.
3441 * If the supplied frame lies before the first meter, the return will be negative,
3442 * in which case the returned quarter note uses the first meter (for BBT subdivisions) and
3443 * the continuation of the tempo curve (backwards).
3445 * This function is tempo-sensitive.
3448 TempoMap::exact_qn_at_frame (const framepos_t& frame, const int32_t sub_num)
3450 Glib::Threads::RWLock::ReaderLock lm (lock);
3452 return exact_qn_at_frame_locked (_metrics, frame, sub_num);
3456 TempoMap::exact_qn_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t sub_num)
3458 double qn = quarter_note_at_minute_locked (metrics, minute_at_frame (frame));
3461 qn = floor (qn) + (floor (((qn - floor (qn)) * (double) sub_num) + 0.5) / sub_num);
3462 } else if (sub_num == 1) {
3463 /* the gui requested exact musical (BBT) beat */
3464 qn = quarter_note_at_beat_locked (metrics, floor (beat_at_minute_locked (metrics, minute_at_frame (frame)) + 0.5));
3465 } else if (sub_num == -1) {
3467 Timecode::BBT_Time bbt = bbt_at_pulse_locked (metrics, qn / 4.0);
3471 const double prev_b = pulse_at_bbt_locked (metrics, bbt) * 4.0;
3473 const double next_b = pulse_at_bbt_locked (metrics, bbt) * 4.0;
3475 if ((qn - prev_b) > (next_b - prev_b) / 2.0) {
3485 /** returns the frame duration of the supplied BBT time at a specified frame position in the tempo map.
3486 * @param pos the frame position in the tempo map.
3487 * @param bbt the distance in BBT time from pos to calculate.
3488 * @param dir the rounding direction..
3489 * @return the duration in frames between pos and bbt
3492 TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir)
3494 Glib::Threads::RWLock::ReaderLock lm (lock);
3496 BBT_Time pos_bbt = bbt_at_minute_locked (_metrics, minute_at_frame (pos));
3498 const double divisions = meter_section_at_minute_locked (_metrics, minute_at_frame (pos)).divisions_per_bar();
3501 pos_bbt.bars += bbt.bars;
3503 pos_bbt.ticks += bbt.ticks;
3504 if ((double) pos_bbt.ticks > BBT_Time::ticks_per_beat) {
3506 pos_bbt.ticks -= BBT_Time::ticks_per_beat;
3509 pos_bbt.beats += bbt.beats;
3510 if ((double) pos_bbt.beats > divisions) {
3512 pos_bbt.beats -= divisions;
3514 const framecnt_t pos_bbt_frame = frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
3516 return pos_bbt_frame - pos;
3520 if (pos_bbt.bars <= bbt.bars) {
3523 pos_bbt.bars -= bbt.bars;
3526 if (pos_bbt.ticks < bbt.ticks) {
3527 if (pos_bbt.bars > 1) {
3528 if (pos_bbt.beats == 1) {
3530 pos_bbt.beats = divisions;
3534 pos_bbt.ticks = BBT_Time::ticks_per_beat - (bbt.ticks - pos_bbt.ticks);
3540 pos_bbt.ticks -= bbt.ticks;
3543 if (pos_bbt.beats <= bbt.beats) {
3544 if (pos_bbt.bars > 1) {
3546 pos_bbt.beats = divisions - (bbt.beats - pos_bbt.beats);
3551 pos_bbt.beats -= bbt.beats;
3554 return pos - frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
3561 TempoMap::round_to_bar (framepos_t fr, RoundMode dir)
3563 return round_to_type (fr, dir, Bar);
3567 TempoMap::round_to_beat (framepos_t fr, RoundMode dir)
3569 return round_to_type (fr, dir, Beat);
3573 TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, RoundMode dir)
3575 Glib::Threads::RWLock::ReaderLock lm (lock);
3576 uint32_t ticks = (uint32_t) floor (max (0.0, beat_at_minute_locked (_metrics, minute_at_frame (fr))) * BBT_Time::ticks_per_beat);
3577 uint32_t beats = (uint32_t) floor (ticks / BBT_Time::ticks_per_beat);
3578 uint32_t ticks_one_subdivisions_worth = (uint32_t) BBT_Time::ticks_per_beat / sub_num;
3580 ticks -= beats * BBT_Time::ticks_per_beat;
3583 /* round to next (or same iff dir == RoundUpMaybe) */
3585 uint32_t mod = ticks % ticks_one_subdivisions_worth;
3587 if (mod == 0 && dir == RoundUpMaybe) {
3588 /* right on the subdivision, which is fine, so do nothing */
3590 } else if (mod == 0) {
3591 /* right on the subdivision, so the difference is just the subdivision ticks */
3592 ticks += ticks_one_subdivisions_worth;
3595 /* not on subdivision, compute distance to next subdivision */
3597 ticks += ticks_one_subdivisions_worth - mod;
3600 if (ticks >= BBT_Time::ticks_per_beat) {
3601 ticks -= BBT_Time::ticks_per_beat;
3603 } else if (dir < 0) {
3605 /* round to previous (or same iff dir == RoundDownMaybe) */
3607 uint32_t difference = ticks % ticks_one_subdivisions_worth;
3609 if (difference == 0 && dir == RoundDownAlways) {
3610 /* right on the subdivision, but force-rounding down,
3611 so the difference is just the subdivision ticks */
3612 difference = ticks_one_subdivisions_worth;
3615 if (ticks < difference) {
3616 ticks = BBT_Time::ticks_per_beat - ticks;
3618 ticks -= difference;
3622 /* round to nearest */
3625 /* compute the distance to the previous and next subdivision */
3627 if ((rem = fmod ((double) ticks, (double) ticks_one_subdivisions_worth)) > ticks_one_subdivisions_worth/2.0) {
3629 /* closer to the next subdivision, so shift forward */
3631 ticks = lrint (ticks + (ticks_one_subdivisions_worth - rem));
3633 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved forward to %1\n", ticks));
3635 if (ticks > BBT_Time::ticks_per_beat) {
3637 ticks -= BBT_Time::ticks_per_beat;
3638 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("fold beat to %1\n", beats));
3641 } else if (rem > 0) {
3643 /* closer to previous subdivision, so shift backward */
3647 /* can't go backwards past zero, so ... */
3650 /* step back to previous beat */
3652 ticks = lrint (BBT_Time::ticks_per_beat - rem);
3653 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("step back beat to %1\n", beats));
3655 ticks = lrint (ticks - rem);
3656 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved backward to %1\n", ticks));
3659 /* on the subdivision, do nothing */
3663 const framepos_t ret_frame = frame_at_minute (minute_at_beat_locked (_metrics, beats + (ticks / BBT_Time::ticks_per_beat)));
3669 TempoMap::round_to_quarter_note_subdivision (framepos_t fr, int sub_num, RoundMode dir)
3671 Glib::Threads::RWLock::ReaderLock lm (lock);
3672 uint32_t ticks = (uint32_t) floor (max (0.0, quarter_note_at_minute_locked (_metrics, minute_at_frame (fr))) * BBT_Time::ticks_per_beat);
3673 uint32_t beats = (uint32_t) floor (ticks / BBT_Time::ticks_per_beat);
3674 uint32_t ticks_one_subdivisions_worth = (uint32_t) BBT_Time::ticks_per_beat / sub_num;
3676 ticks -= beats * BBT_Time::ticks_per_beat;
3679 /* round to next (or same iff dir == RoundUpMaybe) */
3681 uint32_t mod = ticks % ticks_one_subdivisions_worth;
3683 if (mod == 0 && dir == RoundUpMaybe) {
3684 /* right on the subdivision, which is fine, so do nothing */
3686 } else if (mod == 0) {
3687 /* right on the subdivision, so the difference is just the subdivision ticks */
3688 ticks += ticks_one_subdivisions_worth;
3691 /* not on subdivision, compute distance to next subdivision */
3693 ticks += ticks_one_subdivisions_worth - mod;
3696 if (ticks >= BBT_Time::ticks_per_beat) {
3697 ticks -= BBT_Time::ticks_per_beat;
3699 } else if (dir < 0) {
3701 /* round to previous (or same iff dir == RoundDownMaybe) */
3703 uint32_t difference = ticks % ticks_one_subdivisions_worth;
3705 if (difference == 0 && dir == RoundDownAlways) {
3706 /* right on the subdivision, but force-rounding down,
3707 so the difference is just the subdivision ticks */
3708 difference = ticks_one_subdivisions_worth;
3711 if (ticks < difference) {
3712 ticks = BBT_Time::ticks_per_beat - ticks;
3714 ticks -= difference;
3718 /* round to nearest */
3721 /* compute the distance to the previous and next subdivision */
3723 if ((rem = fmod ((double) ticks, (double) ticks_one_subdivisions_worth)) > ticks_one_subdivisions_worth/2.0) {
3725 /* closer to the next subdivision, so shift forward */
3727 ticks = lrint (ticks + (ticks_one_subdivisions_worth - rem));
3729 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved forward to %1\n", ticks));
3731 if (ticks > BBT_Time::ticks_per_beat) {
3733 ticks -= BBT_Time::ticks_per_beat;
3734 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("fold beat to %1\n", beats));
3737 } else if (rem > 0) {
3739 /* closer to previous subdivision, so shift backward */
3743 /* can't go backwards past zero, so ... */
3746 /* step back to previous beat */
3748 ticks = lrint (BBT_Time::ticks_per_beat - rem);
3749 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("step back beat to %1\n", beats));
3751 ticks = lrint (ticks - rem);
3752 DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("moved backward to %1\n", ticks));
3755 /* on the subdivision, do nothing */
3759 const framepos_t ret_frame = frame_at_minute (minute_at_quarter_note_locked (_metrics, beats + (ticks / BBT_Time::ticks_per_beat)));
3765 TempoMap::round_to_type (framepos_t frame, RoundMode dir, BBTPointType type)
3767 Glib::Threads::RWLock::ReaderLock lm (lock);
3769 const double beat_at_framepos = max (0.0, beat_at_minute_locked (_metrics, minute_at_frame (frame)));
3770 BBT_Time bbt (bbt_at_beat_locked (_metrics, beat_at_framepos));
3775 /* find bar previous to 'frame' */
3778 return frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3780 } else if (dir > 0) {
3781 /* find bar following 'frame' */
3785 return frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3787 /* true rounding: find nearest bar */
3788 framepos_t raw_ft = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3791 framepos_t prev_ft = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3793 framepos_t next_ft = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3795 if ((raw_ft - prev_ft) > (next_ft - prev_ft) / 2) {
3806 return frame_at_minute (minute_at_beat_locked (_metrics, floor (beat_at_framepos)));
3807 } else if (dir > 0) {
3808 return frame_at_minute (minute_at_beat_locked (_metrics, ceil (beat_at_framepos)));
3810 return frame_at_minute (minute_at_beat_locked (_metrics, floor (beat_at_framepos + 0.5)));
3819 TempoMap::get_grid (vector<TempoMap::BBTPoint>& points,
3820 framepos_t lower, framepos_t upper, uint32_t bar_mod)
3822 Glib::Threads::RWLock::ReaderLock lm (lock);
3823 int32_t cnt = ceil (beat_at_minute_locked (_metrics, minute_at_frame (lower)));
3825 /* although the map handles negative beats, bbt doesn't. */
3830 if (minute_at_beat_locked (_metrics, cnt) >= minute_at_frame (upper)) {
3834 while (pos >= 0 && pos < upper) {
3835 pos = frame_at_minute (minute_at_beat_locked (_metrics, cnt));
3836 const TempoSection tempo = tempo_section_at_minute_locked (_metrics, minute_at_frame (pos));
3837 const MeterSection meter = meter_section_at_minute_locked (_metrics, minute_at_frame (pos));
3838 const BBT_Time bbt = bbt_at_beat_locked (_metrics, cnt);
3839 points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, tempo.c_func()));
3843 BBT_Time bbt = bbt_at_minute_locked (_metrics, minute_at_frame (lower));
3848 bbt.bars -= bbt.bars % bar_mod;
3852 while (pos >= 0 && pos < upper) {
3853 pos = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
3854 const TempoSection tempo = tempo_section_at_minute_locked (_metrics, minute_at_frame (pos));
3855 const MeterSection meter = meter_section_at_minute_locked (_metrics, minute_at_frame (pos));
3856 points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, tempo.c_func()));
3857 bbt.bars += bar_mod;
3863 TempoMap::tempo_section_at_frame (framepos_t frame) const
3865 Glib::Threads::RWLock::ReaderLock lm (lock);
3867 return tempo_section_at_minute_locked (_metrics, minute_at_frame (frame));
3871 TempoMap::tempo_section_at_minute_locked (const Metrics& metrics, double minute) const
3873 TempoSection* prev = 0;
3877 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
3879 if ((*i)->is_tempo()) {
3880 t = static_cast<TempoSection*> (*i);
3884 if (prev && t->minute() > minute) {
3894 abort(); /*NOTREACHED*/
3901 TempoMap::tempo_section_at_beat_locked (const Metrics& metrics, const double& beat) const
3903 TempoSection* prev_t = 0;
3904 const MeterSection* prev_m = &meter_section_at_beat_locked (metrics, beat);
3908 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
3909 if ((*i)->is_tempo()) {
3910 t = static_cast<TempoSection*> (*i);
3911 if (prev_t && ((t->pulse() - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat() > beat) {
3921 /* don't use this to calculate length (the tempo is only correct for this frame).
3922 do that stuff based on the beat_at_frame and frame_at_beat api
3925 TempoMap::frames_per_quarter_note_at (const framepos_t& frame, const framecnt_t& sr) const
3927 Glib::Threads::RWLock::ReaderLock lm (lock);
3929 const TempoSection* ts_at = 0;
3930 const TempoSection* ts_after = 0;
3931 Metrics::const_iterator i;
3934 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
3936 if ((*i)->is_tempo()) {
3937 t = static_cast<TempoSection*> (*i);
3941 if (ts_at && (*i)->frame() > frame) {
3951 return (60.0 * _frame_rate) / ts_at->tempo_at_minute (minute_at_frame (frame)).quarter_notes_per_minute();
3953 /* must be treated as constant tempo */
3954 return ts_at->frames_per_quarter_note (_frame_rate);
3958 TempoMap::meter_section_at_frame (framepos_t frame) const
3960 Glib::Threads::RWLock::ReaderLock lm (lock);
3961 return meter_section_at_minute_locked (_metrics, minute_at_frame (frame));
3965 TempoMap::meter_section_at_minute_locked (const Metrics& metrics, double minute) const
3967 Metrics::const_iterator i;
3968 MeterSection* prev = 0;
3972 for (i = metrics.begin(); i != metrics.end(); ++i) {
3974 if (!(*i)->is_tempo()) {
3975 m = static_cast<MeterSection*> (*i);
3977 if (prev && (*i)->minute() > minute) {
3987 abort(); /*NOTREACHED*/
3994 TempoMap::meter_section_at_beat_locked (const Metrics& metrics, const double& beat) const
3996 MeterSection* prev_m = 0;
3998 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
4000 if (!(*i)->is_tempo()) {
4001 m = static_cast<MeterSection*> (*i);
4002 if (prev_m && m->beat() > beat) {
4013 TempoMap::meter_section_at_beat (double beat) const
4015 Glib::Threads::RWLock::ReaderLock lm (lock);
4016 return meter_section_at_beat_locked (_metrics, beat);
4020 TempoMap::meter_at_frame (framepos_t frame) const
4022 TempoMetric m (metric_at (frame));
4027 TempoMap::fix_legacy_session ()
4029 MeterSection* prev_m = 0;
4030 TempoSection* prev_t = 0;
4032 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4036 if ((m = dynamic_cast<MeterSection*>(*i)) != 0) {
4037 if (!m->movable()) {
4038 pair<double, BBT_Time> bbt = make_pair (0.0, BBT_Time (1, 1, 0));
4041 m->set_minute (0.0);
4042 m->set_position_lock_style (AudioTime);
4047 pair<double, BBT_Time> start = make_pair (((m->bbt().bars - 1) * prev_m->note_divisor())
4048 + (m->bbt().beats - 1)
4049 + (m->bbt().ticks / BBT_Time::ticks_per_beat)
4051 m->set_beat (start);
4052 const double start_beat = ((m->bbt().bars - 1) * prev_m->note_divisor())
4053 + (m->bbt().beats - 1)
4054 + (m->bbt().ticks / BBT_Time::ticks_per_beat);
4055 m->set_pulse (start_beat / prev_m->note_divisor());
4058 } else if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
4064 if (!t->movable()) {
4066 t->set_minute (0.0);
4067 t->set_position_lock_style (AudioTime);
4073 const double beat = ((t->legacy_bbt().bars - 1) * ((prev_m) ? prev_m->note_divisor() : 4.0))
4074 + (t->legacy_bbt().beats - 1)
4075 + (t->legacy_bbt().ticks / BBT_Time::ticks_per_beat);
4077 t->set_pulse (beat / prev_m->note_divisor());
4079 /* really shouldn't happen but.. */
4080 t->set_pulse (beat / 4.0);
4089 TempoMap::get_state ()
4091 Metrics::const_iterator i;
4092 XMLNode *root = new XMLNode ("TempoMap");
4095 Glib::Threads::RWLock::ReaderLock lm (lock);
4096 for (i = _metrics.begin(); i != _metrics.end(); ++i) {
4097 root->add_child_nocopy ((*i)->get_state());
4105 TempoMap::set_state (const XMLNode& node, int /*version*/)
4108 Glib::Threads::RWLock::WriterLock lm (lock);
4111 XMLNodeConstIterator niter;
4112 Metrics old_metrics (_metrics);
4115 nlist = node.children();
4117 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
4118 XMLNode* child = *niter;
4120 if (child->name() == TempoSection::xml_state_node_name) {
4123 TempoSection* ts = new TempoSection (*child, _frame_rate);
4124 _metrics.push_back (ts);
4127 catch (failed_constructor& err){
4128 error << _("Tempo map: could not set new state, restoring old one.") << endmsg;
4129 _metrics = old_metrics;
4130 old_metrics.clear();
4134 } else if (child->name() == MeterSection::xml_state_node_name) {
4137 MeterSection* ms = new MeterSection (*child, _frame_rate);
4138 _metrics.push_back (ms);
4141 catch (failed_constructor& err) {
4142 error << _("Tempo map: could not set new state, restoring old one.") << endmsg;
4143 _metrics = old_metrics;
4144 old_metrics.clear();
4150 if (niter == nlist.end()) {
4151 MetricSectionSorter cmp;
4152 _metrics.sort (cmp);
4155 /* check for legacy sessions where bbt was the base musical unit for tempo */
4156 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4158 if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
4159 if (t->legacy_bbt().bars != 0) {
4160 fix_legacy_session();
4167 /* check for multiple tempo/meters at the same location, which
4168 ardour2 somehow allowed.
4171 Metrics::iterator prev = _metrics.end();
4172 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4173 if (prev != _metrics.end()) {
4175 MeterSection* prev_m;
4177 TempoSection* prev_t;
4178 if ((prev_m = dynamic_cast<MeterSection*>(*prev)) != 0 && (ms = dynamic_cast<MeterSection*>(*i)) != 0) {
4179 if (prev_m->pulse() == ms->pulse()) {
4180 cerr << string_compose (_("Multiple meter definitions found at %1"), prev_m->pulse()) << endmsg;
4181 error << string_compose (_("Multiple meter definitions found at %1"), prev_m->pulse()) << endmsg;
4184 } else if ((prev_t = dynamic_cast<TempoSection*>(*prev)) != 0 && (ts = dynamic_cast<TempoSection*>(*i)) != 0) {
4185 if (prev_t->pulse() == ts->pulse()) {
4186 cerr << string_compose (_("Multiple tempo definitions found at %1"), prev_t->pulse()) << endmsg;
4187 error << string_compose (_("Multiple tempo definitions found at %1"), prev_t->pulse()) << endmsg;
4195 recompute_map (_metrics);
4197 Metrics::const_iterator d = old_metrics.begin();
4198 while (d != old_metrics.end()) {
4202 old_metrics.clear ();
4205 PropertyChanged (PropertyChange ());
4211 TempoMap::dump (const Metrics& metrics, std::ostream& o) const
4213 Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
4214 const MeterSection* m;
4215 const TempoSection* t;
4216 const TempoSection* prev_t = 0;
4218 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
4220 if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
4221 o << "Tempo @ " << *i << t->note_types_per_minute() << " BPM (pulse = 1/" << t->note_type()
4222 << " type= " << enum_2_string (t->type()) << ") " << " at pulse= " << t->pulse()
4223 << " minute= " << t->minute() << " frame= " << t->frame() << " (movable? " << t->movable() << ')'
4224 << " pos lock: " << enum_2_string (t->position_lock_style()) << std::endl;
4226 o << std::setprecision (17) << " current : " << t->note_types_per_minute()
4227 << " | " << t->pulse() << " | " << t->frame() << " | " << t->minute() << std::endl;
4228 o << " previous : " << prev_t->note_types_per_minute()
4229 << " | " << prev_t->pulse() << " | " << prev_t->frame() << " | " << prev_t->minute() << std::endl;
4230 o << " calculated : " << prev_t->tempo_at_pulse (t->pulse())
4231 << " | " << prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute())
4232 << " | " << frame_at_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()))
4233 << " | " << prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()) << std::endl;
4236 } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
4237 o << "Meter @ " << *i << ' ' << m->divisions_per_bar() << '/' << m->note_divisor() << " at " << m->bbt()
4238 << " frame= " << m->frame() << " pulse: " << m->pulse() << " beat : " << m->beat()
4239 << " pos lock: " << enum_2_string (m->position_lock_style()) << " (movable? " << m->movable() << ')' << endl;
4242 o << "------" << std::endl;
4246 TempoMap::n_tempos() const
4248 Glib::Threads::RWLock::ReaderLock lm (lock);
4251 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4252 if ((*i)->is_tempo()) {
4261 TempoMap::n_meters() const
4263 Glib::Threads::RWLock::ReaderLock lm (lock);
4266 for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4267 if (!(*i)->is_tempo()) {
4276 TempoMap::insert_time (framepos_t where, framecnt_t amount)
4278 for (Metrics::reverse_iterator i = _metrics.rbegin(); i != _metrics.rend(); ++i) {
4279 if ((*i)->frame() >= where && (*i)->movable ()) {
4283 if ((ms = dynamic_cast <MeterSection*>(*i)) != 0) {
4284 gui_move_meter (ms, (*i)->frame() + amount);
4287 if ((ts = dynamic_cast <TempoSection*>(*i)) != 0) {
4288 gui_move_tempo (ts, (*i)->frame() + amount, 0);
4293 PropertyChanged (PropertyChange ());
4297 TempoMap::remove_time (framepos_t where, framecnt_t amount)
4301 std::list<MetricSection*> metric_kill_list;
4303 TempoSection* last_tempo = NULL;
4304 MeterSection* last_meter = NULL;
4305 bool tempo_after = false; // is there a tempo marker at the first sample after the removed range?
4306 bool meter_after = false; // is there a meter marker likewise?
4308 Glib::Threads::RWLock::WriterLock lm (lock);
4309 for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
4310 if ((*i)->frame() >= where && (*i)->frame() < where+amount) {
4311 metric_kill_list.push_back(*i);
4312 TempoSection *lt = dynamic_cast<TempoSection*> (*i);
4315 MeterSection *lm = dynamic_cast<MeterSection*> (*i);
4319 else if ((*i)->frame() >= where) {
4320 // TODO: make sure that moved tempo/meter markers are rounded to beat/bar boundaries
4321 (*i)->set_minute ((*i)->minute() - minute_at_frame (amount));
4322 if ((*i)->frame() == where) {
4323 // marker was immediately after end of range
4324 tempo_after = dynamic_cast<TempoSection*> (*i);
4325 meter_after = dynamic_cast<MeterSection*> (*i);
4331 //find the last TEMPO and METER metric (if any) and move it to the cut point so future stuff is correct
4332 if (last_tempo && !tempo_after) {
4333 metric_kill_list.remove(last_tempo);
4334 last_tempo->set_minute (minute_at_frame (where));
4337 if (last_meter && !meter_after) {
4338 metric_kill_list.remove(last_meter);
4339 last_meter->set_minute (minute_at_frame (where));
4343 //remove all the remaining metrics
4344 for (std::list<MetricSection*>::iterator i = metric_kill_list.begin(); i != metric_kill_list.end(); ++i) {
4345 _metrics.remove(*i);
4350 recompute_map (_metrics);
4353 PropertyChanged (PropertyChange ());
4357 /** Add some (fractional) Beats to a session frame position, and return the result in frames.
4358 * pos can be -ve, if required.
4361 TempoMap::framepos_plus_qn (framepos_t frame, Evoral::Beats beats) const
4363 Glib::Threads::RWLock::ReaderLock lm (lock);
4364 const double frame_qn = quarter_notes_between_frames_locked (_metrics, 0, frame);
4366 return frame_at_minute (minute_at_quarter_note_locked (_metrics, frame_qn + beats.to_double()));
4370 TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
4372 Glib::Threads::RWLock::ReaderLock lm (lock);
4374 BBT_Time pos_bbt = bbt_at_beat_locked (_metrics, beat_at_minute_locked (_metrics, minute_at_frame (pos)));
4375 pos_bbt.ticks += op.ticks;
4376 if (pos_bbt.ticks >= BBT_Time::ticks_per_beat) {
4378 pos_bbt.ticks -= BBT_Time::ticks_per_beat;
4380 pos_bbt.beats += op.beats;
4381 /* the meter in effect will start on the bar */
4382 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();
4383 while (pos_bbt.beats >= divisions_per_bar + 1) {
4385 divisions_per_bar = meter_section_at_beat (beat_at_bbt_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
4386 pos_bbt.beats -= divisions_per_bar;
4388 pos_bbt.bars += op.bars;
4390 return frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
4393 /** Count the number of beats that are equivalent to distance when going forward,
4397 TempoMap::framewalk_to_qn (framepos_t pos, framecnt_t distance) const
4399 Glib::Threads::RWLock::ReaderLock lm (lock);
4401 return Evoral::Beats (quarter_notes_between_frames_locked (_metrics, pos, pos + distance));
4405 bool operator() (const BBT_Time& a, const BBT_Time& b) {
4411 operator<< (std::ostream& o, const Meter& m) {
4412 return o << m.divisions_per_bar() << '/' << m.note_divisor();
4416 operator<< (std::ostream& o, const Tempo& t) {
4417 return o << t.note_types_per_minute() << " 1/" << t.note_type() << "'s per minute";
4421 operator<< (std::ostream& o, const MetricSection& section) {
4423 o << "MetricSection @ " << section.frame() << ' ';
4425 const TempoSection* ts;
4426 const MeterSection* ms;
4428 if ((ts = dynamic_cast<const TempoSection*> (§ion)) != 0) {
4429 o << *((const Tempo*) ts);
4430 } else if ((ms = dynamic_cast<const MeterSection*> (§ion)) != 0) {
4431 o << *((const Meter*) ms);