home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-10 | 6.0 KB | 234 lines |
- //
- //
- // Copyright (C) 1996 Microsoft Corporation. All Rights Reserved.
- //
- // File: dSoundLIB.java
- //
- // Description: Class does basic play, stop of wav files.
- // See dSoundTest.java for sample usage...
- //
-
-
- import java.awt.*;
- import com.ms.com.*;
- import com.ms.directX.*;
-
- class dSoundLIB implements ddConstants
- {
- // got these sizeof() values from a C++ windows app
- public final int sizeofWAVEFORMAT = 14;
- public final int sizeofWAVEFORMATEX = 18;
- public final int sizeofDSBUFFERDESC = 20;
- public final int sizeofDSCAPS = 96;
- public final int sizeofDSBCAPS = 20;
-
-
- // hidden class variablez
- private dSound ds;
- private dSoundBuffer m_dsBuffer;
- private DSBufferDesc m_dsBufferDesc = null;
- private DSResourceDesc m_dsrDesc;
-
- private boolean m_bSoundLoaded;
- private WaveFormatEx m_waveFormat;
- private dSoundResource m_dsr;
- private int m_PlayLoopFlag;
-
- //---------------------------------------
- //---------------------------------------
-
- private void DebugMsg (String s)
- {
- System.out.println ("dSoundLIB: " + s);
- }
-
- //---------------------------------------
-
- public dSound GetDSoundObject()
- {
- return ds;
- }
-
- //---------------------------------------
-
- public dSoundResource GetDSoundResourceObject()
- {
- return m_dsr;
- }
-
- //---------------------------------------
-
- public dSoundBuffer GetDSoundBufferObject()
- {
- return m_dsBuffer;
- }
-
- //---------------------------------------
-
- public int GetBufferSize()
- {
- return m_dsBufferDesc.dwBufferBytes;
- }
-
- //---------------------------------------
-
- public dSoundLIB (int hWnd, int nLevel) throws Exception
- // constructor. Note: if this fails, it will throw an Exception
- {
- DebugMsg ("constructor: hWnd: " + hWnd + " coopLevel: " + nLevel);
- m_bSoundLoaded = false;
-
- try
- {
- DebugMsg ("Calling new dSound()....");
- DebugMsg (" Note: If your app stops here, you need to register DIRECT.DLL");
- ds = new dSound();
-
- // note: if there is no sound device available, this will throw an
- // exception.
- DebugMsg ("Calling SetCooperativeLevel " + nLevel);
- DebugMsg (" Note: If your app stops here, you don't have a sound device");
- try
- {
- ds.SetCooperativeLevel (hWnd, nLevel);
- }
- catch (Exception e)
- {
- throw new Exception ("dSoundLIB: SetCooperativeLevel() failed");
- }
- }
- catch (Exception e)
- {
- DebugMsg ("Constructor caught exception \n " + e);
- throw new Exception ("dSoundLIB: " + e);
- }
-
- DebugMsg ("Constructor finished");
- }
-
- //---------------------------------------
-
- public boolean PlayWave (String sFilename, boolean bLoop)
- {
- DebugMsg ("PlayWave [" + sFilename + "]");
- if (! LoadWaveFile (sFilename))
- return false;
-
- // save the play flag for use w/ StopWave() below
- if (bLoop)
- m_PlayLoopFlag = DSBPLAY_LOOPING;
- else
- m_PlayLoopFlag = 0;
-
- try
- {
- m_dsBuffer.Play (0, 0, m_PlayLoopFlag);
- }
- catch (Exception e)
- {
- DebugMsg ("dSoundBuffer::Play() threw Exception \n" + e);
- return false;
- }
-
- return true;
- }
-
- //---------------------------------------
-
- public boolean PlayWave ()
- //
- // this one restarts a wave file previously stopped with StopWave()
- //
- {
- try
- {
- m_dsBuffer.Play (0, 0, m_PlayLoopFlag);
- }
- catch (Exception e)
- {
- DebugMsg ("dSoundBuffer::Play() threw Exception \n" + e);
- return false;
- }
-
- return true;
- }
-
- //---------------------------------------
-
- public boolean PlayWave (int nFlag)
- //
- // this one plays a wave file with the nFlag play parameter.
- //
- {
- try
- {
- m_dsBuffer.Play (0, 0, nFlag);
- }
- catch (Exception e)
- {
- DebugMsg ("dSoundBuffer::Play() threw Exception \n" + e);
- return false;
- }
-
- return true;
- }
-
- //---------------------------------------
-
- public boolean StopWave()
- {
- try
- {
- m_dsBuffer.Stop();
- }
- catch (Exception e)
- {
- DebugMsg ("dSoundBuffer::Stop() threw Exception \n" + e);
- return false;
- }
-
- return true;
- }
-
- //---------------------------------------
-
- public boolean LoadWaveFile (String sFilename)
-
- {
- DebugMsg ("LoadWaveFile ============");
-
- try
- {
- m_dsrDesc = new DSResourceDesc();
- m_dsr = new dSoundResource();
-
- // this guy returns us the size of the wave file in
- // dwBufferBytes
- m_waveFormat = m_dsr.LoadWaveFile (sFilename, m_dsrDesc);
-
- // create the sound buffer
- m_dsBufferDesc = new DSBufferDesc();
- m_dsBufferDesc.dwSize = sizeofDSBUFFERDESC;
-
- // need to specify DSBCAPS_CTRLDEFAULT so you can control the
- // volume, pan, frequency.
- m_dsBufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_CTRLDEFAULT;
- m_dsBufferDesc.dwBufferBytes = m_dsrDesc.dwBufferBytes;
-
- m_dsBuffer = ds.CreateSoundBuffer (m_dsBufferDesc, m_waveFormat, null);
-
- // get the sound data from the internal memory from LoadWaveFile
- // to the sound buffer. The flags are the same as those used
- // for Lock()
- m_dsBuffer.TransferToSoundBuffer (m_dsrDesc, 0);
-
- return m_bSoundLoaded = true;
- }
- catch (Exception e)
- {
- DebugMsg ("LoadWaveFile caught exception \n " + e);
- return false;
- }
- }
- }
-