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

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Utility.h
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#pragma once
12
13namespace tracktion { inline namespace graph
14{
15
16//==============================================================================
17//==============================================================================
19template<typename IntType>
20constexpr double sampleToTime (IntType samplePosition, double sampleRate)
21{
22 return samplePosition / sampleRate;
23}
24
26constexpr int64_t timeToSample (double timeInSeconds, double sampleRate)
27{
28 return static_cast<int64_t> ((timeInSeconds * sampleRate) + 0.5);
29}
30
32template<typename IntType>
33constexpr juce::Range<double> sampleToTime (juce::Range<IntType> sampleRange, double sampleRate)
34{
35 return { sampleToTime (sampleRange.getStart(), sampleRate),
36 sampleToTime (sampleRange.getEnd(), sampleRate) };
37}
38
40constexpr juce::Range<int64_t> timeToSample (juce::Range<double> timeInSeconds, double sampleRate)
41{
42 return { timeToSample (timeInSeconds.getStart(), sampleRate),
43 timeToSample (timeInSeconds.getEnd(), sampleRate) };
44}
45
47template<typename RangeType>
48constexpr juce::Range<int64_t> timeToSample (RangeType timeInSeconds, double sampleRate)
49{
50 return { timeToSample (timeInSeconds.getStart(), sampleRate),
51 timeToSample (timeInSeconds.getEnd(), sampleRate) };
52}
53
54//==============================================================================
55//==============================================================================
60template<typename NodeType, typename Predicate>
61NodeType* findNode (NodeGraph& nodeGraph, Predicate pred)
62{
63 auto found = std::find_if (nodeGraph.sortedNodes.begin(),
64 nodeGraph.sortedNodes.end(),
65 [&pred] (auto nodeAndID)
66 {
67 if (auto foundType = dynamic_cast<NodeType*> (nodeAndID.node))
68 if (pred (*foundType))
69 return true;
70
71 return false;
72 });
73
74 if (found != nodeGraph.sortedNodes.end())
75 return dynamic_cast<NodeType*> (found->node);
76
77 return nullptr;
78}
79
84template<typename NodeType>
85NodeType* findNodeWithID (NodeGraph& nodeGraph, size_t nodeIDToLookFor)
86{
87 auto found = std::find_if (nodeGraph.sortedNodes.begin(),
88 nodeGraph.sortedNodes.end(),
89 [nodeIDToLookFor] (auto nodeAndID)
90 {
91 return nodeAndID.id == nodeIDToLookFor
92 && dynamic_cast<NodeType*> (nodeAndID.node) != nullptr;
93 });
94
95 if (found != nodeGraph.sortedNodes.end())
96 return dynamic_cast<NodeType*> (found->node);
97
98 return nullptr;
99}
100
105template<typename NodeType>
106NodeType* findNodeWithIDIfNonZero (NodeGraph* nodeGraph, size_t nodeIDToLookFor)
107{
108 return (nodeGraph == nullptr || nodeIDToLookFor == 0)
109 ? nullptr
110 : findNodeWithID<NodeType> (*nodeGraph, nodeIDToLookFor);
111}
112
113}}
constexpr ValueType getStart() const noexcept
constexpr ValueType getEnd() const noexcept
T find_if(T... args)
NodeType * findNodeWithIDIfNonZero(NodeGraph *nodeGraph, size_t nodeIDToLookFor)
Attempts to find a Node of a given type with a specified ID.
NodeType * findNode(NodeGraph &nodeGraph, Predicate pred)
Attempts to find a Node of a given type with a specified ID.
NodeType * findNodeWithID(NodeGraph &nodeGraph, size_t nodeIDToLookFor)
Attempts to find a Node of a given type with a specified ID.
constexpr double sampleToTime(IntType samplePosition, double sampleRate)
Converts an integer sample number to a time in seconds.
typedef int64_t
Describes a range of two positions with a duration separating them.
constexpr Position getEnd() const
Returns the end of the range.
constexpr Position getStart() const
Returns the start of the range.
Holds a graph in an order ready for processing and a sorted map for quick lookups.