예제 #1
0
        public override void Play(AudioBuffer buffer)
        {
            _xa2Buffer            = (XAudio2AudioBuffer)buffer;
            _channelCount         = GetChannelCount(_xa2Buffer.Format);
            _emitter.ChannelCount = _channelCount;
            if ((_channelCount > 1 && !_stereoState) || (_channelCount == 1 && _stereoState))
            {
                float volume = _sourceVoice.Volume;
                _sourceVoice.DestroyVoice();
                _sourceVoice.Dispose();
                WaveFormat waveFormat = new WaveFormat(_xa2Buffer.Frequency, GetChannelCount(_xa2Buffer.Format));
                _sourceVoice = new SourceVoice(_engine.XAudio2, waveFormat, VoiceFlags.None, maxFrequencyRatio: 2.0f);
                _sourceVoice.SetVolume(volume);
                _emitter.ChannelAzimuths = new[] { 0.0f };
                _dspSettings             = new DspSettings(_channelCount, 2);
                UpdateSourcePosition();
                _stereoState = _channelCount == 2;
            }

            if (_sourcePositionDirty)
            {
                UpdateSourcePosition();
                _sourcePositionDirty = false;
            }

            _audioBuffer.Stream     = _xa2Buffer.DataStream;
            _audioBuffer.AudioBytes = _xa2Buffer.SizeInBytes;
            _audioBuffer.Flags      = BufferFlags.EndOfStream;
            _sourceVoice.Stop();
            _sourceVoice.FlushSourceBuffers();
            _sourceVoice.SubmitSourceBuffer(_audioBuffer, null);
            _sourceVoice.Start();
        }
예제 #2
0
 public void Dispose()
 {
     _sourceVoice?.DestroyVoice();
     _sourceVoice?.Dispose();
     _buffer.Stream.Dispose();
     _stream?.Dispose();
 }
예제 #3
0
 /// <summary>
 /// Dispose
 /// </summary>
 public void Dispose()
 {
     _voice.DestroyVoice();
     _voice.Dispose();
     _stream.Dispose();
     _buffer.Stream.Dispose();
 }
예제 #4
0
        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    StopFxSounds();
                    StopEnvSounds();

                    _currentBgm?.DestroyVoice();
                    _currentBgm?.Dispose();
                    _currentBgm = null;

                    _fxLinkedSoundList.Clear();
                    _envLinkedSoundList.Clear();

                    UnloadAll();

                    _fxSubmixVoice.DestroyVoice();
                    _fxSubmixVoice.Dispose();

                    _envSubmixVoice.DestroyVoice();
                    _envSubmixVoice.Dispose();

                    _masteringVoice.DestroyVoice();
                    _masteringVoice.Dispose();

                    _xAudio2.Dispose();
                }

                _disposed = true;
            }
        }
예제 #5
0
        private void PlayWaveHelper(string soundFile, string soundText)
        {
            var filepath = GetFilePath(soundFile, soundText).Result;

            var nativefilestream = new NativeFileStream(filepath, NativeFileMode.Open, NativeFileAccess.Read);

            using (var soundstream = new SoundStream(nativefilestream))
            {
                var waveFormat = soundstream.Format;
                var buffer     = new AudioBuffer
                {
                    Stream     = soundstream.ToDataStream(),
                    AudioBytes = (int)soundstream.Length,
                    Flags      = BufferFlags.EndOfStream
                };

                if (_sourceVoice != null)
                {
                    _sourceVoice.DestroyVoice();
                    _sourceVoice.Dispose();
                }

                _sourceVoice = new SourceVoice(_xAudio, waveFormat);

                _sourceVoice.SubmitSourceBuffer(buffer, soundstream.DecodedPacketsInfo);
                _sourceVoice.BufferEnd += obj =>
                {
                    _lock.Set();
                };

                _sourceVoice.Start();
            }
        }
예제 #6
0
        void CloseDevice()
        {
            _sourceVoice?.Stop(PlayFlags.None, XAUDIO2_COMMIT_NOW);
            _sourceVoice?.FlushSourceBuffers();
            _sourceVoice?.DestroyVoice();
            _sourceVoice?.Dispose();

            _xaudio2?.StopEngine();

            _masteringVoice?.DestroyVoice();
            _masteringVoice?.Dispose();

            _xaudio2?.Dispose();

            _sourceVoice    = null;
            _masteringVoice = null;
            _xaudio2        = null;

            //if (_hidden.handle.IsAllocated)
            //{
            //    _hidden.handle.Free();
            //    _hidden.device = IntPtr.Zero;
            //}

            if (_hidden.mixbuf != null)
            {
                Marshal.FreeHGlobal((IntPtr)_hidden.mixbuf);
                _hidden.mixbuf = null;
            }

            _dev = null;
        }
