Exemplo n.º 1
0
        /// <summary>Initialises the WaveOut device</summary>
        /// <param name="waveProvider">WaveProvider to play</param>
        public void Init(IWaveProvider waveProvider)
        {
            if (this.playbackState != PlaybackState.Stopped)
            {
                throw new InvalidOperationException("Can't re-initialize during playback");
            }
            if (this.hWaveOut != IntPtr.Zero)
            {
                this.DisposeBuffers();
                this.CloseWaveOut();
            }
            this.callbackEvent = new AutoResetEvent(false);
            this.waveStream    = waveProvider;
            int      byteSize = waveProvider.WaveFormat.ConvertLatencyToByteSize((this.DesiredLatency + this.NumberOfBuffers - 1) / this.NumberOfBuffers);
            MmResult result;

            lock (this.waveOutLock)
                result = MyInterop.waveOutOpenWindow(out this.hWaveOut, (IntPtr)this.DeviceNumber, this.waveStream.WaveFormat, this.callbackEvent.SafeWaitHandle.DangerousGetHandle(), IntPtr.Zero, MyInterop.WaveInOutOpenFlags.CallbackEvent);
            MmException.Try(result, "waveOutOpen");
            this.buffers       = new MyBuffer[this.NumberOfBuffers];
            this.playbackState = PlaybackState.Stopped;
            for (int index = 0; index < this.NumberOfBuffers; ++index)
            {
                this.buffers[index] = new MyBuffer(this.hWaveOut, byteSize, this.waveStream, this.waveOutLock);
            }
        }
Exemplo n.º 2
0
        /// <summary>Releases resources held by this WaveBuffer</summary>
        protected void Dispose(bool disposing)
        {
            int num1 = disposing ? 1 : 0;

            if (this.hHeader.IsAllocated)
            {
                this.hHeader.Free();
            }
            if (this.hBuffer.IsAllocated)
            {
                this.hBuffer.Free();
            }
            if (this.hThis.IsAllocated)
            {
                this.hThis.Free();
            }
            if (!(this.hWaveOut != IntPtr.Zero))
            {
                return;
            }
            lock (this.waveOutLock)
            {
                int num2 = (int)MyInterop.waveOutUnprepareHeader(this.hWaveOut, this.header, Marshal.SizeOf((object)this.header));
            }
            this.hWaveOut = IntPtr.Zero;
        }
Exemplo n.º 3
0
        public static float GetWaveOutVolume(IntPtr hWaveOut, object lockObject)
        {
            int      dwVolume;
            MmResult volume;

            lock (lockObject)
                volume = MyInterop.waveOutGetVolume(hWaveOut, out dwVolume);
            MmException.Try(volume, "waveOutGetVolume");
            return((float)(dwVolume & (int)ushort.MaxValue) / (float)ushort.MaxValue);
        }
Exemplo n.º 4
0
        private void WriteToWaveOut()
        {
            MmResult result;

            lock (this.waveOutLock)
                result = MyInterop.waveOutWrite(this.hWaveOut, this.header, Marshal.SizeOf((object)this.header));
            if (result != MmResult.NoError)
            {
                throw new MmException(result, "waveOutWrite");
            }
            GC.KeepAlive((object)this);
        }
Exemplo n.º 5
0
 public static long GetPositionBytes(IntPtr hWaveOut, object lockObject)
 {
     lock (lockObject)
     {
         MmTime mmTime = new MmTime();
         mmTime.wType = 4U;
         MmException.Try(MyInterop.waveOutGetPosition(hWaveOut, ref mmTime, Marshal.SizeOf((object)mmTime)), "waveOutGetPosition");
         if (mmTime.wType != 4U)
         {
             throw new Exception(string.Format("waveOutGetPosition: wType -> Expected {0}, Received {1}", (object)4, (object)mmTime.wType));
         }
         return((long)mmTime.cb);
     }
 }
