13
0

move ticks per beat from Meter to Timecode::BBT_Time, add new constructor for BBT_Time based on a double value; reduce BeatFramesConverted to 1 liners pending likely removal

git-svn-id: svn://localhost/ardour2/branches/3.0@8277 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Paul Davis 2010-12-14 20:28:37 +00:00
parent ee5a37a306
commit f09524b9d3
13 changed files with 86 additions and 55 deletions

View File

@ -2247,7 +2247,7 @@ Editor::show_verbose_duration_cursor (framepos_t start, framepos_t end, double o
ebbt.ticks -= sbbt.ticks;
} else {
ebbt.beats--;
ebbt.ticks = int(Meter::ticks_per_beat) + ebbt.ticks - sbbt.ticks;
ebbt.ticks = int(Timecode::BBT_Time::ticks_per_beat) + ebbt.ticks - sbbt.ticks;
}
snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, ebbt.bars, ebbt.beats, ebbt.ticks);

View File

@ -1260,7 +1260,7 @@ Editor::compute_bbt_ruler_scale (framepos_t lower, framepos_t upper)
bbt_ruler_scale = bbt_show_ticks_detail;
}
if ((bbt_ruler_scale == bbt_show_ticks_detail) && (lower_beat.beats == upper_beat.beats) && (upper_beat.ticks - lower_beat.ticks <= Meter::ticks_per_beat / 4)) {
if ((bbt_ruler_scale == bbt_show_ticks_detail) && (lower_beat.beats == upper_beat.beats) && (upper_beat.ticks - lower_beat.ticks <= Timecode::BBT_Time::ticks_per_beat / 4)) {
bbt_ruler_scale = bbt_show_ticks_super_detail;
}
}
@ -1389,14 +1389,14 @@ Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble /*upp
frame_skip = (framepos_t) floor (frame_skip_error = (_session->frame_rate() * 60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
frame_skip_error -= frame_skip;
skip = (uint32_t) (Meter::ticks_per_beat / bbt_beat_subdivision);
skip = (uint32_t) (Timecode::BBT_Time::ticks_per_beat / bbt_beat_subdivision);
pos = (*i).frame + frame_skip;
accumulated_error = frame_skip_error;
tick = skip;
for (t = 0; (tick < Meter::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
for (t = 0; (tick < Timecode::BBT_Time::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
if (t % bbt_accent_modulo == (bbt_accent_modulo - 1)) {
i_am_accented = true;
@ -1481,14 +1481,14 @@ Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble /*upp
frame_skip = (framepos_t) floor (frame_skip_error = (_session->frame_rate() * 60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
frame_skip_error -= frame_skip;
skip = (uint32_t) (Meter::ticks_per_beat / bbt_beat_subdivision);
skip = (uint32_t) (Timecode::BBT_Time::ticks_per_beat / bbt_beat_subdivision);
pos = (*i).frame + frame_skip;
accumulated_error = frame_skip_error;
tick = skip;
for (t = 0; (tick < Meter::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
for (t = 0; (tick < Timecode::BBT_Time::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
if (t % bbt_accent_modulo == (bbt_accent_modulo - 1)) {
i_am_accented = true;
@ -1578,14 +1578,14 @@ Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble /*upp
frame_skip = (framepos_t) floor (frame_skip_error = (_session->frame_rate() * 60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
frame_skip_error -= frame_skip;
skip = (uint32_t) (Meter::ticks_per_beat / bbt_beat_subdivision);
skip = (uint32_t) (Timecode::BBT_Time::ticks_per_beat / bbt_beat_subdivision);
pos = (*i).frame + frame_skip;
accumulated_error = frame_skip_error;
tick = skip;
for (t = 0; (tick < Meter::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
for (t = 0; (tick < Timecode::BBT_Time::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
if (t % bbt_accent_modulo == (bbt_accent_modulo - 1)) {
i_am_accented = true;

View File

@ -212,7 +212,7 @@ MidiListEditor::redisplay_model ()
bbt.bars = 0;
dur = (*i)->end_time() - (*i)->time();
bbt.beats = floor (dur);
bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Meter::ticks_per_beat);
bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Timecode::BBT_Time::ticks_per_beat);
_session->tempo_map().bbt_duration_at (region->position(), bbt, 0);

View File

@ -273,8 +273,8 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo
up by 1 tick from where the last note ended
*/
at += 1.0/Meter::ticks_per_beat;
len -= 1.0/Meter::ticks_per_beat;
at += 1.0/Timecode::BBT_Time::ticks_per_beat;
len -= 1.0/Timecode::BBT_Time::ticks_per_beat;
}
step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
@ -294,7 +294,7 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo
step_edit_beat_pos += beat_duration;
step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
} else {
step_edit_beat_pos += 1.0/Meter::ticks_per_beat; // tiny, but no longer overlapping
step_edit_beat_pos += 1.0/Timecode::BBT_Time::ticks_per_beat; // tiny, but no longer overlapping
_step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
}

