forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioSampleProvider.bindings.cs
More file actions
231 lines (179 loc) · 8.77 KB
/
Copy pathAudioSampleProvider.bindings.cs
File metadata and controls
231 lines (179 loc) · 8.77 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine.Bindings;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Scripting;
[assembly: InternalsVisibleTo("UnityEngine.VideoModule")]
namespace UnityEngine.Experimental.Audio
{
[NativeType(Header = "Modules/Audio/Public/ScriptBindings/AudioSampleProvider.bindings.h")]
[StaticAccessor("AudioSampleProviderBindings", StaticAccessorType.DoubleColon)]
public class AudioSampleProvider : IDisposable
{
[VisibleToOtherModules]
static internal AudioSampleProvider Lookup(
uint providerId, Object ownerObj, ushort trackIndex)
{
AudioSampleProvider provider = InternalGetScriptingPtr(providerId);
if (provider != null)
return provider;
return new AudioSampleProvider(providerId, ownerObj, trackIndex);
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate uint ConsumeSampleFramesNativeFunction(
uint providerId, IntPtr interleavedSampleFrames, uint sampleFrameCount);
private ConsumeSampleFramesNativeFunction m_ConsumeSampleFramesNativeFunction;
private AudioSampleProvider(uint providerId, Object ownerObj, ushort trackIdx)
{
owner = ownerObj;
id = providerId;
trackIndex = trackIdx;
m_ConsumeSampleFramesNativeFunction = (ConsumeSampleFramesNativeFunction)
Marshal.GetDelegateForFunctionPointer(
InternalGetConsumeSampleFramesNativeFunctionPtr(),
typeof(ConsumeSampleFramesNativeFunction));
ushort chCount = 0;
uint sRate = 0;
InternalGetFormatInfo(providerId, out chCount, out sRate);
channelCount = chCount;
sampleRate = sRate;
InternalSetScriptingPtr(providerId, this);
}
~AudioSampleProvider()
{
Dispose();
}
public void Dispose()
{
if (id != 0)
{
InternalSetScriptingPtr(id, null);
id = 0;
}
GC.SuppressFinalize(this);
}
public uint id { get; private set; }
public ushort trackIndex { get; private set; }
public Object owner { get; private set; }
public bool valid { get { return InternalIsValid(id); } }
public ushort channelCount { get; private set; }
public uint sampleRate { get; private set; }
public uint maxSampleFrameCount
{ get { return InternalGetMaxSampleFrameCount(id); } }
public uint availableSampleFrameCount
{ get { return InternalGetAvailableSampleFrameCount(id); } }
public uint freeSampleFrameCount
{ get { return InternalGetFreeSampleFrameCount(id); } }
public uint freeSampleFrameCountLowThreshold
{
get { return InternalGetFreeSampleFrameCountLowThreshold(id); }
set { InternalSetFreeSampleFrameCountLowThreshold(id, value); }
}
public bool enableSampleFramesAvailableEvents
{
get { return InternalGetEnableSampleFramesAvailableEvents(id); }
set { InternalSetEnableSampleFramesAvailableEvents(id, value); }
}
public bool enableSilencePadding
{
get { return InternalGetEnableSilencePadding(id); }
set { InternalSetEnableSilencePadding(id, value); }
}
unsafe public uint ConsumeSampleFrames(NativeArray<float> sampleFrames)
{
if (channelCount == 0)
return 0;
return m_ConsumeSampleFramesNativeFunction(
id, (IntPtr)sampleFrames.GetUnsafePtr(), (uint)sampleFrames.Length / channelCount);
}
public static ConsumeSampleFramesNativeFunction consumeSampleFramesNativeFunction
{
get
{
return (ConsumeSampleFramesNativeFunction)Marshal.GetDelegateForFunctionPointer(
InternalGetConsumeSampleFramesNativeFunctionPtr(),
typeof(ConsumeSampleFramesNativeFunction));
}
}
public delegate void SampleFramesHandler(
AudioSampleProvider provider, uint sampleFrameCount);
public event SampleFramesHandler sampleFramesAvailable;
public event SampleFramesHandler sampleFramesOverflow;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SampleFramesEventNativeFunction(
IntPtr userData, uint providerId, uint sampleFrameCount);
public void SetSampleFramesAvailableNativeHandler(
SampleFramesEventNativeFunction handler, IntPtr userData)
{
InternalSetSampleFramesAvailableNativeHandler(
id, Marshal.GetFunctionPointerForDelegate(handler), userData);
}
public void ClearSampleFramesAvailableNativeHandler()
{ InternalClearSampleFramesAvailableNativeHandler(id); }
public void SetSampleFramesOverflowNativeHandler(
SampleFramesEventNativeFunction handler, IntPtr userData)
{
InternalSetSampleFramesOverflowNativeHandler(
id, Marshal.GetFunctionPointerForDelegate(handler), userData);
}
public void ClearSampleFramesOverflowNativeHandler()
{ InternalClearSampleFramesOverflowNativeHandler(id); }
[RequiredByNativeCode]
private void InvokeSampleFramesAvailable(int sampleFrameCount)
{
if (sampleFramesAvailable != null)
// ScriptingInvication doesn't support uint, so we get int and cast.
sampleFramesAvailable(this, (uint)sampleFrameCount);
}
[RequiredByNativeCode]
private void InvokeSampleFramesOverflow(int droppedSampleFrameCount)
{
if (sampleFramesOverflow != null)
sampleFramesOverflow(this, (uint)droppedSampleFrameCount);
}
private static extern void InternalGetFormatInfo(
uint providerId, out ushort chCount, out uint sRate);
private static extern AudioSampleProvider InternalGetScriptingPtr(uint providerId);
// Can be done outside of the main thread so that garbage-collected provider can be
// destoryed in any thread. The wanted constraint (must lookup from main thread) is enforced
// by the fact that InternalGetScriptingPtr _must_ be called from the main thread.
[NativeMethod(IsThreadSafe = true)]
private static extern void InternalSetScriptingPtr(
uint providerId, AudioSampleProvider provider);
[NativeMethod(IsThreadSafe = true)]
private static extern bool InternalIsValid(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern uint InternalGetMaxSampleFrameCount(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern uint InternalGetAvailableSampleFrameCount(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern uint InternalGetFreeSampleFrameCount(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern uint InternalGetFreeSampleFrameCountLowThreshold(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern void InternalSetFreeSampleFrameCountLowThreshold(
uint providerId, uint sampleFrameCount);
[NativeMethod(IsThreadSafe = true)]
private static extern bool InternalGetEnableSampleFramesAvailableEvents(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern void InternalSetEnableSampleFramesAvailableEvents(
uint providerId, bool enable);
private static extern void InternalSetSampleFramesAvailableNativeHandler(
uint providerId, IntPtr handler, IntPtr userData);
private static extern void InternalClearSampleFramesAvailableNativeHandler(uint providerId);
private static extern void InternalSetSampleFramesOverflowNativeHandler(
uint providerId, IntPtr handler, IntPtr userData);
private static extern void InternalClearSampleFramesOverflowNativeHandler(uint providerId);
[NativeMethod(IsThreadSafe = true)]
private static extern bool InternalGetEnableSilencePadding(uint id);
[NativeMethod(IsThreadSafe = true)]
private static extern void InternalSetEnableSilencePadding(uint id, bool enabled);
[NativeMethod(IsThreadSafe = true)]
private static extern IntPtr InternalGetConsumeSampleFramesNativeFunctionPtr();
}
}