Exemplo n.º 6
0
        /// <summary>Resume playing after a pause from the same position</summary>
        private void Resume()
        {
            if (this.playbackState != PlaybackState.Paused)
            {
                return;
            }
            MmResult result;

            lock (this.waveOutLock)
                result = MyInterop.waveOutRestart(this.hWaveOut);
            if (result != MmResult.NoError)
            {
                throw new MmException(result, "waveOutRestart");
            }
            this.playbackState = PlaybackState.Playing;
        }
Exemplo n.º 7
0
        /// <summary>Pause the audio</summary>
        public void Pause()
        {
            if (this.playbackState != PlaybackState.Playing)
            {
                return;
            }
            this.playbackState = PlaybackState.Paused;
            MmResult result;

            lock (this.waveOutLock)
                result = MyInterop.waveOutPause(this.hWaveOut);
            if (result != MmResult.NoError)
            {
                throw new MmException(result, "waveOutPause");
            }
        }
Exemplo n.º 8
0
        public static void SetWaveOutVolume(float value, IntPtr hWaveOut, object lockObject)
        {
            if ((double)value < 0.0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0");
            }
            if ((double)value > 1.0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0");
            }
            int      dwVolume = (int)((double)value * (double)ushort.MaxValue) + ((int)((double)value * (double)ushort.MaxValue) << 16);
            MmResult result;

            lock (lockObject)
                result = MyInterop.waveOutSetVolume(hWaveOut, dwVolume);
            MmException.Try(result, "waveOutSetVolume");
        }
Exemplo n.º 9
0
 private void CloseWaveOut()
 {
     if (this.callbackEvent != null)
     {
         this.callbackEvent.Close();
         this.callbackEvent = (AutoResetEvent)null;
     }
     lock (this.waveOutLock)
     {
         if (!(this.hWaveOut != IntPtr.Zero))
         {
             return;
         }
         int num = (int)MyInterop.waveOutClose(this.hWaveOut);
         this.hWaveOut = IntPtr.Zero;
     }
 }
Exemplo n.º 10
0
        /// <summary>Stop and reset the WaveOut device</summary>
        public void Stop()
        {
            if (this.playbackState == PlaybackState.Stopped)
            {
                return;
            }
            this.playbackState = PlaybackState.Stopped;
            MmResult result;

            lock (this.waveOutLock)
                result = MyInterop.waveOutReset(this.hWaveOut);
            if (result != MmResult.NoError)
            {
                throw new MmException(result, "waveOutReset");
            }
            this.callbackEvent.Set();
        }
Exemplo n.º 11
0
 /// <summary>creates a new wavebuffer</summary>
 /// <param name="hWaveOut">WaveOut device to write to</param>
 /// <param name="bufferSize">Buffer size in bytes</param>
 /// <param name="bufferFillStream">Stream to provide more data</param>
 /// <param name="waveOutLock">Lock to protect WaveOut API's from being called on &gt;1 thread</param>
 public MyBuffer(
     IntPtr hWaveOut,
     int bufferSize,
     IWaveProvider bufferFillStream,
     object waveOutLock)
 {
     this.bufferSize          = bufferSize;
     this.buffer              = new byte[bufferSize];
     this.hBuffer             = GCHandle.Alloc((object)this.buffer, GCHandleType.Pinned);
     this.hWaveOut            = hWaveOut;
     this.waveStream          = bufferFillStream;
     this.waveOutLock         = waveOutLock;
     this.header              = new WaveHeader();
     this.hHeader             = GCHandle.Alloc((object)this.header, GCHandleType.Pinned);
     this.header.dataBuffer   = this.hBuffer.AddrOfPinnedObject();
     this.header.bufferLength = bufferSize;
     this.header.loops        = 1;
     this.hThis           = GCHandle.Alloc((object)this);
     this.header.userData = (IntPtr)this.hThis;
     lock (waveOutLock)
         MmException.Try(MyInterop.waveOutPrepareHeader(hWaveOut, this.header, Marshal.SizeOf((object)this.header)), "waveOutPrepareHeader");
 }