Advertisement
Guest User

Untitled

a guest
Oct 6th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. public struct WavHeader
  2.     {
  3.         public int chunkId;
  4.         public int chunkSize;
  5.         public int format;
  6.         public int subchunkid;
  7.         public int subchunksize;
  8.         public short audioFormat;
  9.         public short numChannels;
  10.         public int sampleRate;
  11.         public int byteRate;
  12.         public short blockAlign;
  13.         public short bitsPerSample;
  14.         public int subchunk2Id;
  15.         public int subchunk2Size;
  16.     }
  17.  
  18.     public enum AudioType
  19.     {
  20.         Music,
  21.         Effect
  22.     }
  23.  
  24.     public sealed class AudioStream
  25.     {
  26.         private SecondarySoundBuffer buffer;
  27.  
  28.         public AudioType Type
  29.         {
  30.             get;
  31.             set;
  32.         }
  33.  
  34.         public float Volume
  35.         {
  36.             get
  37.             {
  38.                 return buffer.Volume;
  39.             }
  40.             set
  41.             {
  42.                 buffer.Volume = (int)(value * -2000.0f);
  43.             }
  44.         }
  45.  
  46.         public bool IsPlaying
  47.         {
  48.             get
  49.             {
  50.                 return buffer.Status == (int)BufferStatus.Playing;
  51.             }
  52.         }
  53.  
  54.         public AudioStream()
  55.         {
  56.  
  57.         }
  58.  
  59.         public void Play()
  60.         {
  61.             buffer.Play(0, PlayFlags.None);
  62.         }
  63.  
  64.         public void Stop()
  65.         {
  66.             buffer.Stop();
  67.         }
  68.  
  69.         public void Upload(int sampleRate, int align, int channels, byte[] data, int size)
  70.         {
  71.             if(data != null)
  72.             {
  73.                 if (buffer != null)
  74.                     buffer.Dispose();
  75.  
  76.                 SoundBufferDescription desc = new SoundBufferDescription();
  77.                 desc.Flags = BufferFlags.Software | BufferFlags.ControlVolume;
  78.                 desc.Format = new SharpDX.Multimedia.WaveFormat(sampleRate, align, channels);
  79.                 desc.BufferBytes = size;
  80.                 buffer = new SecondarySoundBuffer(Game.Current.AudioManager.ds, desc);
  81.  
  82.                 DataStream ds2 = null;
  83.                 DataStream strm = buffer.Lock(0, size, LockFlags.EntireBuffer, out ds2);
  84.                 strm.WriteRange<byte>(data, 0, size);
  85.                 buffer.Unlock(strm, ds2);
  86.                
  87.             }
  88.         }
  89.  
  90.         public unsafe static AudioStream LoadWav(System.IO.Stream strm)
  91.         {
  92.             BinaryReader reader = new BinaryReader(strm);
  93.             WavHeader hdr = new WavHeader()
  94.             {
  95.                 chunkId = reader.ReadInt32(),
  96.                 chunkSize = reader.ReadInt32(),
  97.                 format = reader.ReadInt32(),
  98.                 subchunkid = reader.ReadInt32(),
  99.                 subchunksize = reader.ReadInt32(),
  100.                 audioFormat = reader.ReadInt16(),
  101.                 numChannels = reader.ReadInt16(),
  102.                 sampleRate = reader.ReadInt32(),
  103.                 byteRate = reader.ReadInt32(),
  104.                 blockAlign = reader.ReadInt16(),
  105.                 bitsPerSample = reader.ReadInt16(),
  106.                 subchunk2Id = reader.ReadInt32(),
  107.                 subchunk2Size = reader.ReadInt32()
  108.             };
  109.  
  110.             byte[] data = new byte[strm.Length - sizeof(WavHeader)];
  111.             reader.Read(data, 0, data.Length);
  112.  
  113.             AudioStream stream = new AudioStream();
  114.             stream.Upload(hdr.sampleRate, hdr.bitsPerSample, hdr.numChannels, data, data.Length);
  115.  
  116.             return stream;
  117.         }
  118.  
  119.         public unsafe static AudioStream LoadVorbis(System.IO.Stream strm)
  120.         {
  121.             Vorbis.Vorbis vorbisStrm = new Vorbis.Vorbis(strm);
  122.  
  123.             return null;
  124.         }
  125.     }
  126.  
  127.     public sealed class AudioManager
  128.     {
  129.         internal DirectSound ds;
  130.         private PrimarySoundBuffer buffer;
  131.  
  132.         internal AudioManager()
  133.         {
  134.             ds = new DirectSound();
  135.             ds.SetCooperativeLevel(Game.Current.Form.Handle, CooperativeLevel.Normal);
  136.  
  137.             SoundBufferDescription desc = new SoundBufferDescription();
  138.             desc.Flags = BufferFlags.PrimaryBuffer;
  139.             desc.Format = null;
  140.             buffer = new PrimarySoundBuffer(ds, desc);
  141.         }
  142.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement