public static IEnumerable<string> GetDeviceNames()
		{
			using (XAudio2 device = new XAudio2())
			{
				return Enumerable.Range(0, device.DeviceCount).Select(n => device.GetDeviceDetails(n).DisplayName).ToList();
			}
		}
        public Program()
        {
            audioDevice = new XAudio2(XAudio2Flags.DebugEngine, ProcessorSpecifier.AnyProcessor);
            masteringVoice = new MasteringVoice(audioDevice, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate, 0);

            DeviceDetails deviceDetails = audioDevice.GetDeviceDetails(0);
            x3DInstance = new X3DAudio(deviceDetails.OutputFormat.ChannelMask, 340f);

            //x3d.Calculate(listener, emitter, SlimDX.X3DAudio.CalculateFlags.ZeroCenter, 2, 2);
        }
		public XAudio2SoundOutput(Sound sound)
		{
			_sound = sound;
			_device = new XAudio2();
			int? deviceIndex = Enumerable.Range(0, _device.DeviceCount)
				.Select(n => (int?)n)
				.FirstOrDefault(n => _device.GetDeviceDetails(n.Value).DisplayName == Global.Config.SoundDevice);
			_masteringVoice = deviceIndex == null ?
				new MasteringVoice(_device, Sound.ChannelCount, Sound.SampleRate) :
				new MasteringVoice(_device, Sound.ChannelCount, Sound.SampleRate, deviceIndex.Value);
		}
Exemple #4
0
        /// <summary>
        /// Initializes XAudio2 and MasteringVoice.  And registers itself as an <see cref="IContentReaderFactory"/>
        /// </summary>
        /// <exception cref="InvalidOperationException">Is thrown when the IContentManager is not an instance of <see cref="ContentManager"/>.</exception>
        /// <exception cref="AudioException">Is thrown when the <see cref="AudioManager"/> instance could not be initialized (either due to unsupported features or missing audio-device).</exception>
        public override void Initialize()
        {
            base.Initialize();
            contentManager = Content as ContentManager;
            if (contentManager == null)
            {
                throw new InvalidOperationException("Unable to initialize AudioManager. Expecting IContentManager to be an instance of ContentManager");
            }
            try
            {
#if DEBUG && !WIN8METRO && !WP8 && !DIRECTX11_1
                try
                {
                    // "XAudio2Flags.DebugEngine" is supported only in XAudio 2.7, but not in newer versions
                    // msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.xaudio2.xaudio2create(v=vs.85).aspx
                    Device = new XAudio2(XAudio2Flags.DebugEngine, ProcessorSpecifier.DefaultProcessor);
                    Device.StartEngine();
                }
                catch (Exception)
#endif
                {
                    Device = new XAudio2(XAudio2Flags.None, ProcessorSpecifier.DefaultProcessor);
                    Device.StartEngine();
                }
            }
            catch (SharpDXException ex)
            {
                DisposeCore();
                throw new AudioException("Error creating XAudio device.", ex);
            }

#if !W8CORE && !DIRECTX11_1
            if (Device.DeviceCount == 0)
            {
                DisposeCore();
                throw new AudioException("No default audio devices detected.");
            }
#endif

#if W8CORE || DIRECTX11_1
            string deviceId = null;
#else
            const int deviceId = 0;
#endif
            try
            {
                MasteringVoice = new MasteringVoice(Device, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate, deviceId);
            }
            catch (SharpDXException ex)
            {
                DisposeCore();
#if W8CORE
                if (ex.ResultCode == AudioManager.NotFound)
                {
                    throw new AudioException("No default audio devices detected.");
                }
                else
#endif
                {
                    throw new AudioException("Error creating mastering voice.", ex);
                }
            }

            MasteringVoice.SetVolume(masterVolume);

#if W8CORE || DIRECTX11_1
            Speakers = (Speakers)MasteringVoice.ChannelMask;
#else
            var deviceDetails = Device.GetDeviceDetails(deviceId);
            Speakers = deviceDetails.OutputFormat.ChannelMask;
#endif

            if (IsMasteringLimiterEnabled)
            {
                try
                {
                    CreateMasteringLimitier();
                }
                catch (Exception)
                {
                    DisposeCore();
                    throw;
                }
            }

            if (IsSpatialAudioEnabled)
            {
                try
                {
                    x3DAudio = new X3DAudio(Speakers, speedOfSound);
                }
                catch (Exception)
                {
                    DisposeCore();
                    throw;
                }
            }

            if (IsReverbEffectEnabled)
            {
                try
                {
                    CreateReverbSubmixVoice();
                }
                catch (Exception)
                {
                    DisposeCore();
                    throw;
                }
            }

            contentManager.ReaderFactories.Add(new AudioContentReaderFactory());
        }
