home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-15 | 16.2 KB | 518 lines |
- //
- //
- // Copyright (C) 1996 Microsoft Corporation. All Rights Reserved.
- //
- // File: dSoundTest.java
- //
- // Description: shows use of dSound (via dSoundLIB.java).
- //
- //
-
-
- import java.applet.*;
- import java.awt.*;
- import dSoundTestFrame;
- import java.applet.*;
- import java.awt.*;
- import java.awt.peer.ComponentPeer;
- import com.ms.com.*;
- import com.ms.directX.*;
- import com.ms.awt.peer.*;
- import java.net.URL;
- import java.net.MalformedURLException;
- import dSoundLIB;
-
-
- //==============================================================================
- // Main Class for applet dSoundTest
- //
- //==============================================================================
- public class dSoundTest extends Applet implements Runnable,
- ddConstants,
- IDSEnumCallback
- {
- private dSoundLIB m_dsLib;
- private int m_hWnd;
- private TextArea m_TextArea = null;
- private dSoundTestFrame m_DebugFrame;
-
- // THREAD SUPPORT:
- // m_dSoundTest is the Thread object for the applet
- //--------------------------------------------------------------------------
- Thread m_dSoundTest = null;
-
- // STANDALONE APPLICATION SUPPORT:
- // m_fStandAlone will be set to true if applet is run standalone
- //--------------------------------------------------------------------------
- boolean m_fStandAlone = false;
-
- // STANDALONE APPLICATION SUPPORT
- // The main() method acts as the applet's entry point when it is run
- // as a standalone application. It is ignored if the applet is run from
- // within an HTML page.
- //--------------------------------------------------------------------------
- public static void main(String args[])
- {
- // Create Toplevel Window to contain applet dSoundTest
- //----------------------------------------------------------------------
- dSoundTestFrame frame = new dSoundTestFrame("dSoundTest");
-
- // Must show Frame before we size it so insets() will return valid values
- //----------------------------------------------------------------------
- frame.show();
- frame.hide();
- frame.resize(frame.insets().left + frame.insets().right + 320,
- frame.insets().top + frame.insets().bottom + 240);
-
-
- // The following code starts the applet running within the frame window.
- // It also calls GetParameters() to retrieve parameter values from the
- // command line, and sets m_fStandAlone to true to prevent init() from
- // trying to get them from the HTML page.
- //----------------------------------------------------------------------
- dSoundTest applet_dSoundTest = new dSoundTest();
-
- frame.add("Center", applet_dSoundTest);
-
- applet_dSoundTest.m_fStandAlone = true;
- applet_dSoundTest.init();
- applet_dSoundTest.start();
- frame.show();
- }
-
- //------------------------------------------------
-
- private void DebugMsg (String s)
- {
- String ss;
-
- ss = "dSoundTest " + s;
- System.out.println (ss);
- if (m_TextArea != null)
- m_TextArea.appendText (ss + "\n");
-
- // this will make those pesky numbers update to show it's
- // still alive...
- repaint();
- }
-
- // dSoundTest Class Constructor
- //--------------------------------------------------------------------------
- public dSoundTest()
- {
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: dSoundTest\r\n" +
- "Author: Microsoft\r\n" +
- "Created with Microsoft Visual J++ Version 1.0";
- }
-
- //--------------------------
-
- public String GetFullFileName (String rawFileName)
- {
- String file;
-
- try
- {
- URL url = new URL( getDocumentBase(), rawFileName);
-
- file = url.getFile();
- char c0 = file.charAt(0);
- char c2 = file.charAt(2);
- if( (c0 == '/') && (c2 == ':') )
- {
- char ch[] = new char[file.length()-1];
- file.getChars(1,file.length(),ch,0);
- file = new String(ch);
- }
- }
- catch(MalformedURLException me)
- {
- DebugMsg ("GetFullFileName caught exception \n " + me);
- file = new String (rawFileName);
- }
- catch (Exception e)
- {
- DebugMsg ("GetFullFileName caught exception \n " + e);
- file = new String (rawFileName);
- }
-
- return file;
- }
-
- //--------------------------------
-
- public void CreateDebugWindow (int nWidth, int nHeight)
- {
- m_DebugFrame = new dSoundTestFrame ("dSoundTest Msgs");
- m_DebugFrame.resize (nWidth, nHeight);
-
- m_TextArea = new TextArea (nWidth, nHeight);
- m_DebugFrame.add ("Center", m_TextArea);
- m_DebugFrame.show();
- }
-
- // The init() method is called by the AWT when an applet is first loaded or
- // reloaded. Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading images or
- // fonts, creating frame windows, setting the layout manager, or adding UI
- // components.
- //--------------------------------------------------------------------------
- public void init()
- {
- resize (320, 240);
-
- if (m_fStandAlone)
- CreateDebugWindow (300, 400);
-
- //
- // do some stuff to get the handle to our window (frame) for
- // use when creating dSound object.
- ComponentPeer p = getPeer();
- ComponentPeerX peer = (ComponentPeerX)p;
- if (peer == null)
- {
- DebugMsg ("Cant retrieve peer!");
- return;
- }
-
- m_hWnd = peer.gethwnd();
- try
- {
- m_dsLib = new dSoundLIB (m_hWnd, DSSCL_NORMAL);
- }
- catch (Exception e)
- {
- ErrorExit ("m_dsLib exception \n" + e, 1);
- }
-
- String sFile = new String ("walk.wav");
-
- //
- // URL throws an exception when we try to get document base if
- // we're not running from a browser (ie. JVIEW dSoundTest).
- // Otherwise we need to find the path to the wave file.
- if (! m_fStandAlone)
- sFile = GetFullFileName (sFile);
-
- if (! m_dsLib.PlayWave (sFile, true))
- ErrorExit ("Can't play wave file [" + sFile + "]", 3);
- }
-
- //--------------------------------------------------------------------------
-
- public void ErrorExit (String s, int nExitValue)
- {
- DebugMsg ("FatalError: [" + s + "]");
- if(m_fStandAlone)
- System.exit (nExitValue);
- }
-
- // Place additional applet clean up code here. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- }
-
- // dSoundTest Paint Handler
- //--------------------------------------------------------------------------
- public void paint(Graphics g)
- {
- if (! m_fStandAlone)
- {
- g.drawString("For debug output, see ", 10, 20);
- g.drawString("<windows dir>\\java\\MSJava.txt..." , 10, 40);
- }
-
- g.drawString("Note: this window needs focus in ", 10, 100);
- g.drawString(" order to hear the sound.", 10, 120);
- g.drawString("Running (sound should be playing): " + Math.random(), 10, 140);
- }
-
- // The start() method is called when the page containing the applet
- // first appears on the screen. The AppletWizard's initial implementation
- // of this method starts execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void start()
- {
- if (m_dSoundTest == null)
- {
- m_dSoundTest = new Thread(this);
- m_dSoundTest.start();
- }
- }
-
- // The stop() method is called when the page containing the applet is
- // no longer on the screen. The AppletWizard's initial implementation of
- // this method stops execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void stop()
- {
- if (m_dSoundTest != null)
- {
- m_dSoundTest.stop();
- m_dSoundTest = null;
- }
- }
-
- // THREAD SUPPORT
- // The run() method is called when the applet's thread is started. If
- // your applet performs any ongoing activities without waiting for user
- // input, the code for implementing that behavior typically goes here. For
- // example, for an applet that performs animation, the run() method controls
- // the display of images.
- //--------------------------------------------------------------------------
- public void run()
- {
- // Test some basic dSOund api's...
- //
- // Note, if you just wanted to play a wave file, all you'd need is the
- // code in the Init()...
- TestDSoundMethods();
- DebugMsg (" ");
- TestDSoundBufferMethods();
-
- while (true)
- {
- try
- {
- repaint();
- Thread.sleep(50);
- }
- catch (InterruptedException e)
- {
- stop();
- }
- }
- }
-
- //---------------------------------------
-
- public void callbackDSEnum(GuidData g, String Description,
- String mod, IUnknown lpContext)
- {
- DebugMsg (" DSEnum: [" + Description + ", " + mod + "]");
- }
-
- //---------------------------------------
-
- public void TestDSoundMethods()
- //
- // call some dSound api's to make sure things are working...
- {
- dSound ds;
-
- DebugMsg ("TestDSoundMethods =================");
- try
- {
- // Get the initialized dSound object from the library
- ds = m_dsLib.GetDSoundObject();
-
-
- // Enumerate devices on system
- DebugMsg ("TEST: DSEnumCallback...");
- ds.DSEnumerate ((IDSEnumCallback)this, (IUnknown)null);
-
-
- // TEST compact the sound card memory
- DebugMsg ("TEST: Compact()");
- ds.SetCooperativeLevel (m_hWnd, DSSCL_EXCLUSIVE);
- ds.Compact();
- ds.SetCooperativeLevel (m_hWnd, DSSCL_NORMAL);
-
- // TEST speaker config
- DebugMsg ("TEST: Get/SetSpeakerConfig()");
- int nSpeakerConfig[] = new int [1];
- ds.SetSpeakerConfig (DSSPEAKER_HEADPHONE);
- ds.GetSpeakerConfig (nSpeakerConfig);
- if (nSpeakerConfig[0] != DSSPEAKER_HEADPHONE)
- DebugMsg ("Get/Set speaker config failed");
-
-
- // TEST GetCaps()
- DebugMsg ("TEST: GetCaps()");
- DSCaps dsCaps = new DSCaps();
- dsCaps.dwSize = m_dsLib.sizeofDSCAPS;
- ds.GetCaps (dsCaps);
- DebugMsg (" dsCaps.dwFreeHwMemBytes = " + dsCaps.dwFreeHwMemBytes);
- DebugMsg (" dsCaps.dwFreeHwMixingAllBuffers = " + dsCaps.dwFreeHwMixingAllBuffers);
- DebugMsg (" dsCaps.dwPrimaryBuffers = " + dsCaps.dwPrimaryBuffers);
- }
- catch (Exception e)
- {
- DebugMsg ("TestDSoundMethods, caught exception \n " + e);
- stop();
- }
- }
-
- //---------------------------------------
-
- public void TestDSoundBufferMethods()
- //
- // * call some dSound api's to make sure things are working...
- // * assumes there's a wave file playing on LOOP mode.
- //
- {
- DebugMsg ("TestDSoundBufferMethods =================");
- try
- {
- // Get the initialized dSound objects from the library
- dSoundBuffer dsBuffer = m_dsLib.GetDSoundBufferObject();
- dSound ds = m_dsLib.GetDSoundObject();
-
- // TEST GetCaps()
- DebugMsg ("TEST: GetCaps()");
- DSBCaps dsbCaps = new DSBCaps();
- dsbCaps.dwSize = m_dsLib.sizeofDSBCAPS;
- dsBuffer.GetCaps (dsbCaps);
- DebugMsg (" dsbCaps.dwBufferBytes = " + dsbCaps.dwBufferBytes);
- DebugMsg (" dsbCaps.dwUnlockTransferRate = " + dsbCaps.dwUnlockTransferRate);
- DebugMsg (" dsbCaps.dwPlayCpuOverhead = " + dsbCaps.dwPlayCpuOverhead );
-
- // TEST GetCurrentPosition()
- DebugMsg ("TEST: GetCurrentPosition()");
- DSCursors dsCursors = new DSCursors();
-
- dsBuffer.GetCurrentPosition (dsCursors);
- DebugMsg (" dsCursors.dwPlay = " + dsCursors.dwPlay);
- DebugMsg (" dsCursors.dwWrite = " + dsCursors.dwWrite);
-
-
- // TEST GetFormatSize()
- DebugMsg ("TEST: GetFormatSize()");
- int nSize;
- nSize = dsBuffer.GetFormatSize ();
- DebugMsg (" GetFormatSize = " + nSize);
-
-
- // TEST GetFormat()
- // note: GetFormat() returns a new WaveFormatEx();
- DebugMsg ("TEST: GetFormat()");
- WaveFormatEx waveForm;
-
- waveForm = dsBuffer.GetFormat();
- DebugMsg (" waveForm.nChannels = " + waveForm.nChannels);
- DebugMsg (" waveForm.wBitsPerSample = " + waveForm.wBitsPerSample);
- DebugMsg (" waveForm.nSamplesPerSec = " + waveForm.nSamplesPerSec);
-
-
- // let users get used to current volume level
- Wait (5);
-
- // TEST GetVolume()
- DebugMsg ("TEST: GetVolume()");
- int nTemp = 10;
- nTemp = dsBuffer.GetVolume();
- DebugMsg ("TEST: GetVolume() = " + nTemp);
-
-
- // Test SetVolume()
- DebugMsg (" Setting volume to 1/2");
-
- // NOTE: units are in hundreths of decibels
- dsBuffer.SetVolume (nTemp - 1000);
- Wait (6);
- DebugMsg (" Restoring volume to original level");
- dsBuffer.SetVolume (nTemp);
-
- // Test GetPan()
- DebugMsg ("TEST: GetPan()");
- nTemp = dsBuffer.GetPan();
- DebugMsg ("TEST: GetPan() = " + nTemp);
-
- // Test SetPan()
- DebugMsg (" Setting pan from LEFT to RIGHT....");
- for (int nPan = -10000; nPan < 10000; nPan += 1000)
- {
- dsBuffer.SetPan (nPan);
- Wait (1);
- }
-
- DebugMsg (" Restoring original pan setting.... " + nTemp);
- dsBuffer.SetPan (nTemp);
-
-
- // Test GetFrequency()
- DebugMsg ("TEST: GetFrequency()");
- nTemp = dsBuffer.GetFrequency();
- DebugMsg ("TEST: GetFrequency() = " + nTemp);
-
- // Test SetFrequency(); note upper limit is 100000
- DebugMsg (" Setting Frequency from 100 to 50000....");
- for (int nFreq = 100; nFreq < 50000; nFreq += 3000)
- {
- dsBuffer.SetFrequency (nFreq);
- Wait (2);
- }
-
- DebugMsg (" Restoring original frequency setting.... " + nTemp);
- dsBuffer.SetFrequency (nTemp);
-
-
- // Test: GetStatus
- nTemp = dsBuffer.GetStatus();
- DebugMsg ("TEST: GetStatus() = " + nTemp);
-
-
- // Test: Stop
- DebugMsg ("TEST: Stop()");
- m_dsLib.StopWave();
- DebugMsg (" wave playback is stopped now...");
-
-
- // Test: SetCurrentPosition. Wave buffer positions are 0 to maxBufferSize
- DebugMsg ("TEST: SetCurrentPosition()");
- int nBufferSize = m_dsLib.GetBufferSize();
- DebugMsg (" BufferSize = " + nBufferSize);
-
- //
- // play the wave file starting from different positions within
- // the wave file.
- int nPos;
- for (int n = 0; n < 5; n++)
- {
- nPos = (int)((double)nBufferSize * Math.random());
- DebugMsg (" Setting current position to: " + nPos);
- dsBuffer.SetCurrentPosition (nPos);
- m_dsLib.PlayWave (0);
- while (dsBuffer.GetStatus() == DSBSTATUS_PLAYING)
- ;
-
- Wait (3);
- }
-
-
- Wait (2);
- m_dsLib.PlayWave (DSBPLAY_LOOPING);
- DebugMsg ("Normal wave playback has resumed now, \n end of test...");
- }
- catch (Exception e)
- {
- DebugMsg ("TestDSoundBufferMethods, caught exception \n " + e);
- stop();
- }
- }
-
- //--------------------------------------------------
-
- public void Wait (int nLoopCounter)
- {
- try
- {
- Thread.sleep (nLoopCounter * 500);
- }
- catch (InterruptedException e)
- {
- DebugMsg ("Wait: caught exception \n " + e);
- }
- }
- }
-