SoundClass.cs :  » Game » Killer-Instinct » KillerInstinct » DirectX » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » Killer Instinct 
Killer Instinct » KillerInstinct » DirectX » SoundClass.cs
using System;
using Microsoft.DirectX.DirectSound;
using BufferMicrosoft.DirectX.DirectSound.SecondaryBuffer;
using Microsoft.DirectX;
using Global;
using Global.GlobalClass;
using System.IO;
using System.Windows.Forms;

namespace KillerInstinct.DirectX{
  public class SoundClass
  {
    // EFFECTS CONSTANTS /////////////////////////////////////////////////////////////////
    public const string SOUND_GAMESTART    = CONST.DIR_SOUND + "GameStart.wav";
    public const string SOUND_GAMEEXIT    = CONST.DIR_SOUND + "GameExit.wav";
    public const string SOUND_SHOT      = CONST.DIR_SOUND + "Shot";
    public const string SOUND_SHOTEMPTY    = CONST.DIR_SOUND + "ShotEmpty.wav";
    public const string SOUND_EXPLOSION     = CONST.DIR_SOUND + "Explosion.wav";
    public const string SOUND_WIND          = CONST.DIR_SOUND + "Wind.wav";
    public const string SOUND_ALTHIT        = CONST.DIR_SOUND + "AltHit.wav";
    public const string SOUND_MENUNEXT      = CONST.DIR_SOUND + "MenuNext.wav";
    public const string SOUND_MENUSELECT  = CONST.DIR_SOUND + "MenuSelect.wav";
    public const string SOUND_LOWENERGY     = CONST.DIR_SOUND + "LowEnergy.wav";
    public const string SOUND_POWERUP    = CONST.DIR_SOUND + "Powerup";
    public const string SOUND_LOSER1    = CONST.DIR_SOUND + "loser1.wav";
    // VOICE CONSTANTS ///////////////////////////////////////////////////////////////////
    public const string VOICE_AWCRAP    = CONST.DIR_VOICE + "aw_crap.wav";
    public const string VOICE_BREATHIN    = CONST.DIR_VOICE + "breathe_in.wav";
    public const string VOICE_BREATHOUT    = CONST.DIR_VOICE + "breathe_out.wav";
    public const string VOICE_COMEONYO    = CONST.DIR_VOICE + "comeon_yo.wav";
    public const string VOICE_FEELSGOOD    = CONST.DIR_VOICE + "it_feels_so_good.wav";
    public const string VOICE_NOWORD    = CONST.DIR_VOICE + "no_word_bout_it.wav";
    public const string VOICE_STEREOBENEFITS= CONST.DIR_VOICE + "stereo_benefits.wav";
    public const string VOICE_STOPPED    = CONST.DIR_VOICE + "stopped.wav";
    public const string VOICE_STUPID    = CONST.DIR_VOICE + "stupid.wav";
    public const string VOICE_SUCK      = CONST.DIR_VOICE + "suck_my.wav";
    //...
    private SecondaryBuffer    ApplicationBuffer = null;
    private Device        SoundDevice = null;
    public  bool        bMute;
    private int          Volume;
    private Control        Window;
    public SoundClass(Control window, bool mute, int volume)
    {
      Window = window;
      bMute = mute;
      Volume = volume;
      SoundDevice = new Device();
      SoundDevice.SetCooperativeLevel(Window, CooperativeLevel.Priority);
    }
    // returns a random resource file (.WAV) based on searchText
    public static string GetRandomSound(string searchText)
    {
      global.dbg.Out(Debug.DBG2, "GetRandomSound searching for [" + searchText + "].");
      string resourceName = "";
      try
      {
        // get array of Tank*.bmp files
        string[] files = Directory.GetFiles(CONST.DIR_SOUND, searchText);
        if (files.Length > 0)
        {
          global.dbg.Out(Debug.DBG2, "Found [" + files.Length + "] resource files.");
          // return random resource file name, e.g. ".\Bitmaps\Win1.bmp"
          resourceName = files[global.random(0, files.Length-1)];
          global.dbg.Out(Debug.DBG2, "Using random resource file from resource: [" + resourceName + "]");
        }
        else
        {
          global.dbg.Out(Debug.ERR, "No resources were found to load.");
        }
      }
      catch (Exception e)
      {
        global.dbg.Out(Debug.ERR, "An error occurred trying to read list of resources: " + e);
        MessageBox.Show("An error occurred trying to read list of resources: " + e, "Unable to find resources");
      }
      return (resourceName);
    }
    public void PlayRandomIntro()
    {
      global.SC.Play(GetRandomSound("Intro*.wav"), true);
    }
    public void Play(string file, bool loop)
    {
      if (!bMute)
      {
        if (File.Exists(file))
        {
          //DirectXException.IgnoreExceptions();

          BufferDescription desc = new BufferDescription();
          //desc.ControlFrequency  = true;
          //desc.ControlPan = true;
          //desc.ControlVolume = true;
          ApplicationBuffer = new SecondaryBuffer(file, desc, SoundDevice);
          BufferPlayFlags PlayFlags  = loop ? BufferPlayFlags.Looping : 0;
          if (null != ApplicationBuffer)
            ApplicationBuffer.Play(0, PlayFlags);

          //DirectXException.EnableExceptions();
        }
        else
          global.dbg.Out(Debug.ERR, "Cannot open file " + file);
      }
    }
    public void Stop()
    {
      if (!bMute)
      {
        if (null != ApplicationBuffer)
        {
          global.dbg.Out(Debug.DBG2, "SOUND stop");
          //ApplicationBuffer.Stop();
          //ApplicationBuffer.SetCurrentPosition(0);
          //ApplicationBuffer.Dispose();
          //ApplicationBuffer = null;
          SoundDevice.Dispose();
          SoundDevice = new Device();
          SoundDevice.SetCooperativeLevel(Window, CooperativeLevel.Normal);
        }
      }
    }
    public void ChangeVolume(int diff)
    {
      if (!bMute)
      {
        /*if ((Volume+diff <= 0) &&  (Volume+diff >= -50))
          Volume += diff;
        ApplicationBuffer.Volume = Volume * 100;*/
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.