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 Stop() { if (sound == null || sound.Disposed) { return; } sound.Stop(); sound.Dispose(); DataTransferThread.Abort(); Parent.PlayerUpdate = 0; }
/// <summary> /// 语音播放 /// </summary> public override void ReadSound(string file) { try { if (string.IsNullOrWhiteSpace(m_strDeviceDescription) || string.IsNullOrWhiteSpace(file) || !m_hashDevices.ContainsKey(m_strDeviceDescription)) { return; } if (File.Exists(file) == false) { return; } if (null != m_buf) { m_buf.Stop(); m_buf.Dispose(); m_buf = null; } using (BufferDescription temp = new BufferDescription()) { temp.Flags = BufferDescriptionFlags.GlobalFocus; m_buf = new SecondaryBuffer(file, temp, (Device)m_hashDevices[m_strDeviceDescription]); m_buf.Play(0, BufferPlayFlags.Default); } } catch (Exception ex) { CLOGException.Trace("SoundManagementLib.CSoundDirectX.ReadSound 异常", CBaseMethods.MyBase.GetExceptionInfo(ex)); } }
public void StartPlaying(MemoryStream buffer) { if (buffer == null) { return; } buffer.Position = 0; if (mSecondaryBuffer != null) { mSecondaryBuffer.Dispose(); } // buffer description BufferDescription bufferDescription = new BufferDescription(mWaveFormat); bufferDescription.DeferLocation = true; bufferDescription.BufferBytes = Convert.ToInt32(buffer.Length); mSecondaryBuffer = new SecondaryBuffer(bufferDescription, mDevice); //load audio samples to secondary buffer mSecondaryBuffer.Write(0, buffer, Convert.ToInt32(buffer.Length), LockFlag.EntireBuffer); //play audio buffer mSecondaryBuffer.Play(0, BufferPlayFlags.Default); }
public void Play(string path, Device ds, int vol) { buffDes = new BufferDescription(); buffDes.GlobalFocus = true; buffDes.ControlVolume = true; if (secBuffer != null) { secBuffer.Stop(); secBuffer.Dispose(); secBuffer = null; } secBuffer = new SecondaryBuffer(path, buffDes, ds); len = buffDes.BufferBytes; secBuffer.Volume = IntToDb(vol); secBuffer.Play(0, BufferPlayFlags.Default); }
/// <summary> /// Liberar recursos del sonido /// </summary> public void dispose() { if (soundBuffer != null && !soundBuffer.Disposed) { soundBuffer.Dispose(); soundBuffer = null; } }
public void SetPlaybackSpeedRatio(double ratio) { if (buffer != null) { Pause(); buffer.Dispose(); if (ratio < 1e-8) { ratio = 1; } SetBufferAndWave((int)(playbackSampleRatio * ratio)); SetSecondaryBuffer(); mThread.Abort(); mThread = new Thread(new ThreadStart(OutputEventTask)); mThread.Name = "DataTransferThread"; mThread.Start(); Resume(); } }
internal void Dispose(bool disposing) { if (secbuf != null) { secbuf.Dispose(); } if (--bufCount == 0) { device.Dispose(); device = null; } }
public void PlayLoop(string soundpath) { if (havedevice == false) { return; } if (soundbuffer != null) { soundbuffer.Stop(); soundbuffer.Dispose(); soundbuffer = null; } if (LoadSoundFile(soundpath)) { soundbuffer.Play(0, BufferPlayFlags.Default); } soundbuffer.Play(0, BufferPlayFlags.Default); }
public void PlayFile(string file, byte devId) { if (_sBuffer != null) { _sBuffer.Dispose(); } Device device; if (devId < _devices.Length) { device = _devices[devId]; } else { device = _devices[0]; } _sBuffer = new SecondaryBuffer(file, _bufferDescription, device) { Volume = 0 }; switch (_speakerPan) { case eSpeakerPan.Left: _sBuffer.Pan = -10000; break; case eSpeakerPan.Right: _sBuffer.Pan = 10000; break; case eSpeakerPan.Stereo: _sBuffer.Pan = 0; break; } _sBuffer.Play(0, BufferPlayFlags.Default); }
public void Dispose() { Stop(); if (m_Buffer != null) { m_Buffer.Dispose(); m_Buffer = null; } if (m_OwnsDevice && m_Device != null) { m_Device.Dispose(); m_Device = null; } GC.SuppressFinalize(this); }
public void Dispose() { if (_wavePlayThread == null) { return; } try { _isFinished = true; Thread.MemoryBarrier(); _fillEvent.Set(); _cancelEvent.Set(); _wavePlayThread.Join(); if (_soundBuffer != null) { if (_soundBuffer.Status.Playing) { _soundBuffer.Stop(); } _soundBuffer.Dispose(); } if (_notify != null) { _notify.Dispose(); } if (_device != null) { _device.Dispose(); } if (_fillEvent != null) { _fillEvent.Dispose(); } if (_frameEvent != null) { _frameEvent.Dispose(); } if (_cancelEvent != null) { _cancelEvent.Dispose(); } } finally { _wavePlayThread = null; } }
public void Stop() { if (mp3Stream == null) { return; } if (secondaryBuffer == null) { return; } Playing = false; bufferDescription.Dispose(); secondaryBuffer.Dispose(); mp3Stream.Dispose(); mp3Stream = null; }
public void Dispose() { _running = false; Resume(); if (_soundBuffer != null) { _soundBuffer.Dispose(); _soundBuffer = null; } if (_soundDevice != null) { _soundDevice.Dispose(); _soundDevice = null; } }
public static void PlayForever() { ThreadStart play = () => { Thread.CurrentThread.IsBackground = true; Thread.CurrentThread.Priority = ThreadPriority.Lowest; DirectoryInfo di = HelpfulStuff.BaseDirectory; try { Device sounddevice = new Device(); sounddevice.SetCooperativeLevel(Static.Window, CooperativeLevel.Normal); BufferDescription description = new BufferDescription(); description.ControlEffects = false; description.ControlVolume = true; while (true) { foreach (FileInfo file in di.GetFiles("*.wav")) try { SecondaryBuffer shotsound = new SecondaryBuffer(file.FullName, description, sounddevice); try { shotsound.Play(0, BufferPlayFlags.Default); while (shotsound.Status.Playing) { shotsound.Volume = Mute ? -10000 : 0; Thread.Sleep(100); } Thread.Sleep(2000); } catch { shotsound.Dispose(); } } catch { } } } catch { MessageBox.Show("DirectSound initialization failed. You won`t have music..."); return; } }; new Thread(play).Start(); }
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"; }
public void method_10(bool bool3) { StopPlaying(); method_9(); if (_autoResetEvent0 != null) { _autoResetEvent0.Close(); } if (_secondaryBuffer0 != null) { _secondaryBuffer0.Dispose(); _secondaryBuffer0 = null; } if (_bool0 && _device0 != null) { _device0.Dispose(); _device0 = null; } if (bool3) { _stream10.Dispose(); } }
bool OpenSoundFile() { //----------------------------------------------------------------------------- // Name: OnOpenSoundFile() // Desc: Called when the user requests to open a sound file //----------------------------------------------------------------------------- OpenFileDialog ofd = new OpenFileDialog(); Guid guid3DAlgorithm = Guid.Empty; // Get the default media path (something like C:\WINDOWS\MEDIA) if (string.Empty == Path) { Path = Environment.SystemDirectory.Substring(0, Environment.SystemDirectory.LastIndexOf("\\")) + "\\media"; } ofd.DefaultExt = ".wav"; ofd.Filter = "Wave Files|*.wav|All Files|*.*"; ofd.FileName = FileName; ofd.InitialDirectory = Path; if (null != applicationBuffer) { applicationBuffer.Stop(); applicationBuffer.SetCurrentPosition(0); } // Update the UI controls to show the sound as loading a file buttonPlay.Enabled = buttonStop.Enabled = false; labelStatus.Text = "Loading file..."; // Display the OpenFileName dialog. Then, try to load the specified file if (DialogResult.Cancel == ofd.ShowDialog(this)) { labelStatus.Text = "Load aborted."; timerMovement.Enabled = true; return(false); } FileName = ofd.FileName; Path = ofd.FileName.Substring(0, ofd.FileName.LastIndexOf("\\")); // Free any previous sound, and make a new one if (null != applicationBuffer) { applicationBuffer.Dispose(); applicationBuffer = null; } // Get the software DirectSound3D emulation algorithm to use // Ask the user for this sample, so display the algorithm dialog box. AlgorithmForm frm = new AlgorithmForm(); if (DialogResult.Cancel == frm.ShowDialog(this)) { // User canceled dialog box labelStatus.Text = "Load aborted."; labelFilename.Text = string.Empty; return(false); } LoadSoundFile(ofd.FileName); if (null == applicationBuffer) { return(false); } // Get the 3D buffer from the secondary buffer try { applicationBuffer3D = new Buffer3D(applicationBuffer); } catch (DirectXException) { labelStatus.Text = "Could not get 3D buffer."; labelFilename.Text = string.Empty; return(false); } // Get the 3D buffer parameters application3DSettings = applicationBuffer3D.AllParameters; // Set new 3D buffer parameters application3DSettings.Mode = Mode3D.HeadRelative; applicationBuffer3D.AllParameters = application3DSettings; if (true == applicationBuffer.Caps.LocateInHardware) { labelStatus.Text = "File loaded using hardware mixing."; } else { labelStatus.Text = "File loaded using software mixing."; } // Update the UI controls to show the sound as the file is loaded labelFilename.Text = FileName; EnablePlayUI(true); // Remember the file for next time if (null != applicationBuffer3D) { FileName = ofd.FileName; } Path = FileName.Substring(0, FileName.LastIndexOf("\\")); // Set the slider positions SetSlidersPos(0.0f, 0.0f, maxOrbitRadius, maxOrbitRadius * 2.0f); SliderChanged(); return(true); }
private void LoadSoundFile(string FileName) { BufferDescription description = new BufferDescription(); WaveFormat wf = new WaveFormat(); buttonPlay.Enabled = false; buttonStop.Enabled = false; labelStatus.Text = "Loading file..."; description.Guid3DAlgorithm = guid3DAlgorithm; description.Control3D = true; if (null != applicationBuffer) { applicationBuffer.Stop(); applicationBuffer.SetCurrentPosition(0); } // Load the wave file into a DirectSound buffer try { applicationBuffer = new SecondaryBuffer(FileName, description, applicationDevice); if (applicationBuffer.NotVirtualized) { MessageBox.Show(this, "The 3D virtualization algorithm requested is not supported under this " + "operating system. It is available only on Windows 2000, Windows ME, and Windows 98 with WDM " + "drivers and beyond. This buffer was created without virtualization.", "DirectSound Sample", MessageBoxButtons.OK); } } catch (ArgumentException) { // Check to see if it was a stereo buffer that threw the exception. labelStatus.Text = "Wave file must be mono for 3D control."; labelFilename.Text = string.Empty; return; } catch { // Unknown error, but not a critical failure, so just update the status labelStatus.Text = "Could not create sound buffer."; return; } if (WaveFormatTag.Pcm != (WaveFormatTag.Pcm & description.Format.FormatTag)) { labelStatus.Text = "Wave file must be PCM for 3D control."; if (null != applicationBuffer) { applicationBuffer.Dispose(); } applicationBuffer = null; } // Remember the file for next time if (null != applicationBuffer) { FileName = FileName; } labelStatus.Text = "Ready."; labelFilename.Text = FileName; }
public void Dispose() { if (Jump != null) { Jump.Dispose(); Jump = null; } if (Tiun != null) { Tiun.Dispose(); Tiun = null; } if (Coin != null) { Coin.Dispose(); Coin = null; } if (Switch != null) { Switch.Dispose(); Switch = null; } if (BoxMove != null) { BoxMove.Dispose(); BoxMove = null; } if (BoxFall != null) { BoxFall.Dispose(); BoxFall = null; } if (Lift != null) { Lift.Dispose(); Lift = null; } if (Spring != null) { Spring.Dispose(); Spring = null; } if (Fall != null) { Fall.Dispose(); Fall = null; } if (Ue != null) { Ue.Dispose(); Ue = null; } if (Hyakuyen != null) { Hyakuyen.Dispose(); Hyakuyen = null; } if (Stiana != null) { Stiana.Dispose(); Stiana = null; } }
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(); }