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

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_WaveInputDeviceNode.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
12namespace tracktion { inline namespace engine
13{
14
15WaveInputDeviceNode::WaveInputDeviceNode (InputDeviceInstance& idi, WaveInputDevice& owner,
16 const juce::AudioChannelSet& destChannelsToFill)
17 : instance (idi),
18 waveInputDevice (owner),
19 destChannels (destChannelsToFill)
20{
21}
22
23WaveInputDeviceNode::~WaveInputDeviceNode()
24{
25 instance.removeConsumer (this);
26}
27
28tracktion::graph::NodeProperties WaveInputDeviceNode::getNodeProperties()
29{
31 props.hasAudio = true;
32 props.numberOfChannels = destChannels.size();
33
34 return props;
35}
36
37void WaveInputDeviceNode::prepareToPlay (const tracktion::graph::PlaybackInitialisationInfo& info)
38{
39 auto numIncommingChannels = (waveInputDevice.isStereoPair()) ? 2u : 1u;
40 audioFifo.setSize (numIncommingChannels, (uint32_t) info.blockSize * 8);
41 lastCallbackTime = juce::Time::getMillisecondCounter();
42
43 instance.addConsumer (this);
44}
45
46bool WaveInputDeviceNode::isReadyToProcess()
47{
48 return true;
49}
50
51void WaveInputDeviceNode::process (ProcessContext& pc)
52{
53 SCOPED_REALTIME_CHECK
54
55 const auto timeNow = juce::Time::getMillisecondCounter();
56
57 if (timeNow > lastCallbackTime + 150)
58 {
59 // If there's been a break in transmission, reset the fifo to keep in in sync
60 audioFifo.reset();
61 }
62
63 lastCallbackTime = timeNow;
64
65 auto& destAudio = pc.buffers.audio;
66 auto numSamples = destAudio.getNumFrames();
67 const auto numChannelsToRead = std::min (destAudio.getNumChannels(),
68 audioFifo.getNumChannels());
69 jassert (destAudio.getNumChannels() >= audioFifo.getNumChannels());
70
71 if (auto numToRead = std::min ((choc::buffer::FrameCount) audioFifo.getNumReady(), numSamples))
72 {
73 auto destSubView = destAudio.getFirstChannels (numChannelsToRead)
74 .getStart (numToRead);
75 audioFifo.readAdding (destSubView);
76
77 // Copy any additional channels from the last one
78 auto lastChannelView = destSubView.getChannel (destSubView.getNumChannels() - 1);
79
80 for (auto c = numChannelsToRead; c < destAudio.getNumChannels(); ++c)
81 copy (destAudio.getChannel (c).getStart (numToRead), lastChannelView);
82 }
83}
84
85void WaveInputDeviceNode::acceptInputBuffer (choc::buffer::ChannelArrayView<float> newBlock)
86{
87 jassert (audioFifo.getNumChannels() == newBlock.getNumChannels());
88 audioFifo.reset();
89 audioFifo.write (newBlock);
90}
91
92}} // namespace tracktion { inline namespace engine
static uint32 getMillisecondCounter() noexcept
Struct to describe a single iteration of a process call.
#define jassert(expression)
T min(T... args)
typedef uint32_t
Holds some really basic properties of a node.
Passed into Nodes when they are being initialised, to give them useful contextual information that th...