summaryrefslogtreecommitdiff
path: root/libs/ardour/test/samplepos_minus_beats_test.cc
blob: 1e88de6283783eb866259b26942e5b07eb7cb997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "samplepos_minus_beats_test.h"
#include "ardour/tempo.h"
#include "temporal/bbt_time.h"

CPPUNIT_TEST_SUITE_REGISTRATION (FrameposMinusBeatsTest);

using namespace std;
using namespace ARDOUR;
using namespace Timecode;
using namespace Evoral;

/* Basic tests with no tempo / meter changes */
void
FrameposMinusBeatsTest::singleTempoTest ()
{
	int const sampling_rate = 48000;
	int const bpm = 120;

	double const samples_per_beat = (60 / double (bpm)) * double (sampling_rate);

	TempoMap map (sampling_rate);
	Tempo tempo (bpm);
	Meter meter (4, 4);

	map.replace_meter (map.first_meter(), meter, BBT_Time (1, 1, 0), (samplepos_t) 0, AudioTime);
	map.replace_tempo (map.first_tempo(), tempo, 0.0, 0, TempoSection::Constant, AudioTime);

	/* Subtract 1 beat from beat 3 of the first bar */
	samplepos_t r = map.samplepos_minus_qn (samples_per_beat * 2, Beats(1));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (samples_per_beat * 1));

	/* Subtract 4 beats from 3 beats in, to go beyond zero */
	r = map.samplepos_minus_qn (samples_per_beat * 3, Beats(4));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (- samples_per_beat));
}

/* Test adding things that overlap a tempo change */
void
FrameposMinusBeatsTest::doubleTempoTest ()
{
	int const sampling_rate = 48000;

	TempoMap map (sampling_rate);
	Meter meter (4, 4);
	map.replace_meter (map.first_meter(), meter, BBT_Time (1, 1, 0), (samplepos_t) 0, AudioTime);

	/*
	  120bpm at bar 1, 240bpm at bar 4

	  120bpm = 24e3 samples per beat
	  240bpm = 12e3 samples per beat
	*/


	/*

	  120bpm                                                240bpm
	  0 beats                                               12 beats
	  0 samples                                              288e3 samples
	  0 pulses                                              4 pulses
	  |                 |                 |                 |                 |
	  | 1.1 1.2 1.3 1.4 | 2.1 2.2 2.3.2.4 | 3.1 3.2 3.3 3.4 | 4.1 4.2 4.3 4.4 |

	*/

	Tempo tempoA (120);
	map.replace_tempo (map.first_tempo(), tempoA, 0.0, 0, TempoSection::Constant, AudioTime);
	Tempo tempoB (240);
	map.add_tempo (tempoB, 12.0 / tempoA.note_type(), 0, TempoSection::Constant, MusicTime);

	/* Now some tests */

	/* Subtract 1 beat from 1|2 */
	samplepos_t r = map.samplepos_minus_qn (24e3, Beats(1));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (0));

	/* Subtract 2 beats from 4|2 (over the tempo change) */
	r = map.samplepos_minus_qn (288e3 + 12e3, Beats(2));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (288e3 - 24e3));

	/* Subtract 2.5 beats from 4|2 (over the tempo change) */
	r = map.samplepos_minus_qn (288e3 + 12e3, Beats(2.5));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (288e3 - 24e3 - 12e3));
}

/* Same as doubleTempoTest () except put a meter change at the same time as the
   tempo change (which shouldn't affect anything, since we are just dealing with
   beats)
*/

void
FrameposMinusBeatsTest::doubleTempoWithMeterTest ()
{
	int const sampling_rate = 48000;

	TempoMap map (sampling_rate);
	Meter meterA (4, 4);
	map.replace_meter (map.first_meter(), meterA, BBT_Time (1, 1, 0), (samplepos_t) 0, AudioTime);

	/*
	  120bpm at bar 1, 240bpm at bar 4

	  120bpm = 24e3 samples per beat
	  240bpm = 12e3 samples per beat
	*/


	/*

	  120bpm                                                240bpm
	  0 beats                                               12 beats
	  0 samples                                              288e3 samples
	  0 pulses                                              3 pulses
	  |                 |                 |                 |             |
	  | 1.1 1.2 1.3 1.4 | 2.1 2.2 2.3.2.4 | 3.1 3.2 3.3 3.4 | 4.1 4.2 4.3 |

	*/

	Tempo tempoA (120);
	map.replace_tempo (map.first_tempo(), tempoA, 0.0, 0, TempoSection::Constant, AudioTime);
	Tempo tempoB (240);
	map.add_tempo (tempoB, 12.0 / tempoA.note_type(), 0, TempoSection::Constant, MusicTime);
	Meter meterB (3, 4);
	map.add_meter (meterB, 12.0, BBT_Time (4, 1, 0), 0, MusicTime);

	/* Now some tests */

	/* Subtract 1 beat from 1|2 */
	samplepos_t r = map.samplepos_minus_qn (24e3, Beats(1));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (0));

	/* Subtract 2 beats from 4|2 (over the tempo change) */
	r = map.samplepos_minus_qn (288e3 + 12e3, Beats(2));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (288e3 - 24e3));

	/* Subtract 2.5 beats from 4|2 (over the tempo change) */
	r = map.samplepos_minus_qn (288e3 + 12e3, Beats(2.5));
	CPPUNIT_ASSERT_EQUAL (r, samplepos_t (288e3 - 24e3 - 12e3));
}