private void setBufferAndWave(double lengthFactor) { var waveFormat = new WaveFormat(); waveFormat.SamplesPerSecond = samplesPerSecond; waveFormat.Channels = 2; waveFormat.FormatTag = WaveFormatTag.Pcm; waveFormat.BitsPerSample = 16; waveFormat.BlockAlign = (short)(waveFormat.Channels * waveFormat.BitsPerSample / 8); waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * waveFormat.SamplesPerSecond; var bufferDesc = new BufferDescription(waveFormat); bufferDesc.DeferLocation = true; bufferDesc.Control3D = false; bufferDesc.ControlEffects = false; bufferDesc.ControlFrequency = true; bufferDesc.ControlPan = true; bufferDesc.ControlVolume = true; bufferDesc.GlobalFocus = true; bufferDesc.BufferBytes = Convert.ToInt32(lengthFactor * DitLengthSecond * waveFormat.AverageBytesPerSecond); if (buffer != null) { buffer.Stop(); buffer.Dispose(); buffer = null; } buffer = new SecondaryBuffer(bufferDesc, device); bufferDesc.Dispose(); bufferDesc = null; }
public void Dispose() { StopThread(); if (_sBuffer != null) { _sBuffer.Dispose(); } for (int i = 0; i < _devices.Length; i++) { _devices[i].Dispose(); } _bufferDescription.Dispose(); }
public void Stop() { if (mp3Stream == null) { return; } if (secondaryBuffer == null) { return; } Playing = false; bufferDescription.Dispose(); secondaryBuffer.Dispose(); mp3Stream.Dispose(); mp3Stream = null; }
private void stopButton__Click(object sender, EventArgs e) { if (secondaryBuffer_ == null) { return; } secondaryBuffer_.Dispose(); secondaryBuffer_ = null; bufferDescription_.Dispose(); bufferDescription_ = null; deviceComboBox_.Enabled = true; selectFileButton_.Enabled = true; stopButton_.Enabled = false; playOrStopButton_.Text = "Play"; }
private static void PlayThread(object osn) { EmulatorForm myform = (EmulatorForm)osn; SecondaryBuffer SecBuf; AutoResetEvent SecBufNotifyAtHalf = new AutoResetEvent(false); AutoResetEvent SecBufNotifyAtBeginning = new AutoResetEvent(false); int SamplingRate = (int)myform._samplingRate; int HoldThisManySamples = (int)(1 * SamplingRate); int BlockAlign = 2; int SecBufByteSize = HoldThisManySamples * BlockAlign; WaveFormat MyWaveFormat = new WaveFormat(); // Set the format MyWaveFormat.AverageBytesPerSecond = (int)(myform._samplingRate * BlockAlign); MyWaveFormat.BitsPerSample = (short)16; MyWaveFormat.BlockAlign = (short)BlockAlign; MyWaveFormat.Channels = (short)1; MyWaveFormat.SamplesPerSecond = (int)myform._samplingRate; MyWaveFormat.FormatTag = WaveFormatTag.Pcm; BufferDescription MyDescription; // Set BufferDescription MyDescription = new BufferDescription(); MyDescription.Format = MyWaveFormat; MyDescription.BufferBytes = HoldThisManySamples * BlockAlign; MyDescription.CanGetCurrentPosition = true; MyDescription.ControlPositionNotify = true; MyDescription.GlobalFocus = true; // Create the buffer SecBuf = new SecondaryBuffer(MyDescription, myform._directSoundDevice); Notify MyNotify; MyNotify = new Notify(SecBuf); BufferPositionNotify[] MyBufferPositions = new BufferPositionNotify[2]; MyBufferPositions[0].Offset = 0; MyBufferPositions[0].EventNotifyHandle = SecBufNotifyAtBeginning.Handle; MyBufferPositions[1].Offset = (HoldThisManySamples / 2) * BlockAlign; MyBufferPositions[1].EventNotifyHandle = SecBufNotifyAtHalf.Handle; MyNotify.SetNotificationPositions(MyBufferPositions); WaitHandle[] SecBufWaitHandles = { SecBufNotifyAtBeginning, SecBufNotifyAtHalf }; Int16[] buffer; buffer = myform._sn.GenerateSamples((uint)HoldThisManySamples, ""); SecBuf.Write(0, buffer, LockFlag.None); SecBuf.Play(0, BufferPlayFlags.Looping); int SecBufNextWritePosition = 0; while (myform._bufferPlaying) { int WriteCount = 0, PlayPosition = SecBuf.PlayPosition, WritePosition = SecBuf.WritePosition; if (SecBufNextWritePosition < PlayPosition && (WritePosition >= PlayPosition || WritePosition < SecBufNextWritePosition)) { WriteCount = PlayPosition - SecBufNextWritePosition; } else if (SecBufNextWritePosition > WritePosition && WritePosition >= PlayPosition) { WriteCount = (SecBufByteSize - SecBufNextWritePosition) + PlayPosition; } // System.Diagnostics.Debug.WriteLine("WC: "+WriteCount.ToString()); if (WriteCount > 0) { WriteCount = (int)Math.Min(WriteCount, 1000); buffer = myform._sn.GenerateSamples((uint)WriteCount / 2, ""); SecBuf.Write( SecBufNextWritePosition, buffer, LockFlag.None); SecBufNextWritePosition = (SecBufNextWritePosition + WriteCount) % SecBufByteSize; } else { WaitHandle.WaitAny(SecBufWaitHandles, new TimeSpan(0, 0, 5), true); } } SecBuf.Dispose(); MyDescription.Dispose(); MyNotify.Dispose(); }
private static void PlayThread(object osn) { EmulatorForm myform = (EmulatorForm)osn; SecondaryBuffer SecBuf; AutoResetEvent SecBufNotifyAtHalf = new AutoResetEvent(false); AutoResetEvent SecBufNotifyAtBeginning = new AutoResetEvent(false); int SamplingRate = (int)myform._samplingRate; int HoldThisManySamples = (int)(1 * SamplingRate); int BlockAlign = 2; int SecBufByteSize = HoldThisManySamples * BlockAlign; WaveFormat MyWaveFormat = new WaveFormat(); // Set the format MyWaveFormat.AverageBytesPerSecond = (int)(myform._samplingRate * BlockAlign); MyWaveFormat.BitsPerSample = (short)16; MyWaveFormat.BlockAlign = (short)BlockAlign; MyWaveFormat.Channels = (short)1; MyWaveFormat.SamplesPerSecond = (int)myform._samplingRate; MyWaveFormat.FormatTag = WaveFormatTag.Pcm; BufferDescription MyDescription; // Set BufferDescription MyDescription = new BufferDescription(); MyDescription.Format = MyWaveFormat; MyDescription.BufferBytes = HoldThisManySamples * BlockAlign; MyDescription.CanGetCurrentPosition = true; MyDescription.ControlPositionNotify = true; MyDescription.GlobalFocus = true; // Create the buffer SecBuf = new SecondaryBuffer(MyDescription,myform._directSoundDevice); Notify MyNotify; MyNotify = new Notify(SecBuf); BufferPositionNotify[] MyBufferPositions = new BufferPositionNotify[2]; MyBufferPositions[0].Offset = 0; MyBufferPositions[0].EventNotifyHandle = SecBufNotifyAtBeginning.Handle; MyBufferPositions[1].Offset = (HoldThisManySamples / 2) * BlockAlign; MyBufferPositions[1].EventNotifyHandle = SecBufNotifyAtHalf.Handle; MyNotify.SetNotificationPositions(MyBufferPositions); WaitHandle[] SecBufWaitHandles = { SecBufNotifyAtBeginning, SecBufNotifyAtHalf }; Int16[] buffer; buffer = myform._sn.GenerateSamples((uint)HoldThisManySamples, ""); SecBuf.Write(0, buffer, LockFlag.None); SecBuf.Play(0, BufferPlayFlags.Looping); int SecBufNextWritePosition = 0; while (myform._bufferPlaying) { int WriteCount = 0, PlayPosition = SecBuf.PlayPosition, WritePosition = SecBuf.WritePosition; if (SecBufNextWritePosition < PlayPosition && (WritePosition >= PlayPosition || WritePosition < SecBufNextWritePosition)) WriteCount = PlayPosition - SecBufNextWritePosition; else if (SecBufNextWritePosition > WritePosition && WritePosition >= PlayPosition) WriteCount = (SecBufByteSize - SecBufNextWritePosition) + PlayPosition; // System.Diagnostics.Debug.WriteLine("WC: "+WriteCount.ToString()); if (WriteCount > 0) { WriteCount = (int)Math.Min(WriteCount,1000); buffer = myform._sn.GenerateSamples((uint)WriteCount/2, ""); SecBuf.Write( SecBufNextWritePosition, buffer, LockFlag.None); SecBufNextWritePosition = (SecBufNextWritePosition + WriteCount) % SecBufByteSize; } else { WaitHandle.WaitAny(SecBufWaitHandles, new TimeSpan(0, 0, 5), true); } } SecBuf.Dispose(); MyDescription.Dispose(); MyNotify.Dispose(); }