Ejemplo n.º 1
0
        private void DisposeWave()
        {
            lock (mutex)
            {
                if (dataStream != null)
                {
                    dataStream.Dispose();
                    dataStream = null;
                }

                if (audioBuffer != null)
                {
                    audioBuffer.Dispose();
                    audioBuffer = null;
                }

                if (sourceVoice != null)
                {
                    if (currentStream != null)
                    {
                        sourceVoice.BufferEnd -= bufferEndCallback;
                    }
                    sourceVoice.Stop();
                    sourceVoice.FlushSourceBuffers();
                    sourceVoice.Dispose();
                    sourceVoice = null;
                }

                current       = null;
                currentStream = null;
            }
        }
Ejemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();
            var mainThread  = Thread.CurrentThread;
            var audioThread = new Thread(() =>
            {
                var xaDev = new XA.XAudio2();
                xaDev.StartEngine();

                var wf = new WaveFormat
                {
                    FormatTag             = WaveFormatTag.Pcm,
                    Channels              = 2,
                    BitsPerSample         = 16,
                    BlockAlignment        = 4,
                    SamplesPerSecond      = SampleRate,
                    AverageBytesPerSecond = SampleRate * 4
                };

                // ReSharper disable once ObjectCreationAsStatement
                new XA.MasteringVoice(xaDev, 2, SampleRate, 0);
                var xaSv  = new XA.SourceVoice(xaDev, wf, XA.VoiceFlags.None);
                var xaBuf = new XA.AudioBuffer();

                var data = new byte[Samples * 4];
                ReadWave(data);
                xaBuf.AudioData  = new MemoryStream(data, false);
                xaBuf.AudioBytes = data.Length;
                xaSv.SubmitSourceBuffer(xaBuf);

                xaSv.BufferStart += delegate
                {
                    if (!mainThread.IsAlive)
                    {
                        return;
                    }
                    ReadWave(data);
                    xaBuf.AudioData.Position = 0;
                    xaSv.SubmitSourceBuffer(xaBuf);
                };

                xaSv.Start();
                while (mainThread.IsAlive)
                {
                    lock (tones)
                    {
                        MidiUpdate();
                    }
                    Thread.Sleep(0);
                }
                xaSv.Stop();
                xaSv.Dispose();
                xaDev.Dispose();
                midi.Dispose();
            });

            audioThread.Start();
        }
Ejemplo n.º 3
0
        public void Play(IPlayable wave)
        {
            lock (mutex)
            {
                if (wave == current)
                {
                    if (isPaused)
                    {
                        sourceVoice.Start();
                        isPaused = false;
                        return;
                    }
                }

                DisposeWave();
                xaudio.CommitChanges();
                isPaused      = false;
                current       = wave;
                currentStream = wave as IPlayableStream;

                if (decodeThread != null)
                {
                    System.Threading.Monitor.Pulse(mutex);
                    decodeThread = null;
                }

                if (currentStream == null)
                {
                    sourceVoice = new SourceVoice(xaudio, wave.WaveFormat);
                    dataStream  = new SlimDX.DataStream(wave.WaveData, true, false);

                    audioBuffer            = new SlimDX.XAudio2.AudioBuffer();
                    audioBuffer.AudioData  = dataStream;
                    audioBuffer.AudioBytes = wave.WaveData.Length;
                    audioBuffer.Flags      = BufferFlags.EndOfStream;

                    sourceVoice.SubmitSourceBuffer(audioBuffer);
                    sourceVoice.Start();
                }
                else
                {
                    streamLength   = currentStream.StreamLength;
                    streamBuffered = 0;

                    sourceVoice            = new SourceVoice(xaudio, wave.WaveFormat);
                    sourceVoice.BufferEnd += bufferEndCallback;

                    // Fill buffers initially
                    bool isDone = false;
                    for (var i = 0; i < NumStreamingBuffers; i++)
                    {
                        isDone = FillBuffer(i, currentStream);
                        if (isDone)
                        {
                            break;
                        }
                    }

                    sourceVoice.Start();
                    if (!isDone)
                    {
                        System.Threading.ParameterizedThreadStart threadProc = DecoderThread;
                        decodeThread      = new System.Threading.Thread(threadProc);
                        decodeThread.Name = "Vorbis Decoder Thread";
                        decodeThread.Start(currentStream);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void DisposeWave()
        {
            lock (mutex)
            {
                if (dataStream != null)
                {
                    dataStream.Dispose();
                    dataStream = null;
                }

                if (audioBuffer != null)
                {
                    audioBuffer.Dispose();
                    audioBuffer = null;
                }

                if (sourceVoice != null)
                {
                    if (currentStream != null)
                    {
                        sourceVoice.BufferEnd -= bufferEndCallback;
                    }
                    sourceVoice.Stop();
                    sourceVoice.FlushSourceBuffers();
                    sourceVoice.Dispose();
                    sourceVoice = null;
                }

                current = null;
                currentStream = null;
            }
        }
Ejemplo n.º 5
0
        public void Play(IPlayable wave)
        {
            lock (mutex)
            {
                if (wave == current)
                {
                    if (isPaused)
                    {
                        sourceVoice.Start();
                        isPaused = false;
                        return;
                    }
                }

                DisposeWave();
                xaudio.CommitChanges();
                isPaused = false;
                current = wave;
                currentStream = wave as IPlayableStream;

                if (decodeThread != null)
                {
                    System.Threading.Monitor.Pulse(mutex);
                    decodeThread = null;
                }

                if (currentStream == null)
                {
                    sourceVoice = new SourceVoice(xaudio, wave.WaveFormat);
                    dataStream = new SlimDX.DataStream(wave.WaveData, true, false);

                    audioBuffer = new SlimDX.XAudio2.AudioBuffer();
                    audioBuffer.AudioData = dataStream;
                    audioBuffer.AudioBytes = wave.WaveData.Length;
                    audioBuffer.Flags = BufferFlags.EndOfStream;

                    sourceVoice.SubmitSourceBuffer(audioBuffer);
                    sourceVoice.Start();
                }
                else
                {
                    streamLength = currentStream.StreamLength;
                    streamBuffered = 0;

                    sourceVoice = new SourceVoice(xaudio, wave.WaveFormat);
                    sourceVoice.BufferEnd += bufferEndCallback;

                    // Fill buffers initially
                    bool isDone = false;
                    for (var i = 0; i < NumStreamingBuffers; i++)
                    {
                        isDone = FillBuffer(i, currentStream);
                        if (isDone)
                        {
                            break;
                        }
                    }

                    sourceVoice.Start();
                    if (!isDone)
                    {
                        System.Threading.ParameterizedThreadStart threadProc = DecoderThread;
                        decodeThread = new System.Threading.Thread(threadProc);
                        decodeThread.Name = "Vorbis Decoder Thread";
                        decodeThread.Start(currentStream);
                    }
                }
            }
        }