Example #1
0
 private void PlaySound(int soundID,
                        Vector3 emitterPos,
                        float volume,
                        float maxDistance,
                        LinkedSoundList list,
                        ref VoiceSendDescriptor voiceSendDescriptor,
                        Action <IntPtr>?onFxEnd = null)
 {
     PlaySound(
         soundID,
         new Emitter
     {
         ChannelCount = 1,
         VolumeCurve  =
             new[]
         {
             new CurvePoint {
                 Distance = 0.0f, DspSetting = 1.0f
             },
             new CurvePoint {
                 Distance = 1.0f, DspSetting = 0.0f
             }
         },
         CurveDistanceScaler = maxDistance,
         OrientFront         = Vector3.UnitZ,
         OrientTop           = Vector3.UnitY,
         Position            = emitterPos,
         Velocity            = new Vector3(0, 0, 0)
     }, volume, list, ref voiceSendDescriptor, onFxEnd);
 }
Example #2
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);
        }
Example #3
0
        private void PlaySound(int soundID,
                               Emitter emitter,
                               float volume,
                               LinkedSoundList list,
                               ref VoiceSendDescriptor voiceSendDescriptor,
                               Action <IntPtr>?onFxEnd = null)
        {
            if (!_soundBuffer.TryGetValue(soundID, out SoundBuffer buffer))
            {
                return;
            }

            SourceVoice sourceVoice = new SourceVoice(_xAudio2, buffer.Format, VoiceFlags.None, true);

            sourceVoice.SetVolume(volume);
            sourceVoice.SubmitSourceBuffer(buffer.AudioBuffer, buffer.DecodedPacketsInfo);
            sourceVoice.SetOutputVoices(voiceSendDescriptor);

            LinkedSoundList.Sound sound = new LinkedSoundList.Sound(emitter, sourceVoice);
            list.Add(sound);

            sourceVoice.BufferEnd += _ =>
            {
                list.Remove(sound);
                sourceVoice.DestroyVoice();
            };

            if (onFxEnd != null)
            {
                sourceVoice.BufferEnd += onFxEnd;
            }
            sourceVoice.Start();

            DspSettings settings = _x3DAudio.Calculate(
                _listener,
                sound.Emitter,
                CalculateFlags.Matrix | CalculateFlags.Doppler,
                buffer.Format.Channels,
                _inputChannelCount);

            sound.SourceVoice.SetOutputMatrix(buffer.Format.Channels, _inputChannelCount, settings.MatrixCoefficients);
            sound.SourceVoice.SetFrequencyRatio(settings.DopplerFactor);
        }
Example #4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="LinkedSoundList" /> class.
 /// </summary>
 /// <param name="list"> The list. </param>
 public Enumerator(LinkedSoundList list)
 {
     _list    = list;
     _node    = list._head;
     _current = null;
 }