示例#1
0
        private void CheckIfDeviceChanged()
        {
            // DeviceCount cannot be called on XAudio >= 2.8 (Windows 8)
            // Maybe call DeviceCount with XAudio 2.7 and use different API for windows 8
            // http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx
            // DeviceCount is very slow even on Windows 7 sometimes, don't know why
            if (m_deviceLost)// || m_lastDeviceCount != m_audioEngine.DeviceCount)
            {
                m_deviceLost = false;

                try
                {
                    Init();
                }
                catch (Exception ex)
                {
                    MyLog.Default.WriteLine("Exception during loading audio engine. Game continues, but without sound. Details: " + ex.ToString(), LoggingOptions.AUDIO);
                    MyLog.Default.WriteLine("Device ID: " + m_deviceDetails.DeviceID, LoggingOptions.AUDIO);
                    MyLog.Default.WriteLine("Device name: " + m_deviceDetails.DisplayName, LoggingOptions.AUDIO);
                    MyLog.Default.WriteLine("Device role: " + m_deviceDetails.Role, LoggingOptions.AUDIO);
                    MyLog.Default.WriteLine("Output format: " + m_deviceDetails.OutputFormat, LoggingOptions.AUDIO);

                    //  This exception is the only way I can know if we can play sound (e.g. if computer doesn't have sound card).
                    //  I didn't find other ways of checking it.
                    m_canPlay = false;
                }

                if (m_initParams.SimulateNoSoundCard)
                {
                    m_canPlay = false;
                }

                if (m_canPlay)
                {
                    if (m_cueBank != null)
                    {
                        m_cueBank.SetAudioEngine(m_audioEngine);
                    }
                    m_gameAudioVoice.SetVolume(m_volumeDefault);
                    m_hudAudioVoice.SetVolume(m_volumeHud);
                    m_musicAudioVoice.SetVolume(m_volumeMusic);
                    //TODO: JN reinit sounds so they play
                    m_3Dsounds.Clear();

                    if ((m_musicCue != null) && m_musicCue.IsPlaying)
                    {
                        // restarts music cue
                        m_musicCue = PlaySound(m_musicCue.CueEnum);
                        if (m_musicCue != null)
                        {
                            m_musicCue.SetOutputVoices(m_musicAudioVoiceDesc);
                        }

                        UpdateMusic(0);
                    }
                }
            }
        }
示例#2
0
文件: Playback.cs 项目: XAYRGA/JPlay
 public void Update(int deltaMs)
 {
     foreach (KeyValuePair <byte, NoteData> item in voiceTable)
     {
         item.Value.Voice.SetFrequencyRatio(item.Value.FrequencyRatio * PitchFrequencyRatio);
         if (panChanged && !item.Value.IsPercussion)
         {
             item.Value.Voice.SetOutputMatrix(1, 2, outputMatrix);
         }
     }
     submix.SetVolume(ValueToAmplitude(Volume));
     if (panChanged)
     {
         panChanged = false;
     }
     if (echoChanged)
     {
         EchoParameters effectParameter = default(EchoParameters);
         effectParameter.Delay     = 160f;
         effectParameter.Feedback  = 0.6f;
         effectParameter.WetDryMix = (float)(int)Echo / 384f;
         submix.SetEffectParameters(0, effectParameter);
         echoChanged = false;
     }
 }
示例#3
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="AudioManager" /> class.
        /// </summary>
        /// <param name="listener">         The listener. </param>
        /// <param name="fxSoundPoolLimit"> The effects sound pool limit. </param>
        /// <param name="speakers">         The speakers. </param>
        /// <param name="deviceID">         (Optional) Identifier for the device. </param>
        /// <exception cref="ArgumentException">
        ///     Thrown when one or more arguments have unsupported or
        ///     illegal values.
        /// </exception>
        public AudioManager(Listener listener, int fxSoundPoolLimit, Speakers speakers, string?deviceID = null)
        {
            _listener = listener;
            if (fxSoundPoolLimit <= 0)
            {
                throw new ArgumentException("fxSoundPoolLimit must be bigger than 0");
            }
            _soundBuffer        = new Dictionary <int, SoundBuffer>(128);
            _fxLinkedSoundList  = new LinkedSoundList(fxSoundPoolLimit);
            _envLinkedSoundList = new LinkedSoundList();

#if DEBUG
            _xAudio2 = new XAudio2(XAudio2Flags.DebugEngine, ProcessorSpecifier.AnyProcessor);
#else
            _xAudio2 = new XAudio2(XAudio2Flags.None, ProcessorSpecifier.AnyProcessor, XAudio2Version.Default);
#endif
            if (_xAudio2.Version == XAudio2Version.Version27)
            {
                if (int.TryParse(deviceID, out int deviceID27))
                {
                    _masteringVoice = new MasteringVoice(
                        _xAudio2, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate, deviceID27);
                }
                else
                {
                    _masteringVoice = new MasteringVoice(
                        _xAudio2, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate, deviceID);
                }
            }
            else
            {
                _masteringVoice = new MasteringVoice(
                    _xAudio2, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate, deviceID);
            }
            _masteringVoice.SetVolume(_masterVolume);

            _x3DAudio = new X3DAudio(speakers, X3DAudio.SpeedOfSound);

            _masteringVoice.GetVoiceDetails(out VoiceDetails details);
            _inputChannelCount = details.InputChannelCount;

            _fxSubmixVoice = new SubmixVoice(_xAudio2, _inputChannelCount, details.InputSampleRate);
            _fxSubmixVoice.SetVolume(_fxVolume);

            _envSubmixVoice = new SubmixVoice(_xAudio2, _inputChannelCount, details.InputSampleRate);
            _envSubmixVoice.SetVolume(_envVolume);

            _fxVoiceSendDescriptor  = new VoiceSendDescriptor(VoiceSendFlags.None, _fxSubmixVoice);
            _envVoiceSendDescriptor = new VoiceSendDescriptor(VoiceSendFlags.None, _envSubmixVoice);
        }
示例#4
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);

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