Beispiel #1
0
 public static void unloadSound(ref SecondarySoundBuffer sound)
 {
     System.Diagnostics.Trace.WriteLine("In unloadSound, sound is " + ((sound == null)? "null":" not nulll"));
     if (sound == null || sound.IsDisposed)
     {
         sound = null;
         return;
     }
     sound.Stop();
     sound.Dispose();
     sound = null;
 }
Beispiel #2
0
        /// <summary>
        ///  Loads and plays the specified wave file, and disposes it after it is done playing.
        /// </summary>
        /// <param name="fn">The name of the file to play.</param>
        public static void playAndWait(String fn)
        {
            SecondarySoundBuffer s = LoadSound(fn);

            PlaySound(s, true, false);
            while (isPlaying(s))
            {
                Thread.Sleep(100);
            }
            s.Dispose();
            s = null;
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed && disposing)
            {
                _soundBuffer?.Stop();
                _soundBuffer?.Dispose();

                _primarySoundBuffer?.Dispose();
                _directSound?.Dispose();

                _disposed = true;
            }
        }
Beispiel #4
0
 /// <summary>
 /// Disposes of the current sound buffer and device.
 /// </summary>
 protected void DestroyDirectSound()
 {
     if (_secondaryBuffer != null)
     {
         _secondaryBuffer.Dispose();
         _secondaryBuffer = null;
     }
     if (_directSound != null)
     {
         _directSound.Dispose();
         _directSound = null;
     }
     _isPlaying = false;
 }
Beispiel #5
0
 public void Destroy()
 {
     if (sBuffer != null)
     {
         sBuffer.Dispose();
     }
     if (pBuffer != null)
     {
         pBuffer.Dispose();
     }
     if (device != null)
     {
         device.Dispose();
     }
 }
 public void StopSound()
 {
     if (IsPlaying)
     {
         try
         {
             _deviceBuffer.Stop();
         }
         catch (DirectSoundException)
         {
         }
     }
     _deviceBuffer.Dispose();
     _deviceBuffer     = null;
     BufferSizeSamples = 0;
 }
Beispiel #7
0
        protected virtual void Dispose(bool safe)
        {
            if (safe)
            {
                if (audioBuffer != null)
                {
                    audioBuffer.Dispose();
                    audioBuffer = null;
                }

                if (directSound != null)
                {
                    directSound.Dispose();
                    directSound = null;
                }
            }
        }
Beispiel #8
0
        public void Stop()
        {
            lock (this)
            {
                if (Secondary != null)
                {
                    Secondary.Stop();
                    Secondary.Dispose();
                    Secondary = null;
                }

                BytesWritten         = 0;
                CurrentWritePosition = 0;
                PlayingStarted       = false;
                Started = false;
            }
        }
Beispiel #9
0
 /// <inheritdoc/>
 protected override void OnDispose()
 {
     try
     {
         if (SoundBuffer != null && !SoundBuffer.Disposed)
         {
             if (Playing)
             {
                 SoundBuffer.Stop();
             }
             SoundBuffer.Dispose();
         }
     }
     finally
     {
         base.OnDispose();
     }
 }
Beispiel #10
0
        public void Dispose()
        {
            if (_wavePlayThread == null)
            {
                return;
            }
            try
            {
                _isFinished = true;
                Thread.MemoryBarrier();
                _fillEvent.Set();
                _cancelEvent.Set();
                _wavePlayThread.Join();

                if (_soundBuffer != null)
                {
                    if (_soundBuffer.Status == DSBSTATUS.PLAYING)
                    {
                        _soundBuffer.Stop();
                    }
                    _soundBuffer.Dispose();
                }
                if (_device != null)
                {
                    _device.Dispose();
                }
                if (_fillEvent != null)
                {
                    _fillEvent.Dispose();
                }
                if (_frameEvent != null)
                {
                    _frameEvent.Dispose();
                }
                if (_cancelEvent != null)
                {
                    _cancelEvent.Dispose();
                }
            }
            finally
            {
                _wavePlayThread = null;
            }
        }
Beispiel #11
0
 void Dispose(bool disposing)
 {
     if (disposing)
     {
         // Don't dispose twice.
         if (IsDisposing)
         {
             return;
         }
         IsDisposing = true;
         if (ApplicationDevice != null)
         {
             ApplicationDevice.Dispose();
         }
         if (ApplicationBuffer != null)
         {
             ApplicationBuffer.Dispose();
         }
         _Handle = IntPtr.Zero;
     }
 }
Beispiel #12
0
 public void Shutdown()
 {
     IsPaused = true;
     if (buffer != null && buffer.Disposed & !IsRendering)
     {
         try
         {
             buffer.Stop();
         }
         catch { return; }
     }
     while (IsRendering)
     {
     }
     buffer.Dispose();
     _SoundDevice.Dispose();
     if (RECODER.IsRecording)
     {
         RECODER.Stop();
     }
 }
