home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectSound / CaptureSound / Formats.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  7.9 KB  |  227 lines

  1. //----------------------------------------------------------------------------
  2. // File: Formats.cs
  3. //
  4. // Copyright (c) Microsoft Corp. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using System.Drawing;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10. using Microsoft.DirectX;
  11. using Microsoft.DirectX.DirectSound;
  12.  
  13. public class FormatsForm : Form
  14. {
  15.     private struct FormatInfo
  16.     {
  17.         public WaveFormat format;
  18.         public override string ToString()
  19.         {
  20.             return ConvertWaveFormatToString(format);
  21.         }
  22.     };
  23.     private Button buttonOk;
  24.     private Button buttonCancel;
  25.     private ListBox lbFormatsInputListbox;
  26.     private Label labelStatic;
  27.     
  28.     private MainForm    mf = null;
  29.     private ArrayList    formats = new ArrayList();
  30.     private bool[]        InputFormatSupported = new bool[20];
  31.     public FormatsForm(MainForm mf)
  32.     {
  33.         //
  34.         // Required for Windows Form Designer support
  35.         //
  36.         InitializeComponent();
  37.         this.mf = mf;
  38.  
  39.         ScanAvailableInputFormats();
  40.         FillFormatListBox();
  41.     }
  42.     #region InitializeComponent code
  43.     private void InitializeComponent()
  44.     {
  45.         this.buttonOk = new System.Windows.Forms.Button();
  46.         this.buttonCancel = new System.Windows.Forms.Button();
  47.         this.lbFormatsInputListbox = new System.Windows.Forms.ListBox();
  48.         this.labelStatic = new System.Windows.Forms.Label();
  49.         this.SuspendLayout();
  50.         // 
  51.         // buttonOk
  52.         // 
  53.         this.buttonOk.Enabled = false;
  54.         this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.OK;
  55.         this.buttonOk.Location = new System.Drawing.Point(10, 128);
  56.         this.buttonOk.Name = "buttonOk";
  57.         this.buttonOk.TabIndex = 0;
  58.         this.buttonOk.Text = "OK";
  59.         this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
  60.         // 
  61.         // buttonCancel
  62.         // 
  63.         this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  64.         this.buttonCancel.Location = new System.Drawing.Point(97, 128);
  65.         this.buttonCancel.Name = "buttonCancel";
  66.         this.buttonCancel.TabIndex = 1;
  67.         this.buttonCancel.Text = "Cancel";
  68.         // 
  69.         // lbFormatsInputListbox
  70.         // 
  71.         this.lbFormatsInputListbox.Location = new System.Drawing.Point(10, 24);
  72.         this.lbFormatsInputListbox.Name = "lbFormatsInputListbox";
  73.         this.lbFormatsInputListbox.Size = new System.Drawing.Size(162, 95);
  74.         this.lbFormatsInputListbox.TabIndex = 2;
  75.         this.lbFormatsInputListbox.SelectedIndexChanged += new System.EventHandler(this.lbFormatsInputListbox_SelectedIndexChanged);
  76.         // 
  77.         // labelStatic
  78.         // 
  79.         this.labelStatic.Location = new System.Drawing.Point(10, 11);
  80.         this.labelStatic.Name = "labelStatic";
  81.         this.labelStatic.Size = new System.Drawing.Size(75, 13);
  82.         this.labelStatic.TabIndex = 3;
  83.         this.labelStatic.Text = "Input Format:";
  84.         // 
  85.         // FormatsForm
  86.         // 
  87.         this.AcceptButton = this.buttonOk;
  88.         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  89.         this.CancelButton = this.buttonCancel;
  90.         this.ClientSize = new System.Drawing.Size(183, 170);
  91.         this.Controls.AddRange(new System.Windows.Forms.Control[] {
  92.                                                                       this.buttonOk,
  93.                                                                       this.buttonCancel,
  94.                                                                       this.lbFormatsInputListbox,
  95.                                                                       this.labelStatic});
  96.         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  97.         this.Name = "FormatsForm";
  98.         this.Text = "Select Capture Format";
  99.         this.ResumeLayout(false);
  100.  
  101.     }
  102.     #endregion
  103.     private void ScanAvailableInputFormats()
  104.     {
  105.         //-----------------------------------------------------------------------------
  106.         // Name: ScanAvailableInputFormats()
  107.         // Desc: Tests to see if 20 different standard wave formats are supported by
  108.         //       the capture device 
  109.         //-----------------------------------------------------------------------------
  110.         WaveFormat format = new WaveFormat();
  111.         CaptureBufferDescription dscheckboxd = new CaptureBufferDescription();
  112.         CaptureBuffer pDSCaptureBuffer = null;
  113.     
  114.         // This might take a second or two, so throw up the hourglass
  115.         Cursor = Cursors.WaitCursor;
  116.     
  117.         format.FormatTag = WaveFormatTag.Pcm;
  118.  
  119.         // Try 20 different standard formats to see if they are supported
  120.         for (int iIndex = 0; iIndex < 20; iIndex++)
  121.         {
  122.             GetWaveFormatFromIndex(iIndex, ref format);
  123.  
  124.             // To test if a capture format is supported, try to create a 
  125.             // new capture buffer using a specific format.  If it works
  126.             // then the format is supported, otherwise not.
  127.             dscheckboxd.BufferBytes = format.AverageBytesPerSecond;
  128.             dscheckboxd.Format = format;
  129.         
  130.             try
  131.             {
  132.                 pDSCaptureBuffer = new CaptureBuffer(dscheckboxd, mf.applicationDevice);
  133.                 InputFormatSupported[ iIndex ] = true;
  134.             }
  135.             catch
  136.             {
  137.                 InputFormatSupported[ iIndex ] = false;
  138.             }
  139.             if (pDSCaptureBuffer != null)
  140.                 pDSCaptureBuffer.Dispose();
  141.         }
  142.         Cursor = Cursors.Default;
  143.     }
  144.     private void GetWaveFormatFromIndex(int Index, ref WaveFormat format)
  145.     {
  146.         //-----------------------------------------------------------------------------
  147.         // Name: GetWaveFormatFromIndex()
  148.         // Desc: Returns 20 different wave formats based on Index
  149.         //-----------------------------------------------------------------------------
  150.         int SampleRate = Index / 4;
  151.         int iType = Index % 4;
  152.  
  153.         switch (SampleRate)
  154.         {
  155.             case 0: format.SamplesPerSecond = 48000; break;
  156.             case 1: format.SamplesPerSecond = 44100; break;
  157.             case 2: format.SamplesPerSecond = 22050; break;
  158.             case 3: format.SamplesPerSecond = 11025; break;
  159.             case 4: format.SamplesPerSecond =  8000; break;
  160.         }
  161.  
  162.         switch (iType)
  163.         {
  164.             case 0: format.BitsPerSample =  8; format.Channels = 1; break;
  165.             case 1: format.BitsPerSample = 16; format.Channels = 1; break;
  166.             case 2: format.BitsPerSample =  8; format.Channels = 2; break;
  167.             case 3: format.BitsPerSample = 16; format.Channels = 2; break;
  168.         }
  169.  
  170.         format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
  171.         format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
  172.     }
  173.     void FillFormatListBox()
  174.     {
  175.         //-----------------------------------------------------------------------------
  176.         // Name: FillFormatListBox()
  177.         // Desc: Fills the format list box based on the availible formats
  178.         //-----------------------------------------------------------------------------
  179.         FormatInfo        info            = new FormatInfo();        
  180.         string            strFormatName    = string.Empty;
  181.         WaveFormat    format                = new WaveFormat();
  182.  
  183.         for (int iIndex = 0; iIndex < InputFormatSupported.Length; iIndex++)
  184.         {
  185.             if (true == InputFormatSupported[iIndex])
  186.             {
  187.                 // Turn the index into a WaveFormat then turn that into a
  188.                 // string and put the string in the listbox
  189.                 GetWaveFormatFromIndex(iIndex, ref format);
  190.                 info.format = format;
  191.                 formats.Add(info);
  192.             }
  193.         }
  194.         lbFormatsInputListbox.DataSource = formats;
  195.     }
  196.     private static string ConvertWaveFormatToString(WaveFormat format)
  197.     {
  198.         //-----------------------------------------------------------------------------
  199.         // Name: ConvertWaveFormatToString()
  200.         // Desc: Converts a wave format to a text string
  201.         //-----------------------------------------------------------------------------
  202.         return format.SamplesPerSecond + " Hz, " + 
  203.             format.BitsPerSample + "-bit " + 
  204.             ((format.Channels == 1) ? "Mono" : "Stereo");
  205.     }
  206.     private void FormatsOK()
  207.     {
  208.         //-----------------------------------------------------------------------------
  209.         // Name: FormatsOK()
  210.         // Desc: Stores the capture buffer format based on what was selected
  211.         //-----------------------------------------------------------------------------
  212.         
  213.         mf.InputFormat = ((FormatInfo)formats[lbFormatsInputListbox.SelectedIndex]).format;
  214.         this.DialogResult = DialogResult.OK;
  215.         Close();
  216.     }
  217.     private void buttonOk_Click(object sender, System.EventArgs e)
  218.     {
  219.         FormatsOK();
  220.     }
  221.  
  222.     private void lbFormatsInputListbox_SelectedIndexChanged(object sender, System.EventArgs e)
  223.     {
  224.         buttonOk.Enabled = true;
  225.     }
  226. }
  227.