1 /*
2  * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "temporal/beats.h"
20 #include "evoral/TimeConverter.h"
21 
22 #include "ardour/libardour_visibility.h"
23 #include "ardour/types.h"
24 
25 #ifndef __ardour_beats_samples_converter_h__
26 #define __ardour_beats_samples_converter_h__
27 
28 namespace ARDOUR {
29 
30 class TempoMap;
31 
32 /** Converter between quarter-note beats and samples.  Takes distances in quarter-note beats or samples
33  *  from some origin (supplied to the constructor in samples), and converts
34  *  them to the opposite unit, taking tempo changes into account.
35  */
36 class LIBARDOUR_API BeatsSamplesConverter
37 	: public Evoral::TimeConverter<Temporal::Beats,samplepos_t> {
38 public:
BeatsSamplesConverter(const TempoMap & tempo_map,samplepos_t origin)39 	BeatsSamplesConverter (const TempoMap& tempo_map, samplepos_t origin)
40 		: Evoral::TimeConverter<Temporal::Beats, samplepos_t> (origin)
41 		, _tempo_map(tempo_map)
42 	{}
43 
44 	samplepos_t    to (Temporal::Beats beats) const;
45 	Temporal::Beats from (samplepos_t samples) const;
46 
47 private:
48 	const TempoMap& _tempo_map;
49 };
50 
51 /** Converter between quarter-note beats and samples.  Takes distances in quarter-note beats or samples
52  *  from some origin (supplied to the constructor in samples), and converts
53  *  them to the opposite unit, taking tempo changes into account.
54  */
55 class LIBARDOUR_API DoubleBeatsSamplesConverter
56 	: public Evoral::TimeConverter<double,samplepos_t> {
57 public:
DoubleBeatsSamplesConverter(const TempoMap & tempo_map,samplepos_t origin)58 	DoubleBeatsSamplesConverter (const TempoMap& tempo_map, samplepos_t origin)
59 		: Evoral::TimeConverter<double, samplepos_t> (origin)
60 		, _tempo_map(tempo_map)
61 	{}
62 
63 	samplepos_t          to (double beats) const;
64 	double from (samplepos_t samples) const;
65 
66 private:
67 	const TempoMap& _tempo_map;
68 };
69 
70 } /* namespace ARDOUR */
71 
72 #endif /* __ardour_beats_samples_converter_h__ */
73