Beispiel #13
0
        public void Close()
        {
            lock (locker)
            {
                try
                {
                    //Video
                    if (thread_v != null)
                    {
                        IsAborted = true;
                        playing_v.Set();      //Снимаем с паузы PlayingLoop, чтоб там сработала проверка на IsAborted
                        processing.WaitOne(); //Ждем, пока обработается текущий кадр, если его считывание еще не закончилось
                        thread_v.Join();      //Дожидаемся окончания работы PlayingLoop
                        thread_v = null;
                    }
                    if (BitmapSource != null)
                    {
                        BitmapSource = null;
                    }
                    if (VBuffer != IntPtr.Zero)
                    {
                        if (IsInterop)
                        {
                            UnmapViewOfFile(VBuffer);
                        }
                        else
                        {
                            Marshal.FreeHGlobal(VBuffer);
                        }
                        VBuffer = IntPtr.Zero;
                    }
                    if (MemSection != IntPtr.Zero)
                    {
                        CloseHandle(MemSection);
                        MemSection = IntPtr.Zero;
                    }

                    //Audio
                    if (thread_a != null)
                    {
                        IsAborted = true;
                        playing_a.Set();
                        thread_a.Join();
                        thread_a = null;
                    }
                    if (AudioBuffer != null)
                    {
                        AudioBuffer.Dispose();
                        AudioBuffer = null;
                    }
                    if (AudioDevice != null)
                    {
                        AudioDevice.Dispose();
                        AudioDevice = null;
                    }
                }
                catch (Exception)
                {
                    IsError = true;
                    throw;
                }
                finally
                {
                    if (h.IsAllocated)
                    {
                        h.Free();
                    }

                    AdjustMediaTimer(0);

                    UnloadAviSynth();
                }
            }
        }
Beispiel #14
0
 void ShutdownWaveFile()
 {
     secondaryBuffer.Dispose();
 }
Beispiel #15
0
        /// <summary>
        /// Play DX7.
        /// </summary>
        /// <param name="channels">An array of values for each channel (values between 0 and 1, usually 8 channels).</param>
        public static void PlayDX7(Primitive channels)
        {
            Initialise();
            try
            {
                int i, iServo;
                double duration = 0.0225;
                int sampleCount = (int)(duration * waveFormat.SamplesPerSecond);

                // buffer description
                SoundBufferDescription soundBufferDescription = new SoundBufferDescription();
                soundBufferDescription.Format = waveFormat;
                soundBufferDescription.Flags = BufferFlags.Defer;
                soundBufferDescription.SizeInBytes = sampleCount * waveFormat.BlockAlignment;

                SecondarySoundBuffer secondarySoundBuffer = new SecondarySoundBuffer(directSound, soundBufferDescription);

                short[] rawsamples = new short[sampleCount];
                int stopSamples = (int)(0.0004 * waveFormat.SamplesPerSecond);
                List<int> servoSamples = new List<int>();
                Primitive indices = SBArray.GetAllIndices(channels);
                int servoCount = SBArray.GetItemCount(indices);
                for (iServo = 1; iServo <= servoCount; iServo++)
                {
                    servoSamples.Add((int)((0.0007 + 0.0008 * channels[indices[iServo]]) * waveFormat.SamplesPerSecond));
                }
                //Lead-in
                int leading = sampleCount - (servoCount + 1) * stopSamples - servoSamples.Sum();
                int sample = 0;
                for (i = 0; i < leading; i++) rawsamples[sample++] = 0;
                //Servos
                for (i = 0; i < stopSamples; i++) rawsamples[sample++] = (short)(-amplitude);
                for (iServo = 0; iServo < servoCount; iServo++)
                {
                    for (i = 0; i < servoSamples[iServo]; i++) rawsamples[sample++] = amplitude;
                    for (i = 0; i < stopSamples; i++) rawsamples[sample++] = (short)(-amplitude);
                }

                //load audio samples to secondary buffer
                secondarySoundBuffer.Write(rawsamples, 0, LockFlags.EntireBuffer);

                //play audio buffer
                secondarySoundBuffer.Play(0, PlayFlags.None);

                //wait to complete before returning
                while ((secondarySoundBuffer.Status & BufferStatus.Playing) != 0);

                secondarySoundBuffer.Dispose();
            }
            catch (Exception ex)
            {
                TextWindow.WriteLine(ex.Message);
            }
        }
Beispiel #16
0
        private static void Play(double frequency, double duration, int iType)
        {
            Initialise();
            try
            {
                int sampleCount = (int)(duration * waveFormat.SamplesPerSecond);

                // buffer description
                SoundBufferDescription soundBufferDescription = new SoundBufferDescription();
                soundBufferDescription.Format = waveFormat;
                soundBufferDescription.Flags = BufferFlags.Defer;
                soundBufferDescription.SizeInBytes = sampleCount * waveFormat.BlockAlignment;

                SecondarySoundBuffer secondarySoundBuffer = new SecondarySoundBuffer(directSound, soundBufferDescription);

                short[] rawsamples = new short[sampleCount];
                double frac, value;

                switch (iType)
                {
                    case 1: //Sinusoidal
                        for (int i = 0; i < sampleCount; i++)
                        {
                            frac = frequency * duration * i / (double)sampleCount;
                            value = System.Math.Sin(2.0 * System.Math.PI * frac);
                            rawsamples[i] = (short)(amplitude * value);
                        }
                        break;
                    case 2: //Square
                        for (int i = 0; i < sampleCount; i++)
                        {
                            frac = frequency * duration * i / (double)sampleCount;
                            frac = frac - (int)frac;
                            value = frac < 0.5 ? -1.0 : 1.0;
                            rawsamples[i] = (short)(amplitude * value);
                        }
                        break;
                }

                //load audio samples to secondary buffer
                secondarySoundBuffer.Write(rawsamples, 0, LockFlags.EntireBuffer);

                //play audio buffer
                secondarySoundBuffer.Play(0, PlayFlags.None);

                //wait to complete before returning
                while ((secondarySoundBuffer.Status & BufferStatus.Playing) != 0);

                secondarySoundBuffer.Dispose();
            }
            catch (Exception ex)
            {
                TextWindow.WriteLine(ex.Message);
            }
        }