提问人:XTankKiller 提问时间:11/16/2023 最后编辑:XTankKiller 更新时间:11/16/2023 访问量:34
(C# SharpDX)X3DAudio 正在播放 SourceVoice,但没有声音
(C# SharpDX) X3DAudio is playing SourceVoice but no sound comes out
问:
我正在尝试使用 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 未正确计算矩阵。也许它没有正确初始化......
答:
1赞
XTankKiller
11/16/2023
#1
问题解决了,我只是添加了这两行:
var x3dAudio = new X3DAudio(Speakers.FrontLeft | Speakers.FrontRight);
emitter.ChannelCount = 1;
emitter.CurveDistanceScaler = 1f;
评论