rename Timecode::BBT_Time::ticks_per_beat to Timecode::BBT_Time::ticks_per_bar_divisi...
[ardour.git] / libs / timecode / src / bbt_time.cc
1 /*
2   Copyright (C) 2002-2010 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or (at your
7   option) any later version.
8   
9   This program is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
12   License for more details.
13   
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software Foundation,
16   Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <cassert>
21
22 #include "timecode/bbt_time.h"
23
24 using namespace Timecode;
25
26 /* This number doesn't describe the smallest division of a "beat" (which is
27    only defined contextually anyway), but rather the smallest division of the the
28    divisions of a bar. If using a meter of 4/8, there are 4 divisions per bar, and
29    we can divide each one into ticks_per_bar_division pieces; in a separate meter
30    (section) of 3/8, there are 3 divisions per bar, each of which can be divided
31    into ticks_per_bar_division pieces.
32
33    The number is intended to have as many integer factors as possible so that
34    1/Nth divisions are integer numbers of ticks.
35
36    1920 is the largest legal value that be used inside an SMF file, and has many factors.
37 */
38
39 const double BBT_Time::ticks_per_bar_division = 1920.0;
40
41 BBT_Time::BBT_Time (double dbeats)
42 {
43         /* NOTE: this does not construct a BBT time in a canonical form,
44            in that beats may be a very large number, and bars will
45            always be zero.
46         */
47
48         assert (dbeats >= 0);
49
50         bars = 0;
51         beats = rint (floor (dbeats));
52         ticks = rint (floor (BBT_Time::ticks_per_bar_division * fmod (dbeats, 1.0)));
53 }