Skip to content

Commit 4f496e8

Browse files
committed
XAudio + Multimedia: Add SoundStream and RiffParser from SharpDX and improve XAudio CreateSourceVoice to allow events in more C# friendly way as well.
1 parent 92f760c commit 4f496e8

File tree

9 files changed

+1039
-20
lines changed

9 files changed

+1039
-20
lines changed

src/Vortice.DirectX/FourCC.cs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// Copyright (c) Amer Koleci and contributors.
2+
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
3+
4+
using System;
5+
using System.Globalization;
6+
using System.Runtime.InteropServices;
7+
8+
namespace Vortice
9+
{
10+
/// <summary>
11+
/// A FourCC descriptor.
12+
/// </summary>
13+
[StructLayout(LayoutKind.Sequential, Size = 4)]
14+
public readonly struct FourCC : IEquatable<FourCC>, IFormattable
15+
{
16+
/// <summary>
17+
/// Empty FourCC.
18+
/// </summary>
19+
public static readonly FourCC Empty = new(0);
20+
21+
private readonly uint _value;
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="FourCC" /> struct.
25+
/// </summary>
26+
/// <param name="fourCC">The fourCC value as a string .</param>
27+
public FourCC(string fourCC)
28+
{
29+
if (fourCC.Length != 4)
30+
{
31+
throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid length for FourCC(\"{0}\". Must be be 4 characters long ", fourCC), "fourCC");
32+
}
33+
34+
_value = ((uint)fourCC[3]) << 24 | ((uint)fourCC[2]) << 16 | ((uint)fourCC[1]) << 8 | ((uint)fourCC[0]);
35+
}
36+
37+
/// <summary>
38+
/// Initializes a new instance of the <see cref="FourCC" /> struct.
39+
/// </summary>
40+
/// <param name="byte1">The byte1.</param>
41+
/// <param name="byte2">The byte2.</param>
42+
/// <param name="byte3">The byte3.</param>
43+
/// <param name="byte4">The byte4.</param>
44+
public FourCC(char byte1, char byte2, char byte3, char byte4)
45+
{
46+
_value = ((uint)byte4) << 24 | ((uint)byte3) << 16 | ((uint)byte2) << 8 | ((uint)byte1);
47+
}
48+
49+
/// <summary>
50+
/// Initializes a new instance of the <see cref="FourCC" /> struct.
51+
/// </summary>
52+
/// <param name="fourCC">The fourCC value as an uint.</param>
53+
public FourCC(uint fourCC)
54+
{
55+
_value = fourCC;
56+
}
57+
58+
/// <summary>
59+
/// Initializes a new instance of the <see cref="FourCC" /> struct.
60+
/// </summary>
61+
/// <param name="fourCC">The fourCC value as an int.</param>
62+
public FourCC(int fourCC)
63+
{
64+
_value = unchecked((uint)fourCC);
65+
}
66+
67+
/// <summary>
68+
/// Performs an implicit conversion from <see cref="FourCC"/> to <see cref="int"/>.
69+
/// </summary>
70+
/// <param name="d">The d.</param>
71+
/// <returns>
72+
/// The result of the conversion.
73+
/// </returns>
74+
public static implicit operator uint(FourCC d)
75+
{
76+
return d._value;
77+
}
78+
79+
/// <summary>
80+
/// Performs an implicit conversion from <see cref="FourCC"/> to <see cref="int"/>.
81+
/// </summary>
82+
/// <param name="d">The d.</param>
83+
/// <returns>
84+
/// The result of the conversion.
85+
/// </returns>
86+
public static implicit operator int(FourCC d)
87+
{
88+
return unchecked((int)d._value);
89+
}
90+
91+
/// <summary>
92+
/// Performs an implicit conversion from <see cref="int"/> to <see cref="FourCC"/>.
93+
/// </summary>
94+
/// <param name="d">The d.</param>
95+
/// <returns>
96+
/// The result of the conversion.
97+
/// </returns>
98+
public static implicit operator FourCC(uint d)
99+
{
100+
return new FourCC(d);
101+
}
102+
103+
/// <summary>
104+
/// Performs an implicit conversion from <see cref="int"/> to <see cref="FourCC"/>.
105+
/// </summary>
106+
/// <param name="d">The d.</param>
107+
/// <returns>
108+
/// The result of the conversion.
109+
/// </returns>
110+
public static implicit operator FourCC(int d)
111+
{
112+
return new FourCC(d);
113+
}
114+
115+
/// <summary>
116+
/// Performs an implicit conversion from <see cref="FourCC"/> to <see cref="string"/>.
117+
/// </summary>
118+
/// <param name="value">The <see cref="FourCC"/> value.</param>
119+
/// <returns>
120+
/// The result of the conversion.
121+
/// </returns>
122+
public static implicit operator string(FourCC value) => value.ToString();
123+
124+
/// <summary>
125+
/// Performs an implicit conversion from <see cref="string"/> to <see cref="FourCC"/>.
126+
/// </summary>
127+
/// <param name="d">The d.</param>
128+
/// <returns>
129+
/// The result of the conversion.
130+
/// </returns>
131+
public static implicit operator FourCC(string d) => new(d);
132+
133+
public override string ToString()
134+
{
135+
return string.Format("{0}", new string(new[]
136+
{
137+
(char) (_value & 0xFF),
138+
(char) ((_value >> 8) & 0xFF),
139+
(char) ((_value >> 16) & 0xFF),
140+
(char) ((_value >> 24) & 0xFF),
141+
}));
142+
}
143+
144+
public bool Equals(FourCC other) => _value == other._value;
145+
146+
/// <inheritdoc/>
147+
public override bool Equals(object? obj) => obj is FourCC value && Equals(value);
148+
149+
public override int GetHashCode() => (int)_value;
150+
151+
/// <summary>
152+
/// Provides a custom string representation of the FourCC descriptor.
153+
/// </summary>
154+
/// <remarks>
155+
/// The general format "G" is equivalent to the parameterless.
156+
/// <see cref="FourCC.ToString()"/>. The special format "I" returns a
157+
/// string representation which can be used to construct a Media
158+
/// Foundation format GUID. It is equivalent to "X08".
159+
/// </remarks>
160+
/// <param name="format">The format descriptor, which can be "G" (empty
161+
/// or <c>null</c> is equivalent to "G"), "I" or any valid standard
162+
/// number format.</param>
163+
/// <param name="formatProvider">The format provider for formatting
164+
/// numbers.</param>
165+
/// <returns>The requested string representation.</returns>
166+
/// <exception cref="System.FormatException">In case of
167+
/// <paramref name="format"/> is not "G", "I" or a valid number
168+
/// format.</exception>
169+
public string ToString(string format, IFormatProvider formatProvider)
170+
{
171+
if (string.IsNullOrEmpty(format))
172+
{
173+
format = "G";
174+
}
175+
if (formatProvider == null)
176+
{
177+
formatProvider = CultureInfo.CurrentCulture;
178+
}
179+
180+
return format.ToUpperInvariant() switch
181+
{
182+
"G" => ToString(),
183+
"I" => _value.ToString("X08", formatProvider),
184+
_ => _value.ToString(format, formatProvider),
185+
};
186+
}
187+
188+
public static bool operator ==(FourCC left, FourCC right) => left.Equals(right);
189+
190+
public static bool operator !=(FourCC left, FourCC right) => !left.Equals(right);
191+
}
192+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
// Copyright (c) Amer Koleci and contributors.
21+
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
22+
23+
using System;
24+
using System.IO;
25+
using SharpGen.Runtime;
26+
27+
namespace Vortice.Multimedia
28+
{
29+
/// <summary>
30+
/// A chunk of a Riff stream.
31+
/// </summary>
32+
public class RiffChunk
33+
{
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="RiffChunk"/> class.
36+
/// </summary>
37+
/// <param name="stream">The stream holding this chunk</param>
38+
/// <param name="type">The type.</param>
39+
/// <param name="size">The size.</param>
40+
/// <param name="dataPosition">The data offset.</param>
41+
/// <param name="isList">if set to <c>true</c> [is list].</param>
42+
/// <param name="isHeader">if set to <c>true</c> [is header].</param>
43+
public RiffChunk(Stream stream, FourCC type, uint size, uint dataPosition, bool isList = false, bool isHeader = false)
44+
{
45+
Stream = stream;
46+
Type = type;
47+
Size = size;
48+
DataPosition = dataPosition;
49+
IsList = isList;
50+
IsHeader = isHeader;
51+
}
52+
53+
/// <summary>
54+
/// Gets the type.
55+
/// </summary>
56+
public Stream Stream { get; }
57+
58+
/// <summary>
59+
/// Gets the <see cref="FourCC"/> of this chunk.
60+
/// </summary>
61+
public FourCC Type { get; }
62+
63+
/// <summary>
64+
/// Gets the size of the data embedded by this chunk.
65+
/// </summary>
66+
public uint Size { get; }
67+
68+
/// <summary>
69+
/// Gets the position of the data embedded by this chunk relative to the stream.
70+
/// </summary>
71+
public uint DataPosition { get; }
72+
73+
/// <summary>
74+
/// Gets or sets a value indicating whether this instance is a list chunk.
75+
/// </summary>
76+
/// <value>
77+
/// <c>true</c> if this instance is list; otherwise, <c>false</c>.
78+
/// </value>
79+
public bool IsList { get; }
80+
81+
/// <summary>
82+
/// Gets a value indicating whether this instance is a header chunk.
83+
/// </summary>
84+
/// <value>
85+
/// <c>true</c> if this instance is a header; otherwise, <c>false</c>.
86+
/// </value>
87+
public bool IsHeader { get; }
88+
89+
/// <summary>
90+
/// Gets the raw data contained in this chunk.
91+
/// </summary>
92+
/// <returns></returns>
93+
public byte[] GetData()
94+
{
95+
byte[] data = new byte[Size];
96+
Stream.Position = DataPosition;
97+
Stream.Read(data, 0, (int)Size);
98+
return data;
99+
}
100+
101+
/// <summary>
102+
/// Gets structured data contained in this chunk.
103+
/// </summary>
104+
/// <typeparam name="T">The type of the data to return</typeparam>
105+
/// <returns>
106+
/// A structure filled with the chunk data
107+
/// </returns>
108+
public unsafe T GetDataAs<T>() where T : unmanaged
109+
{
110+
T value = new();
111+
byte[] data = GetData();
112+
fixed (void* ptr = data)
113+
{
114+
MemoryHelpers.Read((IntPtr)ptr, ref value);
115+
}
116+
return value;
117+
}
118+
119+
/// <summary>
120+
/// Gets structured data contained in this chunk.
121+
/// </summary>
122+
/// <typeparam name="T">The type of the data to return</typeparam>
123+
/// <returns>A structure filled with the chunk data</returns>
124+
public unsafe T[] GetDataAsArray<T>() where T : unmanaged
125+
{
126+
int sizeOfT = sizeof(T);
127+
if ((Size % sizeOfT) != 0)
128+
throw new ArgumentException("Size of T is incompatible with size of chunk");
129+
130+
T[] values = new T[Size / sizeOfT];
131+
byte[] data = GetData();
132+
fixed (void* ptr = data)
133+
{
134+
MemoryHelpers.Read((IntPtr)ptr, values, 0, values.Length);
135+
}
136+
137+
return values;
138+
}
139+
140+
/// <summary>
141+
/// Returns a <see cref="string"/> that represents this instance.
142+
/// </summary>
143+
/// <returns>
144+
/// A <see cref="string"/> that represents this instance.
145+
/// </returns>
146+
public override string ToString()
147+
{
148+
return $"Type: {Type}, Size: {Size}, Position: {DataPosition}, IsList: {IsList}, IsHeader: {IsHeader}";
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)