예제 #7
0
 private static void finishSource(SourceVoice s)
 {
     if (sources.ContainsKey(s))
     {
         s.DestroyVoice();
         s.Dispose();
         sources[s].Stream.Dispose();
     }
 }
예제 #8
0
 public void Dispose()
 {
     if (m_voice == null)
     {
         return;
     }
     m_voice.DestroyVoice();
     m_voice.Dispose();
     m_voice = null;
 }
예제 #9
0
 public void Dispose()
 {
     if (_voice != null && _voice.IsDisposed == false)
     {
         _voice.Stop();
         _voice.FlushSourceBuffers();
         _voice.DestroyVoice();
         _voice.Dispose();
     }
 }
예제 #10
0
 private void Stop()
 {
     if (playerState != PlayerState.Stopped)
     {
         sourceVoice.DestroyVoice();
         sourceVoice.Dispose();
         audioBuffer.Stream.Dispose();
         SetPlayerState(PlayerState.Stopped);
     }
 }
예제 #11
0
        internal void PlatformSpecificDisposeImpl()
        {
            if (SourceVoice == null)
            {
                return;
            }

            SourceVoice.DestroyVoice();
            SourceVoice.Dispose();
        }
예제 #12
0
 private void DisposeSource()
 {
     for (int i = 0; i < NumberOfBuffers; i++)
     {
         buffers[i].Dispose();
     }
     source.FlushSourceBuffers();
     source.DestroyVoice();
     source.Dispose();
     source = null;
 }
예제 #13
0
 public void Stop()
 {
     thread?.Abort();
     if (sourceVoice != null && buffer != null && xaudio2 != null && masteringVoice != null)
     {
         sourceVoice.DestroyVoice();
         sourceVoice.Dispose();
         sourceVoice = null;
         xaudio2.Dispose();
         masteringVoice.Dispose();
     }
 }
 private void PlatformDispose(bool disposing)
 {
     if (disposing)
     {
         if (_voice != null)
         {
             _voice.DestroyVoice();
             _voice.Dispose();
         }
     }
     _voice  = null;
     _effect = null;
 }
예제 #15
0
        public void Dispose()
        {
            if (_voice != null && !_voice.IsDisposed)
            {
                _voice.DestroyVoice();
                _voice.Dispose();
            }

            if (_stream != null)
            {
                _stream.Dispose();
            }
        }
예제 #16
0
        private async Task PlaySoundImpl(Stream stream)
        {
            using (var vorbis = new VorbisReader(stream, true))
            {
                var channels   = vorbis.Channels;
                var sampleRate = vorbis.SampleRate;
                var samples    = new float[channels * sampleRate];

                var format = new WaveFormat(sampleRate, 32, 2);
                var src    = new SourceVoice(_device, format);

                var bufferQueue = new Queue <AudioBuffer>();
                src.BufferEnd += (IntPtr _) =>
                {
                    bufferQueue.Dequeue().Stream.Dispose();
                };

                src.Start();

                bool doneReading = false;
                do
                {
                    if (src.State.BuffersQueued < 3 && !doneReading)
                    {
                        int bytesRead = vorbis.ReadSamples(samples, 0, samples.Length);
                        if (bytesRead == 0)
                        {
                            doneReading = true;
                            continue;
                        }

                        var dataStream = new DataStream(bytesRead * sizeof(float), true, true);
                        dataStream.WriteRange(samples, 0, bytesRead);
                        dataStream.Position = 0;

                        var buffer = new AudioBuffer(dataStream);
                        buffer.Flags = BufferFlags.EndOfStream;
                        bufferQueue.Enqueue(buffer);
                        src.SubmitSourceBuffer(buffer, null);
                    }

                    await Task.Delay(100).ConfigureAwait(false);
                } while (src.State.BuffersQueued > 0);

                src.DestroyVoice();
                src.Dispose();
            }
        }
예제 #17
0
        public void StopImmediate()
        {
            if (immediateVoice != null)
            {
                immediateVoice.Stop();
                immediateVoice.FlushSourceBuffers();
                immediateVoice.BufferEnd -= ImmediateVoice_BufferEnd;
                immediateVoice.DestroyVoice();
                immediateVoice.Dispose();
                immediateVoice = null;

                Utilities.FreeMemory(immediateAudioBuffer.AudioDataPointer);
                immediateAudioBuffer = null;
                immediateDonePlaying = true;
            }
        }
