Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
WaveFormatExtensible.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 // Original code from NAudio project. http://naudio.codeplex.com/
25 // Greetings to Mark Heath.
26 // -----------------------------------------------------------------------------
27 
28 using System;
29 using System.Globalization;
30 using System.Runtime.InteropServices;
31 
32 using SiliconStudio.Core;
33 
34 namespace SiliconStudio.Paradox.Audio.Wave
35 {
36  /// <summary>
37  /// WaveFormatExtensible
38  /// http://www.microsoft.com/whdc/device/audio/multichaud.mspx
39  /// </summary>
40  internal class WaveFormatExtensible : WaveFormat
41  {
42  private short wValidBitsPerSample; // bits of precision, or is wSamplesPerBlock if wBitsPerSample==0
43 
44  /// <summary>
45  /// Guid of the subformat.
46  /// </summary>
47  public Guid GuidSubFormat;
48 
49  /// <summary>
50  /// Speaker configuration
51  /// </summary>
52  public Speakers ChannelMask; // which channels are present in stream
53 
54 
55  /// <summary>
56  /// Parameterless constructor for marshalling
57  /// </summary>
58  internal WaveFormatExtensible()
59  {
60  }
61 
62  /// <summary>
63  /// Creates a new WaveFormatExtensible for PCM or IEEE
64  /// </summary>
65  public WaveFormatExtensible(int rate, int bits, int channels)
66  : base(rate, bits, channels)
67  {
68  waveFormatTag = WaveFormatEncoding.Extensible;
69  extraSize = 22;
70  wValidBitsPerSample = (short)bits;
71  int dwChannelMask = 0;
72  for (int n = 0; n < channels; n++)
73  dwChannelMask |= (1 << n);
74  ChannelMask = (Speakers)dwChannelMask;
75 
76  // KSDATAFORMAT_SUBTYPE_IEEE_FLOAT // AudioMediaSubtypes.MEDIASUBTYPE_IEEE_FLOAT
77  // KSDATAFORMAT_SUBTYPE_PCM // AudioMediaSubtypes.MEDIASUBTYPE_PCM;
78  GuidSubFormat = bits == 32 ? new Guid("00000003-0000-0010-8000-00aa00389b71") : new Guid("00000001-0000-0010-8000-00aa00389b71");
79  }
80 
81  protected override unsafe IntPtr MarshalToPtr()
82  {
83  var result = Marshal.AllocHGlobal(Utilities.SizeOf<__Native>());
84  __MarshalTo(ref *(__Native*)result);
85  return result;
86  }
87 
88  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 2)]
89  internal new struct __Native
90  {
91  public WaveFormat.__Native waveFormat;
92 
93  public short wValidBitsPerSample; // bits of precision, or is wSamplesPerBlock if wBitsPerSample==0
94 
95  public Speakers dwChannelMask; // which channels are present in stream
96 
97  public Guid subFormat;
98 
99  // Method to free native struct
100  internal void __MarshalFree()
101  {
102  waveFormat.__MarshalFree();
103  }
104  }
105 
106  // Method to marshal from native to managed struct
107  internal void __MarshalFrom(ref __Native @ref)
108  {
109  __MarshalFrom(ref @ref.waveFormat);
110  wValidBitsPerSample = @ref.wValidBitsPerSample;
111  ChannelMask = @ref.dwChannelMask;
112  GuidSubFormat = @ref.subFormat;
113  }
114 
115  // Method to marshal from managed struct tot native
116  internal void __MarshalTo(ref __Native @ref)
117  {
118  __MarshalTo(ref @ref.waveFormat);
119  @ref.wValidBitsPerSample = wValidBitsPerSample;
120  @ref.dwChannelMask = ChannelMask;
121  @ref.subFormat = GuidSubFormat;
122  }
123 
124  internal static __Native __NewNative()
125  {
126  {
127  __Native temp = default(__Native);
128  temp.waveFormat.extraSize = 22;
129  return temp;
130  }
131  }
132 
133  /// <summary>
134  /// String representation
135  /// </summary>
136  public override string ToString()
137  {
138  return String.Format(
139  CultureInfo.InvariantCulture,
140  "{0} wBitsPerSample:{1} ChannelMask:{2} SubFormat:{3} extraSize:{4}",
141  base.ToString(),
142  wValidBitsPerSample,
143  ChannelMask,
144  GuidSubFormat,
145  extraSize);
146  }
147  }
148 }