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

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_MultiThreadedNodePlayer.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
28{
29public:
30 //==============================================================================
33
36
37 //==============================================================================
42 void setNumThreads (size_t);
43
46
48 void setNode (std::unique_ptr<Node> newNode, double sampleRateToUse, int blockSizeToUse);
49
51 void prepareToPlay (double sampleRateToUse, int blockSizeToUse);
52
55 {
56 if (auto cpn = currentPreparedNode.load (std::memory_order_acquire);
57 cpn != nullptr && cpn->graph != nullptr)
58 return cpn->graph->rootNode.get();
59
60 return nullptr;
61 }
62
64 int process (const Node::ProcessContext&);
65
72 void clearNode();
73
75 double getSampleRate() const
76 {
77 return sampleRate.load (std::memory_order_acquire);
78 }
79
80private:
81 //==============================================================================
82 std::atomic<size_t> numThreadsToUse { std::max ((size_t) 0, (size_t) std::thread::hardware_concurrency() - 1) };
83 choc::buffer::FrameCount numSamplesToProcess;
84 juce::Range<int64_t> referenceSampleRange;
85 std::atomic<bool> threadsShouldExit { false };
86
87 class ThreadPool;
89
90 struct PlaybackNode
91 {
92 PlaybackNode (Node& n)
93 : node (n), numInputs (node.getDirectInputNodes().size())
94 {}
95
96 Node& node;
97 const size_t numInputs;
98 std::vector<Node*> outputs;
99 std::atomic<size_t> numInputsToBeProcessed { 0 };
100 std::atomic<bool> hasBeenQueued { true };
101 #if JUCE_DEBUG
102 std::atomic<bool> hasBeenDequeued { false };
103 #endif
104 };
105
106 struct PreparedNode
107 {
110 choc::fifo::MultipleReaderMultipleWriterFIFO<Node*> nodesReadyToBeProcessed;
111 };
112
113 RealTimeSpinLock preparedNodeMutex;
115 std::atomic<PreparedNode*> currentPreparedNode { nullptr };
116
117 std::atomic<size_t> numNodesQueued { 0 };
118 RealTimeSpinLock clearNodesLock;
119
120 //==============================================================================
121 std::atomic<double> sampleRate { 0.0 };
122 int blockSize = 0;
123
124 //==============================================================================
126 std::unique_ptr<NodeGraph> prepareToPlay (std::unique_ptr<Node>, NodeGraph* oldGraph, double sampleRateToUse, int blockSizeToUse);
127
128 //==============================================================================
129 void clearThreads();
130 void createThreads();
131 static void pause();
132
133 //==============================================================================
134 void setNewGraph (std::unique_ptr<NodeGraph>);
135
136 //==============================================================================
137 static void buildNodesOutputLists (std::vector<Node*>&, std::vector<std::unique_ptr<PlaybackNode>>&);
138 void resetProcessQueue();
139 Node* updateProcessQueueForNode (Node&);
140 void processNode (Node&);
141
142 //==============================================================================
143 bool processNextFreeNode();
144};
145
146}}
double getSampleRate() const
Returns the current sample rate.
int process(const Node::ProcessContext &)
Process a block of the Node.
void setNode(std::unique_ptr< Node >)
Sets the Node to process.
MultiThreadedNodePlayer()
Creates an empty MultiThreadedNodePlayer.
void prepareToPlay(double sampleRateToUse, int blockSizeToUse)
Prepares the current Node to be played.
void setNumThreads(size_t)
Sets the number of threads to use for rendering.
Main graph Node processor class.
Struct to describe a single iteration of a process call.
T hardware_concurrency(T... args)
T is_pointer_v
T load(T... args)
T max(T... args)