Exemple #5
0
        private void StartEngine()
        {
            if (m_audioEngine != null)
            {
                DisposeVoices();
                m_audioEngine.Dispose();
            }

            // Init/reinit engine
            m_audioEngine = new XAudio2();

            // A way to disable SharpDX callbacks
            //var meth = m_audioEngine.GetType().GetMethod("UnregisterForCallbacks_", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            //var callbacks = m_audioEngine.GetType().GetField("engineShadowPtr", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            //meth.Invoke((object)m_audioEngine, new object[] { callbacks.GetValue(m_audioEngine) });

            m_audioEngine.CriticalError += m_audioEngine_CriticalError;
            m_lastDeviceCount            = m_audioEngine.DeviceCount;


            m_deviceNumber = 0;
            while (true) //find first non com device
            {
                try
                {
                    m_deviceDetails = m_audioEngine.GetDeviceDetails(m_deviceNumber);
                    if (m_deviceDetails.Role == DeviceRole.DefaultCommunicationsDevice)
                    {
                        m_deviceNumber++;
                        if (m_deviceNumber == m_audioEngine.DeviceCount)
                        {
                            m_deviceNumber--;
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine(string.Format("Failed to get device details.\n\tdevice no.: {0}\n\tdevice count: {1}", m_deviceNumber, m_audioEngine.DeviceCount), LoggingOptions.AUDIO);
                    MyLog.Default.WriteLine(e.ToString());
                    m_deviceNumber  = 0;
                    m_deviceDetails = m_audioEngine.GetDeviceDetails(m_deviceNumber);
                    break;
                }
            }

            m_masterVoice = new MasteringVoice(m_audioEngine, deviceIndex: m_deviceNumber);

            //var limiter = new SharpDX.XAPO.Fx.MasteringLimiter();
            //var param = limiter.Parameter;
            //param.Loudness = 10;
            //limiter.Parameter = param;
            //m_masterVoice.SetEffectChain(new EffectDescriptor[] { new EffectDescriptor(limiter) });
            //m_masterVoice.EnableEffect(0);
            ////limiter.Dispose();

            m_calculateFlags = CalculateFlags.Matrix | CalculateFlags.Doppler;
            if ((m_deviceDetails.OutputFormat.ChannelMask & Speakers.LowFrequency) != 0)
            {
                m_calculateFlags |= CalculateFlags.RedirectToLfe;
            }

            var masterDetails = m_masterVoice.VoiceDetails;

            m_gameAudioVoice      = new SubmixVoice(m_audioEngine, masterDetails.InputChannelCount, masterDetails.InputSampleRate);
            m_musicAudioVoice     = new SubmixVoice(m_audioEngine, masterDetails.InputChannelCount, masterDetails.InputSampleRate);
            m_hudAudioVoice       = new SubmixVoice(m_audioEngine, masterDetails.InputChannelCount, masterDetails.InputSampleRate);
            m_gameAudioVoiceDesc  = new VoiceSendDescriptor[] { new VoiceSendDescriptor(m_gameAudioVoice) };
            m_musicAudioVoiceDesc = new VoiceSendDescriptor[] { new VoiceSendDescriptor(m_musicAudioVoice) };
            m_hudAudioVoiceDesc   = new VoiceSendDescriptor[] { new VoiceSendDescriptor(m_hudAudioVoice) };

            if (m_mute)
            { // keep sounds muted
                m_gameAudioVoice.SetVolume(0);
                m_musicAudioVoice.SetVolume(0);
            }
        }
Exemple #6
0
        private void Initialize(string SoundDeviceName, int maxVoicesNbr)
        {
            //Default Xaudio2 objects ==========
            _xaudio2 = ToDispose(new XAudio2());
            if (SoundDeviceName == null)
            {
                _deviceDetail = _xaudio2.GetDeviceDetails(0);
            }
            _soundDevices = new List <string>();

            int customDeviceId = 0;

            //Get all sound devices
            for (int i = 0; i < _xaudio2.DeviceCount; i++)
            {
                _soundDevices.Add(_xaudio2.GetDeviceDetails(i).DisplayName);
                if (SoundDeviceName == _xaudio2.GetDeviceDetails(i).DisplayName)
                {
                    _deviceDetail  = _xaudio2.GetDeviceDetails(i);
                    customDeviceId = i;
                }
            }

            logger.Info("s33m3 sound engine started for device : " + _deviceDetail.DisplayName);

            _x3DAudio = new X3DAudio(_deviceDetail.OutputFormat.ChannelMask);

            if (SoundDeviceName == null)
            {
                _masteringVoice = ToDispose(new MasteringVoice(_xaudio2, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate, 0));
            }
            else
            {
                _masteringVoice = ToDispose(new MasteringVoice(_xaudio2, _deviceDetail.OutputFormat.Channels, _deviceDetail.OutputFormat.SampleRate, customDeviceId));
            }

            //Default state values =============
            _maxVoicePoolPerFileType = maxVoicesNbr;

            _soundDataSources     = new Dictionary <string, ISoundDataSource>();
            _soundVoices          = new Dictionary <int, ISoundVoice[]>();
            _soundProcessingQueue = new List <ISoundVoice>();

            _listener = new Listener();

            //Start Sound voice processing thread
            _syncro = new ManualResetEvent(false);
            _d3dEngine.RunningThreadedWork.Add("SoundEngine");
            _d3dEngine.OnShuttingDown += d3dEngine_OnShuttingDown;
            _thread = new Thread(DataSoundPocessingAsync)
            {
                Name = "SoundEngine"
            };                                                                      //Start the main loop
            _stopThreading = false;
            _thread.Start();

            GeneralSoundVolume = 1.0f;

            GlobalMusicVolume = 1;
            GlobalFXVolume    = 1;

            _xaudio2.StartEngine();
        }