tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Utility.cpp
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#if GRAPH_UNIT_TESTS_SAMPLECONVERSION
12
13namespace tracktion { inline namespace graph
14{
15
16class SampleConversionTests : public juce::UnitTest
17{
18public:
19 SampleConversionTests()
20 : juce::UnitTest ("SampleConversion", "tracktion_graph")
21 {
22 }
23
24 void runTest() override
25 {
26 constexpr double sampleRate = 44100.0;
27 using STR = juce::String;
28
29 beginTest ("Sample to time and back");
30 {
31 for (int64_t i = 0; i < int64_t (sampleRate) * 2; ++i)
32 {
33 auto time = sampleToTime (i, sampleRate);
34 auto sample = timeToSample (time, sampleRate);
35
36 if (i != sample)
37 expect (false, juce::String ("Sample to time and back not equal s1=S1, t=TIME, s2=S2")
38 .replace ("S1", STR (i)).replace ("TIME", STR (time)).replace ("S2", STR (sample)));
39 }
40 }
41
42 beginTest ("Time to samples and back");
43 {
44 constexpr double startTime = 0.0;
45 constexpr double endTime = 2.0;
46 constexpr double minTimeDiff = sampleToTime (1, sampleRate) - sampleToTime (0, sampleRate);
47 constexpr double increment = minTimeDiff - std::numeric_limits<double>::epsilon();
48
49 for (double t = startTime; t < endTime; t += increment)
50 {
51 auto sample = timeToSample (t, sampleRate);
52 auto time = sampleToTime (sample, sampleRate);
53
54 if (! juce::isWithin (t, time, minTimeDiff))
55 expect (false, juce::String ("Time to sample and back not equal t1=TIME1, s=S1 t=TIME2")
56 .replace ("TIME1", STR (t)).replace ("S1", STR (sample)).replace ("TIME2", STR (time)));
57 }
58 }
59 }
60};
61
62static SampleConversionTests sampleConversionTests;
63
64}}
65
66#endif //GRAPH_UNIT_TESTS_SAMPLECONVERSION
T epsilon(T... args)
bool isWithin(Type a, Type b, Type tolerance) noexcept
constexpr double sampleToTime(IntType samplePosition, double sampleRate)
Converts an integer sample number to a time in seconds.
T sample(T... args)
typedef int64_t
time