(C# SharpDX) X3DAudio is playing SourceVoice but no sound comes out

67 Views Asked by At

I'm trying to play a sound with 3D position using SharpDX. Here is my code:

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();
            }
        }

I'm coding with Visual Studio 2022 and .NET 7.0. I don't know why, when i run this code i can't hear any sound. I tried to play with the settings but nothing seems to work. But when i remove these two lines, everything goes fines:

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

Can anyone help me to have this code working properly ?

I alreay tried to run the code without X3DAudio and everything goes fines, i can hear the sound. I Triedd to initialize X3DAudio this way, but it didn't seem to work:

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

I made some tests and obviously i can't hear anything because the MatrixCoefficient looks like {0, 0, 0, 0} which means no sound will come out. When i use {1,1,1,1} i can hear perfectly. That means X3DAudio is not calculating the matrix properly. Maybe it wasn't properly initialized...

1

There are 1 best solutions below

0
On

Problem solved, i simply added these two lines:

var x3dAudio = new X3DAudio(Speakers.FrontLeft | Speakers.FrontRight);
emitter.ChannelCount = 1;
emitter.CurveDistanceScaler = 1f;