private void OnSoundLoaded(string soundId, AudioData data) { if (data == null) { Logger.Error($"Sound failed to load {GetFullName()}: {soundId}"); return; } _callback?.Dispose(); _callback = new VoiceCallback(); _callback.LoopEnd += CallbackOnLoopEnd; var source = data.AudioSource; _voice = SoundService.XAudio2.CreateSourceVoice(source.WaveFormat, VoiceFlags.None, XAudio2.DefaultFrequencyRatio, null, null, null); var length = (TimeSpan)source.GetLength(); var buffer = source.ToByteArray(); var stream = new DataStream(buffer.Length, true, true); stream.Write(buffer, 0, buffer.Length); _audioBuffer = new XAudio2Buffer { AudioDataPtr = stream.DataPointer, AudioBytes = buffer.Length, Flags = XAudio2BufferFlags.EndOfStream }; _loopedAudioBuffer = new XAudio2Buffer { AudioDataPtr = stream.DataPointer, AudioBytes = buffer.Length, Flags = XAudio2BufferFlags.EndOfStream, LoopCount = XAudio2Buffer.LoopInfinite }; data.AudioSource.SetPosition((System.TimeSpan)StartingPosition); _voice.Volume = _volume; _voice.SetFrequencyRatio(_pitch); TrackLength = length; IsLoaded = true; Loaded.Fire(soundId); }
private void InitializeAudio() { audio = XAudio2.Create(); masteringVoice = audio.CreateMasteringVoice(); WaveFormat fmt = new WaveFormat { ChannelCount = 1 }; voice = audio.CreateSourceVoice(fmt); voice.VoiceProcessingPassStart += voice_VoiceProcessingPassStart; voice.VoiceProcessingPassEnd += voice_VoiceProcessingPassEnd; voice.BufferStart += voice_BufferStart; voice.BufferEnd += voice_BufferEnd; Volume.Value = voice.Volume * 100; }