AudioCapabilities.cs
Upload User: wuming6209
Upload Date: 2013-06-06
Package Size: 161k
Code Size: 4k
Category:

Video Capture

Development Platform:

Visual C++

  1. // ------------------------------------------------------------------
  2. // DirectX.Capture
  3. //
  4. // History:
  5. // 2003-Jan-24 BL - created
  6. //
  7. // Copyright (c) 2003 Brian Low
  8. // ------------------------------------------------------------------
  9. using System;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Runtime.InteropServices;
  13. using DShowNET;
  14. namespace DirectX.Capture
  15. {
  16. /// <summary>
  17. ///  Capabilities of the audio device such as 
  18. ///  min/max sampling rate and number of channels available.
  19. /// </summary>
  20. public class AudioCapabilities
  21. {
  22. // ------------------ Properties --------------------
  23. /// <summary> Minimum number of audio channels. </summary>
  24. public int MinimumChannels;
  25. /// <summary> Maximum number of audio channels. </summary>
  26. public int MaximumChannels;
  27. /// <summary> Granularity of the channels. For example, channels 2 through 4, in steps of 2. </summary>
  28. public int ChannelsGranularity;
  29. /// <summary> Minimum number of bits per sample. </summary>
  30. public int MinimumSampleSize;
  31. /// <summary> Maximum number of bits per sample. </summary>
  32. public int MaximumSampleSize;
  33. /// <summary> Granularity of the bits per sample. For example, 8 bits per sample through 32 bits per sample, in steps of 8. </summary>
  34. public int SampleSizeGranularity;
  35. /// <summary> Minimum sample frequency. </summary>
  36. public int MinimumSamplingRate;
  37. /// <summary> Maximum sample frequency. </summary>
  38. public int MaximumSamplingRate;
  39. /// <summary> Granularity of the frequency. For example, 11025 Hz to 44100 Hz, in steps of 11025 Hz. </summary>
  40. public int SamplingRateGranularity;
  41. // ----------------- Constructor ---------------------
  42. /// <summary> Retrieve capabilities of an audio device </summary>
  43. internal AudioCapabilities(IAMStreamConfig audioStreamConfig)
  44. {
  45. if ( audioStreamConfig == null ) 
  46. throw new ArgumentNullException( "audioStreamConfig" );
  47. AMMediaType mediaType = null;
  48. AudioStreamConfigCaps caps = null;
  49. IntPtr pCaps = IntPtr.Zero;
  50. IntPtr pMediaType;
  51. try
  52. {
  53. // Ensure this device reports capabilities
  54. int c, size;
  55. int hr = audioStreamConfig.GetNumberOfCapabilities( out c, out size );
  56. if ( hr != 0 ) Marshal.ThrowExceptionForHR( hr );
  57. if ( c <= 0 ) 
  58. throw new NotSupportedException( "This audio device does not report capabilities." );
  59. if ( size > Marshal.SizeOf( typeof( AudioStreamConfigCaps ) ) )
  60. throw new NotSupportedException( "Unable to retrieve audio device capabilities. This audio device requires a larger AudioStreamConfigCaps structure." );
  61. if ( c > 1 )
  62. Debug.WriteLine("WARNING: This audio device supports " + c + " capability structures. Only the first structure will be used." );
  63. // Alloc memory for structure
  64. pCaps = Marshal.AllocCoTaskMem( Marshal.SizeOf( typeof( AudioStreamConfigCaps ) ) ); 
  65. // Retrieve first (and hopefully only) capabilities struct
  66. hr = audioStreamConfig.GetStreamCaps( 0, out pMediaType, pCaps );
  67. if ( hr != 0 ) Marshal.ThrowExceptionForHR( hr );
  68. // Convert pointers to managed structures
  69. mediaType = (AMMediaType) Marshal.PtrToStructure( pMediaType, typeof( AMMediaType ) );
  70. caps = (AudioStreamConfigCaps) Marshal.PtrToStructure( pCaps, typeof( AudioStreamConfigCaps ) );
  71. // Extract info
  72. MinimumChannels = caps.MinimumChannels;
  73. MaximumChannels = caps.MaximumChannels;
  74. ChannelsGranularity = caps.ChannelsGranularity;
  75. MinimumSampleSize = caps.MinimumBitsPerSample;
  76. MaximumSampleSize = caps.MaximumBitsPerSample;
  77. SampleSizeGranularity = caps.BitsPerSampleGranularity;
  78. MinimumSamplingRate = caps.MinimumSampleFrequency;
  79. MaximumSamplingRate = caps.MaximumSampleFrequency;
  80. SamplingRateGranularity = caps.SampleFrequencyGranularity;
  81. }
  82. finally
  83. {
  84. if ( pCaps != IntPtr.Zero )
  85. Marshal.FreeCoTaskMem( pCaps ); pCaps = IntPtr.Zero;
  86. if ( mediaType != null )
  87. DsUtils.FreeAMMediaType( mediaType ); mediaType = null;
  88. }
  89. }
  90. }
  91. }