예제 #18
0
        protected virtual void Dispose(bool disposing)
        {
            if (!m_Disposed)
            {
                if (disposing)
                {
                    Stop();
                    m_Audio.DestroyVoice();
                    m_Audio.Dispose();
                    m_AudioBuffer.Stream.Dispose();

                    Sound?.Dispose();
                    m_Device = null;
                }
                m_Disposed = true;
            }
        }
예제 #19
0
        public void StopImmediate()
        {
            Debug.Assert(PlatformUtils.IsInMainThread());

            if (immediateVoice != null)
            {
                immediateVoice.Stop();
                immediateVoice.FlushSourceBuffers();
                immediateVoice.BufferEnd -= ImmediateVoice_BufferEnd;
                immediateVoice.DestroyVoice();
                immediateVoice.Dispose();
                immediateVoice = null;

                Utilities.FreeMemory(immediateAudioBuffer.AudioDataPointer);
                immediateAudioBuffer = null;
                immediateDonePlaying = true;
            }
        }
예제 #20
0
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                Stop();

                if (SourceVoice != null)
                {
                    SourceVoice.DestroyVoice();
                    SourceVoice.Dispose();
                }

                if (AudioBuffer != null)
                {
                    AudioBuffer.Stream.Dispose();
                }
            }
        }
예제 #21
0
        private void PlaySound(int soundID,
                               Emitter emitter,
                               float volume,
                               LinkedSoundList list,
                               ref VoiceSendDescriptor voiceSendDescriptor,
                               Action <IntPtr>?onFxEnd = null)
        {
            if (!_soundBuffer.TryGetValue(soundID, out SoundBuffer buffer))
            {
                return;
            }

            SourceVoice sourceVoice = new SourceVoice(_xAudio2, buffer.Format, VoiceFlags.None, true);

            sourceVoice.SetVolume(volume);
            sourceVoice.SubmitSourceBuffer(buffer.AudioBuffer, buffer.DecodedPacketsInfo);
            sourceVoice.SetOutputVoices(voiceSendDescriptor);

            LinkedSoundList.Sound sound = new LinkedSoundList.Sound(emitter, sourceVoice);
            list.Add(sound);

            sourceVoice.BufferEnd += _ =>
            {
                list.Remove(sound);
                sourceVoice.DestroyVoice();
            };

            if (onFxEnd != null)
            {
                sourceVoice.BufferEnd += onFxEnd;
            }
            sourceVoice.Start();

            DspSettings settings = _x3DAudio.Calculate(
                _listener,
                sound.Emitter,
                CalculateFlags.Matrix | CalculateFlags.Doppler,
                buffer.Format.Channels,
                _inputChannelCount);

            sound.SourceVoice.SetOutputMatrix(buffer.Format.Channels, _inputChannelCount, settings.MatrixCoefficients);
            sound.SourceVoice.SetFrequencyRatio(settings.DopplerFactor);
        }
        private void PlatformDispose(bool disposing)
        {
            if (disposing)
            {
                if (_reverb != null)
                {
                    _reverb.Dispose();
                }

                if (_voice != null && SoundEffect.MasterVoice != null)
                {
                    _voice.DestroyVoice();
                    _voice.Dispose();
                }
            }
            _voice  = null;
            _effect = null;
            _reverb = null;
        }
예제 #23
0
        public void Stop()
        {
            if (playingTask != null)
            {
                quitEvent.Set();
                playingTask.Wait();
                playingTask = null;
            }

            if (sourceVoice != null)
            {
                sourceVoice.Stop();
                sourceVoice.FlushSourceBuffers();
                sourceVoice.DestroyVoice();
                sourceVoice.BufferEnd -= SourceVoice_BufferEnd;
                sourceVoice.Dispose();
                sourceVoice = null;
            }
        }
예제 #24
0
        protected void DestroySourceVoice()
        {
            if (_sourceVoice != null)
            {
                _sourceVoice.BufferEnd -= OnBufferEnd;
                if (!_sourceVoice.IsDisposed)
                {
                    _sourceVoice.DestroyVoice();
                    _sourceVoice.Dispose();
                }
                _sourceVoice = null;
            }
            if (_bufferEndEvent != null)
            {
                _bufferEndEvent.Dispose();
            }
            _bufferEndEvent = null;

            _isPlaying = false;
        }
