示例#1
0
        public void Dispose()
        {
            this.NextBuffer?.Dispose();
            WaveFormNative.waveOutUnprepareHeader(_DeviceHandle, ref _Header, Marshal.SizeOf(_Header));

            _DataHandle.Free();
            _HeaderHandle.Free();
        }
示例#2
0
        public void Play()
        {
            // Make sure we have data
            this.WaitForBufferFull();

            WaveFormNative.waveOutWrite(_DeviceHandle, ref _Header, Marshal.SizeOf(_Header));

            _IsEmpty = true;
        }
示例#3
0
        public virtual void Play()
        {
            this._WaitForCompletion.Reset();

//			this._PlayEvent.WaitOne();
//			this._PlayEvent.Reset();
            this._IsPlaying = true;

            int result = WaveFormNative.waveOutWrite(this._DeviceHandle, ref this._Header, Marshal.SizeOf(this._Header));

            if (result != WaveError.MMSYSERR_NOERROR)
            {
                throw new SoundCoreException(WaveError.GetMessage(result), result);
            }
        }
示例#4
0
        /// <summary>
        /// Creates and prepares a WaveOutBuffer.
        /// </summary>
        /// <param name="device">Handle to WaveOut device to prepare the buffer for</param>
        /// <param name="size">Size of the buffer, in bytes</param>
        public WaveOutBuffer(WavePlayer player, IntPtr device, int size)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            _Player = player;

            if (device == IntPtr.Zero)
            {
                throw new ArgumentException("Device must be a a valid pointer to a wave out device.", "device");
            }

            if (size < 1)
            {
                throw new ArgumentOutOfRangeException("size", size, "Size must be greater than zero.");
            }

            _Length       = size;
            _DeviceHandle = device;

            // Allocate memory for the buffer, and set up a GCHandle pointed to it.
            byte[] buffer = new byte[Length];
            _DataHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            // Create the header and a GC handle pointed to it.
            _Header       = new WaveHeader();
            _HeaderHandle = GCHandle.Alloc(_Header, GCHandleType.Pinned);

            _Header.Data         = this.Data;
            _Header.BufferLength = Length;

            _Header.UserData = (IntPtr)GCHandle.Alloc(this);
            _Header.Loops    = 0;
            _Header.Flags    = 0;

            int result = WaveFormNative.waveOutPrepareHeader(_DeviceHandle,
                                                             ref _Header, Marshal.SizeOf(_Header));

            if (result != WaveError.MMSYSERR_NOERROR)
            {
                throw new SoundCoreException(WaveError.GetMessage(result), result);
            }
        }
示例#5
0
        public SoundCoreException(string message, int nativeError) : base(message)
        {
            _NativeError = nativeError;

            if (nativeError != 0)
            {
                byte[] buffer = new byte[512];

                GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

                IntPtr ptr = handle.AddrOfPinnedObject();

                WaveFormNative.waveOutGetErrorText(nativeError, ptr, buffer.Length);

                _Detail = System.Text.Encoding.ASCII.GetString(buffer);

                handle.Free();
            }
        }
示例#6
0
        public void Dispose()
        {
            if (Disposing)
            {
                return;
            }

            this._Disposing = true;

            if (this.IsPlaying)
            {
                Stop();
            }


            WaveFormNative.waveOutClose(this._Device);

            FreeBuffers();
        }
示例#7
0
        public void Play()
        {
#if DEBUG
            this._FillCount = 0;
#endif
            if (_DataStream != null)
            {
                _DataStream.Position = _StartPosition;
            }

            WaveFormatEx format = Format.GetFormat();

            int result = WaveFormNative.waveOutOpen(out _DeviceHandle, Device, ref format, _CallBack, 0,
                                                    (int)DeviceOpenFlags.CallbackFunction);

            if (result != WaveError.MMSYSERR_NOERROR)
            {
                throw new SoundCoreException(WaveError.GetMessage(result), result);
            }

            _IsPlaying = true;

            AllocateBuffers();

            // Perform Initial fill
            WaveOutBuffer current = _RootBuffer;
            do
            {
                FillBuffer(current);
                current.BufferFilled();

                current = current.NextBuffer;
            } while (current != _RootBuffer);

            _RootBuffer.Play();
            //
            //			_FillThread = new Thread( new ThreadStart( FillProc ) );
            ////			_FillThread.Priority = ThreadPriority.Highest;
            //			_FillThread.Start();
            //
            //			_PlayThread = new Thread( new ThreadStart( PlayProc ) );
            //			_PlayThread.Start();
        }
示例#8
0
        public void Dispose()
        {
            if (Disposing)
            {
                return;
            }

            _Disposing = true;

            WaveFormNative.waveOutUnprepareHeader(this._DeviceHandle, ref _Header, Marshal.SizeOf(_Header));

            if (_HeaderHandle.IsAllocated)
            {
                _HeaderHandle.Free();
            }

            if (_DataHandle.IsAllocated)
            {
                _DataHandle.Free();
            }

            this._WaitForCompletion.Close();
        }
示例#9
0
 public int GetDeviceCount()
 {
     return(WaveFormNative.waveOutGetNumDevs());
 }
示例#10
0
//
//		private Stream _DataStream;
//
//		public WavePlayer( int device, Stream s )
//		{
//			if ( s == null )
//				throw new ArgumentNullException( "s" );
//
//			_DataStream = s;
//
//		}

        public WavePlayer(int device,
                          int bufferDataSize, int bufferCount,
                          WaveFormat format,
                          WaveBufferEmptyHandler bufferEmptyHandler)
        {
            if (bufferDataSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferDataSize", bufferDataSize, "bufferDataSize must be greater than 0.");
            }

            this._BufferDataSize = bufferDataSize;

            if (bufferCount <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferCount", bufferCount, "bufferCount must be greater than 0.");
            }

            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            WaveFormatEx formatEx = format.GetFormat();

            this._BufferCount = 2;

            if (bufferEmptyHandler == null)
            {
                throw new ArgumentNullException("bufferEmptyHandler");
            }

            this._BufferEmptyHandler = bufferEmptyHandler;


            int result = WaveFormNative.waveOutOpen(out _Device,
                                                    device,
                                                    ref formatEx,
                                                    _CallBack,
                                                    0,
                                                    (int)DeviceOpenFlags.CallbackFunction);

            if (result != WaveError.MMSYSERR_NOERROR)
            {
                throw new SoundCoreException(WaveError.GetMessage(result), result);
            }

            AllocateBuffers();

            _IsPlaying = true;

            FillBuffer(_Buffers[0]);

            _Buffers[0].Play();

//			FillBuffers();
//
//			_FillThread = new Thread( new ThreadStart( FillBufferLoop ) );
//			_FillThread.Start();
//
//			_RootBuffer.Play();
            _PlayThread          = new Thread(new ThreadStart(PlayLoop));
            _PlayThread.Priority = ThreadPriority.Highest;
            _PlayThread.Start();
        }