(C# SharpDX) X3DAudio 正在播放 SourceVoice 但没有声音

问题描述 投票:0回答:1

我正在尝试使用 SharpDX 播放 3D 位置的声音。这是我的代码:

using (var xaudio = new XAudio2())
        {
            var masteringVoice = new MasteringVoice(xaudio);
            var x3dAudio = new X3DAudio((Speakers)masteringVoice.ChannelMask);
            const int channels = 2; 
            const int srcChannels = channels;
            const int destChannels = channels;
            Emitter emitter = new Emitter();
            emitter.Position = new RawVector3(0, 0, 0); 
            emitter.Velocity = new RawVector3(0, 0, 0);
            emitter.OrientFront = new RawVector3(0, 0, 1);
            emitter.OrientTop = new RawVector3(0, 1, 0);

            Listener listener = new Listener();
            listener.Position = new RawVector3(0, 0, 0); 
            listener.Velocity = new RawVector3(0, 0, 0);
            listener.OrientFront = new RawVector3(0, 0, 1);
            listener.OrientTop = new RawVector3(0, 1, 0);
            DspSettings dspSettings = x3dAudio.Calculate(listener, emitter, CalculateFlags.Matrix | CalculateFlags.Doppler, srcChannels, destChannels);
            string audioFilePath = "F:/test.wav";
            using (var fileS = new FileStream(audioFilePath, FileMode.Open, FileAccess.Read))
            {
                var waveFormat = new SharpDX.Multimedia.WaveFormat(48000, 2);
                var dataStream = new SoundStream(fileS);

                var audioBuffer = new AudioBuffer
                {
                    Stream = dataStream,
                    AudioBytes = (int)dataStream.Length,
                    Flags = SharpDX.XAudio2.BufferFlags.EndOfStream
                };

                var sourceVoice = new SourceVoice(xaudio, dataStream.Format);
                sourceVoice.SubmitSourceBuffer(audioBuffer, dataStream.DecodedPacketsInfo);
                sourceVoice.Start();
                sourceVoice.SetOutputMatrix(srcChannels, destChannels, dspSettings.MatrixCoefficients);
                sourceVoice.SetFrequencyRatio(dspSettings.DopplerFactor);
                sourceVoice.SetVolume(1f);
                while (sourceVoice.State.BuffersQueued > 0)
                {
                    System.Threading.Thread.Sleep(100);
                }
                sourceVoice.Stop();
                sourceVoice.Dispose();
            }
        }

我正在使用 Visual Studio 2022 和 .NET 7.0 进行编码。我不知道为什么,当我运行这段代码时,我听不到任何声音。我尝试使用这些设置,但似乎没有任何效果。 但是当我删除这两行时,一切都会很好:

sourceVoice.SetOutputMatrix(srcChannels, destChannels, dspSettings.MatrixCoefficients);
sourceVoice.SetFrequencyRatio(dspSettings.DopplerFactor);

任何人都可以帮助我让这段代码正常工作吗?

我已经尝试在没有 X3DAudio 的情况下运行代码,一切都很顺利,我可以听到声音。我尝试以这种方式初始化 X3DAudio,但它似乎不起作用:

var x3dAudio = new X3DAudio(Speakers.FrontLeft | Speakers.FrontRight);

我做了一些测试,显然我听不到任何声音,因为 MatrixCoefficient 看起来像 {0, 0, 0, 0} 这意味着不会发出声音。当我使用 {1,1,1,1} 时,我可以听到完美的声音。这意味着 X3DAudio 没有正确计算矩阵。也许它没有正确初始化...

c# 3d directx sharpdx xaudio2
1个回答
0
投票

问题解决了,我只是添加了这一行:

emitter.ChannelCount = 1;
© www.soinside.com 2019 - 2024. All rights reserved.