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 / dSoundLIB.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  6.0 KB  |  234 lines

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