public WavePlayer(Stream s) { if (s == null) { throw new ArgumentNullException("s"); } _DataStream = s; _Reader = new BinaryReader(_DataStream, System.Text.Encoding.ASCII); if (!SeekChunk("RIFF")) { throw new SoundCoreException("Could not find 'RIFF' Chunk."); } _Reader.ReadInt32(); if (!SeekChunk("WAVE")) { throw new SoundCoreException("Could not find 'WAVE' Chunk."); } _Format = new WaveFormat(_DataStream); if (!SeekChunk("data")) { throw new SoundCoreException("Could not find 'fmt ' Chunk."); } _Reader.ReadInt32(); _StartPosition = _DataStream.Position; _StreamBuffer = new byte[this.BufferSize]; this._FillerDelegate = new WaveBufferEmptyHandler(BufferFiller); }
public WavePlayer(short channels, int rate, short bitsPerSample, WaveBufferEmptyHandler filler) { _Format = new WaveFormat(channels, rate, bitsPerSample); if (filler == null) { throw new ArgumentNullException("filler"); } _FillerDelegate = filler; }
/// <summary> /// Initializes the buffer. /// </summary> /// <param name="device">Handle to the wave out device to use.</param> /// <param name="size">Size of the buffer to allocate.</param> public WaveOutBuffer(IntPtr device, int size, WaveBufferEmptyHandler fillDelegate) { 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."); } if (fillDelegate == null) { throw new ArgumentNullException("fillDelegate"); } _FillDelegate = fillDelegate; _DeviceHandle = device; // Allocate memory for the buffer, and set up a GCHandle pointed to it. _Data = new byte[size]; _DataHandle = GCHandle.Alloc(_Data, GCHandleType.Pinned); // Create the header and a GC handle pointed to it. _Header = new WaveHeader(); _HeaderHandle = GCHandle.Alloc(_Header, GCHandleType.Pinned); _Header.Data = _DataHandle.AddrOfPinnedObject(); _Header.BufferLength = size; _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); } }
// // 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(); }