예제 #25
0
        private static async Task PlaySoundAsync(XAudio2 device, Stream baseStream)
        {
            baseStream.Position = 0;

            var stream = new SoundStream(baseStream);

            await using var dataStream = stream.ToDataStream();
            var buffer = new AudioBuffer(dataStream);

            using var voice = new SourceVoice(device, stream.Format, true);
            voice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
            voice.Start();

            while (voice.State.BuffersQueued > 0)
            {
                await Task.Delay(TimeSpan.FromMilliseconds(1));
            }

            voice.DestroyVoice();
        }
예제 #26
0
        public void Dispose()
        {
            foreach (KeyValuePair <int, SourceVoice> pair in voiceList)
            {
                if (!pair.Value.IsDisposed)
                {
                    pair.Value.DestroyVoice();
                    pair.Value.Dispose();
                }
            }
            if (nextVoice != null && !nextVoice.IsDisposed)
            {
                nextVoice.DestroyVoice();
                nextVoice.Dispose();
            }

            if (stream != null)
            {
                stream.Dispose();
            }
        }
예제 #27
0
        /// <summary>
        /// Play a sound file. Supported format are Wav(pcm+adpcm) and XWMA
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="text">Text to display</param>
        /// <param name="fileName">Name of the file.</param>
        static void PLaySoundFile(XAudio2 device, string text, string fileName)
        {
            Console.WriteLine("{0} => {1} (Press esc to skip)", text, fileName);
            var stream     = new SoundStream(File.OpenRead(fileName));
            var waveFormat = stream.Format;
            var buffer     = new AudioBuffer
            {
                Stream     = stream.ToDataStream(),
                AudioBytes = (int)stream.Length,
                Flags      = BufferFlags.EndOfStream
            };

            stream.Close();

            var sourceVoice = new SourceVoice(device, waveFormat, true);

            // Adds a sample callback to check that they are working on source voices
            sourceVoice.BufferEnd += (context) => Console.WriteLine(" => event received: end of buffer");
            sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
            sourceVoice.Start();

            int count = 0;

            while (sourceVoice.State.BuffersQueued > 0 && !IsKeyPressed(ConsoleKey.Escape))
            {
                if (count == 50)
                {
                    Console.Write(".");
                    Console.Out.Flush();
                    count = 0;
                }
                Thread.Sleep(10);
                count++;
            }
            Console.WriteLine();

            sourceVoice.DestroyVoice();
            sourceVoice.Dispose();
            buffer.Stream.Dispose();
        }
예제 #28
0
        public static void PlayXAudioSound(object soundFile)
        {
            try
            {
                xaudio2        = new XAudio2();
                masteringVoice = new MasteringVoice(xaudio2);

                var stream     = new SoundStream(File.OpenRead(soundFile as string));
                var waveFormat = stream.Format;
                buffer = new AudioBuffer
                {
                    Stream     = stream.ToDataStream(),
                    AudioBytes = (int)stream.Length,
                    Flags      = BufferFlags.EndOfStream
                };
                stream.Close();

                sourceVoice = new SourceVoice(xaudio2, waveFormat, true);
                sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
                sourceVoice.Start();

                while (sourceVoice.State.BuffersQueued > 0)
                {
                    Thread.Sleep(1);
                }

                sourceVoice.DestroyVoice();
                sourceVoice.Dispose();
                sourceVoice = null;
                buffer.Stream.Dispose();

                xaudio2.Dispose();
                masteringVoice.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        /// <summary>
        /// Dispose resources
        /// </summary>
        /// <param name="disposing">Free managed resources</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (voice != null)
                {
                    voice.BufferStart -= SourceVoice_BufferStart;
                    voice.BufferEnd   -= SourceVoice_BufferEnd;
                    voice.LoopEnd     -= SourceVoice_LoopEnd;
                }

                voice?.Stop(0);
                voice?.FlushSourceBuffers();
                if (isReverbSubmixEnabled)
                {
                    voice?.SetOutputVoices(null);
                    isReverbSubmixEnabled = false;
                }
                voice?.DestroyVoice();
                voice?.Dispose();
                voice = null;
            }
        }
예제 #30
0
        private void ReleaseSourceVoice()
        {
            if (voice != null && !voice.IsDisposed)
            {
                voice.Stop(0);
                voice.FlushSourceBuffers();
                if (isReverbSubmixEnabled)
                {
                    voice.SetOutputVoices((VoiceSendDescriptor[])null);
                    isReverbSubmixEnabled = false;
                }

                if (Effect.VoicePool.IsDisposed)
                {
                    voice.DestroyVoice();
                    voice.Dispose();
                }
                else
                {
                    Effect.VoicePool.Return(voice);
                }
            }
            voice = null;
        }