View File

@ -53,8 +53,6 @@ class Tempo {
class Meter {
public:
static const double ticks_per_beat;
Meter (double bpb, double bt)
: _beats_per_bar (bpb), _note_type (bt) {}

View File

@ -1339,7 +1339,7 @@ AUPlugin::get_beat_and_tempo_callback (Float64* outCurrentBeat,
float beat;
beat = metric.meter().beats_per_bar() * bbt.bars;
beat += bbt.beats;
beat += bbt.ticks / Meter::ticks_per_beat;
beat += bbt.ticks / BBT_Time::ticks_per_beat;
*outCurrentBeat = beat;
}
@ -1379,7 +1379,7 @@ AUPlugin::get_musical_time_location_callback (UInt32* outDeltaSampleOffsetToNe
/* on the beat */
*outDeltaSampleOffsetToNextBeat = 0;
} else {
*outDeltaSampleOffsetToNextBeat = (UInt32) floor (((Meter::ticks_per_beat - bbt.ticks)/Meter::ticks_per_beat) * // fraction of a beat to next beat
*outDeltaSampleOffsetToNextBeat = (UInt32) floor (((BBT_Time::ticks_per_beat - bbt.ticks)/BBT_Time::ticks_per_beat) * // fraction of a beat to next beat
metric.tempo().frames_per_beat(_session.frame_rate(), metric.meter())); // frames per beat
}
}
@ -1471,7 +1471,7 @@ AUPlugin::get_transport_state_callback (Boolean* outIsPlaying,
float beat;
beat = metric.meter().beats_per_bar() * bbt.bars;
beat += bbt.beats;
beat += bbt.ticks / Meter::ticks_per_beat;
beat += bbt.ticks / BBT_Time::ticks_per_beat;
*outCycleStartBeat = beat;
}
@ -1483,7 +1483,7 @@ AUPlugin::get_transport_state_callback (Boolean* outIsPlaying,
float beat;
beat = metric.meter().beats_per_bar() * bbt.bars;
beat += bbt.beats;
beat += bbt.ticks / Meter::ticks_per_beat;
beat += bbt.ticks / BBT_Time::ticks_per_beat;
*outCycleEndBeat = beat;
}

View File

@ -27,13 +27,7 @@ namespace ARDOUR {
framecnt_t
BeatsFramesConverter::to(double beats) const
{
Timecode::BBT_Time delta;
delta.bars = 0;
delta.beats = rint (floor (beats));
delta.ticks = rint (floor (Meter::ticks_per_beat * fmod (beats, 1.0)));
return _tempo_map.framepos_plus_bbt (_origin_b, delta);
return _tempo_map.framepos_plus_bbt (_origin_b, Timecode::BBT_Time(beats));
}
double

View File

@ -494,7 +494,7 @@ Session::jack_timebase_callback (jack_transport_state_t /*state*/,
pos->beats_per_bar = metric.meter().beats_per_bar();
pos->beat_type = metric.meter().note_divisor();
pos->ticks_per_beat = Meter::ticks_per_beat;
pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
pos->beats_per_minute = metric.tempo().beats_per_minute();
pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);

View File

@ -45,8 +45,6 @@ using Timecode::BBT_Time;
Meter TempoMap::_default_meter (4.0, 4.0);
Tempo TempoMap::_default_tempo (120.0);
const double Meter::ticks_per_beat = 1920.0;
double Tempo::frames_per_beat (framecnt_t sr, const Meter& meter) const
{
return ((60.0 * sr) / (_beats_per_minute * meter.note_divisor()/_note_type));
@ -707,7 +705,7 @@ TempoMap::timestamp_metrics (bool use_bbt)
first = false;
} else {
if (bbt.ticks > Meter::ticks_per_beat/2) {
if (bbt.ticks > BBT_Time::ticks_per_beat/2) {
/* round up to next beat */
bbt.beats += 1;
}
@ -836,15 +834,15 @@ TempoMap::bbt_time_with_metric (framepos_t frame, BBT_Time& bbt, const TempoMetr
// cerr << "---- BBT time for " << frame << " using metric @ " << metric.frame() << " BBT " << metric.start() << endl;
const double beats_per_bar = metric.meter().beats_per_bar();
const double ticks_per_frame = metric.tempo().frames_per_beat (_frame_rate, metric.meter()) / Meter::ticks_per_beat;
const double ticks_per_frame = metric.tempo().frames_per_beat (_frame_rate, metric.meter()) / BBT_Time::ticks_per_beat;
/* now compute how far beyond that point we actually are. */
frame_diff = frame - metric.frame();
bbt.ticks = metric.start().ticks + (uint32_t)round((double)frame_diff / ticks_per_frame);
uint32_t xtra_beats = bbt.ticks / (uint32_t)Meter::ticks_per_beat;
bbt.ticks %= (uint32_t)Meter::ticks_per_beat;
uint32_t xtra_beats = bbt.ticks / (uint32_t)BBT_Time::ticks_per_beat;
bbt.ticks %= (uint32_t)BBT_Time::ticks_per_beat;
bbt.beats = metric.start().beats + xtra_beats - 1; // correction for 1-based counting, see below for matching operation.
bbt.bars = metric.start().bars + (uint32_t)floor((double)bbt.beats / beats_per_bar);
@ -861,7 +859,7 @@ TempoMap::bbt_time_with_metric (framepos_t frame, BBT_Time& bbt, const TempoMetr
this will also behave badly in the case of meters like
0.1/4, but I can't be bothered to test that.
*/
uint32_t ticks_on_last_beat = (uint32_t)floor(Meter::ticks_per_beat * beat_fraction);
uint32_t ticks_on_last_beat = (uint32_t)floor(BBT_Time::ticks_per_beat * beat_fraction);
if (bbt.beats > (uint32_t)floor(beats_per_bar) && bbt.ticks >= ticks_on_last_beat) {
bbt.ticks -= ticks_on_last_beat;
@ -890,7 +888,7 @@ TempoMap::count_frames_between (const BBT_Time& start, const BBT_Time& end) cons
uint32_t bar_offset = start.bars - m.start().bars;
double beat_offset = bar_offset*m.meter().beats_per_bar() - (m.start().beats-1) + (start.beats -1)
+ start.ticks/Meter::ticks_per_beat;
+ start.ticks/BBT_Time::ticks_per_beat;
start_frame = m.frame() + (framepos_t) rint( beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
@ -900,7 +898,7 @@ TempoMap::count_frames_between (const BBT_Time& start, const BBT_Time& end) cons
bar_offset = end.bars - m.start().bars;
beat_offset = bar_offset * m.meter().beats_per_bar() - (m.start().beats -1) + (end.beats - 1)
+ end.ticks/Meter::ticks_per_beat;
+ end.ticks/BBT_Time::ticks_per_beat;
end_frame = m.frame() + (framepos_t) rint(beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
@ -1028,8 +1026,8 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
*/
uint32_t ticks_at_beat = (uint32_t) ( result.beats == ceil(beats_per_bar) ?
(1 - (ceil(beats_per_bar) - beats_per_bar))* Meter::ticks_per_beat
: Meter::ticks_per_beat );
(1 - (ceil(beats_per_bar) - beats_per_bar))* BBT_Time::ticks_per_beat
: BBT_Time::ticks_per_beat );
while (result.ticks >= ticks_at_beat) {
result.beats++;
@ -1041,8 +1039,8 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
beats_per_bar = metric.meter().beats_per_bar();
}
ticks_at_beat= (uint32_t) ( result.beats == ceil(beats_per_bar) ?
(1 - (ceil(beats_per_bar) - beats_per_bar) ) * Meter::ticks_per_beat
: Meter::ticks_per_beat);
(1 - (ceil(beats_per_bar) - beats_per_bar) ) * BBT_Time::ticks_per_beat
: BBT_Time::ticks_per_beat);
}
@ -1071,7 +1069,7 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
result.ticks = when.ticks - bbt.ticks;
} else {
uint32_t ticks_at_beat= (uint32_t) Meter::ticks_per_beat;
uint32_t ticks_at_beat= (uint32_t) BBT_Time::ticks_per_beat;
uint32_t t = bbt.ticks - when.ticks;
do {
@ -1081,10 +1079,10 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
metric = metric_at(result); // maybe there is a meter change
beats_per_bar = metric.meter().beats_per_bar();
result.beats = (uint32_t) ceil(beats_per_bar);
ticks_at_beat = (uint32_t) ((1 - (ceil(beats_per_bar) - beats_per_bar)) * Meter::ticks_per_beat) ;
ticks_at_beat = (uint32_t) ((1 - (ceil(beats_per_bar) - beats_per_bar)) * BBT_Time::ticks_per_beat) ;
} else {
result.beats --;
ticks_at_beat = (uint32_t) Meter::ticks_per_beat;
ticks_at_beat = (uint32_t) BBT_Time::ticks_per_beat;
}
if (t <= ticks_at_beat) {
@ -1139,7 +1137,7 @@ TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
bbt_time(fr, the_beat);
ticks_one_subdivisions_worth = (uint32_t)Meter::ticks_per_beat / sub_num;
ticks_one_subdivisions_worth = (uint32_t)BBT_Time::ticks_per_beat / sub_num;
ticks_one_half_subdivisions_worth = ticks_one_subdivisions_worth / 2;
if (dir > 0) {
@ -1247,7 +1245,7 @@ TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
float midbar_ticks;
midbar_beats = metric.meter().beats_per_bar() / 2 + 1;
midbar_ticks = Meter::ticks_per_beat * fmod (midbar_beats, 1.0f);
midbar_ticks = BBT_Time::ticks_per_beat * fmod (midbar_beats, 1.0f);
midbar_beats = floor (midbar_beats);
BBT_Time midbar (bbt.bars, lrintf (midbar_beats), lrintf (midbar_ticks));
@ -1300,7 +1298,7 @@ TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
/* "true" rounding */
/* round to nearest beat */
if (bbt.ticks >= (Meter::ticks_per_beat/2)) {
if (bbt.ticks >= (BBT_Time::ticks_per_beat/2)) {
try {
bbt = bbt_add (bbt, one_beat, metric);
@ -1702,9 +1700,9 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const Tempo
BBT_Time op = increment; /* argument is const, but we need to modify it */
uint32_t ticks = result.ticks + op.ticks;
if (ticks >= Meter::ticks_per_beat) {
if (ticks >= BBT_Time::ticks_per_beat) {
op.beats++;
result.ticks = ticks % (uint32_t) Meter::ticks_per_beat;
result.ticks = ticks % (uint32_t) BBT_Time::ticks_per_beat;
} else {
result.ticks += op.ticks;
}
@ -1805,7 +1803,7 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
if (op.ticks > result.ticks) {
/* subtract an extra beat later; meanwhile set ticks to the right "carry" value */
op.beats++;
result.ticks = Meter::ticks_per_beat - (op.ticks - result.ticks);
result.ticks = BBT_Time::ticks_per_beat - (op.ticks - result.ticks);
} else {
result.ticks -= op.ticks;
}
@ -2002,11 +2000,11 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
}
if (op.ticks) {
if (op.ticks >= Meter::ticks_per_beat) {
if (op.ticks >= BBT_Time::ticks_per_beat) {
pos += frames_per_beat;
pos += llrint (frames_per_beat * ((op.ticks % (uint32_t) Meter::ticks_per_beat) / (double) Meter::ticks_per_beat));
pos += llrint (frames_per_beat * ((op.ticks % (uint32_t) BBT_Time::ticks_per_beat) / (double) BBT_Time::ticks_per_beat));
} else {
pos += llrint (frames_per_beat * (op.ticks / (double) Meter::ticks_per_beat));
pos += llrint (frames_per_beat * (op.ticks / (double) BBT_Time::ticks_per_beat));
}
}
@ -2071,7 +2069,7 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
*/
if (ddist < frames_per_beat) {
beats += Meter::ticks_per_beat * (ddist/frames_per_beat);
beats += BBT_Time::ticks_per_beat * (ddist/frames_per_beat);
break;
}

View File

@ -1068,8 +1068,8 @@ MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
subdiv = 3;
}
uint32_t subdivisions = bbt_time.ticks / uint32_t (Meter::ticks_per_beat / subdiv);
uint32_t ticks = bbt_time.ticks % uint32_t (Meter::ticks_per_beat / subdiv);
uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
os << setw(2) << setfill('0') << subdivisions + 1;
os << setw(3) << setfill('0') << ticks;

View File

@ -0,0 +1,37 @@
/*
Copyright (C) 2002-2010 Paul Davis
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <cmath>
#include "timecode/bbt_time.h"
using namespace Timecode;
const double BBT_Time::ticks_per_beat = 1920.0;
BBT_Time::BBT_Time (double beats)
{
/* NOTE: this does not construct a BBT time in a canonical form,
in that beats may be a very large number, and bars will
always be zero.
*/
bars = 0;
beats = rint (floor (beats));
ticks = rint (floor (BBT_Time::ticks_per_beat * fmod (beats, 1.0)));
}

View File

@ -27,6 +27,8 @@ namespace Timecode {
/** Bar, Beat, Tick Time (i.e. Tempo-Based Time) */
struct BBT_Time {
static const double ticks_per_beat;
uint32_t bars;
uint32_t beats;
uint32_t ticks;
@ -36,6 +38,8 @@ struct BBT_Time {
BBT_Time (uint32_t ba, uint32_t be, uint32_t t)
: bars (ba), beats (be), ticks (t) {}
BBT_Time (double beats);
bool operator< (const BBT_Time& other) const {
return bars < other.bars ||

View File

@ -33,7 +33,7 @@ def configure(conf):
def build(bld):
# Library
obj = bld.new_task_gen('cxx', 'shlib')
obj.source = 'src/time.cc'
obj.source = [ 'src/time.cc', 'src/bbt_time.cc' ]
obj.export_incdirs = ['.']
obj.includes = ['.', './src']
obj.name = 'libtimecode'