home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / DirectX / dsound / DSoundLIB.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  5.7 KB  |  227 lines

  1. // 
  2. // 
  3. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  4. // 
  5. //  File: DirectSoundLIB.java
  6. //          
  7. //  Description: Class does basic play, stop of wav files. 
  8. //               See DirectSoundTest.java for sample usage...   
  9. // 
  10. import java.awt.*;
  11. import com.ms.com.*;
  12. import com.ms.directX.*;
  13.  
  14. class DSoundLIB implements DirectXConstants
  15. {
  16.    // hidden class variablez
  17.    private DirectSound            ds;
  18.    private DirectSoundBuffer    m_dsBuffer;
  19.    private DSBufferDesc            m_dsBufferDesc = null;
  20.    private DSResourceDesc        m_dsrDesc;
  21.  
  22.    private boolean                m_bSoundLoaded;
  23.    private WaveFormatEx            m_waveFormat;
  24.    private DirectSoundResource    m_dsr;
  25.    private int                    m_PlayLoopFlag;
  26.  
  27.    protected void finalize()
  28.    {
  29.         ComLib.release(m_dsBuffer);
  30.         ComLib.release(ds);
  31.    }
  32.  
  33.    //---------------------------------------
  34.    //---------------------------------------
  35.  
  36.    private void DebugMsg (String s)
  37.    {
  38.       System.out.println ("DirectSoundLIB: " + s);
  39.    }
  40.  
  41.    //---------------------------------------
  42.    
  43.    public DirectSound GetDirectSoundObject()
  44.    {
  45.       return ds;
  46.    }
  47.  
  48.    //---------------------------------------
  49.    
  50.    public DirectSoundResource GetDirectSoundResourceObject()
  51.    {
  52.       return m_dsr;
  53.    }
  54.  
  55.    //---------------------------------------
  56.    
  57.    public DirectSoundBuffer GetDirectSoundBufferObject()
  58.    {
  59.       return m_dsBuffer;
  60.    }
  61.    
  62.    //---------------------------------------
  63.    
  64.    public int GetBufferSize()
  65.    {
  66.       return m_dsBufferDesc.bufferBytes;
  67.    }
  68.    
  69.    //---------------------------------------
  70.  
  71.    public DSoundLIB (Component c, int nLevel) throws Exception
  72.    {
  73.       m_bSoundLoaded = false;
  74.       
  75.       try 
  76.       {
  77.          DebugMsg ("Calling new DirectSound()....");
  78.          DebugMsg ("   Note: If your app stops here, you need to register DX3J.DLL");
  79.          ds = new DirectSound();
  80.  
  81.          // note: if there is no sound device available, this will throw an 
  82.          // exception.
  83.          DebugMsg ("Calling SetCooperativeLevel " + nLevel);
  84.          DebugMsg ("   Note: If your app stops here, you don't have a sound device");
  85.          try
  86.          {
  87.             ds.setCooperativeLevel (c, nLevel);
  88.          }
  89.          catch (Exception e)
  90.          {
  91.             throw new Exception ("No Sound Card: SetCooperativeLevel() failed");
  92.          }
  93.       }
  94.       catch (Exception e)
  95.       {
  96.          DebugMsg ("Constructor caught exception \n   " + e);
  97.          throw new Exception ("DSLIB: " + e);
  98.       }                            
  99.       
  100.       DebugMsg ("Constructor finished");
  101.    }
  102.  
  103.    //---------------------------------------
  104.  
  105.    public boolean PlayWave (String sFilename, boolean bLoop)
  106.    {
  107.       DebugMsg ("PlayWave [" + sFilename + "]");
  108.       if (! LoadWaveFile (sFilename))
  109.          return false;
  110.  
  111.       // save the play flag for use w/ StopWave() below
  112.       if (bLoop)
  113.          m_PlayLoopFlag = DSBPLAY_LOOPING;
  114.       else
  115.          m_PlayLoopFlag = 0;
  116.  
  117.       try 
  118.       {
  119.          m_dsBuffer.play (m_PlayLoopFlag);
  120.       }
  121.       catch (Exception e)
  122.       {
  123.          DebugMsg ("DirectSoundBuffer::Play() threw Exception \n" + e);
  124.          return false;
  125.       }
  126.  
  127.       return true;
  128.    }
  129.  
  130.    //---------------------------------------
  131.    
  132.    public boolean PlayWave ()
  133.       // 
  134.       // this one restarts a wave file previously stopped with StopWave()
  135.       // 
  136.    {
  137.       try 
  138.       {
  139.          m_dsBuffer.play (m_PlayLoopFlag);
  140.       }
  141.       catch (Exception e)
  142.       {
  143.          DebugMsg ("DirectSoundBuffer::Play() threw Exception \n" + e);
  144.          return false;
  145.       }
  146.  
  147.       return true;
  148.    }
  149.    
  150.    //---------------------------------------
  151.    
  152.    public boolean PlayWave (int nFlag)
  153.       // 
  154.       // this one plays a wave file with the nFlag play parameter.
  155.       // 
  156.    {
  157.       try 
  158.       {
  159.          m_dsBuffer.play (nFlag);
  160.       }
  161.       catch (Exception e)
  162.       {
  163.          DebugMsg ("DirectSoundBuffer::Play() threw Exception \n" + e);
  164.          return false;
  165.       }
  166.  
  167.       return true;
  168.    }
  169.    
  170.    //---------------------------------------
  171.    
  172.    public boolean StopWave()
  173.    {
  174.       try
  175.       {
  176.          m_dsBuffer.stop();
  177.       }
  178.       catch (Exception e)
  179.       {
  180.          DebugMsg ("DirectSoundBuffer::Stop() threw Exception \n" + e);
  181.          return false;
  182.       }
  183.  
  184.       return true;
  185.    }
  186.    
  187.    //---------------------------------------
  188.    
  189.    public boolean LoadWaveFile (String sFilename)
  190.  
  191.    {
  192.       DebugMsg ("LoadWaveFile ============");
  193.  
  194.       try 
  195.       {
  196.          m_dsrDesc    = new DSResourceDesc();
  197.          m_dsr        = new DirectSoundResource();
  198.          
  199.          // this guy returns us the size of the wave file in 
  200.          // dwBufferBytes
  201.          m_waveFormat = m_dsr.loadWaveFile (sFilename, m_dsrDesc);
  202.  
  203.          // create the sound buffer
  204.          m_dsBufferDesc                = new DSBufferDesc();
  205.  
  206.          // need to specify DSBCAPS_CTRLDEFAULT so you can control the
  207.          //    volume, pan, frequency.
  208.          m_dsBufferDesc.flags        = DSBCAPS_STATIC | DSBCAPS_CTRLDEFAULT;
  209.          m_dsBufferDesc.bufferBytes  = m_dsrDesc.bufferBytes;
  210.  
  211.          m_dsBuffer = ds.createSoundBuffer (m_dsBufferDesc, m_waveFormat);
  212.    
  213.          // get the sound data from the internal memory from LoadWaveFile
  214.          //  to the sound buffer.  The flags are the same as those used 
  215.          //  for Lock()
  216.          m_dsBuffer.transferToSoundBuffer (m_dsrDesc, 0);
  217.          
  218.          return m_bSoundLoaded = true;
  219.       }
  220.       catch (Exception e)
  221.       {
  222.          DebugMsg ("LoadWaveFile caught exception \n   " + e);
  223.          return false;
  224.       }
  225.    }
  226. }
  227.