home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / directX / dSound / dSoundTest / dSoundTest.java < prev    next >
Encoding:
Java Source  |  1996-10-15  |  16.2 KB  |  518 lines

  1. // 
  2. // 
  3. //  Copyright (C) 1996 Microsoft Corporation. All Rights Reserved.
  4. // 
  5. //  File: dSoundTest.java
  6. //          
  7. //  Description: shows use of dSound (via dSoundLIB.java).
  8. // 
  9. // 
  10.  
  11.  
  12. import java.applet.*;
  13. import java.awt.*;
  14. import dSoundTestFrame;
  15. import java.applet.*;
  16. import java.awt.*;
  17. import java.awt.peer.ComponentPeer;
  18. import com.ms.com.*;
  19. import com.ms.directX.*;
  20. import com.ms.awt.peer.*;
  21. import java.net.URL;
  22. import java.net.MalformedURLException;
  23. import dSoundLIB;
  24.  
  25.                                                 
  26. //==============================================================================
  27. // Main Class for applet dSoundTest
  28. //
  29. //==============================================================================
  30. public class dSoundTest extends Applet implements Runnable, 
  31.                                                   ddConstants, 
  32.                                                   IDSEnumCallback 
  33. {
  34.    private dSoundLIB m_dsLib;
  35.    private int       m_hWnd;
  36.    private TextArea  m_TextArea = null;
  37.    private dSoundTestFrame m_DebugFrame;
  38.  
  39.    // THREAD SUPPORT:
  40.     //        m_dSoundTest    is the Thread object for the applet
  41.     //--------------------------------------------------------------------------
  42.     Thread     m_dSoundTest = null;
  43.  
  44.     // STANDALONE APPLICATION SUPPORT:
  45.     //        m_fStandAlone will be set to true if applet is run standalone
  46.     //--------------------------------------------------------------------------
  47.     boolean m_fStandAlone = false;
  48.  
  49.     // STANDALONE APPLICATION SUPPORT
  50.     //     The main() method acts as the applet's entry point when it is run
  51.     // as a standalone application. It is ignored if the applet is run from
  52.     // within an HTML page.
  53.     //--------------------------------------------------------------------------
  54.     public static void main(String args[])
  55.     {
  56.         // Create Toplevel Window to contain applet dSoundTest
  57.         //----------------------------------------------------------------------
  58.         dSoundTestFrame frame = new dSoundTestFrame("dSoundTest");
  59.  
  60.         // Must show Frame before we size it so insets() will return valid values
  61.         //----------------------------------------------------------------------
  62.         frame.show();
  63.       frame.hide();
  64.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  65.                      frame.insets().top  + frame.insets().bottom + 240);
  66.  
  67.  
  68.         // The following code starts the applet running within the frame window.
  69.         // It also calls GetParameters() to retrieve parameter values from the
  70.         // command line, and sets m_fStandAlone to true to prevent init() from
  71.         // trying to get them from the HTML page.
  72.         //----------------------------------------------------------------------
  73.         dSoundTest applet_dSoundTest = new dSoundTest();
  74.  
  75.         frame.add("Center", applet_dSoundTest);
  76.  
  77.         applet_dSoundTest.m_fStandAlone = true;
  78.         applet_dSoundTest.init();
  79.         applet_dSoundTest.start();
  80.       frame.show();
  81.     }
  82.  
  83.    //------------------------------------------------
  84.  
  85.    private void DebugMsg (String s)
  86.    {
  87.       String ss;
  88.  
  89.       ss = "dSoundTest " + s;
  90.       System.out.println (ss);
  91.       if (m_TextArea != null)
  92.          m_TextArea.appendText (ss + "\n");
  93.       
  94.       // this will make those pesky numbers update to show it's 
  95.       // still alive...
  96.       repaint();
  97.    }
  98.  
  99.     // dSoundTest Class Constructor
  100.     //--------------------------------------------------------------------------
  101.     public dSoundTest()
  102.     {
  103.     }
  104.  
  105.     // APPLET INFO SUPPORT:
  106.     //        The getAppletInfo() method returns a string describing the applet's
  107.     // author, copyright date, or miscellaneous information.
  108.    //--------------------------------------------------------------------------
  109.     public String getAppletInfo()
  110.     {
  111.         return "Name: dSoundTest\r\n" +
  112.                "Author: Microsoft\r\n" +
  113.                "Created with Microsoft Visual J++ Version 1.0";
  114.     }
  115.  
  116.    //--------------------------
  117.  
  118.     public String GetFullFileName (String rawFileName)
  119.     {
  120.         String file;
  121.  
  122.         try
  123.         {
  124.             URL url = new URL( getDocumentBase(), rawFileName);
  125.  
  126.             file = url.getFile();
  127.             char c0 = file.charAt(0);
  128.             char c2 = file.charAt(2);
  129.             if( (c0 == '/') && (c2 == ':') )
  130.             {
  131.                 char ch[] = new char[file.length()-1];
  132.                 file.getChars(1,file.length(),ch,0);
  133.                 file = new String(ch);
  134.             }
  135.         }
  136.         catch(MalformedURLException me)
  137.         {
  138.          DebugMsg ("GetFullFileName caught exception \n   " + me);
  139.             file = new String (rawFileName);
  140.         }
  141.       catch (Exception e)
  142.       {
  143.          DebugMsg ("GetFullFileName caught exception \n   " + e);
  144.             file = new String (rawFileName);
  145.         }
  146.  
  147.         return file;
  148.     }
  149.  
  150.     //--------------------------------
  151.  
  152.    public void CreateDebugWindow (int nWidth, int nHeight)
  153.    {
  154.       m_DebugFrame = new dSoundTestFrame ("dSoundTest Msgs");
  155.       m_DebugFrame.resize (nWidth, nHeight);
  156.  
  157.       m_TextArea = new TextArea (nWidth, nHeight);
  158.       m_DebugFrame.add ("Center", m_TextArea);
  159.       m_DebugFrame.show();
  160.    }
  161.  
  162.    // The init() method is called by the AWT when an applet is first loaded or
  163.     // reloaded.  Override this method to perform whatever initialization your
  164.     // applet needs, such as initializing data structures, loading images or
  165.     // fonts, creating frame windows, setting the layout manager, or adding UI
  166.     // components.
  167.    //--------------------------------------------------------------------------
  168.     public void init()
  169.     {
  170.       resize (320, 240);
  171.       
  172.       if (m_fStandAlone)
  173.          CreateDebugWindow (300, 400);
  174.  
  175.       //
  176.       // do some stuff to get the handle to our window (frame) for 
  177.       // use when creating dSound object.
  178.       ComponentPeer  p    = getPeer();
  179.       ComponentPeerX peer = (ComponentPeerX)p;
  180.       if (peer == null)
  181.       {
  182.          DebugMsg ("Cant retrieve peer!");
  183.          return;
  184.       }
  185.  
  186.       m_hWnd = peer.gethwnd();
  187.       try
  188.       {
  189.          m_dsLib = new dSoundLIB (m_hWnd, DSSCL_NORMAL);
  190.       }
  191.       catch (Exception e)
  192.       {
  193.          ErrorExit ("m_dsLib exception \n" + e, 1);
  194.       }
  195.  
  196.       String sFile = new String ("walk.wav");   
  197.       
  198.       // 
  199.       // URL throws an exception when we try to get document base if
  200.       // we're not running from a browser (ie. JVIEW dSoundTest).  
  201.       // Otherwise we need to find the path to the wave file.
  202.       if (! m_fStandAlone)
  203.          sFile = GetFullFileName (sFile);
  204.  
  205.       if (! m_dsLib.PlayWave (sFile, true))
  206.          ErrorExit ("Can't play wave file [" + sFile + "]", 3);
  207.     }
  208.    
  209.    //--------------------------------------------------------------------------
  210.  
  211.    public void ErrorExit (String s, int nExitValue)
  212.    {
  213.       DebugMsg ("FatalError: [" + s + "]");
  214.         if(m_fStandAlone)
  215.          System.exit (nExitValue);
  216.    }
  217.  
  218.    // Place additional applet clean up code here.  destroy() is called when
  219.     // when you applet is terminating and being unloaded.
  220.     //-------------------------------------------------------------------------
  221.     public void destroy()
  222.     {
  223.     }
  224.  
  225.     // dSoundTest Paint Handler
  226.     //--------------------------------------------------------------------------
  227.     public void paint(Graphics g)
  228.     {
  229.         if (! m_fStandAlone)
  230.       {
  231.          g.drawString("For debug output, see ", 10, 20);
  232.          g.drawString("<windows dir>\\java\\MSJava.txt..." , 10, 40);
  233.       }
  234.  
  235.       g.drawString("Note: this window needs focus in ", 10, 100);
  236.       g.drawString("     order to hear the sound.", 10, 120);
  237.       g.drawString("Running (sound should be playing): " + Math.random(), 10, 140);
  238.     }
  239.  
  240.     //        The start() method is called when the page containing the applet
  241.     // first appears on the screen. The AppletWizard's initial implementation
  242.     // of this method starts execution of the applet's thread.
  243.     //--------------------------------------------------------------------------
  244.     public void start()
  245.     {
  246.         if (m_dSoundTest == null)
  247.         {
  248.             m_dSoundTest = new Thread(this);
  249.             m_dSoundTest.start();
  250.         }
  251.     }
  252.     
  253.     //        The stop() method is called when the page containing the applet is
  254.     // no longer on the screen. The AppletWizard's initial implementation of
  255.     // this method stops execution of the applet's thread.
  256.     //--------------------------------------------------------------------------
  257.     public void stop()
  258.     {
  259.         if (m_dSoundTest != null)
  260.         {
  261.             m_dSoundTest.stop();
  262.             m_dSoundTest = null;
  263.         }
  264.     }
  265.  
  266.     // THREAD SUPPORT
  267.     //        The run() method is called when the applet's thread is started. If
  268.     // your applet performs any ongoing activities without waiting for user
  269.     // input, the code for implementing that behavior typically goes here. For
  270.     // example, for an applet that performs animation, the run() method controls
  271.     // the display of images.
  272.     //--------------------------------------------------------------------------
  273.     public void run()
  274.     {
  275.       // Test some basic dSOund api's...
  276.       // 
  277.       // Note, if you just wanted to play a wave file, all you'd need is the
  278.       //       code in the Init()...
  279.       TestDSoundMethods();
  280.       DebugMsg (" ");
  281.       TestDSoundBufferMethods();
  282.       
  283.       while (true)
  284.         {
  285.          try
  286.             {
  287.                 repaint();
  288.                 Thread.sleep(50);
  289.             }
  290.             catch (InterruptedException e)
  291.             {
  292.                 stop();
  293.             }
  294.         }
  295.     }
  296.  
  297.    //---------------------------------------
  298.  
  299.    public void callbackDSEnum(GuidData g, String Description, 
  300.                               String mod, IUnknown lpContext)
  301.    {
  302.       DebugMsg ("   DSEnum: [" + Description + ", " + mod + "]");
  303.    }
  304.  
  305.    //---------------------------------------
  306.  
  307.    public void TestDSoundMethods()
  308.       // 
  309.       // call some dSound api's to make sure things are working...
  310.    {
  311.         dSound ds;
  312.  
  313.       DebugMsg ("TestDSoundMethods =================");
  314.       try
  315.       {
  316.          // Get the initialized dSound object from the library
  317.          ds = m_dsLib.GetDSoundObject();
  318.  
  319.          
  320.          // Enumerate devices on system
  321.          DebugMsg ("TEST: DSEnumCallback...");
  322.          ds.DSEnumerate ((IDSEnumCallback)this, (IUnknown)null);
  323.  
  324.  
  325.          // TEST  compact the sound card memory 
  326.          DebugMsg ("TEST: Compact()");
  327.          ds.SetCooperativeLevel (m_hWnd, DSSCL_EXCLUSIVE);
  328.          ds.Compact();
  329.          ds.SetCooperativeLevel (m_hWnd, DSSCL_NORMAL);
  330.  
  331.          // TEST  speaker config
  332.          DebugMsg ("TEST: Get/SetSpeakerConfig()");
  333.          int nSpeakerConfig[] = new int [1];
  334.          ds.SetSpeakerConfig (DSSPEAKER_HEADPHONE);
  335.          ds.GetSpeakerConfig (nSpeakerConfig);
  336.          if (nSpeakerConfig[0] != DSSPEAKER_HEADPHONE)
  337.             DebugMsg ("Get/Set speaker config failed");
  338.  
  339.          
  340.          // TEST GetCaps()
  341.          DebugMsg ("TEST: GetCaps()");
  342.          DSCaps dsCaps = new DSCaps();
  343.          dsCaps.dwSize = m_dsLib.sizeofDSCAPS;
  344.          ds.GetCaps (dsCaps);
  345.          DebugMsg ("   dsCaps.dwFreeHwMemBytes         = " + dsCaps.dwFreeHwMemBytes);
  346.          DebugMsg ("   dsCaps.dwFreeHwMixingAllBuffers = " + dsCaps.dwFreeHwMixingAllBuffers);
  347.          DebugMsg ("   dsCaps.dwPrimaryBuffers         = " + dsCaps.dwPrimaryBuffers);
  348.       }
  349.       catch (Exception e)
  350.       {
  351.          DebugMsg ("TestDSoundMethods, caught exception \n " + e);
  352.          stop();
  353.       }
  354.    }
  355.  
  356.    //---------------------------------------
  357.  
  358.    public void TestDSoundBufferMethods()
  359.       // 
  360.       // * call some dSound api's to make sure things are working...
  361.       // * assumes there's a wave file playing on LOOP mode.
  362.       // 
  363.    {
  364.       DebugMsg ("TestDSoundBufferMethods =================");
  365.       try
  366.       {
  367.          // Get the initialized dSound objects from the library
  368.          dSoundBuffer dsBuffer = m_dsLib.GetDSoundBufferObject();
  369.          dSound       ds       = m_dsLib.GetDSoundObject();
  370.    
  371.          // TEST GetCaps()
  372.          DebugMsg ("TEST: GetCaps()");
  373.          DSBCaps dsbCaps = new DSBCaps();
  374.          dsbCaps.dwSize  = m_dsLib.sizeofDSBCAPS;
  375.          dsBuffer.GetCaps (dsbCaps);
  376.          DebugMsg ("   dsbCaps.dwBufferBytes        = " + dsbCaps.dwBufferBytes);
  377.          DebugMsg ("   dsbCaps.dwUnlockTransferRate = " + dsbCaps.dwUnlockTransferRate);
  378.          DebugMsg ("   dsbCaps.dwPlayCpuOverhead    = " + dsbCaps.dwPlayCpuOverhead );
  379.  
  380.          // TEST GetCurrentPosition()
  381.          DebugMsg ("TEST: GetCurrentPosition()");
  382.          DSCursors dsCursors = new DSCursors();
  383.  
  384.          dsBuffer.GetCurrentPosition (dsCursors);
  385.          DebugMsg ("   dsCursors.dwPlay  = " + dsCursors.dwPlay);
  386.          DebugMsg ("   dsCursors.dwWrite = " + dsCursors.dwWrite);
  387.  
  388.  
  389.          // TEST GetFormatSize()
  390.          DebugMsg ("TEST: GetFormatSize()");
  391.          int nSize;
  392.          nSize = dsBuffer.GetFormatSize ();
  393.          DebugMsg ("   GetFormatSize = " + nSize);
  394.  
  395.  
  396.          // TEST GetFormat()
  397.          // note: GetFormat() returns a new WaveFormatEx(); 
  398.          DebugMsg ("TEST: GetFormat()");
  399.          WaveFormatEx waveForm;  
  400.          
  401.          waveForm = dsBuffer.GetFormat();
  402.          DebugMsg ("   waveForm.nChannels      = " + waveForm.nChannels);
  403.          DebugMsg ("   waveForm.wBitsPerSample = " + waveForm.wBitsPerSample);
  404.          DebugMsg ("   waveForm.nSamplesPerSec = " + waveForm.nSamplesPerSec);
  405.  
  406.  
  407.          // let users get used to current volume level              
  408.          Wait (5);      
  409.  
  410.          // TEST GetVolume()
  411.          DebugMsg ("TEST: GetVolume()");
  412.          int nTemp = 10;
  413.          nTemp = dsBuffer.GetVolume();
  414.          DebugMsg ("TEST: GetVolume() = " + nTemp);
  415.          
  416.  
  417.          // Test SetVolume()
  418.          DebugMsg ("   Setting volume to 1/2");
  419.  
  420.          // NOTE: units are in hundreths of decibels 
  421.          dsBuffer.SetVolume (nTemp - 1000);   
  422.          Wait (6);
  423.          DebugMsg ("   Restoring volume to original level");
  424.          dsBuffer.SetVolume (nTemp);
  425.  
  426.          // Test GetPan()
  427.          DebugMsg ("TEST: GetPan()");
  428.          nTemp = dsBuffer.GetPan();
  429.          DebugMsg ("TEST: GetPan() = " + nTemp);
  430.  
  431.          // Test SetPan()
  432.          DebugMsg ("   Setting pan from LEFT to RIGHT....");
  433.          for (int nPan = -10000; nPan < 10000; nPan += 1000)
  434.          {
  435.             dsBuffer.SetPan (nPan);
  436.             Wait (1);
  437.          }
  438.          
  439.          DebugMsg ("   Restoring original pan setting.... " + nTemp);
  440.          dsBuffer.SetPan (nTemp);
  441.  
  442.  
  443.          // Test GetFrequency()
  444.          DebugMsg ("TEST: GetFrequency()");
  445.          nTemp = dsBuffer.GetFrequency();
  446.          DebugMsg ("TEST: GetFrequency() = " + nTemp);
  447.  
  448.          // Test SetFrequency(); note upper limit is 100000
  449.          DebugMsg ("   Setting Frequency from 100 to 50000....");
  450.          for (int nFreq = 100; nFreq < 50000; nFreq += 3000)
  451.          {
  452.             dsBuffer.SetFrequency (nFreq);
  453.             Wait (2);
  454.          }
  455.          
  456.          DebugMsg ("   Restoring original frequency setting.... " + nTemp);
  457.          dsBuffer.SetFrequency (nTemp);
  458.  
  459.  
  460.          // Test: GetStatus
  461.          nTemp = dsBuffer.GetStatus();
  462.          DebugMsg ("TEST: GetStatus() = " + nTemp);
  463.  
  464.  
  465.          // Test: Stop
  466.          DebugMsg ("TEST: Stop()");
  467.          m_dsLib.StopWave();
  468.          DebugMsg ("   wave playback is stopped now...");
  469.  
  470.  
  471.          // Test: SetCurrentPosition.  Wave buffer positions are 0 to maxBufferSize
  472.          DebugMsg ("TEST: SetCurrentPosition()");
  473.          int nBufferSize = m_dsLib.GetBufferSize();
  474.          DebugMsg ("   BufferSize = " + nBufferSize);
  475.          
  476.          // 
  477.          // play the wave file starting from different positions within
  478.          // the wave file.
  479.          int nPos;
  480.          for (int n = 0; n < 5; n++)
  481.          {
  482.             nPos = (int)((double)nBufferSize * Math.random());
  483.             DebugMsg ("   Setting current position to: " + nPos);
  484.             dsBuffer.SetCurrentPosition (nPos);
  485.             m_dsLib.PlayWave (0);
  486.             while (dsBuffer.GetStatus() == DSBSTATUS_PLAYING)
  487.                ;
  488.  
  489.             Wait (3);
  490.          }
  491.  
  492.  
  493.          Wait (2);
  494.          m_dsLib.PlayWave (DSBPLAY_LOOPING);
  495.          DebugMsg ("Normal wave playback has resumed now, \n   end of test...");
  496.       }
  497.       catch (Exception e)
  498.       {
  499.          DebugMsg ("TestDSoundBufferMethods, caught exception \n " + e);
  500.          stop();
  501.       }
  502.    }
  503.  
  504.    //--------------------------------------------------
  505.  
  506.    public void Wait (int nLoopCounter)
  507.    {
  508.       try
  509.       {
  510.          Thread.sleep (nLoopCounter * 500);
  511.       }
  512.       catch (InterruptedException e)
  513.       {
  514.          DebugMsg ("Wait: caught exception \n   " + e);
  515.       }
  516.    }
  517. }
  518.