Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RiffChunk.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 using System.IO;
26 using SiliconStudio.Core;
27 
28 namespace SiliconStudio.Paradox.Audio.Wave
29 {
30  /// <summary>
31  /// A chunk of a Riff stream.
32  /// </summary>
33  internal class RiffChunk
34  {
35  /// <summary>
36  /// Initializes a new instance of the <see cref="RiffChunk"/> class.
37  /// </summary>
38  /// <param name="stream">The stream holding this chunk</param>
39  /// <param name="type">The type.</param>
40  /// <param name="size">The size.</param>
41  /// <param name="dataPosition">The data offset.</param>
42  /// <param name="isList">if set to <c>true</c> [is list].</param>
43  /// <param name="isHeader">if set to <c>true</c> [is header].</param>
44  public RiffChunk(Stream stream, FourCC type, uint size, uint dataPosition, bool isList = false, bool isHeader = false)
45  {
46  Stream = stream;
47  Type = type;
48  Size = size;
49  DataPosition = dataPosition;
50  IsList = isList;
51  IsHeader = isHeader;
52  }
53 
54  /// <summary>
55  /// Gets the type.
56  /// </summary>
57  public Stream Stream { get; private set; }
58 
59  /// <summary>
60  /// Gets the <see cref="FourCC"/> of this chunk.
61  /// </summary>
62  public FourCC Type { get; private set; }
63 
64  /// <summary>
65  /// Gets the size of the data enbedded by this chunk.
66  /// </summary>
67  public uint Size { get; private set; }
68 
69  /// <summary>
70  /// Gets the position of the data embedded by this chunk relative to the stream.
71  /// </summary>
72  public uint DataPosition { get; private set; }
73 
74  /// <summary>
75  /// Gets or sets a value indicating whether this instance is a list chunk.
76  /// </summary>
77  /// <value>
78  /// <c>true</c> if this instance is list; otherwise, <c>false</c>.
79  /// </value>
80  public bool IsList { get; private set; }
81 
82  /// <summary>
83  /// Gets a value indicating whether this instance is a header chunk.
84  /// </summary>
85  /// <value>
86  /// <c>true</c> if this instance is a header; otherwise, <c>false</c>.
87  /// </value>
88  public bool IsHeader { get; private set; }
89 
90  /// <summary>
91  /// Gets the raw data contained in this chunk.
92  /// </summary>
93  /// <returns></returns>
94  public byte[] GetData()
95  {
96  var data = new byte[Size];
97  Stream.Position = DataPosition;
98  Stream.Read(data, 0, (int)Size);
99  return data;
100  }
101 
102  /// <summary>
103  /// Gets structured data contained in this chunk.
104  /// </summary>
105  /// <typeparam name="T">The type of the data to return</typeparam>
106  /// <returns>
107  /// A structure filled with the chunk data
108  /// </returns>
109  public unsafe T GetDataAs<T>() where T : struct
110  {
111  var value = new T();
112  var data = GetData();
113  fixed (void* ptr = data)
114  {
115  Utilities.Read((IntPtr)ptr, ref value);
116  }
117  return value;
118  }
119 
120  /// <summary>
121  /// Gets structured data contained in this chunk.
122  /// </summary>
123  /// <typeparam name="T">The type of the data to return</typeparam>
124  /// <returns>A structure filled with the chunk data</returns>
125  public unsafe T[] GetDataAsArray<T>() where T : struct
126  {
127  int sizeOfT = Utilities.SizeOf<T>();
128  if ((Size % sizeOfT) != 0)
129  throw new ArgumentException("Size of T is incompatible with size of chunk");
130 
131  var values = new T[Size / sizeOfT];
132  var data = GetData();
133  fixed (void* ptr = data)
134  {
135  Utilities.Read((IntPtr)ptr, values, 0, values.Length);
136  }
137  return values;
138  }
139 
140  /// <summary>
141  /// Returns a <see cref="System.String"/> that represents this instance.
142  /// </summary>
143  /// <returns>
144  /// A <see cref="System.String"/> that represents this instance.
145  /// </returns>
146  public override string ToString()
147  {
148  return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Type: {0}, Size: {1}, Position: {2}, IsList: {3}, IsHeader: {4}", Type, Size, DataPosition, IsList, IsHeader);
149  }
150  }
151 }
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175