forked from juce-framework/JUCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AUInputElement.h
91 lines (77 loc) · 2.85 KB
/
AUInputElement.h
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
/*!
@file AudioUnitSDK/AUInputElement.h
@copyright © 2000-2021 Apple Inc. All rights reserved.
*/
#ifndef AudioUnitSDK_AUInputElement_h
#define AudioUnitSDK_AUInputElement_h
#include <AudioUnitSDK/AUBuffer.h>
#include <AudioUnitSDK/AUScopeElement.h>
namespace ausdk {
/*!
@class AUInputElement
@brief Implements an audio unit input element, managing the source of input from a callback
or connection.
*/
class AUInputElement : public AUIOElement {
public:
using AUIOElement::AUIOElement;
// AUElement override
OSStatus SetStreamFormat(const AudioStreamBasicDescription& fmt) override;
[[nodiscard]] bool NeedsBufferSpace() const override { return IsCallback(); }
void SetConnection(const AudioUnitConnection& conn);
void SetInputCallback(AURenderCallback proc, void* refCon);
[[nodiscard]] bool IsActive() const noexcept { return mInputType != EInputType::NoInput; }
[[nodiscard]] bool IsCallback() const noexcept
{
return mInputType == EInputType::FromCallback;
}
[[nodiscard]] bool HasConnection() const noexcept
{
return mInputType == EInputType::FromConnection;
}
OSStatus PullInput(AudioUnitRenderActionFlags& ioActionFlags, const AudioTimeStamp& inTimeStamp,
AudioUnitElement inElement, UInt32 nFrames);
OSStatus PullInputWithBufferList(AudioUnitRenderActionFlags& ioActionFlags,
const AudioTimeStamp& inTimeStamp, AudioUnitElement inElement, UInt32 nFrames,
AudioBufferList& inBufferList);
protected:
void Disconnect();
private:
enum class EInputType { NoInput, FromConnection, FromCallback };
EInputType mInputType{ EInputType::NoInput };
// if from callback:
AURenderCallback mInputProc{ nullptr };
void* mInputProcRefCon{ nullptr };
// if from connection:
AudioUnitConnection mConnection{};
};
inline OSStatus AUInputElement::PullInputWithBufferList(AudioUnitRenderActionFlags& ioActionFlags,
const AudioTimeStamp& inTimeStamp, AudioUnitElement inElement, UInt32 nFrames,
AudioBufferList& inBufferList)
{
OSStatus theResult = noErr;
if (HasConnection()) {
// only support connections for V2 audio units
theResult = AudioUnitRender(mConnection.sourceAudioUnit, &ioActionFlags, &inTimeStamp,
mConnection.sourceOutputNumber, nFrames, &inBufferList);
} else {
// kFromCallback:
theResult = (mInputProc)(mInputProcRefCon, &ioActionFlags, &inTimeStamp, inElement, nFrames,
&inBufferList);
}
if (mInputType == EInputType::NoInput) { // defense: the guy upstream could have disconnected
// it's a horrible thing to do, but may happen!
return kAudioUnitErr_NoConnection;
}
#if !TARGET_OS_IPHONE || DEBUG
if (theResult == noErr) { // if there's already an error, there's no point (and maybe some harm)
// in validating.
if (ABL::IsBogusAudioBufferList(inBufferList) & 1) {
return kAudioUnitErr_InvalidPropertyValue;
}
}
#endif
return theResult;
}
} // namespace ausdk
#endif // AudioUnitSDK_AUInputElement_h