如何同步两个Xaudio2 Streaming SourceVoices

时间:2014-12-19 00:45:36

标签: c# sharpdx xaudio2

我使用SharpDX Samples(https://github.com/sharpdx/SharpDX-Samples/blob/master/WindowsDesktop/XAudio2/AudioPlayerApp/AudioPlayer.cs)的流媒体原理同时播放多种声音。

我有一个包含同一首歌的多个版本的列表。我想在同一时间开始它们。我为每首歌开始一个新任务,我使用CountDownEvent和ManualResetEvent的组合来确保它们同时启动:

在任务中,我准备缓冲区并向CountDownEvent发出歌曲已准备好播放的信号。然后歌曲等待来自ManualResetEvent的信号。当CountDownEvent有来自每首歌曲的信号准备就绪时,它会发出信号通知ManualResetEvent并调用SubmitSourceBuffer。

10首歌中有9首开始采样同步(歌曲之间没有可检测到的相位,只是响亮),但有时会略微偏离。

以下是一些要详细说明的代码:

            Task playingtask = Task.Factory.StartNew(() =>
        {
            int nextbuffer = 0;
            long zeroticks = MasterClock.Elapsed.Ticks;
            try
            {
                while (true) 
                {
                    if (oursong.ADecoder == null || oursong.Status == PlaybackStatus.Stop) { break; }

                    var sampleiterator = oursong.ADecoder.GetSamples(TimeSpan.FromTicks(oursong.Position)).GetEnumerator();

                    while (true) 
                    {
                        if (oursong.ADecoder == null || oursong.Status == PlaybackStatus.Stop) { break; }

                        foreach (SourceVoice SVoice in oursong.SVoices)
                        {
                            while (SVoice.State.BuffersQueued == oursong.BufferRing.Length && oursong.ADecoder != null)
                            {
                                BufferEndEvent.WaitOne(1);
                            }
                        }

                        if (!sampleiterator.MoveNext())
                        {
                            break;
                        }

                        var bufferpointer = sampleiterator.Current;

                        if (bufferpointer.Size > oursong.MBufferRing[nextbuffer].Size)
                        {
                            if (oursong.MBufferRing[nextbuffer].Pointer != IntPtr.Zero)
                            {
                                SharpDX.Utilities.FreeMemory(oursong.MBufferRing[nextbuffer].Pointer);

                            }

                            oursong.MBufferRing[nextbuffer].Pointer = SharpDX.Utilities.AllocateMemory(bufferpointer.Size);
                            oursong.MBufferRing[nextbuffer].Size = bufferpointer.Size;
                        }



                        SharpDX.Utilities.CopyMemory(oursong.MBufferRing[nextbuffer].Pointer, bufferpointer.Pointer, bufferpointer.Size);

                        oursong.BufferRing[nextbuffer].AudioDataPointer = oursong.MBufferRing[nextbuffer].Pointer;
                        oursong.BufferRing[nextbuffer].AudioBytes = bufferpointer.Size;

                        if (oursong.Status == PlaybackStatus.Ready)
                        {
                            CountDownEvent.Signal();
                            SongManualResetEvent.WaitOne();
                            oursong.Status = PlaybackStatus.Start;
                        }

                        foreach (SourceVoice SVoice in oursong.SVoices)
                        {
                            SVoice.SubmitSourceBuffer(oursong.BufferRing[nextbuffer], null);
                        }
                        oursong.Position += MasterClock.Elapsed.Ticks - zeroticks;
                        zeroticks = MasterClock.Elapsed.Ticks;
                        nextbuffer = ++nextbuffer % oursong.BufferRing.Length;

                    }
                }
            }

            finally
            {
                DisposeAudio(oursong);
                oursong.Position = oursong.Start;
            }

        }, TaskCreationOptions.LongRunning);

旁注:我使用foreach语句将每首歌曲的缓冲区发送到多个音频设备,每首歌曲的顺序相同。 Ready状态预先使用预先设置内存,音频编码器等的加载方法设置。

我知道可以使用操作集同时启动sourcevoices但是有另一种方法(使用时钟或其他同步方法)以确保每首播放歌曲同时发送样本吗?

1 个答案:

答案 0 :(得分:0)

您应该可以使用XAudio2 Operation Sets直接实现此目的,而无需使用外部自定义同步事件解决此问题。