This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 641
/
SourceVoice.cs
403 lines (361 loc) · 41 KB
/
SourceVoice.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
using SharpDX.Multimedia;
namespace SharpDX.XAudio2
{
public partial class SourceVoice
{
private VoiceCallbackImpl voiceCallbackImpl;
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat)
: this(device, sourceFormat, true)
{
}
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <param name="enableCallbackEvents">True to enable delegate callbacks on this instance. Default is false</param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, bool enableCallbackEvents)
: this(device, sourceFormat, VoiceFlags.None, 1.0f, enableCallbackEvents)
{
}
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.? </param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags)
: this(device, sourceFormat, flags, true)
{
}
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.? </param>
/// <param name="enableCallbackEvents">True to enable delegate callbacks on this instance. Default is false</param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, bool enableCallbackEvents)
: this(device, sourceFormat, flags, 1.0f, enableCallbackEvents)
{
}
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.? </param>
/// <param name="maxFrequencyRatio">[in] Highest allowable frequency ratio that can be set on this voice. The value for this argument must be between XAUDIO2_MIN_FREQ_RATIO and XAUDIO2_MAX_FREQ_RATIO. Subsequent calls to <see cref="SharpDX.XAudio2.SourceVoice.SetFrequencyRatio"/> are clamped between XAUDIO2_MIN_FREQ_RATIO and MaxFrequencyRatio. The maximum value for this argument is defined as XAUDIO2_MAX_FREQ_RATIO, which allows pitch to be raised by up to 10 octaves. If MaxFrequencyRatio is less than 1.0, the voice will use that ratio immediately after being created (rather than the default of 1.0). Xbox 360 For XMA voices there is an additional restriction on the MaxFrequencyRatio argument and the voice's sample rate. The product of these two numbers cannot exceed XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MONO for one-channel voices or XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MULTICHANNEL for voices with any other number of channels. If the value specified for MaxFrequencyRatio is too high for the specified format, the call to CreateSourceVoice fails and produces a debug message. Note XAudio2's memory usage can be reduced by using the lowest possible MaxFrequencyRatio value. </param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, float maxFrequencyRatio)
: this(device, sourceFormat, flags, maxFrequencyRatio, true)
{
}
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.? </param>
/// <param name="maxFrequencyRatio">[in] Highest allowable frequency ratio that can be set on this voice. The value for this argument must be between XAUDIO2_MIN_FREQ_RATIO and XAUDIO2_MAX_FREQ_RATIO. Subsequent calls to <see cref="SharpDX.XAudio2.SourceVoice.SetFrequencyRatio"/> are clamped between XAUDIO2_MIN_FREQ_RATIO and MaxFrequencyRatio. The maximum value for this argument is defined as XAUDIO2_MAX_FREQ_RATIO, which allows pitch to be raised by up to 10 octaves. If MaxFrequencyRatio is less than 1.0, the voice will use that ratio immediately after being created (rather than the default of 1.0). Xbox 360 For XMA voices there is an additional restriction on the MaxFrequencyRatio argument and the voice's sample rate. The product of these two numbers cannot exceed XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MONO for one-channel voices or XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MULTICHANNEL for voices with any other number of channels. If the value specified for MaxFrequencyRatio is too high for the specified format, the call to CreateSourceVoice fails and produces a debug message. Note XAudio2's memory usage can be reduced by using the lowest possible MaxFrequencyRatio value. </param>
/// <param name="callback">[in, optional] Pointer to a client-provided callback interface, <see cref="SharpDX.XAudio2.VoiceCallback"/>. </param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, float maxFrequencyRatio, VoiceCallback callback)
: this(device, sourceFormat, flags, maxFrequencyRatio, callback, null)
{
}
/// <summary>
/// Creates and configures a source voice with callback through delegates.
/// </summary>
/// <param name="device">an instance of <see cref="SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat" /> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat" /> have a <see cref="SharpDX.Multimedia.WaveFormat" /> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat" /> structure and use it as the value for pSourceFormat.</param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.?</param>
/// <param name="maxFrequencyRatio">[in] Highest allowable frequency ratio that can be set on this voice. The value for this argument must be between XAUDIO2_MIN_FREQ_RATIO and XAUDIO2_MAX_FREQ_RATIO. Subsequent calls to <see cref="SharpDX.XAudio2.SourceVoice.SetFrequencyRatio" /> are clamped between XAUDIO2_MIN_FREQ_RATIO and MaxFrequencyRatio. The maximum value for this argument is defined as XAUDIO2_MAX_FREQ_RATIO, which allows pitch to be raised by up to 10 octaves. If MaxFrequencyRatio is less than 1.0, the voice will use that ratio immediately after being created (rather than the default of 1.0). Xbox 360 For XMA voices there is an additional restriction on the MaxFrequencyRatio argument and the voice's sample rate. The product of these two numbers cannot exceed XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MONO for one-channel voices or XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MULTICHANNEL for voices with any other number of channels. If the value specified for MaxFrequencyRatio is too high for the specified format, the call to CreateSourceVoice fails and produces a debug message. Note XAudio2's memory usage can be reduced by using the lowest possible MaxFrequencyRatio value.</param>
/// <param name="enableCallbackEvents">if set to <c>true</c> [enable callback events].</param>
/// <returns>No enableCallbackEvents.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, float maxFrequencyRatio, bool enableCallbackEvents)
: this(device, sourceFormat, flags, maxFrequencyRatio, enableCallbackEvents, null)
{
}
/// <summary>
/// Creates and configures a source voice.
/// </summary>
/// <param name="device">an instance of <see cref = "SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat"/> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat"/> have a <see cref="SharpDX.Multimedia.WaveFormat"/> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat"/> structure and use it as the value for pSourceFormat. </param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.? </param>
/// <param name="maxFrequencyRatio">[in] Highest allowable frequency ratio that can be set on this voice. The value for this argument must be between XAUDIO2_MIN_FREQ_RATIO and XAUDIO2_MAX_FREQ_RATIO. Subsequent calls to <see cref="SharpDX.XAudio2.SourceVoice.SetFrequencyRatio"/> are clamped between XAUDIO2_MIN_FREQ_RATIO and MaxFrequencyRatio. The maximum value for this argument is defined as XAUDIO2_MAX_FREQ_RATIO, which allows pitch to be raised by up to 10 octaves. If MaxFrequencyRatio is less than 1.0, the voice will use that ratio immediately after being created (rather than the default of 1.0). Xbox 360 For XMA voices there is an additional restriction on the MaxFrequencyRatio argument and the voice's sample rate. The product of these two numbers cannot exceed XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MONO for one-channel voices or XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MULTICHANNEL for voices with any other number of channels. If the value specified for MaxFrequencyRatio is too high for the specified format, the call to CreateSourceVoice fails and produces a debug message. Note XAudio2's memory usage can be reduced by using the lowest possible MaxFrequencyRatio value. </param>
/// <param name="callback">[in, optional] Pointer to a client-provided callback interface, <see cref="SharpDX.XAudio2.VoiceCallback"/>. </param>
/// <param name="effectDescriptors">[in, optional] Pointer to a list of XAUDIO2_EFFECT_CHAIN structures that describe an effect chain to use in the source voice.</param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, float maxFrequencyRatio, VoiceCallback callback, EffectDescriptor[] effectDescriptors)
: base(device)
{
CreateSourceVoice(device, sourceFormat, flags, maxFrequencyRatio, callback, effectDescriptors);
}
/// <summary>
/// Creates and configures a source voice with callback through delegates.
/// </summary>
/// <param name="device">an instance of <see cref="SharpDX.XAudio2.XAudio2" /></param>
/// <param name="sourceFormat">[in] Pointer to a <see cref="SharpDX.Multimedia.WaveFormat" /> structure. This structure contains the expected format for all audio buffers submitted to the source voice. XAudio2 supports voice types of PCM, xWMA, ADPCM (Windows only), and XMA (Xbox 360 only). XAudio2 supports the following PCM formats. 8-bit (unsigned) integer PCM 16-bit integer PCM (Optimal format for XAudio2) 20-bit integer PCM (either in 24 or 32 bit containers) 24-bit integer PCM (either in 24 or 32 bit containers) 32-bit integer PCM 32-bit float PCM (Preferred format after 16-bit integer) The number of channels in a source voice must be less than or equal to XAUDIO2_MAX_AUDIO_CHANNELS. The sample rate of a source voice must be between XAUDIO2_MIN_SAMPLE_RATE and XAUDIO2_MAX_SAMPLE_RATE. Note Data formats such as XMA, {{ADPCM}}, and {{xWMA}} that require more information than provided by <see cref="SharpDX.Multimedia.WaveFormat" /> have a <see cref="SharpDX.Multimedia.WaveFormat" /> structure as the first member in their format structure. When creating a source voice with one of those formats cast the format's structure as a <see cref="SharpDX.Multimedia.WaveFormat" /> structure and use it as the value for pSourceFormat.</param>
/// <param name="flags">[in] Flags that specify the behavior of the source voice. A flag can be 0 or a combination of one or more of the following: ValueDescriptionXAUDIO2_VOICE_NOPITCHNo pitch control is available on the voice.?XAUDIO2_VOICE_NOSRCNo sample rate conversion is available on the voice, the voice's outputs must have the same sample rate.Note The XAUDIO2_VOICE_NOSRC flag causes the voice to behave as though the XAUDIO2_VOICE_NOPITCH flag also is specified. ?XAUDIO2_VOICE_USEFILTERThe filter effect should be available on this voice.?XAUDIO2_VOICE_MUSICThe voice is used to play background music. The system automatically can replace the voice with music selected by the user.?</param>
/// <param name="maxFrequencyRatio">[in] Highest allowable frequency ratio that can be set on this voice. The value for this argument must be between XAUDIO2_MIN_FREQ_RATIO and XAUDIO2_MAX_FREQ_RATIO. Subsequent calls to <see cref="SharpDX.XAudio2.SourceVoice.SetFrequencyRatio" /> are clamped between XAUDIO2_MIN_FREQ_RATIO and MaxFrequencyRatio. The maximum value for this argument is defined as XAUDIO2_MAX_FREQ_RATIO, which allows pitch to be raised by up to 10 octaves. If MaxFrequencyRatio is less than 1.0, the voice will use that ratio immediately after being created (rather than the default of 1.0). Xbox 360 For XMA voices there is an additional restriction on the MaxFrequencyRatio argument and the voice's sample rate. The product of these two numbers cannot exceed XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MONO for one-channel voices or XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MULTICHANNEL for voices with any other number of channels. If the value specified for MaxFrequencyRatio is too high for the specified format, the call to CreateSourceVoice fails and produces a debug message. Note XAudio2's memory usage can be reduced by using the lowest possible MaxFrequencyRatio value.</param>
/// <param name="enableCallbackEvents">if set to <c>true</c> [enable callback events].</param>
/// <param name="effectDescriptors">[in, optional] Pointer to a list of XAUDIO2_EFFECT_CHAIN structures that describe an effect chain to use in the source voice.</param>
/// <returns>No enableCallbackEvents.</returns>
/// <unmanaged>HRESULT IXAudio2::CreateSourceVoice([Out] IXAudio2SourceVoice** ppSourceVoice,[In] const WAVEFORMATEX* pSourceFormat,[None] UINT32 Flags,[None] float MaxFrequencyRatio,[In, Optional] IXAudio2VoiceCallback* pCallback,[In, Optional] const XAUDIO2_VOICE_SENDS* pSendList,[In, Optional] const XAUDIO2_EFFECT_CHAIN* pEffectChain)</unmanaged>
public SourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, float maxFrequencyRatio, bool enableCallbackEvents, EffectDescriptor[] effectDescriptors)
: base(device)
{
CreateSourceVoice(device, sourceFormat, flags, maxFrequencyRatio, enableCallbackEvents ? (voiceCallbackImpl = new VoiceCallbackImpl(this)) : null, effectDescriptors);
}
private void CreateSourceVoice(XAudio2 device, SharpDX.Multimedia.WaveFormat sourceFormat, SharpDX.XAudio2.VoiceFlags flags, float maxFrequencyRatio, VoiceCallback callback, EffectDescriptor[] effectDescriptors)
{
var waveformatPtr = WaveFormat.MarshalToPtr(sourceFormat);
try
{
if (effectDescriptors != null)
{
unsafe
{
var tempSendDescriptor = new EffectChain();
var effectDescriptorNatives = new EffectDescriptor.__Native[effectDescriptors.Length];
for (int i = 0; i < effectDescriptorNatives.Length; i++)
effectDescriptors[i].__MarshalTo(ref effectDescriptorNatives[i]);
tempSendDescriptor.EffectCount = effectDescriptorNatives.Length;
fixed (void* pEffectDescriptors = &effectDescriptorNatives[0])
{
tempSendDescriptor.EffectDescriptorPointer = (IntPtr)pEffectDescriptors;
device.CreateSourceVoice(this, waveformatPtr, flags, maxFrequencyRatio, callback, null, tempSendDescriptor);
}
}
}
else
{
device.CreateSourceVoice(this, waveformatPtr, flags, maxFrequencyRatio, callback, null, null);
}
}
finally
{
Marshal.FreeHGlobal(waveformatPtr);
}
}
/// <summary>
/// Starts consumption and processing of audio by the voice. Delivers the result to any connected submix or mastering voices, or to the output device, with CommitNow changes.
/// </summary>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2SourceVoice::Start([None] UINT32 Flags,[None] UINT32 OperationSet)</unmanaged>
public void Start()
{
this.Start(0, 0);
}
#if DIRECTX11_1
/// <summary>
/// Gets the state.
/// </summary>
public VoiceState State
{
get
{
return GetState(0);
}
}
#endif
/// <summary>
/// Sets the frequency ratio.
/// </summary>
/// <param name="ratio">The ratio.</param>
/// <returns></returns>
public void SetFrequencyRatio(float ratio)
{
SetFrequencyRatio(ratio, 0);
}
/// <summary>
/// Starts consumption and processing of audio by the voice. Delivers the result to any connected submix or mastering voices, or to the output device.
/// </summary>
/// <param name="operationSet">[in] Identifies this call as part of a deferred batch. See the {{XAudio2 Operation Sets}} overview for more information. </param>
/// <returns>No documentation.</returns>
/// <unmanaged>HRESULT IXAudio2SourceVoice::Start([None] UINT32 Flags,[None] UINT32 OperationSet)</unmanaged>
public void Start(int operationSet)
{
this.Start(0, operationSet);
}
public void Stop()
{
this.Stop(PlayFlags.None, 0);
}
public void Stop(int operationSet)
{
this.Stop(PlayFlags.None, operationSet);
}
/// <summary>
/// No documentation.
/// </summary>
/// <param name="bufferRef">No documentation.</param>
/// <param name="decodedXMWAPacketInfo">No documentation.</param>
/// <returns>No documentation.</returns>
/// <include file='Documentation\CodeComments.xml' path="/comments/comment[@id='IXAudio2SourceVoice::SubmitSourceBuffer']/*"/>
/// <unmanaged>HRESULT IXAudio2SourceVoice::SubmitSourceBuffer([In] const XAUDIO2_BUFFER* pBuffer,[In, Optional] const XAUDIO2_BUFFER_WMA* pBufferWMA)</unmanaged>
public void SubmitSourceBuffer(SharpDX.XAudio2.AudioBuffer bufferRef, uint[] decodedXMWAPacketInfo)
{
unsafe
{
if (decodedXMWAPacketInfo != null)
{
fixed (void* pBuffer = decodedXMWAPacketInfo)
{
SharpDX.XAudio2.BufferWma bufferWmaRef;
bufferWmaRef.PacketCount = decodedXMWAPacketInfo.Length;
bufferWmaRef.DecodedPacketCumulativeBytesPointer = (IntPtr)pBuffer;
SubmitSourceBuffer(bufferRef, bufferWmaRef);
}
}
else
{
SubmitSourceBuffer(bufferRef, (SharpDX.XAudio2.BufferWma?)null);
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Utilities.Dispose(ref voiceCallbackImpl);
}
base.Dispose(disposing);
}
/// <summary>
/// Occurs just before the processing pass for the voice begins.
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event Action<int> ProcessingPassStart;
public delegate void VoidAction();
/// <summary>
/// Occurs just after the processing pass for the voice ends.
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event VoidAction ProcessingPassEnd;
/// <summary>
/// Occurs when the voice has just finished playing a contiguous audio stream.
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event VoidAction StreamEnd;
/// <summary>
/// Occurs when the voice is about to start processing a new audio buffer.
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event Action<IntPtr> BufferStart;
/// <summary>
/// Occurs when the voice finishes processing a buffer.
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event Action<IntPtr> BufferEnd;
/// <summary>
/// Occurs when a critical error occurs during voice processing.
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event Action<IntPtr> LoopEnd;
public struct VoiceErrorArgs
{
public VoiceErrorArgs(IntPtr pointer, Result result)
{
this.Pointer = pointer;
this.Result = result;
}
public IntPtr Pointer;
public Result Result;
}
/// <summary>
/// Occurs when [voice error].
/// </summary>
/// <remarks>
/// In order to use this delegate, this instance must have been initialized with events delegate support.
/// </remarks>
public event Action<VoiceErrorArgs> VoiceError;
private class VoiceCallbackImpl : CallbackBase, VoiceCallback
{
private SourceVoice Voice { get; set; }
public VoiceCallbackImpl(SourceVoice voice)
{
Voice = voice;
}
void VoiceCallback.OnVoiceProcessingPassStart(int bytesRequired)
{
if (Voice.ProcessingPassStart != null) Voice.ProcessingPassStart(bytesRequired);
}
void VoiceCallback.OnVoiceProcessingPassEnd()
{
if (Voice.ProcessingPassEnd != null) Voice.ProcessingPassEnd();
}
void VoiceCallback.OnStreamEnd()
{
if (Voice.StreamEnd != null) Voice.StreamEnd();
}
void VoiceCallback.OnBufferStart(IntPtr context)
{
if (Voice.BufferStart != null) Voice.BufferStart(context);
}
void VoiceCallback.OnBufferEnd(IntPtr context)
{
if (Voice.BufferEnd != null) Voice.BufferEnd(context);
}
void VoiceCallback.OnLoopEnd(IntPtr context)
{
if (Voice.LoopEnd != null) Voice.LoopEnd(context);
}
void VoiceCallback.OnVoiceError(IntPtr context, Result error)
{
if (Voice.VoiceError != null) Voice.VoiceError(new VoiceErrorArgs(context, error));
}
}
}
}