Beispiel #1
0
        /// <summary>
        /// Disposes the object.
        /// </summary>
        /// <param name="disposing">The disposing state.</param>
        public virtual void Dispose(bool disposing)
        {
            if (_buffers != null)
            {
                lock (_lockObj)
                {
                    foreach (WaveOutBuffer buffer in _buffers)
                    {
                        buffer.Dispose();
                    }
                }
                _buffers.Clear();
            }
            if (Stream != null)
            {
                Stream.Close();
            }

            if (disposing)
            {
                if (WaveOutHandle == IntPtr.Zero)
                {
                    return;
                }

                lock (_lockObj)
                {
                    WaveOutResult.Try(MMInterops.waveOutClose(WaveOutHandle));
                    WaveOutHandle = IntPtr.Zero;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the caps of the device.
        /// </summary>
        /// <param name="device">The Device.</param>
        /// <returns>WaveOutCaps.</returns>
        public static WaveOutCaps GetDevice(int device)
        {
            var caps = new WaveOutCaps();

            MMInterops.waveOutGetDevCaps((uint)device, out caps, (uint)Marshal.SizeOf(caps));
            return(caps);
        }
Beispiel #3
0
        /// <summary>
        /// Disposes the object.
        /// </summary>
        /// <param name="disposing">The disposing state.</param>
        public virtual void Dispose(bool disposing)
        {
            lock (_waveOut.LockObj)
            {
                if (_header == null || _waveOut.WaveOutHandle == IntPtr.Zero)
                {
                    return;
                }

                if (_bufferHandle.IsAllocated)
                {
                    _bufferHandle.Free();
                }
                if (_headerHandle.IsAllocated)
                {
                    _headerHandle.Free();
                }
                if (_userDataHandle.IsAllocated)
                {
                    _userDataHandle.Free();
                }

                WaveOutResult.Try(MMInterops.waveOutUnprepareHeader(_waveOut.WaveOutHandle, _header,
                                                                    Marshal.SizeOf(_header)));

                _header = null;
            }

            if (disposing)
            {
            }
        }
Beispiel #4
0
        /// <summary>
        /// Writes data to the buffer.
        /// </summary>
        /// <returns>True on success.</returns>
        public bool WriteData()
        {
            int read;

            lock (_waveOut.Stream)
            {
                read = _waveOut.Stream.Read(_buffer, 0, _buffer.Length);
            }
            if (read > 0)
            {
                if (_disposed)
                {
                    return(false);
                }
                _waveOut.AudioMixer.ApplyEffects(_buffer, _waveOut.Format);
                Array.Clear(_buffer, read, _buffer.Length - read);

                MMResult result = MMInterops.waveOutWrite(_waveOut.WaveOutHandle, _header,
                                                          Marshal.SizeOf(_header));
                if (result != MMResult.MMSYSERR_NOERROR)
                {
                    WaveOutResult.Try(result);
                }
                return(result == MMResult.MMSYSERR_NOERROR);
            }
            return(false);
        }
Beispiel #5
0
        /// <summary>
        /// Sets the Volume.
        /// </summary>
        /// <param name="waveOut">The WaveOut.</param>
        /// <param name="left">The Left.</param>
        /// <param name="right">The Right.</param>
        public static void SetVolume(IntPtr waveOut, float left, float right)
        {
            uint     tmp    = (uint)(left * 0xFFFF) + ((uint)(right * 0xFFFF) << 16);
            MMResult result = MMInterops.waveOutSetVolume(waveOut, tmp);

            if (result != MMResult.MMSYSERR_NOERROR)
            {
                WaveOutResult.Try(MMInterops.waveOutSetVolume(waveOut, tmp));
            }
        }
Beispiel #6
0
 /// <summary>
 /// Resumes the wavestream.
 /// </summary>
 public void Resume()
 {
     if (_playbackState == PlaybackState.Paused)
     {
         lock (_lockObj)
         {
             MMInterops.waveOutRestart(WaveOutHandle);
         }
         _playbackState = PlaybackState.Playing;
         RaisePlaybackChanged();
     }
 }
Beispiel #7
0
 /// <summary>
 /// Pause the wavestream.
 /// </summary>
 public void Pause()
 {
     if (_playbackState == PlaybackState.Playing)
     {
         lock (_lockObj)
         {
             MMInterops.waveOutPause(WaveOutHandle);
         }
         _playbackState = PlaybackState.Paused;
         RaisePlaybackChanged();
     }
 }
Beispiel #8
0
        /// <summary>
        /// Creates new WaveOut.
        /// </summary>
        /// <returns></returns>
        private IntPtr CreateWaveOut()
        {
            IntPtr handle;

            WaveOutResult.Try(MMInterops.waveOutOpen(out handle,
                                                     (IntPtr)Device,
                                                     Format,
                                                     _callback,
                                                     IntPtr.Zero,
                                                     MMInterops.CALLBACK_FUNCTION));

            return(handle);
        }
Beispiel #9
0
 /// <summary>
 /// Stops the wavestream.
 /// </summary>
 public void Stop()
 {
     if (_playbackState != PlaybackState.Stopped)
     {
         _playbackState = PlaybackState.Stopped;
         lock (_lockObj)
         {
             MMResult result = MMInterops.waveOutReset(WaveOutHandle);
             WaveOutResult.Try(result);
         }
         RaisePlaybackChanged();
     }
 }
Beispiel #10
0
        /// <summary>
        /// Initializes the buffer.
        /// </summary>
        public void Initialize()
        {
            _buffer = new byte[_bufferSize];

            var header = new WaveHdr();

            _headerHandle   = GCHandle.Alloc(header);
            _userDataHandle = GCHandle.Alloc(this);
            _bufferHandle   = GCHandle.Alloc(_buffer, GCHandleType.Pinned);

            header.dwUser         = (IntPtr)_userDataHandle;
            header.dwLoops        = 1;
            header.lpData         = _bufferHandle.AddrOfPinnedObject();
            header.dwBufferLength = _bufferSize;

            _header = header;
            lock (_waveOut.LockObj)
            {
                WaveOutResult.Try(MMInterops.waveOutPrepareHeader(_waveOut.WaveOutHandle, header,
                                                                  Marshal.SizeOf(header)));
            }
        }
Beispiel #11
0
 /// <summary>
 /// Gets the available device amount.
 /// </summary>
 /// <returns>Int32.</returns>
 public static int GetDeviceCount()
 {
     return(MMInterops.waveOutGetNumDevs());
 }