protected override void Init(MyObjectBuilder_DefinitionBase builder) { base.Init(builder); var ob = builder as MyObjectBuilder_AudioDefinition; MyDebug.AssertDebug(ob != null); this.SoundData = ob.SoundData; this.SoundData.SubtypeId = base.Id.SubtypeId; }
/// <summary> /// Updates values of omnidirectional emitter. /// Omnidirectional means it's same for all directions. There's no Cone and Front/Top vectors are not used. /// </summary> internal static void UpdateValuesOmni(this Emitter emitter, Vector3 position, Vector3 velocity, MySoundData cue, int channelsCount, float? customMaxDistance) { emitter.Position = new SharpDX.Vector3(position.X, position.Y, position.Z); emitter.Velocity = new SharpDX.Vector3(velocity.X, velocity.Y, velocity.Z); float maxDistance = customMaxDistance.HasValue ? customMaxDistance.Value : cue.MaxDistance; emitter.DopplerScaler = 1f; emitter.CurveDistanceScaler = maxDistance; emitter.VolumeCurve = MyDistanceCurves.Curves[(int)cue.VolumeCurve]; emitter.InnerRadius = (channelsCount > 2) ? maxDistance : 0f; emitter.InnerRadiusAngle = (channelsCount > 2) ? 0.5f * SharpDX.AngleSingle.RightAngle.Radians : 0f; }
public MyInMemoryWave(MySoundData cue, string path) { using (var stream = MyFileSystem.OpenRead(path)) { m_stream = new SoundStream(stream); m_waveFormat = m_stream.Format; m_buffer = new AudioBuffer { Stream = m_stream.ToDataStream(), AudioBytes = (int)m_stream.Length, Flags = BufferFlags.None }; if (cue.Loopable) m_buffer.LoopCount = AudioBuffer.LoopInfinite; m_stream.Close(); } }
protected override void Init(MyObjectBuilder_DefinitionBase builder) { base.Init(builder); var ob = builder as MyObjectBuilder_AudioDefinition; MyDebug.AssertDebug(ob != null); this.SoundData = ob.SoundData; this.SoundData.SubtypeId = base.Id.SubtypeId; if (this.SoundData.Loopable) { bool hasLoop = true; for (int i = 0; i < this.SoundData.Waves.Count; i++) { hasLoop &= this.SoundData.Waves[i].Loop != null; } //MyDebug.AssertDebug(hasLoop, String.Format("Sound '{0}' has <Loopable> tag set to TRUE, but is missing a <Loop> in <Wave>, please fix the .sbc", this.SoundData.SubtypeId)); } }
private float PitchVariation(MySoundData cue) { return MyUtils.GetRandomFloat(-1f, 1f) * cue.PitchVariation / 100f; }
private float VolumeVariation(MySoundData cue) { return MyUtils.GetRandomFloat(-1f, 1f) * cue.VolumeVariation * 0.07f; }
void cuesCombo_OnSelect() { m_currentCueSelectedItem = (int)m_cuesCombo.GetSelectedKey(); var cue = new MyCueId(MyStringHash.TryGet(m_cuesCombo.GetSelectedValue().ToString())); m_currentCue = MyAudio.Static.GetCue(cue); UpdateCueValues(); //RecreateControls(false); }
private void PlayMusic(MyCueId cue, MyStringHash effect, int effectDuration = 2000, MyCueId[] cueIds = null, bool play = true) { if (MyAudio.Static == null) return; if(play) m_musicSourceVoice = MyAudio.Static.PlayMusicCue(cue, true); if (m_musicSourceVoice != null) { if (effect != MyStringHash.NullOrEmpty) { m_musicSourceVoice = MyAudio.Static.ApplyEffect(m_musicSourceVoice, effect, cueIds, effectDuration, true).OutputSound; } if (m_musicSourceVoice != null) m_musicSourceVoice.StoppedPlaying += MusicStopped; } m_lastMusicData = MyAudio.Static.GetCue(cue); }
public void PlaySoundWithDistance(MyCueId soundId, bool stopPrevious = false, bool skipIntro = false, bool force2D = false, bool useDistanceCheck = true, bool alwaysHearOnRealistic = false, bool skipToEnd = false) { m_lastSoundData = MyAudio.Static.GetCue(soundId); if (useDistanceCheck) m_closeSoundCueId = soundId; if (useDistanceCheck && ShouldPlay2D() == false && force2D == false) soundId = CheckDistanceSounds(soundId); bool usesDistanceSoundsCache = m_usesDistanceSounds; if (m_sound != null) { if (stopPrevious) StopSound(true); else if (m_sound.IsLoopable) { var sound = Sound; StopSound(true); m_soundsQueue.Add(sound.CueEnum); } } if (m_secondarySound != null) { m_secondarySound.Stop(true); } SoundId = soundId; PlaySoundInternal((skipIntro || skipToEnd), force2D: force2D, alwaysHearOnRealistic: alwaysHearOnRealistic, skipToEnd: skipToEnd); m_usesDistanceSounds = usesDistanceSoundsCache; }
private MyCueId SelectCue(MySoundPair sound) { if (m_useRealisticByDefault) { if (m_lastSoundData == null) m_lastSoundData = MyAudio.Static.GetCue(sound.Realistic); if (m_lastSoundData != null && m_lastSoundData.AlwaysUseOneMode) { m_realistic = true; return sound.Realistic; } MyCockpit cockpit = MySession.Static.LocalCharacter != null ? MySession.Static.LocalCharacter.Parent as MyCockpit : null; bool isLargePressurizedCockpit = (cockpit != null && cockpit.CubeGrid.GridSizeEnum == VRage.Game.MyCubeSize.Large && cockpit.BlockDefinition.IsPressurized); if (IsThereAir() || isLargePressurizedCockpit) { m_realistic = false; return sound.Arcade; } else { m_realistic = true; return sound.Realistic; } } else { m_realistic = false; return sound.Arcade; } }
public bool Add(MySoundData cue, MyAudioWave cueWave) { string[] files = { cueWave.Start, cueWave.Loop, cueWave.End }; SharpDX.Multimedia.WaveFormatEncoding encoding = SharpDX.Multimedia.WaveFormatEncoding.Unknown; bool result = true; int i = 0; foreach (var waveFilename in files) { i++; if (string.IsNullOrEmpty(waveFilename) || m_waves.ContainsKey(waveFilename)) continue; var fsPath = Path.IsPathRooted(waveFilename) ? waveFilename : Path.Combine(MyFileSystem.ContentPath, "Audio", waveFilename); var exists = MyFileSystem.FileExists(fsPath); result |= exists; if (exists) { if (cue.StreamSound) { return true; } try { MyInMemoryWave wave = new MyInMemoryWave(cue, fsPath, this); if (i != 2) wave.Buffer.LoopCount = 0; m_waves[waveFilename] = wave; // check the formats if (encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown) { encoding = wave.WaveFormat.Encoding; } // check the formats if (wave.WaveFormat.Encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown) { if (MyAudio.OnSoundError != null) { var msg = string.Format("Unknown audio encoding '{0}', '{1}'", cue.SubtypeId.ToString(), waveFilename); MyAudio.OnSoundError(cue, msg); } result = false; } // 3D sounds must be mono if (cueWave.Type == MySoundDimensions.D3 && wave.WaveFormat.Channels != 1) { if (MyAudio.OnSoundError != null) { var msg = string.Format("3D sound '{0}', '{1}' must be in mono, got {2} channels", cue.SubtypeId.ToString(), waveFilename, wave.WaveFormat.Channels); MyAudio.OnSoundError(cue, msg); } result = false; } // all parts of the sound must have the same encoding if (wave.WaveFormat.Encoding != encoding) { if (MyAudio.OnSoundError != null) { var msg = string.Format("Inconsistent sound encoding in '{0}', '{1}', got '{2}', expected '{3}'", cue.SubtypeId.ToString(), waveFilename, wave.WaveFormat.Encoding, encoding); MyAudio.OnSoundError(cue, msg); } result = false; } } catch (Exception e) { if (MyAudio.OnSoundError != null) { var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), waveFilename, e.ToString()); MyAudio.OnSoundError(cue, msg); } result = false; } // Second catch shouldn't be needed according to http://stackoverflow.com/questions/5345436/net-exception-catch-block // all non-exceptions will be wrapped as type derived from Exception and caught above. //catch //{ // if (MyAudio.OnSoundError != null) // { // var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), waveFilename, "Something went horribly wrong"); // MyAudio.OnSoundError(cue, msg); // } // result = false; //} } else { if (MyAudio.OnSoundError != null) { var msg = string.Format("Unable to find audio file: '{0}', '{1}'", cue.SubtypeId.ToString(), waveFilename); MyAudio.OnSoundError(cue, msg); } result = false; } } return result; }
public MyInMemoryWave GetStreamedWave(string filename, MySoundData cue, MySoundDimensions dim = MySoundDimensions.D2) { if (string.IsNullOrEmpty(filename)) return null; SharpDX.Multimedia.WaveFormatEncoding encoding = SharpDX.Multimedia.WaveFormatEncoding.Unknown; var fsPath = Path.IsPathRooted(filename) ? filename : Path.Combine(MyFileSystem.ContentPath, "Audio", filename); var exists = MyFileSystem.FileExists(fsPath); if (exists) { try { MyInMemoryWave wave; if (!LoadedStreamedWaves.TryGetValue(fsPath, out wave)) wave = LoadedStreamedWaves[fsPath] = new MyInMemoryWave(cue, fsPath, this, true); else wave.Reference(); // check the formats if (encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown) { encoding = wave.WaveFormat.Encoding; } // check the formats if (wave.WaveFormat.Encoding == SharpDX.Multimedia.WaveFormatEncoding.Unknown) { if (MyAudio.OnSoundError != null) { var msg = string.Format("Unknown audio encoding '{0}', '{1}'", cue.SubtypeId.ToString(), filename); MyAudio.OnSoundError(cue, msg); } return null; } // 3D sounds must be mono if (dim == MySoundDimensions.D3 && wave.WaveFormat.Channels != 1) { if (MyAudio.OnSoundError != null) { var msg = string.Format("3D sound '{0}', '{1}' must be in mono, got {2} channels", cue.SubtypeId.ToString(), filename, wave.WaveFormat.Channels); MyAudio.OnSoundError(cue, msg); } return null; } // all parts of the sound must have the same encoding if (wave.WaveFormat.Encoding != encoding) { if (MyAudio.OnSoundError != null) { var msg = string.Format("Inconsistent sound encoding in '{0}', '{1}', got '{2}', expected '{3}'", cue.SubtypeId.ToString(), filename, wave.WaveFormat.Encoding, encoding); MyAudio.OnSoundError(cue, msg); } return null; } return wave; } catch (Exception e) { if (MyAudio.OnSoundError != null) { var msg = string.Format("Unable to load audio file: '{0}', '{1}': {2}", cue.SubtypeId.ToString(), filename, e.ToString()); MyAudio.OnSoundError(cue, msg); } return null; } } else { if (MyAudio.OnSoundError != null) { var msg = string.Format("Unable to find audio file: '{0}', '{1}'", cue.SubtypeId.ToString(), filename); MyAudio.OnSoundError(cue, msg); } } return null; }