public X3DAudioEngine() { _xaudio2 = new XAudio2(); _masteringVoice = new MasteringVoice(_xaudio2); _deviceFormat = _xaudio2.GetDeviceDetails(0).OutputFormat; _x3dAudio = new X3DAudio(_deviceFormat.ChannelMask); Position = new Vector3D(0, 0, 0); Rotation = System.Windows.Media.Media3D.Quaternion.Identity; }
// This region is currently nor implemented nor exposed to the client private void AudioEngineImpl(AudioDevice device) { try { XAudio2 = new XAudio2(); X3DAudio = new X3DAudio(Speakers.Stereo); // only stereo mode currently supported XAudio2.CriticalError += XAudio2OnCriticalError; MasteringVoice = new MasteringVoice(XAudio2, 2, (int)AudioSampleRate); // Let XAudio choose an adequate sampling rate for the platform and the configuration but not number of channels [force to stereo 2-channels]. if (!mediaEngineStarted) { // MediaManager.Startup(); <- MediaManager.Shutdown is buggy (do not set isStartUp to false) so we are forced to directly use MediaFactory.Startup here while sharpDX is not corrected. MediaFactory.Startup(0x20070, 0); mediaEngineStarted = true; } PlatformSpecificInit(); } catch (DllNotFoundException exp) { State = AudioEngineState.Invalidated; Logger.Warning("One or more of the XAudio and MediaFoundation dlls were not found on the computer. " + "Audio functionalities will not fully work for the current game instance." + "To fix the problem install or repair the installation of XAudio and Media Foundation. [Exception details: {0}]", exp.Message); } catch (SharpDX.SharpDXException exp) { State = AudioEngineState.Invalidated; if (exp.ResultCode == XAudioErrorCodes.ErrorInvalidCall) { Logger.Warning("Initialization of the audio engine failed. This may be due to missing audio hardware or missing audio outputs. [Exception details: {0}]", exp.Message); } else if (exp.ResultCode == 0x8007007E) { Logger.Warning( "Audio engine initialization failed. This is probably due to missing dll on your computer. " + "Please check that XAudio2 and MediaFoundation are correctly installed.[Exception details: {0}]", exp.Message); } else { Logger.Warning("Audio engine initialization failed. [Exception details: {0}]", exp.Message); } } }
public MyX3DAudio(WaveFormatExtensible format) { m_x3dAudio = new X3DAudio(format.ChannelMask); m_dsp = new DspSettings(1, format.Channels); }
public MyX3DAudio(AudioEngine engine) { m_x3dAudio = new X3DAudio(engine.FinalMixFormat.ChannelMask); m_dsp = new DspSettings(1, engine.FinalMixFormat.Channels); }
/// <summary> /// SharpDX X3DAudio sample. Plays a generated sound rotating around the listener. /// </summary> static void Main(string[] args) { var xaudio2 = new XAudio2(); using (var masteringVoice = new MasteringVoice(xaudio2)) { // Instantiate X3DAudio var x3dAudio = new X3DAudio(Speakers.Stereo); var emitter = new Emitter { ChannelCount = 1, CurveDistanceScaler = float.MinValue, OrientFront = new Vector3(0, 0, 1), OrientTop = new Vector3(0, 1, 0), Position = new Vector3(0, 0, 0), Velocity = new Vector3(0, 0, 0) }; var listener = new Listener { OrientFront = new Vector3(0, 0, 1), OrientTop = new Vector3(0, 1, 0), Position = new Vector3(0, 0, 0), Velocity = new Vector3(0, 0, 0) }; var waveFormat = new WaveFormat(44100, 32, 1); var sourceVoice = new SourceVoice(xaudio2, waveFormat); int bufferSize = waveFormat.ConvertLatencyToByteSize(60000); var dataStream = new DataStream(bufferSize, true, true); int numberOfSamples = bufferSize/waveFormat.BlockAlign; for (int i = 0; i < numberOfSamples; i++) { float value = (float) (Math.Cos(2*Math.PI*220.0*i/waveFormat.SampleRate)*0.5); dataStream.Write(value); } dataStream.Position = 0; var audioBuffer = new AudioBuffer {Stream = dataStream, Flags = BufferFlags.EndOfStream, AudioBytes = bufferSize}; //var reverb = new Reverb(); //var effectDescriptor = new EffectDescriptor(reverb); //sourceVoice.SetEffectChain(effectDescriptor); //sourceVoice.EnableEffect(0); sourceVoice.SubmitSourceBuffer(audioBuffer, null); sourceVoice.Start(); Console.WriteLine("Play a sound rotating around the listener"); for (int i = 0; i < 1200; i++) { // Rotates the emitter var rotateEmitter = Matrix.RotationY(i/5.0f); var newPosition = Vector3.Transform(new Vector3(0, 0, 1000), rotateEmitter); var newPositionVector3 = new Vector3(newPosition.X, newPosition.Y, newPosition.Z); emitter.Velocity = (newPositionVector3 - emitter.Position)/0.05f; emitter.Position = newPositionVector3; // Calculate X3DAudio settings var dspSettings = x3dAudio.Calculate(listener, emitter, CalculateFlags.Matrix | CalculateFlags.Doppler, 1, 2); // Modify XAudio2 source voice settings sourceVoice.SetOutputMatrix(1, 2, dspSettings.MatrixCoefficients); sourceVoice.SetFrequencyRatio(dspSettings.DopplerFactor); // Wait for 50ms Thread.Sleep(50); } } }